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 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())
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 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 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))