def main(argv): """Parse and run any Tiger program""" # check for arguments try: file = argv[1] except IndexError: print( "Expected one file name argument to be passed, e.g. ./tiger-interpreter program.tig" ) return 40 program_contents = read_file(argv[1]) # set up environment environment = create_empty_environment() # parse input program try: program = Parser(program_contents, argv[1]).parse(create_native_functions()) except ParseError as e: print("Parse failure: %s" % e.to_string()) return 42 # evaluate the program result = program.evaluate(environment) # print the result and exit if result: print(result.to_string()) return 0
def test(): create_environment_with_natives() program = Parser("let var a := 0 in (for i := 1 to 9 do a := a - i; a) end").parse() environment = SimpleEnvironment() result = program.evaluate(environment) assert isinstance(result, IntegerValue) return result.integer
def test(): program = Parser(""" let var max : int := 50 var s : int := 0 var n : int := 2 in while n <= max do let var p : int := 1 var d : int := 2 in while d <= (n - 1) do let var m : int := d * (n / d) in if n <= m then p := 0; d := d + 1 end; if p <> 0 then s := s + n; n := n + 1 end; s end """).parse() environment = create_environment_with_natives( ) # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field result = program.evaluate(environment) assert isinstance(result, IntegerValue) return result.integer
def test(): program = Parser( 'let var a := 0 in (while a < 100 do a := a + 1; a) end' ).parse(create_native_functions()) environment = create_environment_with_natives( ) # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field result = program.evaluate(environment) assert isinstance(result, IntegerValue) return result.integer
def evaluate(self, program, *existing_declarations): existing_declarations_ast = [Parser(decl).parse() if isinstance(decl, str) else decl for decl in existing_declarations] if not existing_declarations_ast: # ensure we have at least one existing declaration or the parser will not run the lvalue-transformation pass existing_declarations_ast.append(NativeNoArgumentFunctionDeclaration('unused', None, lambda: None)) assert all(isinstance(a, Program) for a in existing_declarations_ast) assert isinstance(program, str) program_ast = Parser(program).parse(existing_declarations_ast) return program_ast.evaluate(Environment.empty())