public static Cons Last(int num, Cons list) { int len = Length(list); if (num > len) return null; return NthTail(len - num, list); }
public static Object MakeList(params Object[] args) { Cons ret = null; for (int i = args.Length - 1; i >= 0; --i) { ret = new Cons(args[i], ret); } return ret; }
public static int Length(Cons list) { int len = 0; while (list != null) { len++; list = list.rest; } return len; }
internal Closure(Cons args, Env env, Interpreter interpreter, Loc loc) { this.interpreter = interpreter; this.loc = loc; ArgSpecs specs = analyzeArgSpec((Cons)args.first, env, loc); //create an env expanded by params in which to analyze body Env env2 = new Env(specs.parameters, null, env); this.argSpecs = specs; this.body = interpreter.analyze(new Cons(interpreter.BLOCK, args.rest), env2, loc); this.env = env; }
internal ApplyExpression(Cons args, Env env, Interpreter interpreter, Loc loc) { this.loc = loc; this.interpreter = interpreter; fexpr = interpreter.analyze(args.first, env, loc); if (fexpr is IVar) { fsym = ((IVar)fexpr).getSymbol(); } Int32 len = Cons.Length(args.rest); argexprs = new IExpression[len]; args = args.rest; for (Int32 i = 0; i < argexprs.Length; i++, args = args.rest) { argexprs[i] = interpreter.analyze(args.first, env, loc); } }
internal DynamicLet(Cons args, Env env, Interpreter interpreter, Loc loc) { this.loc = loc; this.interpreter = interpreter; Cons bindlist = (Cons)args.first; Int32 blen = Cons.Length(bindlist); if ((blen % 2) != 0) //odd { throw new Exception("Odd number of args in dynamic-let binding list"); } binds = new BindPair[blen / 2]; for (int i = 0; i < binds.Length; i++) { binds[i].dvar = (DynamicVar)interpreter.analyze(bindlist.first, env, loc); bindlist = bindlist.rest; binds[i].expr = interpreter.analyze(bindlist.first, env, loc); bindlist = bindlist.rest; } this.body = interpreter.analyze(new Cons(interpreter.BLOCK, args.rest), env, loc); }
public static Object Rest(Cons list) { return list.rest; }
private Boolean equalsRest(Cons that) { return (rest == null) ? that.rest == null : rest.Equals(that.rest); }
internal Object map_to_list(params Object[] args) { Object f = Primitives.arg(0, args); IEnumerator[] enums = new IEnumerator[args.Length - 1]; for (int i = 0; i < enums.Length; i++) { enums[i] = (IEnumerator)get_enum_gf.Invoke(Primitives.arg(i + 1, args)); } //n.b. setting up arg array which will be reused //mean functions cannot assume ownership of args w/o copying them Object[] fargs = new Object[enums.Length]; Cons ret = null; Cons tail = null; while (true) { for (int i = 0; i < enums.Length; i++) { if (enums[i].MoveNext()) fargs[i] = enums[i].Current; else //bail on shortest return ret; } Object x = Util.InvokeObject(f, fargs); Cons node = new Cons(x, null); if (ret == null) ret = tail = node; else tail = tail.rest = node; } }
internal CompositeSymbol(Cons symAsList) { this.symbolAsList = symAsList; }
internal SetExpression(Cons args, Env env, Interpreter interpreter, Loc loc) { this.loc = loc; this.interpreter = interpreter; Int32 len = Cons.Length(args); if (len != 2) throw new Exception("Wrong number of args for set"); var = (IVar)interpreter.analyze(args.first, env, loc); val = interpreter.analyze(Cons.Second(args), env, loc); }
public static Object First(Cons list) { return list.first; }
public static Object Fourth(Cons list) { return Third(list.rest); }
public Cons(Object first, Cons rest) { this.first = first; this.rest = rest; }
public static Cons Append(Cons x, Cons y) { return (x != null) ? new Cons(x.first, Append(x.rest, y)) : y; }
public void Reset() { this.current = null; this.next = start; }
public Boolean MoveNext() { current = next; if (current != null) next = current.rest; return current != null; }
public ConsEnumerator(Cons start) { this.start = start; this.current = null; this.next = start; }
internal IfExpression(Cons args, Env env, Interpreter interpreter, Loc loc) { this.loc = loc; this.interpreter = interpreter; Int32 len = Cons.Length(args); if (len < 2 || len > 3) throw new Exception("Wrong number of args for if"); test = interpreter.analyze(args.first, env, loc); brtrue = interpreter.analyze(Cons.Second(args), env, loc); if (len == 3) brfalse = interpreter.analyze(Cons.Third(args), env, loc); else brfalse = new QuoteExpr(null); }
public static Object[] ToVector(Cons list) { int len = Length(list); if (len == 0) return Util.EMPTY_VECTOR; else { Object[] result = new Object[len]; for (int i = 0; list != null; i++, list = list.rest) { result[i] = list.first; } return result; } }
internal OrExpression(Cons args, Env env, Interpreter interpreter, Loc loc) { this.loc = loc; this.interpreter = interpreter; exprs = new IExpression[Cons.Length(args)]; for (Int32 i = 0; i < exprs.Length; i++, args = args.rest) { exprs[i] = interpreter.analyze(args.first, env, loc); } }
public static Object Third(Cons list) { return Second(list.rest); }
internal WhileExpression(Cons args, Env env, Interpreter interpreter, Loc loc) { this.loc = loc; this.interpreter = interpreter; this.test = interpreter.analyze(args.first, env, loc); this.body = interpreter.analyze(new Cons(interpreter.BLOCK, args.rest), env, loc); }
public static Object Second(Cons list) { return list.rest.first; }
internal Cons readDelimitedList(LocTextReader t, Int32 delim) { Cons ret = null; Cons tail = null; Int32 ch = t.Peek(); while (Char.IsWhiteSpace((Char)ch)) { t.Read(); ch = t.Peek(); } while (ch != delim) { Object o = doRead(t, delim == ')' && ret == null); if (eof(o)) { throw new Exception("Read error - eof found before matching: " + (Char)delim + "\n File: " + t.file + ", line: " + t.line); } EndDelimiter ed = o as EndDelimiter; if (ed != null) { if (ed.delim == delim) { return ret; } else throw new Exception("Read error - read unmatched: " + ed.delim + "\n File: " + t.file + ", line: " + t.line); } Cons link = new Cons(o, null); if (delim == ')' && ret == null && o is CompositeSymbol) { ret = ((CompositeSymbol)o).symbolAsList; tail = ret.rest; } else if (ret == null) { ret = tail = link; } else { tail.rest = link; tail = link; } ch = t.Peek(); while (Char.IsWhiteSpace((Char)ch)) { t.Read(); ch = t.Peek(); } } //eat delim t.Read(); return ret; }
public static Object Nth(int n, Cons list) { return NthTail(n, list).first; }
internal Object ReadVector(params Object[] args) { LocTextReader t = (LocTextReader)args[0]; Int32 line = t.line; Cons largs = readDelimitedList(t, ']'); Object ret = new Cons(interpreter.VECTOR, largs); //record the location locTable[ret] = new Loc(t.file, line); return ret; }
public static Cons NthTail(int n, Cons list) { while (n > 0) { n--; list = list.rest; } return list; }
internal Macro(Cons args, Env env, Interpreter interpreter, Loc loc) : base(args, env, interpreter, loc) { }
private Boolean equalsFirst(Cons that) { return (first == null) ? that.first == null : first.Equals(that.first); }