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_execute(): p = create_block() pr = Protocall() print "Program:" print dump.dump(p) result = pr.execute(p) print "Result:" print result return result
def test_conditional_expression(i1, i2, op): p = create_conditional_expression(i1, i2, op) pr = Protocall() print "Program:" print dump.dump(p) result = pr.execute(p) print "Result:" print result return result
def test_conditional(c1, c2): p = create_conditional(c1, c2) pr = Protocall() print "Program:" print dump.dump(p) result = pr.execute(p) print "Result:" print result return result
def test_call3(): # This test fails when it's just a bare call() without a return(). # The interpreter is supposed to return the return value of the last statement but it doesn't seem to. p = create_call3() pr = Protocall() print "Program:" print dump.dump(p) result = pr.execute(p) print "Result:" print result return result
def test_evaluate(): expression = create_expression() a = protocall_pb2.Atom() a.literal.integer.value = 5 symbols = Symbols({'xyz': a}) pr = Protocall(symbols) print "Expression:" print dump.dump_expression(expression) result = pr.evaluate(expression) print "Result:" print result return result
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_evaluate_proto_expression(): expression = create_proto_expression() a = protocall_pb2.Atom() p = a.literal.proto p.field.component.add().name = "Integer" p.value = "value: 9" b = protocall_pb2.Atom() p = b.literal.proto p.field.component.add().name = "Integer" p.value = "value: 5" xyz = protocall_pb2.Atom() p = xyz.literal.proto p.field.component.add().name = "Integer" p.value = "value: 5" symbols = Symbols({'a': a, 'b': b, 'xyz': xyz}) pr = Protocall(symbols) result = pr.evaluate(expression) return result
def test_evaluate_array(): expression = create_expression() a = expression.atom array = a.literal.array element = array.element.add() op = element.arithmetic_operator op.left.atom.field.component.add().name = "x" op.right.atom.literal.integer.value = 5 op.operator = protocall_pb2.ArithmeticOperator.Op.Value("PLUS") atom = protocall_pb2.Atom() atom.literal.integer.value = 5 symbols = Symbols({'x': atom}) pr = Protocall(symbols) print "Expression:" print dump.dump_expression(expression) result = pr.evaluate(expression) print "Result:" print 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_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_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_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
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