示例#1
0
        public void Visit(ConditionalNode node)
        {
            node.If.Accept(this);
            node.Then.Accept(this);
            node.Else.Accept(this);

            if (node.If.staticType.Text != "Bool")
            {
                errors.Add(ErrorSemantic.CannotConvert(node.If, node.If.staticType, scope.GetType("Bool")));
            }

            node.staticType = SemanticAlgorithm.LowerCommonAncestor(node.Then.staticType, node.Else.staticType);
        }
示例#2
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));
            }
        }