Exemplo n.º 1
0
 /// <summary>
 ///   The new Parser entry point
 /// </summary>
 /// <returns></returns>
 public ArrayList Parse(COMPILATION_CONTEXT ctx)
 {
     GetNext();  // Get the Next Token
     //
     // Parse all the statements
     //
     return(StatementList(ctx));
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            if (m_proc != null)
            {
                _type = m_proc.TypeCheck(cont);
            }

            return(_type);
        }
        /// <summary>
        ///     Creates a new symbol and puts into the symbol table
        ///     and stores the key ( variable name )
        /// </summary>
        /// <param name="st"></param>
        /// <param name="name"></param>
        /// <param name="_value"></param>
        public Variable(COMPILATION_CONTEXT st, String name, double _value)
        {
            SYMBOL_INFO s = new SYMBOL_INFO();

            s.SymbolName = name;
            s.Type       = TYPE_INFO.TYPE_NUMERIC;
            s.dbl_val    = _value;
            st.TABLE.Add(s);
            m_name = name;
        }
        /// <summary>
        ///     Creates a new symbol and puts into the symbol table
        ///     and stores the key ( variable name )
        /// </summary>
        /// <param name="st"></param>
        /// <param name="name"></param>
        /// <param name="_value"></param>
        public Variable(COMPILATION_CONTEXT st, String name, string _value)
        {
            SYMBOL_INFO s = new SYMBOL_INFO();

            s.SymbolName = name;
            s.Type       = TYPE_INFO.TYPE_STRING;
            s.str_val    = _value;
            st.TABLE.Add(s);
            m_name = name;
        }
Exemplo n.º 5
0
        /// <summary>
        ///    Parse the PrintLine Staement .. The grammar is
        ///    PRINTLINE <expr> ;
        ///    Once you are in this subroutine , we are expecting
        ///    a valid expression ( which will be compiled ) and a
        ///    semi collon to terminate the line..
        ///    Once Parse Process is successful , we create a PrintLineStatement
        ///    Object..
        /// </summary>
        /// <returns></returns>
        private Stmt ParsePrintLNStatement(COMPILATION_CONTEXT ctx)
        {
            GetNext();
            Exp a = Expr(ctx);

            if (Current_Token != TOKEN.TOK_SEMI)
            {
                throw new Exception("; is expected");
            }
            return(new PrintLineStatement(a));
        }
Exemplo n.º 6
0
        /// <summary>
        ///    Parse the Assignment Statement
        ///    <variable> = <expr>
        /// </summary>
        /// <param name="pb"></param>
        /// <returns></returns>
        public Stmt ParseAssignmentStatement(COMPILATION_CONTEXT ctx)
        {
            //
            // Retrieve the variable and look it up in
            // the symbol table ..if not found throw exception
            //
            string      variable = base.last_str;
            SYMBOL_INFO s        = ctx.TABLE.Get(variable);

            if (s == null)
            {
                CSyntaxErrorLog.AddLine("Variable not found  " + last_str);
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, "Variable not found", SaveIndex());
            }

            //------------ The next token ought to be an assignment
            // expression....

            GetNext();

            if (Current_Token != TOKEN.TOK_ASSIGN)
            {
                CSyntaxErrorLog.AddLine("= expected");
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, "= expected", SaveIndex());
            }

            //-------- Skip the token to start the expression
            // parsing on the RHS
            GetNext();
            Exp exp = Expr(ctx);

            //------------ Do the type analysis ...

            if (exp.TypeCheck(ctx) != s.Type)
            {
                throw new Exception("Type mismatch in assignment");
            }

            // -------------- End of statement ( ; ) is expected
            if (Current_Token != TOKEN.TOK_SEMI)
            {
                CSyntaxErrorLog.AddLine("; expected");
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, " ; expected", -1);
            }
            // return an instance of AssignmentStatement node..
            //   s => Symbol info associated with variable
            //   exp => to evaluated and assigned to symbol_info
            return(new AssignmentStatement(s, exp));
        }
Exemplo n.º 7
0
        /// <summary>
        ///  The Grammar is
        ///
        ///  <stmtlist> :=  { <statement> }+
        ///
        ///  {<statement> :=  <printstmt> | <printlinestmt>
        ///  <printstmt> :=   print   <expr >;
        ///  <vardeclstmt> := STRING <varname>;  |
        ///                   NUMERIC <varname>; |
        ///                   BOOLEAN <varname>;
        ///
        /// <printlinestmt>:= printline <expr>;
        ///
        /// <Expr>  ::=  <Term> | <Term> { + | - } <Expr>
        /// <Term> ::=  <Factor> | <Factor>  {*|/} <Term>
        /// <Factor>::=  <number> | ( <expr> ) | {+|-} <factor>
        ///              <variable> | TRUE | FALSE
        ///
        ///
        ///
        /// </summary>
        /// <returns></returns>
        private ArrayList StatementList(COMPILATION_CONTEXT ctx)
        {
            ArrayList arr = new ArrayList();

            while (Current_Token != TOKEN.TOK_NULL)
            {
                Stmt temp = Statement(ctx);
                if (temp != null)
                {
                    arr.Add(temp);
                }
            }
            return(arr);
        }
Exemplo n.º 8
0
        /// <summary>
        ///    Parse Variable declaration statement
        /// </summary>
        /// <param name="type"></param>

        public Stmt ParseVariableDeclStatement(COMPILATION_CONTEXT ctx)
        {
            //--- Save the Data type
            TOKEN tok = Current_Token;

            // --- Skip to the next token , the token ought
            // to be a Variable name ( UnQouted String )
            GetNext();

            if (Current_Token == TOKEN.TOK_UNQUOTED_STRING)
            {
                SYMBOL_INFO symb = new SYMBOL_INFO();
                symb.SymbolName = base.last_str;
                symb.Type       = (tok == TOKEN.TOK_VAR_BOOL) ?
                                  TYPE_INFO.TYPE_BOOL : (tok == TOKEN.TOK_VAR_NUMBER) ?
                                  TYPE_INFO.TYPE_NUMERIC : TYPE_INFO.TYPE_STRING;

                //---------- Skip to Expect the SemiColon

                GetNext();



                if (Current_Token == TOKEN.TOK_SEMI)
                {
                    // ----------- Add to the Symbol Table
                    // for type analysis
                    ctx.TABLE.Add(symb);

                    // --------- return the Object of type
                    // --------- VariableDeclStatement
                    // This will just store the Variable name
                    // to be looked up in the above table
                    return(new VariableDeclStatement(symb));
                }
                else
                {
                    CSyntaxErrorLog.AddLine("; expected");
                    CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                    throw new CParserException(-100, ", or ; expected", SaveIndex());
                }
            }
            else
            {
                CSyntaxErrorLog.AddLine("invalid variable declaration");
                CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
                throw new CParserException(-100, ", or ; expected", SaveIndex());
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="local"></param>
        /// <param name="global"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left = exp1.TypeCheck(cont);


            if (eval_left == TYPE_INFO.TYPE_NUMERIC)
            {
                _type = eval_left;
                return(_type);
            }
            else
            {
                throw new Exception("Type mismatch failure");
            }
        }
Exemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left = ex1.TypeCheck(cont);


            if (
                eval_left == TYPE_INFO.TYPE_BOOL)
            {
                _type = TYPE_INFO.TYPE_BOOL;
                return(_type);
            }
            else
            {
                throw new Exception("Wrong Type in expression");
            }
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Look it up in the Symbol Table and
        ///     return the type
        /// </summary>
        /// <param name="local"></param>
        /// <param name="global"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            if (cont.TABLE == null)
            {
                return(TYPE_INFO.TYPE_ILLEGAL);
            }
            else
            {
                SYMBOL_INFO a = cont.TABLE.Get(m_name);
                if (a != null)
                {
                    _type = a.Type;
                    return(_type);
                }


                return(TYPE_INFO.TYPE_ILLEGAL);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
        {
            TYPE_INFO eval_left  = ex1.TypeCheck(cont);
            TYPE_INFO eval_right = ex2.TypeCheck(cont);

            // The Types should be Boolean...
            // Logical Operators only make sense
            // with Boolean Types

            if (eval_left == eval_right &&
                eval_left == TYPE_INFO.TYPE_BOOL)
            {
                _type = TYPE_INFO.TYPE_BOOL;
                return(_type);
            }
            else
            {
                throw new Exception("Wrong Type in expression");
            }
        }
Exemplo n.º 13
0
        /// <summary>
        ///    <Expr>  ::=  <Term> | <Term> { + | - } <Expr>
        ///
        /// </summary>
        /// <returns></returns>
        public Exp Expr(COMPILATION_CONTEXT ctx)
        {
            TOKEN l_token;
            Exp   RetValue = Term(ctx);

            while (Current_Token == TOKEN.TOK_PLUS || Current_Token == TOKEN.TOK_SUB)
            {
                l_token       = Current_Token;
                Current_Token = GetToken();
                Exp e1 = Expr(ctx);

                if (l_token == TOKEN.TOK_PLUS)
                {
                    RetValue = new BinaryPlus(RetValue, e1);
                }
                else
                {
                    RetValue = new BinaryMinus(RetValue, e1);
                }
            }

            return(RetValue);
        }
Exemplo n.º 14
0
        /// <summary>
        /// <Term> ::=  <Factor> | <Factor>  {*|/} <Term>
        /// </summary>
        public Exp Term(COMPILATION_CONTEXT ctx)
        {
            TOKEN l_token;
            Exp   RetValue = Factor(ctx);

            while (Current_Token == TOKEN.TOK_MUL || Current_Token == TOKEN.TOK_DIV)
            {
                l_token       = Current_Token;
                Current_Token = GetToken();


                Exp e1 = Term(ctx);
                if (l_token == TOKEN.TOK_MUL)
                {
                    RetValue = new Mul(RetValue, e1);
                }
                else
                {
                    RetValue = new Div(RetValue, e1);
                }
            }

            return(RetValue);
        }
Exemplo n.º 15
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="local"></param>
 /// <param name="global"></param>
 /// <returns></returns>
 public override TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
 {
     return(info.Type);
 }
Exemplo n.º 16
0
 public abstract TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont);
 /// <summary>
 ///
 /// </summary>
 /// <param name="cont"></param>
 /// <returns></returns>
 public TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
 {
     return(_type);
 }
Exemplo n.º 18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="_ctx"></param>

        public ProcedureBuilder(string name, COMPILATION_CONTEXT _ctx)
        {
            ctx       = _ctx;
            proc_name = name;
        }
Exemplo n.º 19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="cont"></param>
 /// <returns></returns>
 public TYPE_INFO TypeCheck(COMPILATION_CONTEXT cont)
 {
     return(TYPE_INFO.TYPE_NUMERIC);
 }
Exemplo n.º 20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="_ctx"></param>

        public ProcedureBuilder(string name, COMPILATION_CONTEXT _ctx, SymbolLookup lk = null)
        {
            ctx       = _ctx;
            proc_name = name;
            _slk      = lk;
        }
Exemplo n.º 21
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);
        }