예제 #1
0
        public void Push(Syntax.Identifier identifier)
        {
            Reg r1 = GetFreeReg();

            Symbols.Var var = identifier.GetVariable();
            AddCode(Com.LEA, new Addr(r1), new Val(this.address[var]));                         //lea eax, cast[addr]
            AddCode(Com.PUSH, new Addr(r1));                                                    //push eax
            FreeReg(r1);
            this.stack.Push(Type.GetType(var.GetType().GetSizeType()));
        }
예제 #2
0
        private Syntax.Expression ParsePrimaryExpr()
        {
            Syntax.Expression res = null;
            switch (scan.Peek().type)
            {
            case Token.Type.CONST_CHAR:
                res = new Syntax.Const(scan.Read(), tstack.GetType("char"));
                break;

            case Token.Type.CONST_DOUBLE:
                res = new Syntax.Const(scan.Read(), tstack.GetType("double"));
                break;

            case Token.Type.CONST_INT:
                res = new Syntax.Const(scan.Read(), tstack.GetType("int"));
                break;

            case Token.Type.CONST_STRING:
                this.count_string++;
                Symbols.ARRAY strt = new Symbols.ARRAY(tstack.GetType("char"));
                Token         str  = scan.Read();
                strt.SetSize(new Syntax.Const(str.GetStrVal().Length.ToString(), tstack.GetType("int")));
                res = new Syntax.Const(str, strt);
                break;

            case Token.Type.IDENTIFICATOR:
                Token       t = scan.Read();
                Symbols.Var v = new Symbols.SuperVar();
                try
                {
                    v = tstack.GetVariable(t);
                }
                catch (Symbols.Exception e)
                {
                    this.logger.Add(e);
                }

                res = new Syntax.Identifier(t, v);
                break;

            case Token.Type.LPAREN:
                scan.Read();
                res = ParseExpression();

                CheckToken(scan.Peek(), Token.Type.RPAREN, true);
                break;
            }
            return(res);
        }
예제 #3
0
        private Syntax.Expression ParsePrimaryExpr()
        {
            Syntax.Expression res = null;
            switch (scan.Peek().type)
            {
                case Token.Type.CONST_CHAR:
                    res = new Syntax.Const(scan.Read(), tstack.GetType("char"));
                    break;

                case Token.Type.CONST_DOUBLE:
                    res = new Syntax.Const(scan.Read(), tstack.GetType("double"));
                    break;

                case Token.Type.CONST_INT:
                    res = new Syntax.Const(scan.Read(), tstack.GetType("int"));
                    break;

                case Token.Type.CONST_STRING:
                    this.count_string++;
                    Symbols.ARRAY strt = new Symbols.ARRAY(tstack.GetType("char"));
                    Token str = scan.Read();
                    strt.SetSize(new Syntax.Const(str.GetStrVal().Length.ToString(), tstack.GetType("int")));
                    res = new Syntax.Const(str, strt);
                    break;

                case Token.Type.IDENTIFICATOR:
                    Token t = scan.Read();
                    Symbols.Var v = new Symbols.SuperVar();
                    try
                    {
                        v = tstack.GetVariable(t);
                    }
                    catch (Symbols.Exception e)
                    {
                        this.logger.Add(e);
                    }

                    res = new Syntax.Identifier(t, v);
                    break;

                case Token.Type.LPAREN:
                    scan.Read();
                     res = ParseExpression();

                    CheckToken(scan.Peek(), Token.Type.RPAREN, true);
                    break;
            }
            return res;
        }
예제 #4
0
        private void ParseDeclaration()
        {
            while (scan.Peek().type == Token.Type.SEMICOLON)
            {
                scan.Read();
            }
            if (scan.Peek().type == Token.Type.EOF)
            {
                return;
            }

            Token first_token = scan.Peek();
            Symbols.Type type = ParseTypeSpecifier(), variable_type = null;

            Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>> pair = null;
            bool function = true;

            while (true)
            {
                variable_type = type;

                if ((pair = ParseDeclarator()) != null)
                {
                    if (pair.last != null)
                    {
                        pair.last.last.SetType(type);
                        variable_type = pair.last.first;
                    }

                    ///Когда мы устанавливаем финальный тип, то нужно пересчитать размер всех типов

                    if (pair.first == null && !(variable_type is Symbols.RECORD))
                    {
                        throw new Syntax.Exception("требуется идентификатор", scan.GetPos(), scan.GetLine());
                    }

                    if (pair.first != null)
                    {
                        pair.first.SetType(variable_type);
                    }

                    if (scan.Peek().type == Token.Type.OP_ASSIGN) // init declarator
                    {
                        Symbols.Var v = pair.first;
                        Syntax.Expression node = new Syntax.Identifier(
                            new Token(v.GetIndex(), v.GetLine(), Token.Type.IDENTIFICATOR, v.GetName()),
                            v
                        );
                        node = ParseBinaryOper(0, node, true);

                        //pair.first.SetInitializer(ParseInitializer());
                        pair.first.SetInitializer(node);
                        function = false;
                    }

                    if (function && scan.Peek().type == Token.Type.LBRACE && tstack.IsGlobal())
                    {
                        pair.last.first.SetName(pair.first.GetName());
                        tstack.AddSymbol(pair.first);
                        tstack.NewTable();

                        if (((Symbols.Func)pair.last.first).GetArguments()
                            .Count(x => x.GetName() == pair.first.GetName()) == 0)
                        {
                            tstack.AddSymbol(pair.first);
                        }

                        foreach (Symbols.Var arg in ((Symbols.Func)pair.last.first).GetArguments())
                        {
                            tstack.AddSymbol(arg);
                        }
                        this.ret_func_type = ((Symbols.Func)pair.last.first).GetRefType();
                        ((Symbols.Func)pair.last.first).SetBody(ParseCompound(false));
                        tstack.GetCurrentTable().RemoveSymbol(pair.first);
                        ((Symbols.Func)pair.last.first).SetTable(tstack.PopTable());
                    }
                    else
                    {
                        function = false;
                    }

                    if (!function && variable_type is Symbols.VOID)
                    {
                        throw new Symbols.Exception("недопустимо использование типа \"void\"",
                            first_token.GetIndex(), first_token.GetLine());
                    }

                    if (pair.first != null && !function)
                    {
                        try
                        {
                            tstack.AddSymbol(pair.first);
                        }
                        catch (Symbols.Exception e)
                        {
                            this.logger.Add(e);
                        }
                    }
                }

                if (scan.Peek().type != Token.Type.COMMA || function)
                {
                    break;
                }

                scan.Read();
                function = false;
            }

            if (!function)
            {
                CheckToken(scan.Peek(), Token.Type.SEMICOLON, true);
            }
        }
예제 #5
0
        private void ParseDeclaration()
        {
            while (scan.Peek().type == Token.Type.SEMICOLON)
            {
                scan.Read();
            }
            if (scan.Peek().type == Token.Type.EOF)
            {
                return;
            }

            Token first_token = scan.Peek();

            Symbols.Type type = ParseTypeSpecifier(), variable_type = null;

            Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> > pair = null;
            bool function = true;

            while (true)
            {
                variable_type = type;

                if ((pair = ParseDeclarator()) != null)
                {
                    if (pair.last != null)
                    {
                        pair.last.last.SetType(type);
                        variable_type = pair.last.first;
                    }

                    ///Когда мы устанавливаем финальный тип, то нужно пересчитать размер всех типов


                    if (pair.first == null && !(variable_type is Symbols.RECORD))
                    {
                        throw new Syntax.Exception("требуется идентификатор", scan.GetPos(), scan.GetLine());
                    }

                    if (pair.first != null)
                    {
                        pair.first.SetType(variable_type);
                    }

                    if (scan.Peek().type == Token.Type.OP_ASSIGN)                     // init declarator
                    {
                        Symbols.Var       v    = pair.first;
                        Syntax.Expression node = new Syntax.Identifier(
                            new Token(v.GetIndex(), v.GetLine(), Token.Type.IDENTIFICATOR, v.GetName()),
                            v
                            );
                        node = ParseBinaryOper(0, node, true);

                        //pair.first.SetInitializer(ParseInitializer());
                        pair.first.SetInitializer(node);
                        function = false;
                    }

                    if (function && scan.Peek().type == Token.Type.LBRACE && tstack.IsGlobal())
                    {
                        pair.last.first.SetName(pair.first.GetName());
                        tstack.AddSymbol(pair.first);
                        tstack.NewTable();

                        if (((Symbols.Func)pair.last.first).GetArguments()
                            .Count(x => x.GetName() == pair.first.GetName()) == 0)
                        {
                            tstack.AddSymbol(pair.first);
                        }

                        foreach (Symbols.Var arg in ((Symbols.Func)pair.last.first).GetArguments())
                        {
                            tstack.AddSymbol(arg);
                        }
                        this.ret_func_type = ((Symbols.Func)pair.last.first).GetRefType();
                        ((Symbols.Func)pair.last.first).SetBody(ParseCompound(false));
                        tstack.GetCurrentTable().RemoveSymbol(pair.first);
                        ((Symbols.Func)pair.last.first).SetTable(tstack.PopTable());
                    }
                    else
                    {
                        function = false;
                    }


                    if (!function && variable_type is Symbols.VOID)
                    {
                        throw new Symbols.Exception("недопустимо использование типа \"void\"",
                                                    first_token.GetIndex(), first_token.GetLine());
                    }


                    if (pair.first != null && !function)
                    {
                        try
                        {
                            tstack.AddSymbol(pair.first);
                        }
                        catch (Symbols.Exception e)
                        {
                            this.logger.Add(e);
                        }
                    }
                }

                if (scan.Peek().type != Token.Type.COMMA || function)
                {
                    break;
                }

                scan.Read();
                function = false;
            }

            if (!function)
            {
                CheckToken(scan.Peek(), Token.Type.SEMICOLON, true);
            }
        }