示例#1
0
        static public int PositionIf(object predicate, object sequence)
        {
            if (sequence == null)
            {
                return(-1);
            }
            Cons list = sequence as Cons;

            if (list != null)
            {
                return(PositionIf(predicate, list));
            }
            throw new NotImplementedException("PositionIf on non-list");
        }
示例#2
0
        static public int PositionIf <T> (Predicate <T> predicate, Cons sequence)
        {
            int pos = 0;

            foreach (T element in sequence)
            {
                if (predicate(element))
                {
                    return(pos);
                }
                pos += 1;
            }
            return(-1);
        }
示例#3
0
        static public Cons Memq(object item, object list)
        {
            if (list == null)
            {
                return(null);
            }
            Cons pair = list as Cons;

            if (pair == null)
            {
                throw new NotImplementedException();
            }
            return((item == pair.Car)
                ? pair
                : Memq(item, pair.Cdr));
        }
示例#4
0
        static int Position(object item, object sequence, bool fromEnd, EqualityTest test, int start, int end, Function1 key)
        {
            if (sequence == null)
            {
                return(-1);
            }
            Cons list = sequence as Cons;

            if (list != null)
            {
                return(Position(item, list, fromEnd, test, start, end, key));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
示例#5
0
        static public object GetArgStar(object list, ConsCollection <Symbol> keys, object defaultValue)
        {
            if (list == null)
            {
                return(defaultValue);
            }
            Cons firstPair  = (Cons)list;
            Cons secondPair = (Cons)(firstPair.Cdr);

            if (CL.Memq <Symbol> ((Symbol)firstPair.Car, keys) != null)
            {
                return(secondPair.Car);
            }
            else
            {
                return(GetArgStar(secondPair.Cdr, keys, defaultValue));
            }
        }
示例#6
0
        static public ConsCollection <T> GetArgs <T> (Cons list, object key)
        {
            if (list == null)
            {
                return(null);
            }
            Cons firstPair  = (Cons)list;
            Cons secondPair = (Cons)(firstPair.Cdr);

            if (firstPair.Car == key)
            {
                return(new ConsCollection <T> ((T)secondPair.Car, GetArgs <T> ((Cons)secondPair.Cdr, key)));
            }
            else
            {
                return(GetArgs <T> ((Cons)secondPair.Cdr, key));
            }
        }
示例#7
0
        // RECONC
        static public object Reconc(object tail, object head)
        {
            if (tail == null)
            {
                return(head);
            }
            Cons ctail = tail as Cons;

            if (ctail != null)
            {
                // yeah I'll fix it...
                return(Reconc(ctail.Cdr, new Cons(ctail.Car, head)));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
示例#8
0
        static public object GetArg(object list, object key, object defaultValue)
        {
            if (list == null)
            {
                return(defaultValue);
            }
            Cons firstPair  = (Cons)list;
            Cons secondPair = (Cons)(firstPair.Cdr);

            if (firstPair.Car == key)
            {
                return(secondPair.Car);
            }
            else
            {
                return(GetArg(secondPair.Cdr, key, defaultValue));
            }
        }
示例#9
0
        public override ReaderMacroStep NextStep()
        {
            Cons   head = null;
            object tail = null;

            Readtable currentReadtable = CL.Readtable;

            while (true)
            {
                int  xx;
                char x;
                // Discard whitespace
                do
                {
                    xx = this.context.InputStream.Peek();
                    if (xx == -1)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        x = (char)xx;
                    }
                }while (currentReadtable.IsWhitespaceSyntax(x) && (this.context.InputStream.Read() != -1));
                if (x == ')')
                {
                    this.context.InputStream.Read();  // discard the close paren
                    return((CL.ReadSuppress == true)
                    ? new FinalReaderMacroStep()
                    : new FinalReaderMacroStep(CL.Reconc(head, tail)));
                }
                //else if (x == '.') {
                //    throw new NotImplementedException ();
                //}
                else
                {
                    object next = CL.Read(this.context.InputStream, true, null, true);  // recursive read
                    if (CL.ReadSuppress != true)
                    {
                        head = CL.Cons(next, head);
                    }
                }
            }
        }
示例#10
0
 static public Cons RemoveIf(Delegate predicate, Cons sequence)
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     if (sequence == null)
     {
         return(null);
     }
     else if ((bool)predicate.DynamicInvoke(sequence.Car))
     {
         return(RemoveIf(predicate, (Cons)sequence.Cdr));
     }
     else
     {
         return(new Cons(sequence.Car, RemoveIf(predicate, (Cons)sequence.Cdr)));
     }
 }
示例#11
0
        static public int Position(object item, object sequence)
        {
            if (sequence == null)
            {
                return(-1);
            }
            Cons list = sequence as Cons;

            if (list != null)
            {
                return(Position(item, list));
            }
            return(Position(item, sequence,
                            KW.FromEnd, false,
                            KW.Key, new Function1(CL.Identity),
                            KW.Test, new EqualityTest(CL.Eql),
                            KW.Start, 0,
                            KW.End, CL.Length(sequence)));
        }
示例#12
0
 public bool MoveNext()
 {
     if (current == null)
     {
         if (exhausted)
         {
             throw new NotImplementedException();
         }
         current = head;
         return(true);
     }
     else
     {
         current = (Cons)current.Cdr;
         if (current == null)
         {
             exhausted = true;
         }
         return(!exhausted);
     }
 }
示例#13
0
        public static Cons SubvectorToList(object [] vector, int start, int limit)
        {
            if (vector == null)
            {
                if (start == limit)
                {
                    return(null);
                }
                throw new ArgumentNullException("vector");
            }

            Cons answer = null;
            int  count  = 1;

            for (int i = start; i < limit; i++)
            {
                answer = new Cons(vector [limit - count], answer);
                count += 1;
            }
            return(answer);
        }
示例#14
0
        static public object RemoveIf(object predicate, object sequence)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            if (sequence == null)
            {
                return(null);
            }
            Cons seq = sequence as Cons;

            if (seq != null)
            {
                return(RemoveIf(ResolveFunctionSpecifier(predicate), seq));
            }
            else
            {
                throw new NotImplementedException("RemoveIf");
            }
        }
示例#15
0
        static public object Eval(object form)
        {
            Cons pair = form as Cons;

            if (pair != null)
            {
                object op       = pair.Car;
                object operands = pair.Cdr;
                object sym      = op as Symbol;
                if (sym != null)
                {
                    return(CL.Apply(CL.SymbolFunction(sym), Evlis(operands)));
                }
                else
                {
                    throw new NotImplementedException("Operator not a symbol");
                }
            }
            else
            {
                Symbol identifier = form as Symbol;
                if (identifier != null)
                {
                    return(CL.SymbolValue(form));
                }
                else
                {
                    string literal = form as String;
                    if (literal != null)
                    {
                        return(literal);
                    }
                    else
                    {
                        throw new NotImplementedException("No lambda functions yet");
                    }
                }
            }
        }
示例#16
0
        static Cons Evlis(object formlist)
        {
            Cons evaluated = null;

            while (true)
            {
                Cons formpair = formlist as Cons;
                if (formpair != null)
                {
                    evaluated = new Cons(Eval(formpair.Car), evaluated);
                    formlist  = formpair.Cdr;
                }
                else if (formpair == null)
                {
                    return((Cons)CL.Reverse(evaluated));
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }
示例#17
0
        static Cons MapListToList1(object functionSpecifier, Cons sequence)
        {
            Delegate function = ResolveFunctionSpecifier(functionSpecifier);
            Cons     answer   = null;

            while (sequence != null)
            {
                answer = new Cons(function.DynamicInvoke(sequence.Car), answer);
                object temp = sequence.Cdr;
                if (temp == null)
                {
                    break;
                }
                Cons next = temp as Cons;
                if (next == null)
                {
                    throw new NotImplementedException();
                }
                sequence = next;
            }
            return((Cons)CL.Reverse(answer));
        }
示例#18
0
 static Cons MapToList1(object functionSpecifier, object sequence)
 {
     if (sequence == null)
     {
         return(null);
     }
     object [] sa = sequence as object [];
     if (sa != null)
     {
         return(MapVectorToList1(functionSpecifier, sa));
     }
     else
     {
         Cons sc = sequence as Cons;
         if (sc != null)
         {
             return(MapListToList1(functionSpecifier, sc));
         }
         else
         {
             throw new NotImplementedException();
         }
     }
 }
示例#19
0
 public ConsEnumerator(Cons head)
 {
     this.head      = head;
     this.current   = null;
     this.exhausted = false;
 }
示例#20
0
 static public Cons Reverse(Cons list)
 {
     return((Cons)CL.Reconc(list, null));
 }
示例#21
0
 // CDAR
 public static object Cdar(Cons thing)
 {
     return((thing == null) ? null : CL.Cdr(thing.Car));
 }
示例#22
0
 // CDR
 public static object Cdr(Cons thing)
 {
     return((thing == null) ? null : thing.Cdr);
 }
示例#23
0
 public void Reset()
 {
     current   = null;
     exhausted = false;
 }