public void Visit(ArithmeticOperationNode node) { node.LeftOperand.Accept(this); node.RightOperand.Accept(this); if (node.LeftOperand.StaticType.Text != node.RightOperand.StaticType.Text) { errors.Add(SemanticError.InvalidUseOfOperator(node, node.LeftOperand.StaticType, node.RightOperand.StaticType)); } else if (node.LeftOperand.StaticType.Text != "Int" || node.RightOperand.StaticType.Text != "Int") { errors.Add(SemanticError.InvalidUseOfOperator(node)); } else if (!scope.IsDefinedType("Int", out node.StaticType)) { errors.Add(SemanticError.NotDeclaredType(new TypeNode(node.Line, node.Column, "Int"))); } }
public void Visit(MethodNode node) { if (!scope.IsDefinedType(node.TypeReturn.Text, out TypeInfo typeReturn)) { errors.Add(SemanticError.NotDeclaredType(node.TypeReturn)); } node.TypeReturn = new TypeNode(node.TypeReturn.Line, node.TypeReturn.Column, typeReturn.Text); TypeInfo[] typeArgs = new TypeInfo[node.Arguments.Count]; for (int i = 0; i < node.Arguments.Count; ++i) { if (!scope.IsDefinedType(node.Arguments[i].Type.Text, out typeArgs[i])) { errors.Add(SemanticError.NotDeclaredType(node.Arguments[i].Type)); } } scope.Define(node.Id.Text, typeArgs, typeReturn); }
public void Visit(ProgramNode node) { if (!SemanticAlgorithm.TopologicalSort(node.Classes, errors)) { return; } node.Classes.ForEach(cclass => scope.AddType(cclass.TypeClass.Text, new TypeInfo(cclass.TypeClass.Text, scope.GetType(cclass.TypeInherit.Text), cclass))); int idMain = -1; for (int i = 0; i < node.Classes.Count; ++i) { if (node.Classes[i].TypeClass.Text == "Main") { idMain = i; } } if (idMain == -1) { errors.Add(SemanticError.NotFoundClassMain()); return; } bool mainOK = false; foreach (var item in node.Classes[idMain].FeatureNodes) { if (item is MethodNode) { var method = item as MethodNode; if (method.Id.Text == "main" && method.Arguments.Count == 0) { mainOK = true; } } } if (!mainOK) { errors.Add(SemanticError.NotFoundMethodmain(node.Classes[idMain])); } foreach (var cclass in node.Classes) { if (!scope.IsDefinedType(cclass.TypeInherit.Text, out TypeInfo type)) { errors.Add(SemanticError.NotDeclaredType(cclass.TypeInherit)); return; } if (new List <string> { "Bool", "Int", "String" }.Contains(type.Text)) { errors.Add(SemanticError.NotInheritsOf(cclass, type)); return; } cclass.Accept(this); } }