|
|
|
@ -27,6 +27,16 @@ class InvalidCode(InterpreterException): |
|
|
|
|
class InvalidArguments(InterpreterException): |
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
class InterpreterObject(): |
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
class Executable(InterpreterObject): |
|
|
|
|
|
|
|
|
|
def __init__(self, name, sources): |
|
|
|
|
self.name = name |
|
|
|
|
self.sources = sources |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Interpreter(): |
|
|
|
|
|
|
|
|
|
def __init__(self, code): |
|
|
|
@ -34,7 +44,8 @@ class Interpreter(): |
|
|
|
|
self.sanity_check_ast() |
|
|
|
|
self.project = None |
|
|
|
|
self.compilers = [] |
|
|
|
|
|
|
|
|
|
self.executables = {} |
|
|
|
|
|
|
|
|
|
def sanity_check_ast(self): |
|
|
|
|
if not isinstance(self.ast, nodes.CodeBlock): |
|
|
|
|
raise InvalidCode('AST is of invalid type. Possibly a bug in the parser.') |
|
|
|
@ -43,7 +54,7 @@ class Interpreter(): |
|
|
|
|
first = self.ast.get_statements()[0] |
|
|
|
|
if not isinstance(first, nodes.FunctionCall) or first.get_function_name() != 'project': |
|
|
|
|
raise InvalidCode('First statement must be a call to project') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
|
i = 0 |
|
|
|
|
statements = self.ast.get_statements() |
|
|
|
@ -54,7 +65,7 @@ class Interpreter(): |
|
|
|
|
else: |
|
|
|
|
print("Unknown statement in line %d." % cur.lineno()) |
|
|
|
|
i += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_arguments(self, args, argcount, arg_types): |
|
|
|
|
if argcount is not None: |
|
|
|
|
if argcount != len(args): |
|
|
|
@ -88,6 +99,17 @@ class Interpreter(): |
|
|
|
|
else: |
|
|
|
|
raise InvalidCode('Tried to use unknown language "%s".' % lang) |
|
|
|
|
|
|
|
|
|
def func_executable(self, node, args): |
|
|
|
|
self.validate_arguments(args, 2, (nodes.StringStatement, nodes.StringStatement)) |
|
|
|
|
name = args[0].get_string() |
|
|
|
|
sources = [args[1].get_string()] |
|
|
|
|
if name in self.executables: |
|
|
|
|
raise InvalidCode('Line %d, tried to create executable "%s", which already exists.' % (node.lineno(), name)) |
|
|
|
|
exe = Executable(name, sources) |
|
|
|
|
self.executables[name] = exe |
|
|
|
|
print('Creating executable %s with file %s' % (name, sources[0])) |
|
|
|
|
return exe |
|
|
|
|
|
|
|
|
|
def function_call(self, node): |
|
|
|
|
func_name = node.get_function_name() |
|
|
|
|
args = node.arguments.arguments |
|
|
|
@ -97,6 +119,8 @@ class Interpreter(): |
|
|
|
|
self.func_message(node, args) |
|
|
|
|
elif func_name == 'language': |
|
|
|
|
self.func_language(node, args) |
|
|
|
|
elif func_name == 'executable': |
|
|
|
|
self.func_executable(node, args) |
|
|
|
|
else: |
|
|
|
|
raise InvalidCode('Unknown function "%s".' % func_name) |
|
|
|
|
|
|
|
|
@ -105,6 +129,7 @@ if __name__ == '__main__': |
|
|
|
|
message('I can haz text printed out?') |
|
|
|
|
message('It workses!') |
|
|
|
|
language('c') |
|
|
|
|
executable('prog', 'prog.c') |
|
|
|
|
""" |
|
|
|
|
i = Interpreter(code) |
|
|
|
|
i.run() |
|
|
|
|