def visit(self, node: FuncDeclarationNode, scope: Scope): current_method = self.current[TYPE].get_method(node.id, node.pos) name = Utils.CreateName2(FUNCTION, node.id, self.current[TYPE].name) function_node = CILFunctionNode(name, [], [], [], COUNT) increment() self.cil_program_node_info[CODE].append(function_node) self.current[FUNCTION] = function_node param_node = CILParamNode(SELF_LOWERCASE, self.current[TYPE].name, COUNT) increment() self.current[FUNCTION].params.append(param_node) for p_name, p_type in node.params: param_node = CILParamNode(p_name, p_type.value, COUNT) increment() self.current[FUNCTION].params.append(param_node) value, typex = self.visit(node.body, scope) if not isinstance(value, str): local_node = self.generateLocalNode(LOCAL) self.saveCilInstruction(CILAssignNode(local_node, value)) else: local_node = value if typex.name in [STRING, BOOL, INT] and current_method.return_type.name == OBJECT: self.saveCilInstruction(CILBoxingNode(local_node, typex.name)) self.saveCilInstruction(CILReturnNode(local_node))
def visit(self, node: ClassDeclarationNode, scope: Scope): self.current[TYPE] = self.context.get_type(node.id, node.pos) cil_type = CILTypeNode(node.id) self.cil_program_node_info[TYPE].append(cil_type) attrs = self.current[TYPE].all_attributes() if len(attrs) > 0: constr = FuncDeclarationNode(node.token, [], node.token, BlockNode([], node.token)) func_declarations = [constr] self.constrs.append(node.id) self.current[TYPE].define_method(self.current[TYPE].name, [], [], self.current[TYPE], node.pos) scopes = [scope] + list(scope.functions.values()) else: func_declarations = [] scopes = list(scope.functions.values()) for attr, a_type in attrs: cil_type.attributes.append( (attr.name, Utils.CreateName2(ATTRIBUTE, attr.name, a_type.name))) if attr.expr: constr.body.expr_list.append(AssignNode(attr.name, attr.expr)) elif attr.type == INT: constr.body.expr_list.append(AssignNode(attr.name, ConstantNumNode(0))) elif attr.type == BOOL: constr.body.expr_list.append(AssignNode(attr.name, ConstantBoolNode(False))) elif attr.type == STRING: constr.body.expr_list.append( AssignNode(attr.name, ConstantStrNode(""))) else: constr.body.expr_list.append(AssignNode(attr.name, ConstantVoidNode(attr.name))) if attrs: constr.body.expr_list.append(SelfNode()) for method, mtype in self.current[TYPE].all_methods(): cil_type.methods.append( (method.name, Utils.CreateName2(FUNCTION, method.name, mtype.name))) func_declarations += [ f for f in node.features if isinstance(f, FuncDeclarationNode)] for feature, child_scope in zip(func_declarations, scopes): self.visit(feature, child_scope)