Created the executable command.

pull/15/head
Jussi Pakkanen 12 years ago
parent 1ead90f199
commit 0339d3bcf2
  1. 31
      interpreter.py

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

Loading…
Cancel
Save