Esempio n. 1
0
 /// <summary>
 /// constructor 
 /// </summary>
 /// <param name="type"></param>
 public Expression(int type)
 {
     m_type = type;
     m_leftExp = null;
     m_rightExp = null;
     m_funcParams = null;
 }
Esempio n. 2
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="bd"></param>
 public Expression(BaseData bd)
 {
     m_type = Expression.VALUE;
     m_leftExp = null;
     m_rightExp = null;
     m_funcParams = null;
     m_value = bd;
 }
Esempio n. 3
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;
        }
Esempio n. 4
0
        protected Expression expression6()
        {
            Expression exp = expression7();

            int tt = lookAhead();

            while( tt == Token.POWER )
            {
                eatToken(Token.POWER);
                Expression subExp = new Expression(Expression.OP_POWER);

                subExp.m_leftExp = exp;
                subExp.m_rightExp = expression7();

                exp = subExp;

                tt = lookAhead();
            }

            return exp;
        }
Esempio n. 5
0
        protected Expression expression5()
        {
            Expression exp = expression6();

            int tt = lookAhead();

            while (tt == Token.MUL || tt == Token.DIV)
            {
                Expression subExp = null;

                eatToken(tt);
                if (tt == Token.MUL)
                    subExp = new Expression(Expression.OP_MUL);
                else if (tt == Token.DIV)
                    subExp = new Expression(Expression.OP_DIV);

                subExp.m_leftExp = exp;
                subExp.m_rightExp = expression6();

                exp = subExp;

                tt = lookAhead();
            }

            return exp;
        }
Esempio n. 6
0
        protected Expression expression4()
        {
            Expression exp = expression5();

            int tt = lookAhead();

            while (tt == Token.PLUS || tt == Token.MINUS)
            {
                Expression subExp = null;

                eatToken(tt);
                if (tt == Token.PLUS)
                    subExp = new Expression(Expression.OP_ADD);
                else if (tt == Token.MINUS)
                    subExp = new Expression(Expression.OP_MINUS);

                subExp.m_leftExp = exp;
                subExp.m_rightExp = expression5();

                exp = subExp;

                tt = lookAhead();
            }

            return exp;
        }
Esempio n. 7
0
        protected Expression expression3()
        {
            Expression exp = expression4();

            int tt = lookAhead();
            while( tt == Token.EQUAL || tt == Token.GTR || tt == Token.LT || tt == Token.GTE || tt == Token.LTE )
            {
                Expression subExp = null;

                eatToken( tt );

                if( tt == Token.EQUAL )
                    subExp = new Expression(Expression.OP_EQUAL);
                else if( tt == Token.GTR )
                    subExp = new Expression(Expression.OP_GREATER);
                else if( tt == Token.GTE)
                    subExp = new Expression(Expression.OP_GREATER_EQU);
                else if( tt == Token.LT)
                    subExp = new Expression(Expression.OP_LESS);
                else if( tt == Token.LTE)
                    subExp = new Expression(Expression.OP_LESS_EQ);

                subExp.m_leftExp = exp;
                subExp.m_rightExp = expression4();

                exp = subExp;

                tt = lookAhead();
            }

            return exp;
        }
Esempio n. 8
0
        protected Expression expression2()
        {
            Expression exp = null;
            Expression subExp = null;

            int tt = lookAhead();

            // NOT at the first 
            while( tt == Token.NOT )
            {
                eatToken(tt);
                subExp = new Expression(Expression.OP_NOT);

                if (exp != null)
                    subExp.m_leftExp = exp;

                exp = subExp;

                tt = lookAhead();
            }

            if (exp != null)
            {
                subExp = exp;
                while( subExp.m_leftExp != null )
                {
                    subExp = subExp.m_leftExp;
                }
                subExp.m_leftExp = expression3();
            }
            else
            {
                exp = expression3();
            }

            return exp;
        }
Esempio n. 9
0
        /// <summary>
        /// parse expression
        /// </summary>
        /// <returns></returns>
        protected Expression expression()
        {
            Expression exp = expression2();

            int tt = lookAhead();
            while( tt == Token.AND || tt == Token.OR )
            {
                Expression subExp = null;

                eatToken(tt);

                if (tt == Token.AND)
                    subExp = new Expression(Expression.OP_AND);
                else if (tt == Token.OR)
                    subExp = new Expression(Expression.OP_OR);

                subExp.m_leftExp = exp;
                subExp.m_rightExp = expression2();

                exp = subExp;

                tt = lookAhead();
            }

            return exp;
        }
Esempio n. 10
0
        /// <summary>
        /// for statement 
        /// </summary>
        /// <returns></returns>
        protected Statement forBegin()
        {
            Statement forS = new Statement(Statement.TYPE_FOR_BEGIN);
            forS.m_expressList = new List<Expression>();

            // keyword
            eatToken(Token.FOR);
            // loop var 
            Token tok = eatToken(Token.SYMBOL);
            string symbolName = tok.m_strVal;
            forS.m_symbol = symbolName;

            if (symbolName.EndsWith("%"))
                throw new ErrorCode(ErrorCode.ERROR_CODE_02);
            else if (symbolName.EndsWith("$"))
                throw new ErrorCode(ErrorCode.ERROR_CODE_12);

            // equal 
            eatToken(Token.EQUAL);

            // start number 
            forS.m_expressList.Add(expression());

            // to 
            eatToken(Token.TO);

            // end number 
            forS.m_expressList.Add(expression());

            // if has step or not ? 
            int t = lookAhead();
            if( t == Token.STEP )
            {
                eatToken(Token.STEP);

                forS.m_expressList.Add(expression());
            }
            else
            {
                // default step 
                Expression step = new Expression(new BaseData(1));
                forS.m_expressList.Add(step);
            }

            return forS;
        }
Esempio n. 11
0
        protected int expInkeyCount( Expression exp )
        {
            if (exp.m_type == Expression.EXP_INKEY)
                return 1;

            int cnt = 0;

            if (exp.m_leftExp != null)
                cnt += expInkeyCount(exp.m_leftExp);

            if (exp.m_rightExp != null)
                cnt += expInkeyCount(exp.m_rightExp);

            if( exp.m_funcParams != null )
            {
                foreach (Expression e in exp.m_funcParams)
                    cnt += expInkeyCount(e);
            }

            return cnt;
        }
Esempio n. 12
0
        /// <summary>
        /// replace symbol name in the expression 
        /// </summary>
        /// <param name="exp"></param>
        protected void replaceSymbolName( Expression exp, string oldName, string newName )
        {
            if ( exp.m_type == Expression.EXP_SYMBOL && exp.m_symbolName == oldName)
                exp.m_symbolName = newName;

            if( exp.m_funcParams != null)
            {
                foreach (Expression e in exp.m_funcParams)
                    replaceSymbolName(e, oldName, newName);
            }

            if (exp.m_leftExp != null)
                replaceSymbolName(exp.m_leftExp, oldName, newName);

            if (exp.m_rightExp != null)
                replaceSymbolName(exp.m_rightExp, oldName, newName);
        }
Esempio n. 13
0
        /// <summary>
        /// reduce the expession 
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        protected Expression calculateExp( Expression exp )
        {
            Expression result = null;
            Expression midExp = null;
            Expression expLeft = null;
            Expression expRight = null;

            switch( exp.m_type )
            {
                case Expression.VALUE:
                    result = exp;
                    break;
                case Expression.TYPE_CLOSE_TO:
                    result = exp;
                    result.m_value = new BaseData(BaseData.TYPE_CLOSE_TO, 0);
                    break;
                case Expression.TYPE_NEXT_LINE:
                    result = exp;
                    result.m_value = new BaseData(BaseData.TYPE_NEXT_LINE, 0);
                    break;
                case Expression.EXP_SYMBOL:
                    VarSymbol s = m_symbolTable.ResolveVar(exp.m_symbolName);
                    result = new Expression( s.VALUE );
                    break;
                case Expression.EXP_ARRAY_SYMBOL:
                    List<int> indexs = expressListToIndexs(exp.m_funcParams);
                    ArraySymbol arr = m_symbolTable.ResolveArray(exp.m_symbolName, indexs);
                    result = new Expression(arr.GetValue(indexs));
                    break;
                // 内置函数
                case Expression.EXP_FUNC:
                    List<BaseData> param = new List<BaseData>();
                    // convert the parameters 
                    foreach (Expression e in exp.m_funcParams)
                        param.Add( calculateExp(e).m_value );
                    // call the function 
                    BaseData returnVal = m_innerFunc.CallFunc(exp.m_symbolName, param);
                    result = new Expression(returnVal);
                    break;
                // 用户自定义函数 
                case Expression.EXP_USER_FUNC:
                    string funcName = exp.m_symbolName;
                    FunSymbol func = m_symbolTable.ResolveFunc(funcName);
                    Expression funcParam = exp.m_funcParams[0];
                    BaseData paramVal = calculateExp(funcParam).m_value;
                    m_symbolTable.Define(new VarSymbol(CommonDef.FN_PARAM_SYMBOL, paramVal));
                    result = calculateExp(func.EXP);
                    break;
                case Expression.OP_NOT:
                    midExp = calculateExp( exp.m_leftExp );
                    if( midExp.m_type == Expression.VALUE )
                    {
                        if (midExp.m_value != BaseData.ZERO)
                            result = new Expression( new BaseData(0) );
                        else
                            result = new Expression( new BaseData(1) );
                    }
                    else
                    {
                        throw new ErrorCode(ErrorCode.ERROR_CODE_12);
                    }
                    break;
                case Expression.OP_NEG:
                    midExp = calculateExp(exp.m_leftExp);
                    if (midExp.m_type == Expression.VALUE)
                    {
                        result = midExp;
                        result.m_value.NegValue();
                    }
                    else
                    {
                        throw new ErrorCode(ErrorCode.ERROR_CODE_12);
                    }
                    break;
                case Expression.EXP_INKEY:
                    result = new Expression(new BaseData(m_apiCall.Inkey()));
                    break;
                // 二元运算符
                case Expression.OP_ADD:
                case Expression.OP_MINUS:
                case Expression.OP_MUL:
                case Expression.OP_DIV:
                case Expression.OP_POWER:
                case Expression.OP_AND:
                case Expression.OP_OR:
                case Expression.OP_EQUAL:
                case Expression.OP_GREATER:
                case Expression.OP_GREATER_EQU:
                case Expression.OP_LESS:
                case Expression.OP_LESS_EQ:
                    // check the value 
                    expLeft = calculateExp(exp.m_leftExp);
                    expRight = calculateExp(exp.m_rightExp);
                    if (expLeft.m_type != Expression.VALUE || expRight.m_type != Expression.VALUE)
                        throw new ErrorCode(ErrorCode.ERROR_CODE_12);

                    if( exp.m_type == Expression.OP_ADD )
                        result = new Expression(expLeft.m_value + expRight.m_value);
                    else if( exp.m_type == Expression.OP_MINUS )
                        result = new Expression(expLeft.m_value - expRight.m_value);
                    else if( exp.m_type == Expression.OP_MUL)
                        result = new Expression(expLeft.m_value * expRight.m_value);
                    else if( exp.m_type == Expression.OP_DIV)
                        result = new Expression(expLeft.m_value / expRight.m_value);
                    else if( exp.m_type == Expression.OP_POWER)
                        result = new Expression(expLeft.m_value ^ expRight.m_value);
                    else if( exp.m_type == Expression.OP_AND)
                        result = new Expression(expLeft.m_value & expRight.m_value);
                    else if( exp.m_type == Expression.OP_OR)
                        result = new Expression(expLeft.m_value | expRight.m_value);
                    else if( exp.m_type == Expression.OP_EQUAL)
                        result = new Expression(expLeft.m_value == expRight.m_value ? 1 : 0 );
                    else if( exp.m_type == Expression.OP_GREATER)
                        result = new Expression( new BaseData( expLeft.m_value > expRight.m_value ? 1 : 0 ) );
                    else if( exp.m_type == Expression.OP_GREATER_EQU)
                        result = new Expression( new BaseData( expLeft.m_value >= expRight.m_value ? 1 : 0 ) );
                    else if( exp.m_type == Expression.OP_LESS)
                        result = new Expression(new BaseData(expLeft.m_value < expRight.m_value ? 1 : 0));
                    else if( exp.m_type == Expression.OP_LESS_EQ)
                        result = new Expression(new BaseData(expLeft.m_value <= expRight.m_value ? 1 : 0));
                    break;
                default:
                    throw new ErrorCode(ErrorCode.ERROR_CODE_02);
            }

            return result;
        }
Esempio n. 14
0
 /// <summary>
 /// constrcutor
 /// </summary>
 /// <param name="exp"></param>
 public WhileRecord( Expression exp, int beginIndex )
 {
     m_judgeExp = exp;
     m_beginIndex = beginIndex;
 }
Esempio n. 15
0
 /// <summary>
 /// default constructor 
 /// </summary>
 public Expression()
 {
     m_leftExp = null;
     m_rightExp = null;
     m_funcParams = null;
 }
Esempio n. 16
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <param name="exp"></param>
        public FunSymbol( string name, Expression exp )
        {
            m_type = Symbol.FUNC;
            m_name = name;

            m_exp = exp;
        }