Exemplo n.º 1
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);
        }
Exemplo n.º 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;
        }
Exemplo n.º 3
0
        private Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>> ParseDirectDeclarator(
			bool parse_abstract = false, bool parse_parameter = false)
        {
            Symbols.Var variable = null;
            Pair<Symbols.RefType, Symbols.RefType> tpair = null;
            Symbols.RefType type = null;

            if (scan.Peek().type == Token.Type.IDENTIFICATOR && !parse_abstract)
            {
                if (parse_parameter)
                {
                    variable = new Symbols.ParamVar(scan.Read());
                }
                else if (tstack.IsGlobal())
                {
                    variable = new Symbols.GlobalVar(scan.Read());
                }
                else
                {
                    variable = new Symbols.LocalVar(scan.Read());
                }
            }
            else if (scan.Peek().type == Token.Type.LPAREN)
            {
                scan.Read();
                Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>> res =
                    ParseDeclarator(parse_abstract, parse_parameter);

                variable = res.first;
                if (res.last != null)
                {
                    if (tpair == null)
                    {
                        tpair = new Pair<Symbols.RefType, Symbols.RefType>(res.last.first, res.last.last);
                    }
                    else
                    {
                        tpair.last.SetType(res.last.first);
                        tpair.last = res.last.last;
                    }
                }

                CheckToken(scan.Peek(), Token.Type.RPAREN, false);
                if (res.last == null && res.first == null)
                {
                    throw new Syntax.Exception("требуется выражение", scan.GetPos(), scan.GetLine());
                }
                scan.Read();
            }

            bool ret = false;
            while (true)
            {
                if (scan.Peek().type == Token.Type.LBRACKET)
                {
                    type = new Symbols.ARRAY(scan.Read());
                    ((Symbols.ARRAY)type).SetSize(ParseAssignmentExpression());
                    CheckToken(scan.Peek(), Token.Type.RBRACKET, true);

                }
                else if (scan.Peek().type == Token.Type.LPAREN)
                {
                    type = new Symbols.Func("", scan.Peek().pos, scan.Peek().line);
                    scan.Read();
                    tstack.NewTable();
                    ((Symbols.Func)type).SetArguments(ParseParameterList());
                    ((Symbols.Func)type).SetTable(tstack.PopTable());
                    CheckToken(scan.Peek(), Token.Type.RPAREN, true);
                }
                else
                {
                    ret = true;
                }

                if (type != null )
                {
                    if (tpair != null)
                    {
                        tpair.last.SetType(type);
                        tpair.last = type;
                    }
                    else
                    {
                        tpair = new Pair<Symbols.RefType, Symbols.RefType>(type, type);
                    }
                }
                type = null;

                if (ret)
                {
                    return new Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>>(variable, tpair);
                }
            }
        }
Exemplo n.º 4
0
        private Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> > ParseDirectDeclarator(
            bool parse_abstract = false, bool parse_parameter = false)
        {
            Symbols.Var variable = null;
            Pair <Symbols.RefType, Symbols.RefType> tpair = null;

            Symbols.RefType type = null;

            if (scan.Peek().type == Token.Type.IDENTIFICATOR && !parse_abstract)
            {
                if (parse_parameter)
                {
                    variable = new Symbols.ParamVar(scan.Read());
                }
                else if (tstack.IsGlobal())
                {
                    variable = new Symbols.GlobalVar(scan.Read());
                }
                else
                {
                    variable = new Symbols.LocalVar(scan.Read());
                }
            }
            else if (scan.Peek().type == Token.Type.LPAREN)
            {
                scan.Read();
                Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> > res =
                    ParseDeclarator(parse_abstract, parse_parameter);

                variable = res.first;
                if (res.last != null)
                {
                    if (tpair == null)
                    {
                        tpair = new Pair <Symbols.RefType, Symbols.RefType>(res.last.first, res.last.last);
                    }
                    else
                    {
                        tpair.last.SetType(res.last.first);
                        tpair.last = res.last.last;
                    }
                }

                CheckToken(scan.Peek(), Token.Type.RPAREN, false);
                if (res.last == null && res.first == null)
                {
                    throw new Syntax.Exception("требуется выражение", scan.GetPos(), scan.GetLine());
                }
                scan.Read();
            }


            bool ret = false;

            while (true)
            {
                if (scan.Peek().type == Token.Type.LBRACKET)
                {
                    type = new Symbols.ARRAY(scan.Read());
                    ((Symbols.ARRAY)type).SetSize(ParseAssignmentExpression());
                    CheckToken(scan.Peek(), Token.Type.RBRACKET, true);
                }
                else if (scan.Peek().type == Token.Type.LPAREN)
                {
                    type = new Symbols.Func("", scan.Peek().pos, scan.Peek().line);
                    scan.Read();
                    tstack.NewTable();
                    ((Symbols.Func)type).SetArguments(ParseParameterList());
                    ((Symbols.Func)type).SetTable(tstack.PopTable());
                    CheckToken(scan.Peek(), Token.Type.RPAREN, true);
                }
                else
                {
                    ret = true;
                }

                if (type != null)
                {
                    if (tpair != null)
                    {
                        tpair.last.SetType(type);
                        tpair.last = type;
                    }
                    else
                    {
                        tpair = new Pair <Symbols.RefType, Symbols.RefType>(type, type);
                    }
                }
                type = null;

                if (ret)
                {
                    return(new Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> >(variable, tpair));
                }
            }
        }