Beispiel #1
0
        /// <summary>
        ///
        /// </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);

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

                if (m_op == TOKEN.TOK_AND)
                {
                    ret_val.bol_val = (eval_left.bol_val && eval_right.bol_val);
                }
                else if (m_op == TOKEN.TOK_OR)
                {
                    ret_val.bol_val = (eval_left.bol_val || eval_right.bol_val);
                }
                else
                {
                    return(null);
                }
                return(ret_val);
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>

        public override SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont)
        {
            SYMBOL_INFO eval_left  = exp1.Evaluate(cont);
            SYMBOL_INFO eval_right = exp2.Evaluate(cont);

            if (eval_left.Type == TYPE_INFO.TYPE_STRING &&
                eval_right.Type == TYPE_INFO.TYPE_STRING)
            {
                SYMBOL_INFO ret_val = new SYMBOL_INFO();
                ret_val.str_val    = eval_left.str_val + eval_right.str_val;
                ret_val.Type       = TYPE_INFO.TYPE_STRING;
                ret_val.SymbolName = "";
                return(ret_val);
            }
            else if (eval_left.Type == TYPE_INFO.TYPE_NUMERIC &&
                     eval_right.Type == TYPE_INFO.TYPE_NUMERIC)
            {
                SYMBOL_INFO ret_val = new SYMBOL_INFO();
                ret_val.dbl_val    = eval_left.dbl_val + eval_right.dbl_val;
                ret_val.Type       = TYPE_INFO.TYPE_NUMERIC;
                ret_val.SymbolName = "";
                return(ret_val);
            }
            else
            {
                throw new Exception("Type mismatch");
            }
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont)
        {
Test:

            SYMBOL_INFO m_cond = cond.Evaluate(cont);


            if (m_cond == null || m_cond.Type != TYPE_INFO.TYPE_BOOL)
            {
                return(null);
            }

            if (m_cond.bol_val != true)
            {
                return(null);
            }

            SYMBOL_INFO tsp = null;

            foreach (Stmt rst in stmnts)
            {
                tsp = rst.Execute(cont);
                if (tsp != null)
                {
                    return(tsp);
                }
            }

            goto Test;
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <param name="actuals"></param>
        /// <returns></returns>
        public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont, ArrayList actuals)
        {
            ArrayList vars = new ArrayList();
            int       i    = 0;



            if (m_formals != null && actuals != null)
            {
                i = 0;
                foreach (SYMBOL_INFO b in m_formals)
                {
                    SYMBOL_INFO inf = actuals[i] as SYMBOL_INFO;
                    inf.SymbolName = b.SymbolName;
                    cont.TABLE.Add(inf);
                    i++;
                }
            }

            foreach (Stmt e1 in m_statements)
            {
                return_value = e1.Execute(cont);

                if (return_value != null)
                {
                    return(return_value);
                }
            }



            return(null);
        }
Beispiel #5
0
        public override SYMBOL_INFO  Execute(RUNTIME_CONTEXT cont)
        {
            SYMBOL_INFO val = exp1.Evaluate(cont);

            Console.WriteLine((val.Type == TYPE_INFO.TYPE_NUMERIC) ? val.dbl_val.ToString()  :
                              (val.Type == TYPE_INFO.TYPE_STRING) ? val.str_val : val.bol_val ? "TRUE" : "FALSE");
            return(null);
        }
Beispiel #6
0
        public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont, ArrayList actuals)
        {
            Procedure p = Find("Main");

            if (p != null)
            {
                return(p.Execute(cont, actuals));
            }

            return(null);
        }
Beispiel #7
0
 /// <summary>
 ///    To Evaluate a variable , we just need to do a lookup
 ///    in the Symbol table ( of RUNTIME_CONTEXT )
 /// </summary>
 /// <param name="st"></param>
 /// <param name="glb"></param>
 /// <returns></returns>
 public override SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont)
 {
     if (cont.TABLE == null)
     {
         return(null);
     }
     else
     {
         SYMBOL_INFO a = cont.TABLE.Get(m_name);
         return(a);
     }
 }
Beispiel #8
0
        /// <summary>
        ///    Interpret the if statement
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont)
        {
            //
            //  Evaluate the Condition
            //
            SYMBOL_INFO m_cond = cond.Evaluate(cont);

            //
            // if cond is not boolean..or evaluation failed
            //
            if (m_cond == null ||
                m_cond.Type != TYPE_INFO.TYPE_BOOL)
            {
                return(null);
            }


            SYMBOL_INFO ret = null;

            if (m_cond.bol_val == true)
            {
                //
                // if cond is true
                foreach (Stmt rst in stmnts)
                {
                    ret = rst.Execute(cont);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            }
            else if (else_part != null)
            {
                // if cond is false and there is
                // else statement ..!
                foreach (Stmt rst in else_part)
                {
                    ret = rst.Execute(cont);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            }

            return(null);
        }
Beispiel #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont)
        {
            SYMBOL_INFO eval_left = ex1.Evaluate(cont);


            if (eval_left.Type == TYPE_INFO.TYPE_BOOL)
            {
                SYMBOL_INFO ret_val = new SYMBOL_INFO();
                ret_val.Type       = TYPE_INFO.TYPE_BOOL;
                ret_val.SymbolName = "";
                ret_val.bol_val    = !eval_left.bol_val;
                return(ret_val);
            }
            else
            {
                return(null);
            }
        }
Beispiel #10
0
        public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont)
        {
            //if (isAsync)
            //{
            //    ComputeAsynecAssigment work = new ComputeAsynecAssigment(this.Execute);
            //    //        IAsyncResult pendingOp = work.BeginInvoke(4, null, null);
            //    //SYMBOL_INFO val2 = exp1.Evaluate(cont);
            //    //    c
            //    IAsyncResult pendingOp = work.BeginInvoke(cont, null, null);
            //    SYMBOL_INFO val2 = exp1.Evaluate(cont);
            //     cont.TABLE.Assign(variable, val2);
            //    //cont.TABLE.Assign(variable, val);
            //    // Do some other useful work.
            //    Thread.Sleep(30);
            //    //SYMBOL_INFO val2 = work.EndInvoke(pendingOp);
            //    return null;
            //}
            SYMBOL_INFO val = null;

            if (isAsync)
            {// call asyncroniously Fib function
                ///  Let's make the asynchronous call by creating the delegate and calling it


                iComputeAsynecCallAssigment work = new iComputeAsynecCallAssigment(exp1.Evaluate);
                IAsyncResult pendingOp           = work.BeginInvoke(cont, null, null);// the operation is pend  to be complet in other  thread go to other statment excution(b=async{return fib(n-2) ..})
                Console.WriteLine("Computing Async call(fib) in thread {0}", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(250);
                // Finish the async call.
                Console.WriteLine("Waiting for operation to complete.");
                val = work.EndInvoke(pendingOp);
                cont.TABLE.Assign(variable, val);
                return(null);
            }

            val = exp1.Evaluate(cont);
            cont.TABLE.Assign(variable, val);
            return(null);
        }
Beispiel #11
0
        /// <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);
        }
Beispiel #12
0
 /// <summary>
 ///    Evaluation of boolean will given the value
 /// </summary>
 /// <param name="local"></param>
 /// <param name="global"></param>
 /// <returns></returns>
 public override SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont)
 {
     return(info);
 }
Beispiel #13
0
 public abstract SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont);
Beispiel #14
0
 public abstract SYMBOL_INFO Execute(RUNTIME_CONTEXT cont);
Beispiel #15
0
 /// <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);
 }
Beispiel #16
0
        //

        public abstract SYMBOL_INFO Execute(RUNTIME_CONTEXT cont, ArrayList formals);
Beispiel #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public override SYMBOL_INFO Evaluate(RUNTIME_CONTEXT cont)
        {
            if (m_proc != null)
            {
                //
                // This is a Ordinary Function Call
                //
                //
                RUNTIME_CONTEXT ctx = new RUNTIME_CONTEXT(cont.GetProgram());

                ArrayList lst = new ArrayList();
                //TYPE_INFO clsrcall;

                foreach (Exp ex in m_actuals)
                {
                    lst.Add(ex.Evaluate(cont));
                }
                SymbolTable x = cont.TABLE;


                ///for closure function
                /// use the previous/saved the symbol table(context)  when the clouser function call for the second time :)

                if (m_proc.clossure != null)
                {
                    TModule.ClosureSymbol++;
                    // for the function(variable) closure function

                    if (TModule.ClosureSymbol < 2 || TModule.PrewFun != m_proc.Name)
                    {
                        //if (m_proc.IsVariable == true && (TModule.ClosureSymbol >= 2))
                        //{ return m_proc.clossure.Execute(Procedure.ClosureMentain, lst); }
                        Procedure.ClosureMentain = ctx;
                        m_proc.Execute(ctx, lst);
                        TModule.PrewFun = m_proc.Name;
                        return(m_proc.clossure.Execute(ctx, lst));
                    }


                    TModule.PrewFun = m_proc.Name;
                    return(m_proc.clossure.Execute(Procedure.ClosureMentain, lst));
                    //}
                    //else
                    //   return m_proc.clossure.Execute(ctx, lst);
                }
                return(m_proc.Execute(ctx, lst));
            }
            else
            {
                // Recursive function call...by the time we
                // reach here..whole program has already been
                // parsed. Lookup the Function name table and
                // resolve the Address
                //
                //
                m_proc = cont.GetProgram().Find(_procname);

                // note GetProgram() return the module
                RUNTIME_CONTEXT ctx = new RUNTIME_CONTEXT(cont.GetProgram());
                ArrayList       lst = new ArrayList();

                foreach (Exp ex in m_actuals)
                {
                    lst.Add(ex.Evaluate(cont));
                }

                return(m_proc.Execute(ctx, lst));
            }
        }
Beispiel #18
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="cont"></param>
 /// <returns></returns>
 public override SYMBOL_INFO Execute(RUNTIME_CONTEXT cont)
 {
     cont.TABLE.Add(m_inf);
     var = new Variable(m_inf);
     return(null);
 }