def test_block_parsing(self): line = "{ Integer b = 10; return 0; if (a <= 10) {a = a + b;}} " put_line_in_lexer_text_source(self.parser, line) par = self.parser._parse_block() self.assertEqual(par[0].__class__.__name__, "InitStat") self.assertEqual(par[0].name, "b") self.assertEqual(par[0].type, TokenType.K_INTEGER) self.assertEqual(par[0].right.__class__.__name__, "Integer") self.assertEqual(par[0].right.value, 10) self.assertEqual(par[1].__class__.__name__, "ReturnStat") self.assertEqual(par[1].return_value.__class__.__name__, "Integer") self.assertEqual(par[1].return_value.value, 0) self.assertEqual(par[2].__class__.__name__, "IfElseStat") self.assertEqual(par[2].condition.__class__.__name__, "LessEqualOperation") self.assertEqual(par[2].condition.left, nodes.Variable("a")) self.assertEqual(par[2].condition.right.value, 10) self.assertEqual(par[2].instructions[0].__class__.__name__, "AssignStat") self.assertEqual(par[2].instructions[0].left, nodes.Variable("a")) self.assertEqual(par[2].instructions[0].right.__class__.__name__, "AddOperation") self.assertEqual(par[2].instructions[0].right.left, nodes.Variable("a")) self.assertEqual(par[2].instructions[0].right.right, nodes.Variable("b"))
def copy_variables(variables=variables): global variableValues variable_keys = list(variables.keys()) for name in variable_keys: v_ = variables[name] if isinstance(v_, nodes.Variable): context.variables[name] = v_ context.variableValues[name] = v_.value continue context.variableValues[name] = v_ context.variables[name] = nodes.Variable(name=name, value=v_, type=type(v_)) variables[name] = nodes.Variable(name=name, value=v_, type=type(v_))
def __setitem__(self, key, value): print(("Reflector __setitem__ %s %s" % (key, value))) if key in the.variables: the.variables[key].value = value else: the.variables[key] = nodes.Variable(name=key, value=value) the.variableValues[key] = value the.result = value
def test_condition(self): line = "true | b & a != ! c <= 3 " put_line_in_lexer_text_source(self.parser, line) par = self.parser._parse_condition() self.assertEqual(par.__class__.__name__, "OrOperation") self.assertEqual(par.left.value, "true") self.assertEqual(par.right.__class__.__name__, "AndOperation") self.assertEqual(par.right.left, nodes.Variable("b")) self.assertEqual(par.right.right.__class__.__name__, "NotEqualOperation") self.assertEqual(par.right.right.left, nodes.Variable("a")) self.assertEqual(par.right.right.right.__class__.__name__, "NotOperation") self.assertEqual(par.right.right.right.right.__class__.__name__, "LessEqualOperation") self.assertEqual(par.right.right.right.right.left, nodes.Variable("c")) self.assertEqual(par.right.right.right.right.right.value, 3)
def test_variable_or_method_detection(self): line = "a a.b a() a.b() " put_line_in_lexer_text_source(self.parser, line) par_type = self.parser._parse_variable_or_method() expected = nodes.Variable("a") self.assertEqual(expected, par_type) par_type = self.parser._parse_variable_or_method() expected = nodes.ObjectVariable("a", ["b"]) self.assertEqual(expected, par_type) par_type = self.parser._parse_variable_or_method() expected = nodes.FunctionCall(nodes.Variable("a"), []) self.assertEqual(expected, par_type) par_type = self.parser._parse_variable_or_method() expected = nodes.ObjectMethod("a", ["b"], []) self.assertEqual(expected, par_type)
def test_expression(self): line = "a * 2 + 6 / b() " put_line_in_lexer_text_source(self.parser, line) par = self.parser._parse_expression() self.assertEqual(par.left.left.name, "a") self.assertEqual(par.left.right.value, 2) self.assertEqual(par.right.left.value, 6) self.assertEqual(par.right.right, nodes.FunctionCall(nodes.Variable("b"), []))
def test_x_name(self): variables['x'] = nodes.Variable(name='x', value=7) init('x') assert_equals(name(parser.nod(), ), 'x')