def test_eol_tabs_by_scope_depth(self): # depth = 0 scope = Scope() eol = EOL() eol.annotate(scope, None) self.assertEqual(eol.string(), "\n") # depth = 1 scope = Scope(parent=scope) eol.annotate(scope, None) self.assertEqual(eol.string(), "\n\t") # depth = 2 scope = Scope(parent=scope) eol.annotate(scope, None) self.assertEqual(eol.string(), "\n\t\t")
def test_blocks_indent_in_tree(self): myeol = EOL() tree = SemanticParseTree(Nonterminal("foo"), [ SemanticParseTree(Block()), SemanticParseTree(Nonterminal("foo"), [SemanticParseTree(myeol)]) ]) tree.annotate(scope=Scope()) self.assertEqual(tree.string(), "\n\t")
def test_block_creates_child_scope(self): scope = Scope() block = Block() subtree = SemanticParseTree(block) tree = SemanticParseTree("parent", [subtree]) block.annotate(scope, subtree) self.assertEqual(scope, scope.next_scope.parent) tree._run_deferred() self.assertEqual(scope, scope.next_scope)
def test_declaration_adds_variable_to_scope_later(self): scope = Scope() dec = Declaration(Int) subtree = SemanticParseTree(dec) tree = SemanticParseTree(Assignment(None), [subtree]) dec.annotate(scope, subtree) self.assertEqual(len(scope.variables), 0) tree._run_deferred() self.assertEqual(len(scope.variables), 1)
def test_function_parameters_in_scope(self): outer_scope = Scope() outer_scope.add_child(Scope(None, Int)) function = Function() interceptor = Interceptor(None) tree = SemanticParseTree( FuncDeclaration(), [function, SemanticParseTree(block, [Block(), interceptor])]) def testclosure(inner_scope: Scope, context): arguments = function.annotations["arguments"] self.assertGreater(len(arguments), 0, "useless test") for argument in arguments: self.assertIsNotNone( inner_scope.choose_variable(name=argument)) interceptor.testclosure = testclosure tree.annotate(scope=outer_scope)
def test_static_function_declared_static(self): T = DataType("T") scope = Scope(None, T) funcdeclaration = FuncDeclaration(binding=Binding.static) tree = SemanticParseTree(funcdeclaration, [ BindingModifier(), Function(), SemanticParseTree(block, [Block()]) ]) tree.annotate(scope=scope) declaration = tree.string() self.assertTrue(declaration.startswith("static"))
def testclosure(inner_scope: Scope, context): arguments = function.annotations["arguments"] self.assertGreater(len(arguments), 0, "useless test") for argument in arguments: self.assertIsNotNone( inner_scope.choose_variable(name=argument))
def test_variable_annotated_by_tree(self): scope = Scope() scope.declare("foo", Int, False) tree = SemanticParseTree(Variable(Int)) tree.annotate(scope=scope) self.assertEqual(tree.string(), "foo")
def test_variable_falls_back_to_literal(self): v = Variable(Int) v.annotate(Scope(), None) int(v.string()) # ValueError if it didn't fall back to an int literal
def test_variable_resolves_in_scope(self): scope = Scope() scope.declare("foo", Int, False) v = Variable(Int) v.annotate(scope, None) self.assertEqual(v.string(), "foo")