コード例 #1
0
        /// <summary>
        ///    Driver routine to call the program script
        /// </summary>
        static void TestFileScript(string filename)
        {
            if (filename == null)
            {
                return;
            }


            // -------------- Read the contents from the file

            StreamReader sr        = new StreamReader(filename);
            string       programs2 = sr.ReadToEnd();


            //---------------- Creates the Parser Object
            // With Program text as argument
            RDParser pars = null;

            pars = new RDParser(programs2);

            // Create a Compilation Context
            //
            //
            COMPILATION_CONTEXT ctx = new COMPILATION_CONTEXT();

            //
            // Call the top level Parsing Routine with
            // Compilation Context as the Argument
            //
            ArrayList stmts = pars.Parse(ctx);

            //
            // if we have reached here , the parse process
            // is successful... Create a Run time context and
            // Call Execute statements of each statement...
            //

            RUNTIME_CONTEXT f = new RUNTIME_CONTEXT();

            foreach (Object obj in stmts)
            {
                Stmt s = obj as Stmt;
                s.Execute(f);
            }
        }
コード例 #2
0
        /// <summary>
        ///    Parse A Single Function.
        /// </summary>
        /// <returns></returns>
        ProcedureBuilder ParseFunction2()
        {
            COMPILATION_CONTEXT cnt = new COMPILATION_CONTEXT();

            foreach (KeyValuePair <string, SYMBOL_INFO> temp in _rules)
            {
                cnt.TABLE.Add(temp.Value);
            }
            //
            // Create a Procedure builder Object
            //    We are passing the method "CollectSymbols" functions as parameters
            //    All the Symbol Table Lookup in the ProcedureBuilder will be
            //    captured by CollectSymbols
            //
            ProcedureBuilder p = new ProcedureBuilder("", cnt, CollectSymbols);

            if (Current_Token != TOKEN.TOK_FUNCTION)
            {
                throw new CParserException(-1, "FUNCTION expected ", SaveIndex());
            }


            GetNext();
            // return type of the Procedure ought to be
            // Boolean , Numeric or String
            if (!(Current_Token == TOKEN.TOK_VAR_BOOL ||
                  Current_Token == TOKEN.TOK_VAR_NUMBER ||
                  Current_Token == TOKEN.TOK_VAR_STRING))
            {
                throw new CParserException(-1, "A Legal data type expected ", SaveIndex());
            }

            //-------- Assign the return type
            p.TYPE = (Current_Token == TOKEN.TOK_VAR_BOOL) ?
                     TYPE_INFO.TYPE_BOOL : (Current_Token == TOKEN.TOK_VAR_NUMBER) ?
                     TYPE_INFO.TYPE_NUMERIC : TYPE_INFO.TYPE_STRING;

            // Parse the name of the Function call
            GetNext();
            if (Current_Token != TOKEN.TOK_UNQUOTED_STRING)
            {
                throw new CParserException(-1, "Function name expected ", SaveIndex());
            }

            p.Name = this.last_str; // assign the name

            // ---------- Opening parenthesis for
            // the start of <paramlist>
            GetNext();
            if (Current_Token != TOKEN.TOK_OPAREN)
            {
                throw new CParserException(-1, "Opening  Parenthesis expected", SaveIndex());
            }

            //---- Parse the Formal Parameter list
            FormalParameters(p);



            if (Current_Token != TOKEN.TOK_CPAREN)
            {
                throw new CParserException(-1, "Closing Parenthesis expected", SaveIndex());
            }

            GetNext();

            // --------- Parse the Function code
            ArrayList lst = StatementList(p);

            if (Current_Token != TOKEN.TOK_END)
            {
                throw new CParserException(-1, "END expected", SaveIndex());
            }

            // Accumulate all statements to
            // Procedure builder
            //
            foreach (Stmt s in lst)
            {
                p.AddStatement(s);
            }
            return(p);
        }