예제 #1
0
 public static Object Or(Environment environment, List objects)
 => Evaluate(objects.FirstValidOrLast <Object>((obj) =>
 {
     Object temp = Evaluate(obj, environment);
     if (!(temp is Boolean))
     {
         throw new ArgumentException($"Argument {obj} does not evaluate to a boolean");
     }
     return(temp == Boolean.True ? Boolean.True : null);
 }), environment);
예제 #2
0
        public static Object If(Environment env, Object testInput, Object consequent, Object alternative)
        {
            Object test = Evaluate(testInput, env);

            if (!(test is Boolean condition))
            {
                throw new ArgumentException("Test is not a boolean.");
            }

            return(Evaluate(condition ? consequent : alternative, env));
        }
예제 #3
0
        public static Object LetRec(Environment environment, List bindings, List body)
        {
            Environment child = new Environment(environment);

            bindings.ForEach <Pair>(binding =>
            {
                Object expression = Cadr <Object>(binding);
                Match(child, binding.Car, Evaluate(expression, child));
            });
            return(Sequence(child, body));
        }
예제 #4
0
        public static Operative Vau(Environment env, Object formals, Object eformal, List expr)
        {
            if ((formals is Symbol && formals == eformal) ||
                (formals is Pair p && eformal is Symbol && p.Contains(eformal)))
            {
                throw new ArgumentException("Formals contain eformal");
            }

            return(new Operative(env,
                                 Applicatives.CopyEvaluationStructureImmutable(formals),
                                 eformal,
                                 expr.Select <Object>(Applicatives.CopyEvaluationStructureImmutable)));
        }
예제 #5
0
        public static Object Cond(Environment env, List objects)
        {
            Object result = Inert.Instance;

            objects.ForEach <Pair>(p =>
            {
                Object test = env.Evaluate(p.Car);
                if (!(test is Boolean condition))
                {
                    throw new ArgumentException($"Test in {p.Car} is not a boolean.");
                }

                if (condition)
                {
                    result = Sequence(env, (p.Cdr as Pair));
                    return(true);
                }
                return(false);
            });
            return(result);
        }
예제 #6
0
 public static Object Sequence(Environment env, List objects)
 => !objects.Any <Object>() ? Inert.Instance : Evaluate(objects.ForEachReturnLast <Object>((obj) => Evaluate(obj, env)), env);
예제 #7
0
 public static Applicative Lambda(Environment env, Object formals, List exprs)
 => Applicatives.Wrap(Vau(env, formals, Ignore.Instance, exprs));
예제 #8
0
 public static Inert Define(Environment env, Object definiend, Object expr)
 {
     Match(env, definiend, env.Evaluate(expr));
     return(Inert.Instance);
 }
예제 #9
0
 public static Boolean Binds(Environment environment, Object expr, List symbols)
 => environment.Evaluate(expr) is Environment e?symbols.All <Symbol>(e.Contains) :
예제 #10
0
 public static Boolean Defined(Environment environment, List symbols)
 => symbols.All <Symbol>(environment.Contains);
예제 #11
0
 public static Object LetSafe(Environment environment, List bindings, List body)
 => LetRedirect(environment, Applicatives.StandartEnvironment(), bindings, body);
예제 #12
0
 public static Object LetRedirect(Environment environment, Object exp, List bindings, List body)
 => (Evaluate(exp, environment) is Environment env) ?