Exemplo n.º 1
0
        public override ASTNode VisitParameterDeclaration([NotNull] ParameterDeclarationContext ctx)
        {
            DeclSpecsNode declSpecs = this.Visit(ctx.declarationSpecifiers()).As <DeclSpecsNode>();
            DeclNode      decl      = this.Visit(ctx.declarator()).As <DeclNode>();

            return(new FuncParamNode(ctx.Start.Line, declSpecs, decl));
        }
Exemplo n.º 2
0
 public DeclaratorMismatchWarning(DeclNode expected, DeclNode actual)
 {
     if (expected.Equals(actual))
     {
         throw new ArgumentException("Expected different objects");
     }
     this.Expected = expected;
     this.Actual   = actual;
 }
 public static DeclaredSymbol From(DeclSpecsNode specs, DeclNode decl)
 {
     return(decl switch
     {
         VarDeclNode var => new DeclaredVariableSymbol(decl.Identifier, specs, var, var.Initializer),
         ArrDeclNode arr => new DeclaredArraySymbol(decl.Identifier, specs, arr, arr.SizeExpression, arr.Initializer),
         FuncDeclNode f => new DeclaredFunctionSymbol(decl.Identifier, specs, f),
         _ => throw new NotImplementedException("Declarator node type not yet implemented"),
     });
Exemplo n.º 4
0
        private void Block(BlockNode node)
        {
            RequiredToken(Tag.DL_LBRACE);

            while (!TagIs(Tag.DL_RBRACE) && !TagIs(Tag.NULL))
            {
                if (Utils.IsTypeTag(next.Tag) || TagIs(Tag.KW_CONST))
                {
                    DeclNode decl = new DeclNode();
                    node.AddChild(decl);
                    Decl(decl);
                }
                else
                {
                    StmtNode stmt = new StmtNode();
                    node.AddChild(stmt);
                    Stmt(stmt);
                }
            }

            RequiredToken(Tag.DL_RBRACE);
        }
 public ExtraDeclarationWarning(DeclSpecsNode declarationSpecifiers, DeclNode declarator)
 {
     this.DeclarationSpecifiers = declarationSpecifiers;
     this.Declarator            = declarator;
 }
Exemplo n.º 6
0
 public FuncParamNode(int line, DeclSpecsNode declSpecs, DeclNode declarator)
     : base(line, declSpecs, declarator)
 {
 }
Exemplo n.º 7
0
        private void Decl(DeclNode node)
        {
            FunSymbol function = SymbolTable.FindFun(ScopeManager.CurrentFun);
            VarSymbol variable = new VarSymbol();

            if (TagIs(Tag.KW_CONST))
            {
                variable.isConst = true;
                Move();
            }

            node.type = Utils.TagToType(next.Tag);
            Move();

            if (node.type == VarType.TYPE_VOID)
            {
                new Error().PrintErrMsg();
            }

            node.name = RequiredToken(Tag.ID);

            if (TagIs(Tag.DL_SET))
            {
                Move();
                node.init = new ExprNode();
                Expr(node.init);

                if (node.init.Type() != node.type)
                {
                    if (!Utils.IsNumType(node.init.Type()) || !Utils.IsNumType(node.type))
                    {
                        new TypeMismatchError(node.type, node.init.Type()).PrintErrMsg();
                    }
                }
            }
            else if (TagIs(Tag.DL_LSQU))
            {
                Move();
                variable.eleSize = Utils.SizeOf(node.type);
                variable.eleType = node.type;
                variable.isConst = true;
                node.type        = VarType.TYPE_PTR;
                node.size        = new ExprNode();
                Expr(node.size);

                if (!Utils.IsNumType(node.size.Type()) || !node.size.IsConstant())
                {
                    new Error().PrintErrMsg();
                }
                else
                {
                    function.localVarSize += (int)(node.size.Val() as IntNode).value * variable.eleSize;
                }

                RequiredToken(Tag.DL_RSQU);
            }

            if (node.name != null)
            {
                function.localVarSize += Utils.SizeOf(node.type);

                variable.name        = node.name;
                variable.type        = node.type;
                variable.scopeId     = ScopeManager.CurrentScope;
                variable.offsetInFun = function.localVarSize;

                if (SymbolTable.AddVar(variable) == TableAddStatus.SYMBOL_EXIST)
                {
                    new ConflictingDeclarationError().PrintErrMsg();
                }
            }

            RequiredToken(Tag.DL_SEM);
        }