public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args) { ScheminPair bindings = (ScheminPair) args.Car; IScheminType expression = args.ElementAt(1); ScheminPair letArgs = new ScheminPair(); ScheminPair argExps = new ScheminPair(); foreach (ScheminPair bindingPair in bindings) { letArgs = letArgs.Append(bindingPair.Car); argExps = argExps.Append(bindingPair.ElementAt(1)); } ScheminPair body = new ScheminPair(new ScheminPrimitive("begin")); ScheminPair next = letArgs; ScheminPair nextExp = argExps; while (!next.Empty) { IScheminType symbol = next.Car; IScheminType exp = nextExp.Car; ScheminPair setExp = new ScheminPair(new ScheminPrimitive("set!")); setExp = setExp.Append(symbol); setExp = setExp.Append(exp); body = body.Append(setExp); next = next.ListCdr(); nextExp = nextExp.ListCdr(); } body = body.Append(expression); ScheminPair lambdaDef = new ScheminPair(letArgs); foreach (IScheminType type in body) { lambdaDef = lambdaDef.Append(type); } Environment closure = env; ScheminLambda lam = new ScheminLambda(lambdaDef, closure); ScheminPair toEvaluate = new ScheminPair(lam); foreach (IScheminType arg in argExps) { toEvaluate = toEvaluate.Append(new ScheminPair()); } return toEvaluate; }
public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args) { bool isNamed = false; if ((args.Car as ScheminAtom) != null) { isNamed = true; } ScheminPair bindings; ScheminPair expression; if (!isNamed) { expression = args.ListCdr(); bindings = (ScheminPair)args.Car; } else { expression = args.ListCdr().ListCdr(); bindings = (ScheminPair)args.ElementAt(1); } ScheminPair letArgs = new ScheminPair(); ScheminPair argExps = new ScheminPair(); foreach (ScheminPair bindingPair in bindings) { letArgs = letArgs.Append(bindingPair.Car); argExps = argExps.Append(bindingPair.ElementAt(1)); } ScheminPair lambdaDef = new ScheminPair(letArgs); foreach (IScheminType type in expression) { lambdaDef = lambdaDef.Append(type); } Environment closure = env; if (isNamed) { closure = new Environment(); closure.parent = env; } ScheminLambda lam = new ScheminLambda(lambdaDef, closure); if (isNamed) { ScheminAtom name = (ScheminAtom)args.Car; closure.AddBinding(name, lam); } ScheminPair toEvaluate = new ScheminPair(lam); foreach (IScheminType arg in argExps) { toEvaluate = toEvaluate.Append(arg); } return toEvaluate; }
public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args) { ScheminLambda lam = new ScheminLambda(args, env); return lam; }
public ScheminRewriter(ScheminLambda rewriter, Environment definingEnv) { this.Rewriter = rewriter; this.DefiningEnv = definingEnv; }
public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args) { bool deffun = false; if ((args.Car as ScheminPair) != null) { deffun = true; } if (!deffun) { ScheminAtom symbol = (ScheminAtom) args.ElementAt(0); IScheminType definition = args.ElementAt(1); if (env.bindings.ContainsKey(symbol.Name)) { env.RemoveBinding(symbol); env.AddBinding(symbol, definition); } else { env.AddBinding(symbol, definition); } return new ScheminPair(); } else { ScheminPair arguments = (ScheminPair) args.Car; ScheminPair expression = args.ListCdr(); ScheminAtom name = (ScheminAtom) arguments.Car; IScheminType lamParams = arguments.Cdr; if (lamParams == null) lamParams = new ScheminPair(); ScheminPair lamArgs = new ScheminPair(lamParams, expression); ScheminLambda lam = new ScheminLambda(lamArgs, env); if (env.bindings.ContainsKey(name.Name)) { env.RemoveBinding(name); env.AddBinding(name, lam); } else { env.AddBinding(name, lam); } return new ScheminPair(); } }
public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args) { bool isNamed = false; if ((args.Car() as ScheminAtom) != null) { isNamed = true; } ScheminList bindings; ScheminList expression; if (!isNamed) { expression = args.Cdr(); bindings = (ScheminList) args.Car(); } else { expression = args.Cdr().Cdr(); bindings = (ScheminList) args.Cdr().Car(); } ScheminList letArgs = new ScheminList(); ScheminList argExps = new ScheminList(); letArgs.UnQuote(); argExps.UnQuote(); foreach (ScheminList bindingPair in bindings) { letArgs.Append(bindingPair.Car()); argExps.Append(bindingPair.Cdr().Car()); } ScheminList lambdaDef = new ScheminList(letArgs); lambdaDef.UnQuote(); foreach (IScheminType type in expression) { lambdaDef.Append(type); } Environment closure = env; if (isNamed) { closure = new Environment(); closure.parent = env; } ScheminLambda lam = new ScheminLambda(lambdaDef, closure); if (isNamed) { ScheminAtom name = (ScheminAtom) args.Car(); closure.AddBinding(name, lam); } ScheminList toEvaluate = new ScheminList(lam); toEvaluate.UnQuote(); foreach (IScheminType arg in argExps) { toEvaluate.Append(arg); } return toEvaluate; }
public override IScheminType Execute(Environment env, Evaluator eval, ScheminList args) { bool deffun = false; if ((args.Car() as ScheminList) != null) { deffun = true; } if (!deffun) { ScheminAtom symbol = (ScheminAtom) args.Car(); IScheminType definition = args.Cdr().Car(); if (env.bindings.ContainsKey(symbol.Name)) { env.RemoveBinding(symbol); env.AddBinding(symbol, definition); } else { env.AddBinding(symbol, definition); } return new ScheminList(false); } else { ScheminList arguments = (ScheminList) args.Car(); ScheminList expression = args.Cdr(); ScheminAtom name = (ScheminAtom) arguments.Car(); ScheminList argSymbols = arguments.Cdr(); ScheminList lamArgs = new ScheminList(argSymbols, expression); lamArgs.UnQuote(); ScheminLambda lam = new ScheminLambda(lamArgs, env); if (env.bindings.ContainsKey(name.Name)) { env.RemoveBinding(name); env.AddBinding(name, lam); } else { env.AddBinding(name, lam); } return new ScheminList(false); } }