def test_variable_assign(self): text = 'INT a = 123123 ; a = 123 + 123 ; STRING s = "" ; s = "sweetnight" + " " + "to znaczy slodka noc"' scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() self.eval_in_scope(scope, "a", ("NUMBER", 246)) self.eval_in_scope(scope, "s", ("STRING_T", "sweetnight to znaczy slodka noc"))
def test_global(self): text = "INT i = 0 ; FOR (INT i = 0 ; i 4 < ; i = i 1 +) { i = i i +}" scope = Root(code=self.parser.parse(text)) scope.eval_in_scope() scope.code = self.parser.parse("i") evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("NUMBER", 0)) text = "{ GLOBAL INT x = 2138 }" scope = Root(code=self.parser.parse(text)) scope.eval_in_scope() scope.code = self.parser.parse("x") evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("NUMBER", 2138)) text = "DEF STRING fun1( INT i , STRING s ) { s i + }" scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() scope.code = self.parser.parse('fun1(1 , "halo" )') evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("STRING_T", "halo1"))
def test_nested(self): text = "DEF INT fun1(INT i) { i }; DEF INT fun2(INT i ) { 1 + fun1(1)}; DEF INT fun3(INT i) { i - 1 }" scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() self.eval_in_scope(scope, "fun1(1)", ("NUMBER", 1)) self.eval_in_scope(scope, "fun2(1)", ("NUMBER", 2)) self.eval_in_scope(scope, "fun3(fun1(2))", ("NUMBER", 1))
def test_hoist(self): text1 = "INT x = 12; INT z = 0; INT y = 1; FOR(INT i = 0 ; i < 2 ; i = i + 1 ) { z = x + y } " text2 = "x + y" for_loop = self.parser.parse(text1) expr = self.parser.parse(text2) self.assertTrue(expr[0] in for_loop[-1].before.values()) scope = Root(code=for_loop) scope.eval_in_scope() self.eval_in_scope(scope, "z", ("NUMBER", 13))
def test_variable_init(self): text = 'INT a = 0 ; STRING b = "test" ; DOUBLE c = 0.15 ; BOOL d = True' scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() self.eval_in_scope(scope, "a", ("NUMBER", 0)) self.eval_in_scope(scope, "b", ("STRING_T", "test")) self.eval_in_scope(scope, "c", ("NUMBER", 0.15)) self.eval_in_scope(scope, "d", ("BOOL", True))
def test_for(self): text = "INT a = 0 ; FOR (INT i = 0 ; i < 4 ; i = i + 1) { a = a + i }" scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() scope.code = self.parser.parse("a") evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("NUMBER", 6))
def check_in_scope_i(self, text, value): scope = Root() scope.code = self.parser.parse(text) evaluated = scope.eval_in_scope() if not isinstance(value, list): evaluated = evaluated self.assertEqual(evaluated, value)
def test_fn_definition(self): text = "DEF INT fun( INT i ) { i }" scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() scope.code = self.parser.parse("fun(10)") evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("NUMBER", 10)) text = "DEF STRING fun1( INT i , STRING s ) { s + i }" scope = Root() scope.code = self.parser.parse(text) scope.eval_in_scope() scope.code = self.parser.parse('fun1(1 , "halo" )') evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("STRING_T", "halo1"))
def check_in_scope_f(self, text, value): error = 1e-5 scope = Root() scope.code = self.parser.parse(text) ret_type, ret_val = scope.eval_in_scope() v_type, v_val = value self.assertEqual(ret_type, v_type) self.assertTrue(abs(ret_val - v_val) < error)
def test_global(self): text = "INT i = 0 ; FOR (INT i = 0 ; i < 4 ; i = i + 1) { i = i + i }" scope = Root(code=self.parser.parse(text)) scope.eval_in_scope() scope.code = self.parser.parse("i") evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("NUMBER", 0)) text = "{ GLOBAL INT x = 2138 }" scope = Root(code=self.parser.parse(text)) scope.eval_in_scope() scope.code = self.parser.parse("x") evaluated = scope.eval_in_scope() self.assertEqual(evaluated, ("NUMBER", 2138))