public void Visit(IfNode node) { node.Condition.Accept(this); node.Body.Accept(this); node.ElseBody.Accept(this); if (node.Condition.StaticType.Text != "Bool") { errors.Add(SemanticError.CannotConvert(node.Condition, node.Condition.StaticType, scope.GetType("Bool"))); } node.StaticType = SemanticAlgorithm.LowerCommonAncestor(node.Body.StaticType, node.ElseBody.StaticType); }
public void Visit(CaseNode node) { node.ExpressionCase.Accept(this); int branchSelected = -1; var typeExp0 = node.ExpressionCase.StaticType; var typeExpK = scope.GetType(node.Branches[0].Formal.Type.Text); for (int i = 0; i < node.Branches.Count; ++i) { if (!scope.IsDefinedType(node.Branches[i].Formal.Type.Text, out TypeInfo type)) { errors.Add(SemanticError.NotDeclaredType(node.Branches[i].Formal.Type)); } var typeK = scope.GetType(node.Branches[i].Formal.Type.Text); var scopeBranch = scope.CreateChild(); scopeBranch.Define(node.Branches[i].Formal.Id.Text, typeK); node.Branches[i].Expression.Accept(new SecondSemanticVisit(scopeBranch, errors)); typeExpK = node.Branches[i].Expression.StaticType; if (branchSelected == -1 && typeExp0 <= typeK) { branchSelected = i; } if (i == 0) { node.StaticType = node.Branches[0].Expression.StaticType; } node.StaticType = SemanticAlgorithm.LowerCommonAncestor(node.StaticType, typeExpK); } node.BranchSelected = branchSelected; if (node.BranchSelected == -1) { errors.Add(SemanticError.NotMatchedBranch(node)); } }
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); } }