def as_language_type(type_: types.Type, symbol_table: SymbolTable) -> types.Type: assert isinstance(type_, types.Placeholder) text = type_.text if text == "i32": return types.I32() if text == "void": return types.Void() if text == "boolean": return types.Boolean() if symbol_table.has(text): symbol = symbol_table.get(text) assert symbol.type.is_data() or symbol.type.is_trait() return types.TypeRef(name=text) else: raise TypeError(f"Type not supported: {type_}")
def checkMethodOverrideTypes(self, classTable: SymbolTable): definedClasses = classTable.getAllSymbols() for definedClass in definedClasses: for method in definedClass.getAllClassDefinedMethods(): parentMethod = definedClass.getMethodFromParents(method.id) if parentMethod is None: continue if method.dataType != parentMethod.dataType: raise SemanticTypeIncompatibilityError( "Method overriden with wrong data types")
def setUp(self): self.sym = SymbolTable() self.sym.push()
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)
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)
def __init__(self, print_table=False, table_logfile='log_symbol_table.txt', print_tokens=False, print_source_scanner=True, scanner_logfile='log_scanner_tokens.txt', print_productions=False, print_source_parser=False, print_info=False, parser_logfile=sys.stdout, print_warnings=False, **kwargs): # Initialize variables self.source_code = None self.source_lines = None # Initialize table self.symbol_table = SymbolTable() # Initialize symbol table logger if table_logfile in {sys.stdout, sys.stderr}: self.symbol_table_logger = Logger(table_logfile) else: self.symbol_table_logger = Logger(open(table_logfile, 'w')) if print_table: self.symbol_table_logger.add_switch(Logger.SYMBOL_TABLE) # Initialize token/lexer logger if scanner_logfile in {sys.stdout, sys.stderr}: self.token_logger = Logger(scanner_logfile) else: self.token_logger = Logger(open(scanner_logfile, 'w')) if print_source_scanner: self.token_logger.add_switch(Logger.SOURCE) if print_tokens: self.token_logger.add_switch(Logger.TOKEN) # Initialize parser logger if parser_logfile in {sys.stdout, sys.stderr}: self.parser_logger = Logger(parser_logfile) else: self.parser_logger = Logger(open(parser_logfile, 'w')) if print_source_parser: self.parser_logger.add_switch(Logger.SOURCE) if print_productions: self.parser_logger.add_switch(Logger.PRODUCTION) if print_info: self.parser_logger.add_switch(Logger.INFO) # Initialize warning logger self.warning_logger = Logger(sys.stdout) if print_warnings: self.warning_logger.add_switch(Logger.WARNING) # Other stuff self.function_scope_entered = False self.insert_mode = True # for debugging purposes self.clone_symbol_table_on_next_scope_exit = False self.cloned_tables = [] # Create JSTLexer and the lexer object self.jst_lexer = JSTLexer(self) self.lexer = lex.lex(module=self.jst_lexer) # Create JSTParser and the parser object self.jst_parser = JSTParser(self) self.parser = yacc.yacc(module=self.jst_parser, start='program') # we will need a reference to the symbol for the main function self.main_function = None
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