예제 #1
0
        /// <summary>
        /// if statement ( including if...then && if...goto )
        /// </summary>
        /// <returns></returns>
        protected Statement ifStatement()
        {
            Statement ifS = new Statement(Statement.TYPE_IF);

            eatToken(Token.IF);
            ifS.m_expressList = new List <Expression>();
            ifS.m_statements  = new List <Statement>();

            ifS.m_expressList.Add(expression());

            int tt = lookAhead();

            if (tt != Token.THEN && tt != Token.GOTO)
            {
                throw new ErrorCode(ErrorCode.ERROR_CODE_02);
            }

            eatToken(tt);

            Token            tok = null;
            List <Statement> s   = new List <Statement>();
            Statement        ss  = null;

            if (lookAhead() == Token.INT)
            {
                tok = eatToken(Token.INT);

                ss          = new Statement(Statement.TYPE_GOTO);
                ss.m_intVal = tok.m_intVal;
                s.Add(ss);
            }
            else
            {
                if (tt == Token.GOTO)
                {
                    throw new ErrorCode(ErrorCode.ERROR_CODE_02);
                }

                s = statements();
            }

            ifS.m_statements = s;

            // else case
            if (lookAhead() == Token.ELSE)
            {
                eatToken(Token.ELSE);

                s = new List <Statement>();

                if (lookAhead() == Token.INT)
                {
                    tok = eatToken(Token.INT);

                    ss          = new Statement(Statement.TYPE_GOTO);
                    ss.m_intVal = tok.m_intVal;
                    s.Add(ss);
                }
                else
                {
                    s = statements();
                }

                ifS.m_elseStatements = s;
            }
            else
            {
                ifS.m_elseStatements = new List <Statement>();
            }

            return(ifS);
        }
예제 #2
0
        protected Expression expression7()
        {
            Expression exp = null;

            int   tt  = lookAhead();
            Token tok = null;

            if (tt == Token.SYMBOL)
            {
                if (lookAhead(1) == Token.LEFT_PAREN)
                {
                    // array
                    exp = new Expression(Expression.EXP_ARRAY_SYMBOL);
                    tok = eatToken(Token.SYMBOL);
                    exp.m_symbolName = tok.m_strVal;
                    eatToken(Token.LEFT_PAREN);
                    exp.m_funcParams = expressList();
                    eatToken(Token.RIGHT_PAREN);
                }
                else
                {
                    exp = new Expression(Expression.EXP_SYMBOL);
                    tok = eatToken(Token.SYMBOL);
                    exp.m_symbolName = tok.m_strVal;
                }
            }
            else if (tt == Token.INT)
            {
                tok = eatToken(Token.INT);
                exp = new Expression(new BaseData(tok.m_intVal));
            }
            else if (tt == Token.FLOAT)
            {
                tok = eatToken(Token.FLOAT);
                exp = new Expression(new BaseData(tok.m_floatVal));
            }
            else if (tt == Token.LEFT_PAREN)
            {
                eatToken(Token.LEFT_PAREN);
                exp = expression();
                eatToken(Token.RIGHT_PAREN);
            }
            else if (tt == Token.MINUS)
            {
                exp = new Expression(Expression.OP_NEG);
                eatToken(Token.MINUS);
                exp.m_leftExp = expression7();
            }
            else if (tt == Token.FUNC)
            {
                exp = new Expression(Expression.EXP_FUNC);
                exp.m_funcParams = new List <Expression>();
                // function name
                tok = eatToken(Token.FUNC);
                exp.m_symbolName = tok.m_strVal;
                // function parameters
                eatToken(Token.LEFT_PAREN);
                exp.m_funcParams.Add(expression());
                while (lookAhead() == Token.COMMA)
                {
                    eatToken(Token.COMMA);
                    exp.m_funcParams.Add(expression());
                }
                eatToken(Token.RIGHT_PAREN);
            }
            else if (tt == Token.STRING)
            {
                tok = eatToken(Token.STRING);
                exp = new Expression(new BaseData(tok.m_strVal));
            }
            else if (tt == Token.FN)
            {
                exp = new Expression(Expression.EXP_USER_FUNC);
                eatToken(Token.FN);
                tok = eatToken(Token.SYMBOL);
                exp.m_symbolName = tok.m_strVal;

                eatToken(Token.LEFT_PAREN);
                exp.m_funcParams = new List <Expression>();
                exp.m_funcParams.Add(expression());
                eatToken(Token.RIGHT_PAREN);
            }
            else if (tt == Token.INKEY)
            {
                exp = new Expression(Expression.EXP_INKEY);
                eatToken(Token.INKEY);
            }
            else
            {
                throw new ErrorCode(ErrorCode.ERROR_CODE_02);
            }

            return(exp);
        }