public static Object And(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.False ? Boolean.False : null); }), environment);
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)); }
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)); }
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))); }
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); }
public static bool IsInputPort(Object obj) => obj is Port p && p.Type == PortType.Input;
public static Applicative Lambda(Environment env, Object formals, List exprs) => Applicatives.Wrap(Vau(env, formals, Ignore.Instance, exprs));
public static Inert Define(Environment env, Object definiend, Object expr) { Match(env, definiend, env.Evaluate(expr)); return(Inert.Instance); }
public static Boolean Binds(Environment environment, Object expr, List symbols) => environment.Evaluate(expr) is Environment e?symbols.All <Symbol>(e.Contains) :
public static Object LetRedirect(Environment environment, Object exp, List bindings, List body) => (Evaluate(exp, environment) is Environment env) ?