Example #1
0
 TYPE_INFO _type;       // Type
 /// <summary>
 ///     this Ctor just stores the variable name
 /// </summary>
 /// <param name="inf"></param>
 public Variable(SYMBOL_INFO inf)
 {
     m_name = inf.SymbolName;
 }
 public AssignmentStatement(SYMBOL_INFO var, Exp e)
 {
     variable = new Variable(var);
     exp1     = e;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="inf"></param>
 public VariableDeclStatement(SYMBOL_INFO inf)
 {
     m_inf = inf;
 }
Example #4
0
        /// <summary>
        ///     <Factor>::=  <number> | ( <expr> ) | {+|-} <factor>
        ///           <variable> | TRUE | FALSE
        /// </summary>
        public Exp Factor(COMPILATION_CONTEXT ctx)
        {
            TOKEN l_token;
            Exp   RetValue = null;



            if (Current_Token == TOKEN.TOK_NUMERIC)
            {
                RetValue      = new NumericConstant(GetNumber());
                Current_Token = GetToken();
            }
            else if (Current_Token == TOKEN.TOK_STRING)
            {
                RetValue      = new StringLiteral(last_str);
                Current_Token = GetToken();
            }
            else if (Current_Token == TOKEN.TOK_BOOL_FALSE ||
                     Current_Token == TOKEN.TOK_BOOL_TRUE)
            {
                RetValue = new BooleanConstant(
                    Current_Token == TOKEN.TOK_BOOL_TRUE ? true : false);
                Current_Token = GetToken();
            }
            else if (Current_Token == TOKEN.TOK_OPAREN)
            {
                Current_Token = GetToken();

                RetValue = Expr(ctx);  // Recurse

                if (Current_Token != TOKEN.TOK_CPAREN)
                {
                    Console.WriteLine("Missing Closing Parenthesis\n");
                    throw new Exception();
                }
                Current_Token = GetToken();
            }

            else if (Current_Token == TOKEN.TOK_PLUS || Current_Token == TOKEN.TOK_SUB)
            {
                l_token       = Current_Token;
                Current_Token = GetToken();
                RetValue      = Factor(ctx);
                if (l_token == TOKEN.TOK_PLUS)
                {
                    RetValue = new UnaryPlus(RetValue);
                }
                else
                {
                    RetValue = new UnaryMinus(RetValue);
                }
            }
            else if (Current_Token == TOKEN.TOK_UNQUOTED_STRING)
            {
                ///
                ///  Variables
                ///
                String      str = base.last_str;
                SYMBOL_INFO inf = ctx.TABLE.Get(str);

                if (inf == null)
                {
                    throw new Exception("Undefined symbol");
                }

                GetNext();
                RetValue = new Variable(inf);
            }



            else
            {
                Console.WriteLine("Illegal Token");
                throw new Exception();
            }


            return(RetValue);
        }
        /// <summary>
        ///    The logic of this method is obvious...
        ///    Evaluate the Left and Right Expression...
        ///    Query the Type of the expressions and perform
        ///    appropriate action
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont)
        {
            SYMBOL_INFO eval_left  = ex1.Evaluate(cont);
            SYMBOL_INFO eval_right = ex2.Evaluate(cont);

            SYMBOL_INFO ret_val = new SYMBOL_INFO();

            if (eval_left.Type == TYPE_INFO.TYPE_NUMERIC &&
                eval_right.Type == TYPE_INFO.TYPE_NUMERIC)
            {
                ret_val.Type       = TYPE_INFO.TYPE_BOOL;
                ret_val.SymbolName = "";

                if (m_op == RELATION_OPERATOR.TOK_EQ)
                {
                    ret_val.bol_val = eval_left.dbl_val == eval_right.dbl_val;
                }
                else if (m_op == RELATION_OPERATOR.TOK_NEQ)
                {
                    ret_val.bol_val = eval_left.dbl_val != eval_right.dbl_val;
                }
                else if (m_op == RELATION_OPERATOR.TOK_GT)
                {
                    ret_val.bol_val = eval_left.dbl_val > eval_right.dbl_val;
                }
                else if (m_op == RELATION_OPERATOR.TOK_GTE)
                {
                    ret_val.bol_val = eval_left.dbl_val >= eval_right.dbl_val;
                }
                else if (m_op == RELATION_OPERATOR.TOK_LTE)
                {
                    ret_val.bol_val = eval_left.dbl_val <= eval_right.dbl_val;
                }
                else if (m_op == RELATION_OPERATOR.TOK_LT)
                {
                    ret_val.bol_val = eval_left.dbl_val < eval_right.dbl_val;
                }


                return(ret_val);
            }
            else if (eval_left.Type == TYPE_INFO.TYPE_STRING &&
                     eval_right.Type == TYPE_INFO.TYPE_STRING)
            {
                ret_val.Type       = TYPE_INFO.TYPE_BOOL;
                ret_val.SymbolName = "";

                if (m_op == RELATION_OPERATOR.TOK_EQ)
                {
                    ret_val.bol_val = (String.Compare(
                                           eval_left.str_val,
                                           eval_right.str_val) == 0) ? true:false;
                }
                else if (m_op == RELATION_OPERATOR.TOK_NEQ)
                {
                    ret_val.bol_val = String.Compare(
                        eval_left.str_val,
                        eval_right.str_val) != 0;
                }
                else
                {
                    ret_val.bol_val = false;
                }


                return(ret_val);
            }
            if (eval_left.Type == TYPE_INFO.TYPE_BOOL &&
                eval_right.Type == TYPE_INFO.TYPE_BOOL)
            {
                ret_val.Type       = TYPE_INFO.TYPE_BOOL;
                ret_val.SymbolName = "";

                if (m_op == RELATION_OPERATOR.TOK_EQ)
                {
                    ret_val.bol_val = eval_left.bol_val == eval_right.bol_val;
                }
                else if (m_op == RELATION_OPERATOR.TOK_NEQ)
                {
                    ret_val.bol_val = eval_left.bol_val != eval_right.bol_val;
                }
                else
                {
                    ret_val.bol_val = false;
                }
                return(ret_val);
            }
            return(null);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="cont"></param>
 /// <returns></returns>
 public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont)
 {
     inf = (m_e1 == null) ? null : m_e1.Evaluate(cont);
     return(inf);
 }