Exemple #1
0
        public void Push(Syntax.Const c)
        {
            int size_t = c.GetType().GetSizeType();

            if (c.GetType() is Symbols.INT || c.GetType() is Symbols.CHAR)
            {
                this.icode.Add(new Instr(Com.PUSH, new Val(c, size_t)));
                this.stack.Push(Type.GetType(size_t));
            }
            else if (c.GetType() is Symbols.DOUBLE)
            {
                AddCode(Com.PUSH, new Val(c.GetValue(), size: 4));                              //push val
                AddCode(Com.FLD, new Addr(Reg.ESP, size: 4, cast: true));                       //fld dword[esp]
                AddCode(Com.SUB, Reg.ESP, new Val(4, size: 4));                                 //sub esp, 4
                AddCode(Com.FSTP, new Addr(Reg.ESP, size: 8, cast: true));                      //fstp qword[esp]
                this.stack.Push(Type.GetType(size_t));
            }
            else if (c.GetType() is Symbols.ARRAY && ((Symbols.ARRAY)c.GetType()).GetRefType() is Symbols.CHAR)
            {
                if (!this.consts.ContainsKey(c.GetValue()))
                {
                    string name = "@const" + ++this.count_str;
                    string val  = c.GetValue();
                    val = "\"" + val + "\", 0";
                    this.consts.Add(c.GetValue(), new Const(name, Type.DB, val, c.GetValue().Length));
                }

                AddCode(Com.PUSH, new Addr(this.consts[c.GetValue()].name, size: 4));
                this.stack.Push(Type.GetType(4));
            }
        }
Exemple #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);
        }
Exemple #3
0
 public Val(Syntax.Const constant, int size = -1)
 {
     this.value = constant.GetValue();
     this.size  = size == -1 ? constant.GetType().GetSizeType() : size;
 }
Exemple #4
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;
        }