Esempio n. 1
0
        internal static TableAddStatus AddVar(VarSymbol varItem)
        {
            if (FindVar(varItem.name)?.scopeId == ScopeManager.CurrentScope)
            {
                return(TableAddStatus.SYMBOL_EXIST);
            }

            if (!vars.ContainsKey(varItem.name))
            {
                vars.Add(varItem.name, new List <VarSymbol>());
            }

            vars[varItem.name].Add(varItem);
            vars[varItem.name].Sort((x, y) => y.scopeId - x.scopeId);

            return(TableAddStatus.SUCCEED);
        }
Esempio n. 2
0
        private void Para(FunSymbol fun)
        {
            VarSymbol para = new VarSymbol();

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

            string id = TagIs(Tag.ID) ? next.Value : null;

            if (id != null)
            {
                Move();
            }

            if (para.type != VarType.TYPE_VOID)
            {
                para.scopeId     = ScopeManager.CurrentScope;
                para.offsetInFun = -(2 + fun.parasType.Count) * 8;

                if (TagIs(Tag.DL_LSQU))
                {
                    para.eleType = para.type;
                    para.eleSize = Utils.SizeOf(para.eleType);
                    para.type    = VarType.TYPE_PTR;
                    Move();
                    RequiredToken(Tag.DL_RSQU);
                }
            }

            if (id != null)
            {
                para.name = id;
                if (SymbolTable.AddVar(para) == TableAddStatus.SYMBOL_EXIST)
                {
                    new ConflictingDeclarationError().PrintErrMsg();
                }
            }

            fun.parasType.Add(para.type);
        }
Esempio n. 3
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);
        }