|
|
|
@ -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,6 +44,7 @@ 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): |
|
|
|
@ -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() |
|
|
|
|