Exemplo n.º 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");
        }
Exemplo n.º 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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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);
                    }
                }
            }
        }
Exemplo n.º 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)));
     }
 }
Exemplo n.º 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)));
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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");
            }
        }
Exemplo n.º 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");
                    }
                }
            }
        }
Exemplo n.º 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();
                }
            }
        }
Exemplo n.º 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));
        }
Exemplo n.º 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();
         }
     }
 }
Exemplo n.º 19
0
 public ConsEnumerator(Cons head)
 {
     this.head      = head;
     this.current   = null;
     this.exhausted = false;
 }
Exemplo n.º 20
0
 static public Cons Reverse(Cons list)
 {
     return((Cons)CL.Reconc(list, null));
 }
Exemplo n.º 21
0
 // CDAR
 public static object Cdar(Cons thing)
 {
     return((thing == null) ? null : CL.Cdr(thing.Car));
 }
Exemplo n.º 22
0
 // CDR
 public static object Cdr(Cons thing)
 {
     return((thing == null) ? null : thing.Cdr);
 }
Exemplo n.º 23
0
 public void Reset()
 {
     current   = null;
     exhausted = false;
 }