예제 #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;
            }
        }
예제 #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));
        }
예제 #3
0
 static void ExpandInline(CatExpr fxns, QuotedFunction q, int nMaxDepth)
 {
     foreach (Function f in q.GetSubFxns())
     {
         ExpandInline(fxns, f, nMaxDepth - 1);
     }
 }
예제 #4
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());
                    }
                }
            }
예제 #5
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;
                }
            }
        }
예제 #6
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;
                }
            }
예제 #7
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);
     }
 }
예제 #8
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())));
        }