Example #1
0
        public QuotedFunction(QuotedFunction first, QuotedFunction second)
        {
            mSubFxns = new CatExpr(first.GetSubFxns().ToArray());
            mSubFxns.AddRange(second.GetSubFxns().ToArray());

            msDesc = "anonymous composed function";
            msName = "";
            for (int i = 0; i < mSubFxns.Count; ++i)
            {
                if (i > 0)
                {
                    msName += " ";
                }
                msName += mSubFxns[i].GetName();
            }

            try {
                mpFxnType = new CatQuotedType(CatTypeReconstructor.ComposeTypes(first.GetFxnType(), second.GetFxnType()));
                // TODO: remove once everythign tests okay.
                //mpFxnType = new CatQuotedType(CatTypeReconstructor.ComposeTypes(first.GetUnquotedFxnType(), second.GetUnquotedFxnType()));
            } catch (Exception e) {
                Output.WriteLine("unable to type quotation: " + ToString());
                Output.WriteLine("type error: " + e.Message);
                mpFxnType = null;
            }
        }
Example #2
0
        static public QuotedFunction ApplyMacros(INameLookup names, QuotedFunction f)
        {
            CatExpr list = new CatExpr(f.GetSubFxns().ToArray());

            MetaCat.ApplyMacros(names, list);
            return(new QuotedFunction(list));
        }
Example #3
0
 static void ExpandInline(CatExpr fxns, QuotedFunction q, int nMaxDepth)
 {
     foreach (Function f in q.GetSubFxns())
     {
         ExpandInline(fxns, f, nMaxDepth - 1);
     }
 }
Example #4
0
        static public QuotedFunction ExpandInline(QuotedFunction f, int nMaxDepth)
        {
            CatExpr ret = new CatExpr();

            ExpandInline(ret, f, nMaxDepth);
            return(new QuotedFunction(ret));
        }
Example #5
0
 public QuotedFunction GetQuotedFxn()
 {
     // An important optimization: this can be very slow.
     if (mQF == null)
     {
         mQF = new QuotedFunction(mSubFxns, CatFxnType.Unquote(mpFxnType));
     }
     return(mQF);
 }
Example #6
0
            public override void Eval(Executor exec)
            {
                QuotedFunction f = exec.TypedPeek <QuotedFunction>();

                foreach (Function g in f.GetSubFxns())
                {
                    if (exec.TestFunction(g))
                    {
                        Output.WriteLine("tests succeeded for " + g.GetName());
                    }
                }
            }
Example #7
0
            public override void Eval(Executor exec)
            {
                QuotedFunction f = exec.TypedPeek <QuotedFunction>();

                if (f.GetMetaData() != null)
                {
                    exec.Push(f.GetMetaData().ToList());
                }
                else
                {
                    exec.Push(new CatList());
                }
            }
Example #8
0
 /// <summary>
 /// This is a simple yet effective combination of optimizations.
 /// </summary>
 static public QuotedFunction Optimize(INameLookup names, QuotedFunction qf)
 {
     //qf = ApplyMacros(qf);
     //qf = ApplyMacros(qf);
     //qf = Expand(qf);
     //qf = ApplyMacros(qf);
     //qf = PartialEval(qf);
     //qf = Expand(qf);
     //qf = ReplaceSimpleQuotations(qf);
     qf = ApplyMacros(names, qf);
     qf = ExpandInline(qf, 4);
     qf = ApplyMacros(names, qf);
     return(qf);
 }
Example #9
0
        /// <summary>
        /// This function is optimized to handle tail-calls with increasing the stack size.
        /// </summary>
        /// <param name="fxns"></param>
        public void Execute(CatExpr fxns)
        {
            int i = 0;

            while (i < fxns.Count)
            {
                Function f = fxns[i];

                // Check if this is a tail call
                // if so then we are going to avoid creating a new stack frame
                if (i == fxns.Count - 1 && f.GetSubFxns() != null)
                {
                    Trace("tail-call of '" + f.GetName() + "' function");
                    fxns = f.GetSubFxns();
                    i    = 0;
                }
                else if (i == fxns.Count - 1 && f is Primitives.If)
                {
                    Trace("tail-call of 'if' function");
                    QuotedFunction onfalse = PopFxn();
                    QuotedFunction ontrue  = PopFxn();

                    if (PopBool())
                    {
                        fxns = ontrue.GetSubFxns();
                    }
                    else
                    {
                        fxns = onfalse.GetSubFxns();
                    }

                    i = 0;
                }
                else if (i == fxns.Count - 1 && f is Primitives.ApplyFxn)
                {
                    Trace("tail-call of 'apply' function");
                    QuotedFunction q = PopFxn();
                    fxns = q.GetSubFxns();
                    i    = 0;
                }
                else
                {
                    Trace(f.ToString());
                    f.Eval(this);
                    ++i;
                }
            }
        }
Example #10
0
            public override void Eval(Executor exec)
            {
                QuotedFunction f        = exec.TypedPeek <QuotedFunction>();
                bool           bVerbose = Config.gbVerboseInference;
                bool           bInfer   = Config.gbTypeChecking;

                Config.gbVerboseInference = true;
                Config.gbTypeChecking     = true;
                try {
                    CatFxnType ft = CatTypeReconstructor.Infer(f.GetSubFxns());
                    if (ft == null)
                    {
                        Output.WriteLine("type could not be inferred");
                    }
                } finally {
                    Config.gbVerboseInference = bVerbose;
                    Config.gbTypeChecking     = bInfer;
                }
            }
Example #11
0
 public static Function ValueToFunction(Object o)
 {
     if (o is Int32)
     {
         return(new PushInt((int)o));
     }
     else if (o is Double)
     {
         return(new PushValue <double>((double)o));
     }
     else if (o is String)
     {
         return(new PushValue <string>((string)o));
     }
     else if (o is Boolean)
     {
         bool b = (bool)o;
         if (b)
         {
             return(new Primitives.True());
         }
         else
         {
             return(new Primitives.False());
         }
     }
     else if (o is CatList)
     {
         return(new PushValue <CatList>(o as CatList));
     }
     else if (o is QuotedFunction)
     {
         QuotedFunction qf   = o as QuotedFunction;
         CatExpr        fxns = qf.GetSubFxns();
         PushFunction   q    = new PushFunction(fxns);
         return(q);
     }
     else
     {
         throw new Exception("Partial evaluator does not yet handle objects of type " + o);
     }
 }
Example #12
0
            public override void Eval(Executor exec)
            {
                QuotedFunction qf = exec.PopFxn();

                exec.Push(Optimizer.ApplyMacros(exec, qf));
            }
Example #13
0
            public override void Eval(Executor exec)
            {
                QuotedFunction qf = exec.PopFxn();

                exec.Push(Optimizer.ExpandInline(qf, 1));
            }
Example #14
0
        /// <summary>
        /// This will reduce an expression by evaluating as much at compile-time as possible.
        /// </summary>
        public static QuotedFunction PartialEval(QuotedFunction qf)
        {
            Executor exec = new Executor();

            return(new QuotedFunction(PartialEval(exec, qf.GetSubFxns())));
        }