示例#1
0
        public void Visit(CaseNode node)
        {
            node.CaseBase.Accept(this);

            int branchSelected = -1;
            var typeExp0       = node.CaseBase.staticType;
            var typeExpK       = scope.GetType(node.Branchs[0].formal.type.Text);

            for (int i = 0; i < node.Branchs.Count; ++i)
            {
                if (!scope.IsDefinedType(node.Branchs[i].formal.type.Text, out TypeInfo type))
                {
                    errors.Add(ErrorSemantic.NotDeclaredType(node.Branchs[i].formal.type));
                }

                var typeK = scope.GetType(node.Branchs[i].formal.type.Text);

                var scopeBranch = scope.CreateChild();
                scopeBranch.Define(node.Branchs[i].formal.id.text, typeK);

                node.Branchs[i].expr.Accept(new SemanticVisit(errors, scopeBranch));

                typeExpK = node.Branchs[i].expr.staticType;

                if (branchSelected == -1 && typeExp0 <= typeK)
                {
                    branchSelected = i;
                }

                if (i == 0)
                {
                    node.staticType = node.Branchs[0].expr.staticType;
                }
                node.staticType = SemanticAlgorithm.LowerCommonAncestor(node.staticType, typeExpK);
            }
            node.Select = branchSelected;

            if (node.Select == -1)
            {
                errors.Add(ErrorSemantic.NotMatchedBranch(node));
            }
        }