def visit(self, node, enviroment): current_enviroment = enviroment for _decl in node.declaration_list: if _decl.name == 'self': errors.throw_error( errors.SemanticError( text= f"It is an error to bind self in a let expression.", line=_decl.line, column=_decl.column)) if _decl._type == 'SELF_TYPE': _type = Type('SELF_TYPE', self.current_type.name) else: _type = current_enviroment.get_type(_decl._type) if _type is None: errors.throw_error( errors.TypeError( text= f"Type '{_decl._type}' declared for variable '{_decl.name}' is not defined.", line=_decl.line, column=_decl.column)) # check if _decl has an init expression if _decl.expression is not None: self.visit(_decl.expression, current_enviroment) if not enviroment.conforms(_decl.expression.computed_type, _type): errors.throw_error( errors.TypeError( text= f"Type of initialization expression for variable '{_decl.name}' does not conform with its declared type '{_decl._type}'.", line=_decl.line, column=_decl.column)) new_child_enviroment = current_enviroment.create_child_enviroment() if _type.name == 'SELF_TYPE': new_child_enviroment.define_symbol(_decl.name, 'SELF_TYPE' + _type.parent, _decl.line, _decl.column) else: new_child_enviroment.define_symbol(_decl.name, _type.name, _decl.line, _decl.column) current_enviroment = new_child_enviroment self.visit(node.expression, current_enviroment) node.computed_type = node.expression.computed_type
def visit(self, node): # node.type_attribute can be SELF_TYPE if node.type_attribute == 'SELF_TYPE': attribute_type = Type('SELF_TYPE', self.current_type.name) else: attribute_type = self.enviroment.get_type(node.type_attribute) if attribute_type is not None: if node.name == "self": errors.throw_error( errors.SemanticError( text=f"Name attribute can not be self.", line=node.line, column=node.column)) else: ans = self.current_type.define_attribute( node.name, node.type_attribute, node.line, node.column) if not ans: errors.throw_error( errors.SemanticError( text= f"In class '{self.current_type.name}' attribute '{node.name}' is defined multiple times.", line=node.line, column=node.column)) else: errors.throw_error( errors.TypeError( text= f"The type '{node.type_attribute}' of attribute '{node.name}' is missing.", line=node.line, column=node.column))
def visit(self, node, enviroment): child_enviroment = enviroment.create_child_enviroment() child_enviroment.define_symbol('self', 'SELF_TYPE' + self.current_type.name, 0, 0) for formal_param in node.formal_parameter_list: child_enviroment.define_symbol(formal_param.name, formal_param.type_parameter, formal_param.line, formal_param.column) self.visit(node.expression, child_enviroment) if node.return_type_method == 'SELF_TYPE': _type = Type('SELF_TYPE', self.current_type.name) else: # type_builder guarantee that _type exists _type = enviroment.get_type(node.return_type_method) if not enviroment.conforms(node.expression.computed_type, _type): errors.throw_error( errors.TypeError( text= f"Body expression type for method '{node.name}' does not conforms with its return type.", line=node.line, column=node.column)) node.computed_type = node.expression.computed_type
def visit(self, node, enviroment): self.visit(node.expression, enviroment) if node.expression.computed_type.name != "Int": errors.throw_error( errors.TypeError( text= f"The expression for unary operation 'Neg' must have type 'Int'.", line=node.line, column=node.column)) node.computed_type = enviroment.get_type("Int")
def visit(self, node, enviroment): self.visit(node.while_expression, enviroment) self.visit(node.loop_expression, enviroment) if node.while_expression.computed_type.name != "Bool": errors.throw_error( errors.TypeError( text=f"The predicate of a loop must have type 'Bool'.", line=node.line, column=node.column)) node.computed_type = enviroment.get_type("Object")
def visit(self, node, enviroment): self.visit(node.left_expression, enviroment) self.visit(node.right_expression, enviroment) if node.left_expression.computed_type.name != 'Int' or node.right_expression.computed_type.name != 'Int': errors.throw_error( errors.TypeError( text= f"In arithmetic operation type of letf and right expressions must be 'Int'.", line=node.line, column=node.column)) node.computed_type = enviroment.get_type("Int")
def visit(self, node, enviroment): if node.new_type == 'SELF_TYPE': node.computed_type = Type('SELF_TYPE', self.current_type.name) else: _type = enviroment.get_type(node.new_type) if _type is None: errors.throw_error( errors.TypeError( text= f"In new expression type '{node.new_type}' does not exists.", line=node.line, column=node.column)) node.computed_type = _type
def visit(self, node, enviroment): self.visit(node.if_expression, enviroment) self.visit(node.then_expression, enviroment) self.visit(node.else_expression, enviroment) if node.if_expression.computed_type.name != "Bool": errors.throw_error( errors.TypeError(text=f"If expression type must be 'Bool'.", line=node.line, column=node.column)) node.computed_type = enviroment.lca([ node.then_expression.computed_type, node.else_expression.computed_type ])
def visit(self, node, enviroment): self.visit(node.instance, enviroment) for _expr in node.arguments: self.visit(_expr, enviroment) instance_type = node.instance.computed_type if node.instance.computed_type.name != 'SELF_TYPE' else enviroment.get_type( node.instance.computed_type.parent) meth = instance_type.get_method( node.method, enviroment) # enviroment is used to search for inherited methods if meth is None: errors.throw_error( errors.AttributeError( text= f"Class '{instance_type.name}' does not contain a method named '{node.method}'.", line=node.line, column=node.column)) # check if the number of arguments is correct if len(node.arguments) != len(meth.attribute_list): errors.throw_error( errors.SemanticError( text= f"Dynamic dispatch does not match with the signature of method '{node.method}'.", line=node.line, column=node.column)) # check if each arg type conforms with the formal parameter type in the method signautre for i in range(len(node.arguments)): if not enviroment.conforms( node.arguments[i].computed_type, enviroment.get_type(meth.attribute_list[i]._type)): errors.throw_error( errors.TypeError( text= f"In dynamic dispatch of method '{node.method}' the type of the argument at index {i} does not conforms with the type of the formal parameter in the method signature.", line=node.line, column=node.column)) # The return type of a method always exits, its garanty in the type_builder if meth.return_type == 'SELF_TYPE': node.computed_type = node.instance.computed_type else: node.computed_type = enviroment.get_type(meth.return_type)
def visit(self, node, enviroment): self.visit(node.case_expression, enviroment) variable_types = [] for _branch in node.branch_list: if _branch.name == 'self': errors.throw_error( errors.SemanticError( text= f"It is an error to bind self in a case expression.", line=_branch.line, column=_branch.column)) if _branch.type_branch == 'SELF_TYPE': errors.throw_error( errors.SemanticError( text=f"Branch declared type cannot be 'SELF_TYPE'.", line=_branch.line, column=_branch.column)) _type = enviroment.get_type(_branch.type_branch) if _type is None: errors.throw_error( errors.TypeError( text= f"Type '{_branch.type_branch}' declared for variable '{_branch.name}' is not defined.", line=_branch.line, column=_branch.column)) if _type in variable_types: errors.throw_error( errors.SemanticError( text= f"In case expression all types declared type must be different.", line=node.line, column=node.column)) variable_types.append(_type) static_types = [] for _branch in node.branch_list: child_enviroment = enviroment.create_child_enviroment() child_enviroment.define_symbol(_branch.name, _branch.type_branch, _branch.line, _branch.column) self.visit(_branch.expression, child_enviroment) _branch.computed_type = _branch.expression.computed_type # in a BranchNode computed_type means the computed type of its expression static_types.append(_branch.computed_type) node.computed_type = enviroment.lca(static_types)
def visit(self, node, enviroment): self.visit(node.left_expression, enviroment) self.visit(node.right_expression, enviroment) basic_types = ['Int', 'String', 'Bool'] if node.left_expression.computed_type.name in basic_types \ or node.right_expression.computed_type.name in basic_types: if node.left_expression.computed_type.name != node.right_expression.computed_type.name: errors.throw_error( errors.TypeError( text= f"In an equal operation, both expressions must have the same basic type.", line=node.line, column=node.column)) node.computed_type = enviroment.get_type('Bool')
def visit(self, node, enviroment): if node.instance.name == 'self': errors.throw_error( errors.SemanticError(text=f"It is an error to assign to self.", line=node.line, column=node.column)) self.visit(node.instance, enviroment) self.visit(node.expression, enviroment) if not enviroment.conforms(node.expression.computed_type, node.instance.computed_type): errors.throw_error( errors.TypeError( text= f"In assign expression type does not conforms with variable '{node.instance.name}' declared type.", line=node.line, column=node.column)) node.computed_type = node.expression.computed_type
def visit(self, node, enviroment): # [Attr-No-Init] node.computed_type = enviroment.get_symbol_type(node.name) # check if attribute has an initialization expression if node.expression is not None: # [Attr-Init] child_enviroment = enviroment.create_child_enviroment() child_enviroment.define_symbol( 'self', 'SELF_TYPE' + self.current_type.name, 0, 0) self.visit(node.expression, child_enviroment) if not enviroment.conforms(node.expression.computed_type, node.computed_type): errors.throw_error( errors.TypeError( text= f"Initialization expression type for attribute '{node.name}' does not conforms with its declared type.", line=node.line, column=node.column)) node.computed_type = node.expression.computed_type
def visit(self, node): self.current_type = self.enviroment.get_type(node.name) parent_type = self.enviroment.get_type(node.parent) if parent_type is None and node.name != "Object": errors.throw_error( errors.TypeError( text= f"In class '{self.current_type.name}' parent type '{node.parent}' is missing.", line=node.line, column=node.column)) if parent_type.name in ['Int', 'String', 'Bool']: errors.throw_error( errors.SemanticError( text= f"In class '{self.current_type.name}' it is an error to inherit from basic class '{node.parent}'.", line=node.line, column=node.column)) for attribute in node.attribute_list: self.visit(attribute) for method in node.method_list: self.visit(method)
def visit(self, node): # node.return_type_method can be SELF_TYPE if node.return_type_method == 'SELF_TYPE': return_type = Type('SELF_TYPE', self.current_type.name) else: return_type = self.enviroment.get_type(node.return_type_method) if return_type is not None: # formal_parameter_list argument_list = [] for parameter in node.formal_parameter_list: if parameter.name == 'self': errors.throw_error( errors.SemanticError( text= f"In method '{node.name}' it is an error to bind self as a formal parameter.", line=node.line, column=node.column)) if parameter.name in argument_list: errors.throw_error( errors.SemanticError( text= f"In method '{node.name}' the argument '{parameter.name}' is defined multiple times.", line=node.line, column=node.column)) argument_list.append(parameter.name) argument_types = [] for parameter in node.formal_parameter_list: if parameter.type_parameter == 'SELF_TYPE': errors.throw_error( errors.TypeError( text= f"In method '{node.name}' the type of argument '{parameter.name}' cannot be SELF_TYPE.", line=node.line, column=node.column)) _type = self.enviroment.get_type(parameter.type_parameter) if _type is not None: argument_types.append(parameter.type_parameter) else: errors.throw_error( errors.TypeError( text= f"The type of the parameter '{parameter.name}' in method '{node.name}' is missing.", line=node.line, column=node.column)) ans = self.current_type.define_method(node.name, node.return_type_method, argument_list, argument_types, node.line, node.column) if not ans: errors.throw_error( errors.SemanticError( text= f"In class '{self.current_type.name}' method '{node.name}' is defined multiple times.", line=node.line, column=node.column)) else: errors.throw_error( errors.TypeError( text= f"In class '{self.current_type.name}' return type of method '{node.name}' is missing.", line=node.line, column=node.column))