-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParaCode.py
92 lines (74 loc) · 2.82 KB
/
ParaCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/python3
from lexer import Lexer, TokenType, LexerToken
from parse.parser import Parser
from parse.node import NodeImport
from parse.source_location import SourceLocation
from interpreter.interpreter import Interpreter
from error import InterpreterError
from repl.repl import Repl
from ast_printer import AstPrinter
class ParaCode():
instance = None
data = ""
initialized = False
# Language Info
version = "2.1.0"
release_stage = "development"
def __init__(self):
ParaCode.instance = self
def eval(self, data=None, filename=None, interpret=True, default_imports=['std/__core__.para'], source_location=None):
self.initialized = True
debug_name = "<none>"
if filename != None:
try:
self.file = open(filename, 'r')
except FileNotFoundError:
print("Script '{}' could not be found".format(filename))
return None
debug_name = filename
self.data = self.file.read()
elif data != None:
self.data = data
else:
self.data = ""
self.lexer = Lexer(self.data, SourceLocation(debug_name))
self.parser = Parser(self.lexer.lex(), self.lexer.source_location)
# all default imports should be here
global_import_nodes = []
for path in default_imports:
global_import_nodes.append(self.parser.import_file(path))
# combine global imports and parser ast
self.ast = global_import_nodes+self.parser.parse()
error_list = self.parser.error_list
if len(error_list.errors) > 0:
error_list.print_errors()
return
source_loc = source_location
if source_loc is None:
source_loc = self.parser.source_location
return_code = None
if interpret:
# init interpreter and visit nodes
self.interpreter = Interpreter(source_loc)
try:
for node in self.ast:
return_code = self.interpreter.visit(node)
except InterpreterError:
# errors printed in interpreter
self.interpreter.error_list.clear_errors()
return return_code
def eval_file(self, filename):
return self.eval(filename=filename)
def eval_data(self, data):
return self.eval(data=data)
def call_function(self, function_name, arguments=[]):
# if not self.initialized:
# self.eval()
if self.interpreter == None:
raise Exception("ParaCode not initialized! please run ")
if type(arguments) != list:
raise Exception("Arguments type is not list!")
return self.interpreter.call_function(function_name, arguments)
def repl(self):
repl = Repl(self)
repl.loop()