def visit(self, node, variables : Variables): self.code.append(PushIL()) res = variables.add_temp() child_vars = variables.get_copy() for expr in node.let_part: self.visit(expr, child_vars) child_vars = child_vars.get_copy() self.visit(node.in_part, child_vars) p = child_vars.peek_last() self.code.append(VarToVarIL(res, p)) self.code.append(PopIL(len(node.let_part) + 1))
def visit(self, node, variables): node.branches.sort(key = lambda x : self.depth[x.type], reverse = True) res = variables.add_temp() self.code.append(PushIL()) self.visit(node.main_expr, variables) m = variables.peek_last() b_labels = [LabelIL('branch', self.getInt()) for _ in node.branches] i = 0 for b in node.branches: temp = variables.add_temp() self.code.append(PushIL()) self.code.append(InheritIL(variables.id(m), b.type, variables.id(temp))) self.code.append(IfJumpIL(variables.id(temp),b_labels[i].label)) self.code.append(PopIL(1)) variables.pop_var() i += 1 end = LabelIL('end_case', self.get_Int()) i = 0 for b in node.branches: self.code.append(b_labels[i]) i += 1 Variables.pop_var() self.code.append(PopIL(1)) self.code.append(PushIL()) r = variables.add_var(b.id) self.code.append(VarToVarIL(variables.id(r), variables.id(m))) self.visit(b.expr, variables) p = variables.peek_last() self.code.append(VarToVarIL(variables.id(res), variables.id(p))) for _ in range(3): variables.pop_var() self.code.append(PopIL(3))
def bin_op(self, node, variables : Variables, sym : str): self.code.append(CommentIL('Binary_op')) self.code.append(PushIL(0)) res = variables.add_temp() self.visit(node.left, variables) l = variables.peek_last() self.visit(node.right, variables) r = variables.peek_last() self.code.append(BinOpIL(variables.id(res), variables.id(l), variables.id(r), sym)) variables.pop_var() variables.pop_var() self.code.append(PopIL(2))
def visit(self, node): self.code.append(LabelIL(self.cur_class, node.name, True)) #This has being push already before calling dispatch # El self se adiciona en el class node variables = Variables() variables.add_var('self') #Self its alway the first parameter for p in node.params: variables.add_var(p.name) variables.add_temp() # return address self.visit(node.body, variables) self.code.append(ReturnIL())
def visit(self, node, variables : Variables): self.code.append(PushIL()) p = variables.add_var(node.name.value) if node.expr != None: self.visit(node.expr, variables) r = variables.peek_last() self.code.append(VarToVarIL(variables.id(p), variables.id(r) )) variables.pop_var() self.code.append(PopIL(1))
def unary_op(self, node, variables : Variables , symb : str): self.code.append(CommentIL('Unary_op')) res = variables.add_temp() self.code.append(PushIL()) self.visit(node.expr, variables) l = variables.peek_last() self.code.append(UnaryOpIL(variables.id(res), variables.id(l), symb)) variables.pop_var() self.code.append(PopIL(1))
def create_constructor(self, attrs): self.code.append(LabelIL(self.cur_class, 'Constructor', True)) variables = Variables() variables.add_var('self') variables.add_temp() # return address for node in attrs: if node.value == None: continue self.visit(node.value, variables) p = variables.peek_last() idx = self.vt.get_attr_id(self.cur_class, node.name.value) self.code.append(VarToMemoIL(variables.id('self'), variables.id(p), idx)) variables.pop_var() self.code.append(PopIL(1)) self.code.append(PushIL()) #No result, but needed in logic self.code.append(ReturnIL())