Пример #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) ?