Ejemplo n.º 1
0
        static Object.Environment extendFunctionEnv(Object.Function fn, List <Object.Object> args)
        {
            Object.Environment env = Object.Environment.NewEnclosedEnvironment(fn.Env);

            for (int paramIdx = 0; paramIdx < args.Count; paramIdx++)
            {
                env.Set(fn.Parameters[paramIdx].Value, args[paramIdx]);
            }

            return(env);
        }
Ejemplo n.º 2
0
        private static Object.Environment ExtendFuncitonEnv(Object.Function function, IEnumerable <Object.Object> args)
        {
            var env = new Object.Environment(function.Env);
            var idx = 0;

            foreach (var arg in args)
            {
                env.Set(function.Parameters[idx].Value, arg);
                idx++;
            }
            return(env);
        }
Ejemplo n.º 3
0
 private static Object.Object ApplyFunction(Object.Object fn, IEnumerable <Object.Object> args)
 {
     if (fn.GetType() == typeof(Object.Function))
     {
         Object.Function function    = fn as Object.Function;
         var             extendedEnv = ExtendFuncitonEnv(function, args);
         var             evaluated   = Eval(function.Body, extendedEnv);
         return(unwrapReturnValue(evaluated));
     }
     if (fn.GetType() == typeof(Object.Builtin))
     {
         return(((Object.Builtin)fn).Fn(args.ToArray()));
     }
     return(new Object.Error()
     {
         Message = $"not a funciton. {fn.Type()}"
     });
 }