コード例 #1
0
def main():
    sym = SymbolTable()

    sym.insert(Symbol('Apple'))
    sym.insert(Symbol('Banana'))
    sym.push()
    sym.insert(Symbol('Cantaloupe'))
    print(sym)

    assert sym.insert(Symbol('Apple')) is Scope.INSERT_SHADOWED
    assert sym.insert(Symbol('Cantaloupe')) is Scope.INSERT_REDECL
    assert sym.insert(Symbol('Blueberries')) is Scope.INSERT_SUCCESS

    assert sym.size() is 2

    found = sym.find('Apple')
    assert found is not None
    assert found[0].identifier is 'Apple'

    found = sym.find('Cantaloupe')
    assert found is not None
    assert found[0].identifier is 'Cantaloupe'

    found = sym.find('Durian')
    assert found is None

    sym.pop()
    assert sym.size() is 1
コード例 #2
0
class TestSymbolTable(unittest.TestCase):
    def setUp(self):
        self.sym = SymbolTable()
        self.sym.push()

    def tearDown(self):
        self.sym.pop()
        self.sym = None

    def test_push_pop_and_size(self):
        self.assertEqual(1, self.sym.size())
        self.sym.push()
        self.sym.push()
        self.assertTrue(self.sym.size() == 3)
        self.sym.pop()
        self.assertTrue(self.sym.size() == 2)

    def test_find_same_scope(self):
        self.assertTrue(self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_SUCCESS)
        found, in_scope = self.sym.find('A')
        self.assertTrue(found is not None)
        self.assertTrue(found.identifier is 'A')
        self.assertTrue(type(found) is Symbol)

    def test_find_diff_scope(self):
        self.sym.insert(Symbol('A', 0, 0))
        self.sym.push()
        self.sym.insert(Symbol('B', 0, 0))
        found, in_scope = self.sym.find('A')
        self.assertTrue(found is not None)
        self.assertTrue(found.identifier is 'A')
        self.assertTrue(type(found) is Symbol)

    def test_find_in_top_scope(self):
        self.sym.insert(Symbol('A', 0, 0))
        found = self.sym.find_in_top('A')
        self.assertTrue(found is not None)
        self.assertTrue(found.identifier is 'A')
        self.assertTrue(type(found) is Symbol)

    def test_insert(self):
        self.assertTrue(self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_SUCCESS)
        self.assertTrue(self.sym.insert(Symbol('B', 0, 0))[0] == Scope.INSERT_SUCCESS)

        self.sym.push()
        self.assertTrue(self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_SHADOWED)
        self.assertTrue(self.sym.insert(Symbol('B', 0, 0))[0] == Scope.INSERT_SHADOWED)

        self.assertTrue(self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_REDECL)
        self.assertTrue(self.sym.insert(Symbol('B', 0, 0))[0] == Scope.INSERT_REDECL)

    def test_symbol_table_clone(self):
        self.sym.insert(Symbol('A', 0, 0))
        self.sym.push()
        self.sym.insert(Symbol('B', 0, 0))

        clone = self.sym.clone()
        self.assertTrue(self.sym.size() == clone.size())
        self.assertTrue(self.sym.find('A')[0] is not clone.find('A')[0])
        self.assertTrue(self.sym.find('B')[0] is not clone.find('B')[0])
        self.sym.pop()
        self.assertTrue(self.sym.size() == clone.size() - 1)
        self.assertTrue(self.sym.find('B')[0] is None and clone.find('B')[0] is not None)
コード例 #3
0
class TestSymbolTable(unittest.TestCase):
    def setUp(self):
        self.sym = SymbolTable()
        self.sym.push()

    def tearDown(self):
        self.sym.pop()
        self.sym = None

    def test_push_pop_and_size(self):
        self.assertEqual(1, self.sym.size())
        self.sym.push()
        self.sym.push()
        self.assertTrue(self.sym.size() == 3)
        self.sym.pop()
        self.assertTrue(self.sym.size() == 2)

    def test_find_same_scope(self):
        self.assertTrue(
            self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_SUCCESS)
        found, in_scope = self.sym.find('A')
        self.assertTrue(found is not None)
        self.assertTrue(found.identifier is 'A')
        self.assertTrue(type(found) is Symbol)

    def test_find_diff_scope(self):
        self.sym.insert(Symbol('A', 0, 0))
        self.sym.push()
        self.sym.insert(Symbol('B', 0, 0))
        found, in_scope = self.sym.find('A')
        self.assertTrue(found is not None)
        self.assertTrue(found.identifier is 'A')
        self.assertTrue(type(found) is Symbol)

    def test_find_in_top_scope(self):
        self.sym.insert(Symbol('A', 0, 0))
        found = self.sym.find_in_top('A')
        self.assertTrue(found is not None)
        self.assertTrue(found.identifier is 'A')
        self.assertTrue(type(found) is Symbol)

    def test_insert(self):
        self.assertTrue(
            self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_SUCCESS)
        self.assertTrue(
            self.sym.insert(Symbol('B', 0, 0))[0] == Scope.INSERT_SUCCESS)

        self.sym.push()
        self.assertTrue(
            self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_SHADOWED)
        self.assertTrue(
            self.sym.insert(Symbol('B', 0, 0))[0] == Scope.INSERT_SHADOWED)

        self.assertTrue(
            self.sym.insert(Symbol('A', 0, 0))[0] == Scope.INSERT_REDECL)
        self.assertTrue(
            self.sym.insert(Symbol('B', 0, 0))[0] == Scope.INSERT_REDECL)

    def test_symbol_table_clone(self):
        self.sym.insert(Symbol('A', 0, 0))
        self.sym.push()
        self.sym.insert(Symbol('B', 0, 0))

        clone = self.sym.clone()
        self.assertTrue(self.sym.size() == clone.size())
        self.assertTrue(self.sym.find('A')[0] is not clone.find('A')[0])
        self.assertTrue(self.sym.find('B')[0] is not clone.find('B')[0])
        self.sym.pop()
        self.assertTrue(self.sym.size() == clone.size() - 1)
        self.assertTrue(
            self.sym.find('B')[0] is None and clone.find('B')[0] is not None)