def gencode(self, symbol_table, code): code_start = len(code) if isinstance(self.dict['Rhs'], BinaryExpression): self.dict['Rhs'].gencode(symbol_table, code) else: if isinstance(self.dict['Rhs'].data, lexer.Number.Int): code.append(('LIT', 0, self.dict['Rhs'].data.value)) elif isinstance(self.dict['Rhs'].data, lexer.Identifier): item, level, offset = symbol_table.search(self.dict['Rhs'].data.value) if item[1] in ['var', 'const']: code.append(('LOD', level, offset)) # print(type(self.dict['Rhs'].data)) # self.dict['Rhs'].print() level, offset = 0, 0 table_len = len(symbol_table.block_sym) for i in range(table_len): for j, item in enumerate(symbol_table[table_len - i - 1]): name = item[0] # print(self.dict['Lhs']) if name == self.dict['Lhs'].data.value: if item[1] == 'var': level = i offset = j # print("level {}| offset {}".format(level, offset)) code.append(('STO', level, offset)) # print(code[code_start:]) return else: raise SemanticException("get type <{}>, but expect <var> in [Assign]".format(item[1])) raise SemanticException('undefined reference for {}'.format(self.dict['Lhs'].data.value))
def gencode(self, symbol_table, code): insert = BinaryExpression.oprator_to_code[self.data] # print(insert) if isinstance(self.dict['Lhs'], BinaryExpression): self.dict['Lhs'].gencode(symbol_table, code) else: if isinstance(self.dict['Lhs'].data, lexer.Identifier): name = self.dict['Lhs'].data.value item, level, offset = symbol_table.search(name) if item[1] in ['var', 'const']: code.append(('LOD', level, offset)) else: raise SemanticException("{} get type <{}>, but expect <var> <const> in [{}]".format(name, item[1], self.data)) else: code.append(('LIT', 0, self.dict['Lhs'].data.value)) if isinstance(self.dict['Rhs'], BinaryExpression): self.dict['Rhs'].gencode(symbol_table, code) else: if isinstance(self.dict['Rhs'].data, lexer.Identifier): name = self.dict['Rhs'].data.value item, level, offset = symbol_table.search(name) if item[1] in ['var', 'const']: code.append(('LOD', level, offset)) else: raise SemanticException("{} get type <{}>, but expect <var> <const> in [{}]".format(name, item[1], self.data)) else: code.append(('LIT', 0, self.dict['Rhs'].data.value)) # print(self.dict['Rhs']) code.append(insert)
def gencode(self, symbol_table, code): block_chain_len = len(symbol_table.block_sym) for i in range(block_chain_len): block = symbol_table[block_chain_len - i - 1] for j, item in enumerate(block): if item[0] == self.dict['Identifier'].data.value: if item[1] == 'proc': # print('level {}'.format(i)) code.append(('CAL', i, item[2])) return else: raise SemanticException("get type <{}>, but expect <procedure> in call".format(item[1])) raise SemanticException('undefined reference {}'.format(self.dict['Identifier'].data.value))
def gencode(self, symbol_table, code): code_start = len(code) # print(type(self.dict['Expression'])) if isinstance(self.dict['Expression'], BinaryExpression): self.dict['Expression'].gencode(symbol_table, code) else: name = self.dict['Expression'].data.value item, level, offset = symbol_table.search(name) expected = ['var', 'const'] if item[1] in expected: code.append(('LOD', level, offset)) else: raise SemanticException("{} get type <{}>, but expect <var> <const> in [{}]".format(name, item[1], self.data)) code.append(('OPR', 0, 6)) # odd operator
def gencode(self, symbol_table, code): # code_start = len(code) - 1 iotype = self.dict['IOType'].data.value args = [self.dict['Arguments'].child.data.value] item, level, offset = symbol_table.search(args[0]) if iotype == 'write': if item[1] != 'proc': code.append(('LOD', level, offset)) code.append(('WRT', 0, 0)) else: raise SemanticException("get type <{}>, but expect <var> or <const>".format(item[1])) elif iotype == 'read': if item[1] == 'var': code.append(('RED', level, offset)) else: raise SemanticException("get type <{}>, but expect <var>".format(item[1])) other_args = [ arg.data.value for arg in self.dict['Arguments'].child.sublings] args.extend(other_args) last_args = args[0] for i, arg_name in enumerate(other_args): item, level, offset = symbol_table.search(arg_name) if iotype == 'write': if item[1] != 'proc': code.append(('LOD', level, offset)) code.append(('WRT', 0, 0)) else: raise SemanticException("get type <{}>, but expect <var> or <const> in write()".format(item[1])) elif iotype == 'read': if item[1] == 'var': if arg_name == last_args: # duplicated read operation print('[WARING] duplicated read operation') last_args = arg_name code.append(('RED', level, offset)) else: raise SemanticException("get type <{}>, but expect <var> in read".format(item[1]))
def insert(self, table_item): if len(table_item) >= 3: name = table_item[0] item, level, offset = self.search_safe(name) if level == 0: raise SemanticException( 'duplicate declaration of {}'.format(name)) # if item[1] == table_item[1]: # print('[WARNING] duplicate declaration of {}, overwriting'.format(name)) # return offset # else: # print('[WARNING] change type of {}, from <{}> to <{}>'.format(name, item[1], table_item[1])) # self.block_sym[-1].table[offset] = table_item # return offset # self.block_sym[-1][offset] = table_item else: self.block_sym[-1].append(table_item) return -1 else: raise RuntimeError('table item must be at least 3')
def search(self, name): item, level, offset = self.search_safe(name) if item is not None: return item, level, offset raise SemanticException('undefined reference {}'.format(name))