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)); } }