public Lambda(Cons arguments, EnviromentFrame clojure, Cons body) { Argument = arguments; Closure = clojure; Arity = arguments.Car() == null ? Arity = 0 : arguments.Count(); Body = body.GetIterator().Select(subExp => (Cons)subExp).ToArray(); }
public Cons Expand(Cons values) { var vls = values.GetIterator().ToArray(); var dic = new Dictionary <string, object>(); for (int i = 0; i < Args.Length; i++) { dic[Args[i].Name] = vls[i].Car(); } return(Replacer(Body, dic)); }
public Cons MacroProcessing(Cons seq) // TODO { var rseq = new Cons(); foreach (var o in seq.GetIterator()) { var exp = o.Car(); if (exp is Cons) { var op = exp.Car() as Symbol; var arguments = exp.Cdr() as Cons; if (op?.Name == "define-syntax") { DefineMacro(arguments); continue; } else if (Macro.Lookup(op)) { foreach (var o1 in (Macro.Get(op).Expand(arguments)).GetIterator()) { if (o1.Car() is Cons) { rseq.Add(MacroProcessing(o1.Car())); } else { rseq.Add(o1.Car()); } } continue; } rseq.Add(MacroProcessing(exp)); } else { rseq.Add(exp); var rest = o?.Cdr(); if (!(rest is Cons)) { rseq.Add(rest, true); } } } return(rseq); }
private static MemoryStream WriteConsSeq(MemoryStream ms, Cons tree, SymbolTable symbolTable) { ms.WriteByte(Codes.Open); foreach (var o in tree.GetIterator()) { WriteItem(ms, o.Car(), symbolTable); var cdr = o.Cdr(); if (cdr is Cons || (cdr == null)) { continue; } ms.WriteByte(Codes.Dot); WriteItem(ms, cdr, symbolTable); } ms.WriteByte(Codes.Close); return(ms); }
private Cons Replacer(Cons seq, Dictionary <string, object> vals) { var rSeq = new Cons(); foreach (var o in seq.GetIterator()) { var itm = o.Car(); if (itm is Cons) { itm = Replacer(itm as Cons, vals); } else if (itm is Symbol) { if (vals.ContainsKey((itm as Symbol).Name)) { itm = vals[(itm as Symbol).Name]; } } rSeq.Add(itm); } return(rSeq); }
public static Cons MExpand(Cons seq) // TODO { var rseq = new Cons(); foreach (var o in seq.GetIterator()) { var exp = o.Car(); if (exp is Cons) { var op = exp.Car() as Symbol; var arguments = exp.Cdr() as Cons; if (op?.Name == "defmacro") { var mName = arguments?.Car().Car() as Symbol; var mArg = arguments?.Car().Cdr() as Cons; var mBody = arguments?.Cdr() as Cons; macro.Define(mName, new Macro(mArg, mBody)); continue; } else if (macro.Lookup(op)) { foreach (var o1 in (macro.Get(op).Expand(arguments)).GetIterator()) { rseq.Add(o1.Car()); } continue; } } rseq.Add(exp); } return(rseq); }
public Macro(Cons args, Cons body) { Args = args.GetIterator().Select(i => i.Car() as Symbol).ToArray(); Body = body; }
public static Operation CompileBegin(Cons seq, Operation next) { var res = Operation.Nop(); return(seq.GetIterator().Reverse().Aggregate(res, (current, exp) => Compile(exp.Car(), current.Cmd == Operation.Comand.Nop ? next : current))); }