Esempio n. 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);
        }
Esempio n. 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));
            }
        }
        public void Visit(ProgNode node)
        {
            if (!SemanticAlgorithm.CheckTopology(node.classes, errors))
            {
                return;
            }

            node.classes.ForEach(cclass => scope.AddType(cclass.type.Text, new TypeInfo(cclass.type.Text, scope.GetType(cclass.Inherit.Text), cclass)));

            int idMain = -1;

            for (int i = 0; i < node.classes.Count; ++i)
            {
                if (node.classes[i].type.Text == "Main")
                {
                    idMain = i;
                }
            }

            if (idMain == -1)
            {
                errors.Add(ErrorSemantic.NotFoundClassMain());
                return;
            }

            bool mainOK = false;

            foreach (var item in node.classes[idMain].features)
            {
                if (item is MethodNode)
                {
                    var method = item as MethodNode;
                    if (method.id.text == "main" && method.paramsFormal.Count == 0)
                    {
                        mainOK = true;
                    }
                }
            }

            if (!mainOK)
            {
                errors.Add(ErrorSemantic.NotFoundMethodmain(node.classes[idMain]));
            }

            foreach (var cclass in node.classes)
            {
                if (!scope.IsDefinedType(cclass.Inherit.Text, out TypeInfo type))
                {
                    errors.Add(ErrorSemantic.NotDeclaredType(cclass.Inherit));
                    return;
                }
                if (new List <string> {
                    "Bool", "Int", "String"
                }.Contains(type.Text))
                {
                    errors.Add(ErrorSemantic.NotInheritsOf(cclass, type));
                    return;
                }
                cclass.Accept(this);
            }
        }