public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var argArray = DatumHelpers.enumerate(args).ToArray();
     Array.Reverse(argArray);
     c = c.PushTask(new InvokeFunction(function, argArray.Length));
     return argArray.Aggregate(c, (current, arg) => current.Evaluate(env, arg));
 }
Esempio n. 2
0
 public override Continuation Evaluate(Continuation oldContinuation, Datum args)
 {
     // Replace the old continuation with the new continuation - but pass in the
     // supplied argument as the 'return value' of the new continuation.
     var returnValue = args.ToArray()[0];
     return c.PushResult(returnValue);
 }
Esempio n. 3
0
 public override Continuation Evaluate(Continuation s, Datum args)
 {
     var argArray = args.ToArray();
     var expression = argArray[0];
     var environment = (Environment) argArray[1].CastObject();
     return s.Evaluate(environment, expression);
 }
Esempio n. 4
0
 private static Datum evaluate(Continuation c, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length != 1)
         throw c.error("invalid syntax '{0}'", args);
     return argList[0];
 }
Esempio n. 5
0
 public Datum Evaluate(Continuation c)
 {
     try
     {
         while (c.Task != null)
         {
             if (debug)
             {
                 Console.WriteLine("{0}", c.GetStackTrace());
                 Console.Write("Press enter for next step.");
                 Console.ReadLine();
             }
             c = c.Task.Perform(c.PopTask());
         }
         c = c.PopTask();
         var result = c.Result;
         c = c.PopResult();
         if (c.Result != null)
             throw new Exception(string.Format("Additional '{0}' on result stack", c.Result));
         return result;
     }
     catch (EvaluationException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw c.error(ex, "EvaluationError", ex.Message);
     }
 }
 public override Continuation Evaluate(Continuation c, Datum args)
 {
     var argArray = args.ToArray();
     if (argArray.Length != 2)
         throw DatumHelpers.error("Invalid syntax. ArgCount ({0}) != 2. Usage: (execute-with-error-handler <error-function> <fn>)", argArray.Length);
     var errorHandler = makeErrorHandler(c.ErrorHandler, (StackFunction)argArray[0]);
     var fn = (StackFunction)argArray[1];
     return fn.Evaluate(c.NewErrorHandler(errorHandler), DatumHelpers.compound());
 }
Esempio n. 7
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length != 2)
         throw c.error("Expected 2 arguments: (set! <symbol> <expression>). Got {0} instead", argList.Length);
     var name = argList[0].CastIdentifier();
     var expression = argList[1];
     c = c.PushTask(new SetName(env, name));
     return c.Evaluate(env, expression);
 }
Esempio n. 8
0
 public override Continuation Evaluate(Continuation c, Datum args)
 {
     var datumArgs = args.ToArray();
     if (datumArgs.Length != 2)
         throw c.error("Apply expects 2 arguments. {0} passed", datumArgs.Length);
     var function = datumArgs[0] as StackFunction;
     if (function == null)
         throw c.error("'{0}' is not a function", datumArgs[0]);
     return function.Evaluate(c, datumArgs[1]);
 }
 public Continuation Perform(Continuation c)
 {
     var argResults = DatumHelpers.nil;
     for (var i = 0; i < argCount; ++i)
     {
         argResults = DatumHelpers.cons(c.Result, argResults);
         c = c.PopResult();
     }
     return function.Evaluate(c, argResults);
 }
Esempio n. 10
0
 public Continuation Perform(Continuation c)
 {
     var args = new Datum[argCount];
     for (var i = 0; i < argCount; ++i)
     {
         args[i] = c.Result;
         c = c.PopResult();
     }
     return function.Evaluate(c, DatumHelpers.compound(args));
 }
Esempio n. 11
0
 public override Continuation Evaluate(Continuation c, Datum args)
 {
     var argArray = args.ToArray();
     if(argArray.Length != 1)
         throw DatumHelpers.error("call/cc: expect a single function as an argument. Got {0}", argArray.Length);
     var arg = argArray[0];
     var function = arg as StackFunction;
     if(function == null)
         throw DatumHelpers.error("call/cc: {0} must be a function", arg);
     return function.Evaluate(c, DatumHelpers.compound(new ContinuationFunction(c)));
 }
Esempio n. 12
0
 public override Continuation Evaluate(Continuation oldContinuation, Datum args)
 {
     // Replace the old continuation with the new continuation - but pass in the
     // supplied argument as the 'return value' of the new continuation.
     var argArray = args.ToArray();
     // We allow calling a "continuation" with 0 args. Such a continuation
     // only arises from the error function.
     // TODO: we should differentiate the two with an "expected args" member which we can error check.
     if (argArray.Length == 0)
         return c;
     return c.PushResult(argArray[0]);
 }
Esempio n. 13
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length != 2)
         throw c.error("Expected 2 arguments: (define <symbol> <expression>). Got {0} instead", argList.Length);
     var name = argList[0].CastSymbol();
     var expression = argList[1];
     c = c.PushTask(
         tc => { env.Define(name, tc.Result);
                 return tc;},
         "define '{0}'", name);
     return c.Evaluate(env, expression);
 }
Esempio n. 14
0
 public Continuation Perform(Continuation c)
 {
     // The result datum may be a graph. This makes certain
     // optimizations risky.
     var expansion = c.Result;
     if(macroDatum != null)
     {
         // Cache macro expansions. In the extremely
         // common case of the same macro being used on the
         // same Datum, re-use the expansion.
         macroDatum.Cache = cons(macro, expansion);
     }
     return c.PopResult().PopEnv().Evaluate(c.Env, expansion);
 }
Esempio n. 15
0
        private static Datum evaluate(Continuation c, Environment env, Datum args)
        {
            var macroArgs = args.ToArray();
            if (macroArgs.Length % 2 != 0)
                throw c.error("Invalid macro syntax for lambda. Argument count for '{0}' is not even ({1}). Syntax is (lambda [args body]+)", args, macroArgs.Length);

            var argBodies = new List<ArgBody>();
            for (var i = 0; i < macroArgs.Length; i += 2)
            {
                var closureArgs = macroArgs[i];
                var body = macroArgs[i + 1];
                argBodies.Add(new ArgBody(closureArgs, body));
            }
            return new Closure(env, argBodies);
        }
Esempio n. 16
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length < 1)
         throw c.error("Expected at least 1 expression for begin. Got none.");
     // Scope any local definitions.
     var localEnv = env.NewFrame();
     var remaining = argList.Reverse().ToArray();
     for (var i = 0; i < remaining.Length; ++i)
     {
         if (i > 0)
             c = c.PushTask(popResult, "Discard result");
         c = c.Evaluate(localEnv, remaining[i]);
     }
     return c;
 }
Esempio n. 17
0
 private static Datum Evaluate(Continuation c)
 {
     while (c.Task != null)
     {
         try
         {
             c = c.Task.Perform(c.PopTask());
             c.Statistics.Steps++;
         }
         catch (Exception ex)
         {
             c = c.ErrorHandler(c, ex);
         }
     }
     return(c.Result);
 }
Esempio n. 18
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length < 1)
         throw c.error("Expected at least 1 expression for begin. Got none.");
     // Scope any local definitions.
     var localEnv = new Environment(env);
     var remaining = argList.Reverse().ToArray();
     for (var i = 0; i < remaining.Length; ++i)
     {
         if (i > 0)
             c = c.PushTask(Ignore.Instance);
         c = c.Evaluate(localEnv, remaining[i]);
     }
     return c;
 }
Esempio n. 19
0
 private static Datum Evaluate(Continuation c)
 {
     while (c.Task != null)
     {
         try
         {
             c = c.Task.Perform(c.PopTask());
             c.Statistics.Steps++;
         }
         catch (Exception ex)
         {
             c = c.ErrorHandler(c, ex);
         }
     }
     return c.Result;
 }
Esempio n. 20
0
        public static Datum Lookup(this Continuation c, string name)
        {
            var env = GetEnvironment(c);

            if (env == null)
            {
                return(null);
            }

            Datum datum;

            if (env.TryLookup(name, out datum))
            {
                return(datum);
            }

            return(DatumHelpers.atom("undefined"));
        }
Esempio n. 21
0
 private static FExpression toFExpression(Continuation c)
 {
     return(c.Result.accept(new FExpressionConverter(c)));
 }
Esempio n. 22
0
 public static Continuation Unhandled(Continuation c, Exception ex)
 {
     throw new EvaluationException(c, ex);
 }
Esempio n. 23
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     c = c.PushTask(new EvaluateExpansion(env));
     return argFunction.Evaluate(c, args);
 }
Esempio n. 24
0
 public Continuation Perform(Continuation c)
 {
     var expansion = c.Result;
     c = c.PopResult();
     return c.Evaluate(env, expansion);
 }
Esempio n. 25
0
 public ContinuationFunction(Continuation c)
 {
     this.c = c;
 }
Esempio n. 26
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     return c.PushResult(env.ToAtom());
 }
Esempio n. 27
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     return c.PushResult(evaluate(c, args));
 }
Esempio n. 28
0
 public EvaluationException(string msg, Continuation c, Exception cause)
     : base(msg, cause)
 {
     this.c = c;
 }
Esempio n. 29
0
 public static StackFunction MakeContinuationFunction(Continuation c)
 {
     return new ContinuationFunction(c);
 }
Esempio n. 30
0
        public Continuation Perform(Continuation c)
        {
            var fexpression = toFExpression(c);

            return(fexpression.Evaluate(c.PopResult(), env, args));
        }
Esempio n. 31
0
 public Continuation Perform(Continuation c)
 {
     return c.PopResult();
 }
Esempio n. 32
0
 public Continuation Perform(Continuation c)
 {
     return(taskDelegate(c));
 }
Esempio n. 33
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var p = args as Pair;
     c = c.PushEnv(env).PushTask(new EvaluateExpansion(this, p));
     // Optimization - if this macro has been expanded on this Datum before,
     // use the same expansion.
     // See "macro-cache-in-datum" unit test for a demonstration of why
     // we need to check against "this" also.
     if(p != null)
     {
         var cachedPair = p.Cache as Pair;
         if(cachedPair != null && ReferenceEquals(cachedPair.First, this))
             return c.PushResult(cachedPair.Second);
     }
     c.Statistics.Expansions++;
     return argFunction.Evaluate(c, args);
 }
Esempio n. 34
0
 public static EvaluationException error(this Continuation c, Exception cause, string msg, params object[] args)
 {
     return(new EvaluationException(string.Format(msg, args), c, cause));
 }
Esempio n. 35
0
 public FExpressionConverter(Continuation c)
 {
     this.c = c;
 }
 public abstract Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args);
Esempio n. 37
0
 public override Continuation Evaluate(Continuation c, Datum args)
 {
     foreach (var ab in argBodies)
     {
         var closureEnv = ab.binding(env, args);
         if (closureEnv == null) continue;
         return c.Evaluate(closureEnv, ab.body);
     }
     throw bindError(args);
 }
Esempio n. 38
0
 public Continuation Perform(Continuation c)
 {
     env.Set(name, c.Result);
     return c;
 }
Esempio n. 39
0
 public Continuation Perform(Continuation c)
 {
     return(datum.accept(new Visitor(c, env)));
 }
 public EvaluationException(Continuation continuation, Exception ex)
     : base("Evaluation failed", ex)
 {
     this.Continuation = continuation;
 }
 public abstract Continuation Evaluate(Continuation c, Datum args);
Esempio n. 42
0
 public static EvaluationException error(this Continuation c, string msg, params object[] args)
 {
     return(error(c, null, msg, args));
 }
 public override Continuation Evaluate(Continuation c, Datum args)
 {
     return c.PushResult(function.Evaluate(args));
 }
Esempio n. 44
0
 public EvaluationException(string msg, Continuation c, Exception cause)
     : base(msg, cause)
 {
     this.c = c;
 }
Esempio n. 45
0
 public Continuation Perform(Continuation c)
 {
     return(datum.accept(new Visitor(c.PopEnv(), c.Env)));
 }
Esempio n. 46
0
 public static Continuation Evaluate(this Continuation s, Environment e, Datum expression)
 {
     return(s.PushTask(new EvaluateTask(e, expression)));
 }
Esempio n. 47
0
 public static Continuation Invoke(this Continuation s, Function f, Datum args)
 {
     return(toStack(f).Evaluate(s, args));
 }
Esempio n. 48
0
 public override Continuation Evaluate(Continuation c, Datum args)
 {
     return(c.PushResult(function.Evaluate(args)));
 }