def test_factorial_code_test(): s = """ { define factorial { if (x.value==0) { return 1; } else { y= x.value * factorial(x=x.value-1); return y; }; }; x = Integer<value: 12>; y = factorial(x=x); return y; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_while_code_test(): s = """ { x=5; print_(x=x); while (x > 0) { print_(x=x); x = x - 1; }; print_(x=x); if (x>5) { print_(x=0); } elif (x<0) { print_(x=1); } else { print_(x=2); }; return x; }""" pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) return result
def testRoundtrip(self): program = """{ x=5; print_(x=x); while (x > 0) { print_(x=x); x = x - 1; }; print_(x=x); if (x>5) { print_(x=0); } elif (x<0) { print_(x=1); } else { print_(x=2); }; return x; } """ result = scope.parseString(program) print result[0] roundtrip = scope.parseString(repr(result[0])) print roundtrip[0] proto = parser_converter.convert_scope(result[0].scope) print proto pr = Protocall() pr.execute(proto.block)
def test_basic_code_test(): s = """ { x=5; x=x+1; print_(x=x); return x; }""" pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) return result
def test_proto_code_test(): # want to return a[3] at end but that causes exception s = """ { x = Person<id: 7 name: "Bar" email: "*****@*****.**">; print_(id=x.id); return x.id; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_function_code_test(): s = """ { define f { print_(x=x); print_(y=y); return 0; }; y=f(x=5,y=6); return 0; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) return result
def test_proto_assignment_code_test(): # want to return x.person.id at end but that causes exception s = """ { x = Person<id: 7 name: "Bar" email: "*****@*****.**">; x.id = 6; x.name = "Foo"; x.person = Person<id: 17 name: "Bar" email: "*****@*****.**">; return 0; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_array_append_code_test(): # want to return a[3] at end but that causes exception s = """ { x = 2; a = {5, 6, 7}; print_(a=a); a[0] = 6; print_(a=a); append(a=a, v=5); print_(a=a); return x; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_array_code_test(): s = """ { x = 2; a = {5,6,x+5}; print_symbols(); print_(a=a); b=a[0]; print_(b=b); b=a[1]; print_(b=b); b=a[2]; print_(b=b); return a[0]; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_factorial_code_test(): s = """ { define factorial { if (x==0) { return 1; } else { return x * factorial(x=x-1); }; }; return factorial(x=12); } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_define_code_test(): s = """ { define f { x = x - 1; print_(x=x); return x; }; x = 5; print_(x=x); f(x=x); while (x>0) { print_(x=x); x = x - 1; }; return x; } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) print "Result=", result return result
def test_fibonacci_code_test(): s = """ { define f { if (x == 0) { return 0; } elif (x == 1) { return 1; } else { a = f(x=x-2); b = f(x=x-1); return a+b; }; }; return f(x=5); } """ pr = Protocall() result = grammar.scope.parseString(s) sc = parser_converter.convert_scope(result[0].scope) result = pr.execute(sc.block) return result
while True: try: line = raw_input('> ') try: print "line=", line result = block.parseString(line) except ParseException as e: print "Error parsing input at line", e.lineno, "column", e.col print e.line print ' ' * (e.col - 2), '^' print e else: bl = parser_converter.convert_block(result[0]) # try: print pr.execute(bl) # except Exception as e: # print e except EOFError: break else: filename = sys.argv[1] lines = open(filename).read() result = scope.parseString(lines) sc = parser_converter.convert_scope(result[0].scope) # print "dump" # s= dump.dump(sc.block) # print s # end-to-end parse of file, print as program, parse that. # result2 = scope.parseString(s) print pr.execute(sc.block)
while True: try: line = raw_input('> ') try: print "line=", line result = block.parseString(line) except ParseException as e: print "Error parsing input at line", e.lineno, "column", e.col print e.line print ' ' * (e.col-2), '^' print e else: bl = parser_converter.convert_block(result[0]) # try: print pr.execute(bl) # except Exception as e: # print e except EOFError: break else: filename = sys.argv[1] lines = open(filename).read() result = scope.parseString(lines) sc = parser_converter.convert_scope(result[0].scope) # print "dump" # s= dump.dump(sc.block) # print s # end-to-end parse of file, print as program, parse that. # result2 = scope.parseString(s) print pr.execute(sc.block)