Exemplo n.º 1
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);
        }