def evaluate(self, symbol_table): var_id = self.children[0].value var_type = self.children[1].evaluate(symbol_table) if var_id in symbol_table.table: raise Exception(f'Variable {var_id} already exists!') Translator.insert('PUSH DWORD 0; \n') symbol_table.alloc(var_id, var_type) return
def evaluate(self, symbol_table): Translator.insert(f'LOOP_{self.id}; \n') self.children[0].evaluate(symbol_table) Translator.insert('CMP EBX, False; \n') Translator.insert(f'JE EXIT_{self.id}; \n') self.children[1].evaluate(symbol_table) Translator.insert(f'JMP LOOP_{self.id}; \n') Translator.insert(f'EXIT_{self.id}; \n') # while self.children[0].evaluate(symbol_table)[0] == True: # self.children[1].evaluate(symbol_table) return
def evaluate(self, symbol_table): _, var_type, displac = symbol_table.table[self.left.value] var_value, var_type2 = self.right.evaluate(symbol_table) if var_type != var_type2: raise Exception( f'Variable type ({var_type}) do not match with expression type ({var_type2})' ) disp = symbol_table.table[self.left.value][2] Translator.insert(f'MOV [EBP {disp}], EBX; \n') symbol_table.set_value(self.left.value, var_value) return
def evaluate(self, symbol_table): print(self.children[0].evaluate(symbol_table)[0]) Translator.insert('PUSH EBX; \n') Translator.insert('CALL print; \n') Translator.insert('POP EBX; \n') # print(''.join(Translator.code)) return
def evaluate(self, symbol_table): Translator.insert(f'MOV EBX, {self.value.capitalize()}; \n') return self.value, 'BOOLEAN'
def evaluate(self, symbol_table): val1, var_type1 = self.left.evaluate(symbol_table) Translator.insert('PUSH EBX; \n') val2, var_type2 = self.right.evaluate(symbol_table) Translator.insert('POP EAX; \n') if var_type1 != var_type2: raise Exception( f'Cannot perform {self.value} with {var_type1} and {var_type2}' ) if var_type1 == 'INT': if self.value == '+': Translator.insert('ADD EAX, EBX; \n') Translator.insert('MOV EBX, EAX; \n') return val1 + val2, var_type1 elif self.value == '-': Translator.insert('SUB EAX, EBX; \n') Translator.insert('MOV EBX, EAX; \n') return val1 - val2, var_type1 elif self.value == '*': Translator.insert('IMUL EBX; \n') Translator.insert('MOV EBX, EAX; \n') return val1 * val2, var_type1 elif self.value == '/': Translator.insert('IDIV EBX; \n') Translator.insert('MOV EBX, EAX; \n') return val1 // val2, var_type1 elif self.value == '>': Translator.insert('CMP EAX, EBX; \n') Translator.insert('CALL binop_jg; \n') return val1 > val2, var_type1 elif self.value == '<': Translator.insert('CMP EAX, EBX; \n') Translator.insert('CALL binop_jl; \n') return val1 < val2, var_type1 elif self.value == '=': Translator.insert('CMP EAX, EBX; \n') Translator.insert('CALL binop_je; \n') return val1 == val2, var_type1 elif var_type1 == 'BOOLEAN': if self.value == 'AND': Translator.insert('AND EAX, EBX; \n') Translator.insert('MOV EBX, EAX; \n') return val1 and val2, var_type1 elif self.value == 'OR': Translator.insert('OR EAX, EBX; \n') Translator.insert('MOV EBX, EAX; \n') return val1 or val2, var_type1 elif self.value == '=': Translator.insert('CMP EAX, EBX; \n') Translator.insert('CALL binop_je; \n') return val1 == val2, var_type1 else: raise Exception( f'Cannot perform {self.value} with {var_type1} and {var_type2}' )
def evaluate(self, symbol_table): disp = symbol_table.table[self.value][2] Translator.insert(f'MOV EBX, [EBP {disp}]; \n') return symbol_table.get_value(self.value)
def evaluate(self, symbol_table): Translator.insert(f'MOV EBX, {self.value}; \n') return self.value, 'INT'