コード例 #1
0
 public static Cons Last(int num, Cons list)
 {
     int len = Length(list);
     if (num > len)
         return null;
     return NthTail(len - num, list);
 }
コード例 #2
0
 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;
 }
コード例 #3
0
 public static int Length(Cons list)
 {
     int len = 0;
     while (list != null)
     {
         len++;
         list = list.rest;
     }
     return len;
 }
コード例 #4
0
        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;
        }
コード例 #5
0
 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);
     }
 }
コード例 #6
0
 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);
 }
コード例 #7
0
 public static Object Rest(Cons list)
 {
     return list.rest;
 }
コード例 #8
0
 private Boolean equalsRest(Cons that)
 {
     return (rest == null) ? that.rest == null :
     rest.Equals(that.rest);
 }
コード例 #9
0
        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;
            }
        }
コード例 #10
0
 internal CompositeSymbol(Cons symAsList)
 {
     this.symbolAsList = symAsList;
 }
コード例 #11
0
 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);
 }
コード例 #12
0
 public static Object First(Cons list)
 {
     return list.first;
 }
コード例 #13
0
 public static Object Fourth(Cons list)
 {
     return Third(list.rest);
 }
コード例 #14
0
 public Cons(Object first, Cons rest)
 {
     this.first = first;
     this.rest = rest;
 }
コード例 #15
0
 public static Cons Append(Cons x, Cons y)
 {
     return (x != null) ? new Cons(x.first, Append(x.rest, y)) : y;
 }
コード例 #16
0
 public void Reset()
 {
     this.current = null;
     this.next = start;
 }
コード例 #17
0
 public Boolean MoveNext()
 {
     current = next;
     if (current != null)
         next = current.rest;
     return current != null;
 }
コード例 #18
0
 public ConsEnumerator(Cons start)
 {
     this.start = start;
     this.current = null;
     this.next = start;
 }
コード例 #19
0
 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);
 }
コード例 #20
0
 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;
     }
 }
コード例 #21
0
 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);
     }
 }
コード例 #22
0
 public static Object Third(Cons list)
 {
     return Second(list.rest);
 }
コード例 #23
0
 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);
 }
コード例 #24
0
 public static Object Second(Cons list)
 {
     return list.rest.first;
 }
コード例 #25
0
        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;
        }
コード例 #26
0
 public static Object Nth(int n, Cons list)
 {
     return NthTail(n, list).first;
 }
コード例 #27
0
 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;
 }
コード例 #28
0
 public static Cons NthTail(int n, Cons list)
 {
     while (n > 0)
     {
         n--;
         list = list.rest;
     }
     return list;
 }
コード例 #29
0
 internal Macro(Cons args, Env env, Interpreter interpreter, Loc loc)
     : base(args, env, interpreter, loc)
 {
 }
コード例 #30
0
 private Boolean equalsFirst(Cons that)
 {
     return (first == null) ? that.first == null :
     first.Equals(that.first);
 }