Esempio n. 1
0
    def test_type_control(self):

        text = 'INT a = 0 ; STRING b = "test" ; DOUBLE c = a + b'

        scope = Root()
        scope.code = self.parser.parse(text)

        self.assertRaises(Exception, scope.eval_in_scope)
Esempio n. 2
0
    def test_unnecessary(self):
        text1 = "{1; 2; 3; RETURN 4; 5}"
        text2 = "{1; 2; 3; RETURN 4}"

        scope1 = Root(code=self.parser.parse(text1))

        scope2 = Root(code=self.parser.parse(text2))

        self.assertEqual(scope1.code, scope2.code)
Esempio n. 3
0
    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)
Esempio n. 4
0
    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"))
Esempio n. 5
0
    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))
Esempio n. 6
0
    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))
Esempio n. 7
0
    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))
Esempio n. 8
0
    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)
Esempio n. 9
0
    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))
Esempio n. 10
0
    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"))
Esempio n. 11
0
    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))
Esempio n. 12
0
    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"))