Beispiel #1
0
 private static bool IsMember(Pair list_so_far, object token)
 {
     return ((list_so_far != null) && (list_so_far.car is Pair) &&
         (token is Symbol) && (((Symbol)token).IsMember()));
 }
Beispiel #2
0
 public Pair(object car, Pair cdr)
 {
     this.car= car; this.cdr = cdr;
 }
Beispiel #3
0
        public static object read_token(TextReader str)
        {
            if (str.Peek() == -1)
                return null;

            char c = (char) str.Read();
            if (Char.IsWhiteSpace(c)) // is_char_whitespace(c)) // .IsWhiteSpace(c))
                return read_token(str);
            else if (c.Equals(';'))
            {
                str.ReadLine();
                /*
                char curr = (char) str.Read();
                Console.WriteLine("skipping chars: ");
                while (!c.Equals('\n') && !c.Equals('\r') && (str.Peek() != -1))
                {

                    curr = (char) str.Read();
                    Console.Write(curr);
                }
                Console.WriteLine("skipping line");
                */
                return read_token(str);
            }
            else if (c.Equals('('))
                return left_paren_token;
            else if (c.Equals(')'))
                return right_paren_token;
            else if (c.Equals('\''))																			// Quote
            {
                object curr = read(str);
                Pair newpair = new Pair(curr);
                return Pair.Cons(Symbol.Create("quote"), newpair);
            }
            else if (c.Equals('"'))
            {
                char curr = (char) str.Read();
                string strtok = "";
                while(!curr.Equals('"'))
                {
                    strtok += curr;
                    int curr_as_int = str.Read();
                    if (curr_as_int == -1)
                        throw new Exception("Error parsing string: \"" + strtok + "\"");
                    else
                        curr = (char) curr_as_int;
                }
                return strtok;
            }
            else if (c.Equals('#'))																			// Boolean OR Character OR Vector
            {
                char curr = (char) str.Read();
                if (curr.Equals('\\')) // itsa char
                {
                    return (char) str.Read();
                } else if (curr.Equals('(')) {
                  return start_vector_token;
              } else  // itsa bool
              {
                      if (curr.Equals('t'))
                          return true;
                      else
                          return false; // should maybe also check for #a, etc.
              }
            }
            else if (Char.IsNumber(c) || ( c.Equals('-') && !Char.IsWhiteSpace((char) str.Peek()) ) )
            {
                string numstr = new string(c, 1);

                while (Char.IsNumber((char) str.Peek()) || ((char) str.Peek()).Equals('.'))
                    numstr += (char) str.Read();

                if (numstr.IndexOf('.') != -1)
                    return Convert.ToSingle(numstr);
                else
                    return Convert.ToInt32(numstr);
            }
            else //if (Char.IsLetter(c))
            {
                string symstr = new string(c, 1);

                while (!Char.IsWhiteSpace((char)str.Peek()) && !((char)str.Peek()).Equals(')') && (str.Peek() != -1))
                    symstr += (char) str.Read();

                return Symbol.Create(symstr);
            }
        }
Beispiel #4
0
 private static void AddMemberToCar(Pair pair, object token)
 {
     ((Pair)pair.car).member = ((Symbol) token).val.Substring(1);
     ((Pair)pair.car).hasMember = true;
 }
Beispiel #5
0
 public static Type GetType(String tname, Pair prefixes)
 {
     Type type = GetType(tname);
     if (type != null)
         return type;
     foreach (string prefix in prefixes)
     {
         type = GetType(prefix + "." + tname);
         if (type != null)
             return type;
     }
     return null;
 }
Beispiel #6
0
        public static Pair read_list(Pair list_so_far, TextReader str)
        {
            object token = read_token(str);

            if (token == null) // at the end of the stream, but no list. this is bad.
                throw new Exception("Error parsing list: " + Util.Dump(Pair.reverse(list_so_far)).TrimEnd(new char[] {')'})); // a continuation would be handy here

            if (right_paren_token.Equals(token))
                return Pair.reverse(list_so_far); // if this far we're cool
            else if (start_vector_token.Equals(token))
                return read_list(Pair.Cons(read_vector(str), list_so_far), str);
            else if (left_paren_token.Equals(token))
            {
            // 				if (str is DocumentReader)
            // 				{
            // 					DocumentReader documentReader = (DocumentReader) str;
            // 					//EditPoint startEditPoint = documentReader.editPoint.CreateEditPoint();
            // 					//startEditPoint.CharLeft(1);
            // 					int startEditPointLine = documentReader.Line;
            // 					int startEditPointCol = documentReader.Col;

            // 					Pair curList = read_list(null,str);
            // // 					if (curList != null)
            // // 						curList.marker = new Marker(documentReader.document, startEditPointLine, startEditPointCol, documentReader.Line, documentReader.Col + 1, "");//startEditPoint.GetText(documentReader.editPoint));

            // 					return read_list(Pair.Cons(curList, list_so_far), str);
            // 				}
            // 				else
                    return read_list(Pair.Cons(read_list(null,str), list_so_far),str);
            }
            else if (IsMember(list_so_far, token))
            {
                AddMemberToCar(list_so_far, token);
                return read_list(list_so_far,str);
            }
            else
                return read_list(Pair.Cons(token, list_so_far), str);
        }
Beispiel #7
0
 public PairEnumerator(Pair pair)
 {
     this.pair = pair; this.current = null;
 }
Beispiel #8
0
        // TODO: new cons for each?
        public static Pair reverse(Pair pair)
        {
            if (pair == null)
                return null;
            else
            {
                //TODO: change recursive copy to single copy
                Pair p = pair.Clone();
                p.cdr = null;
                return append(reverse(pair.cdr), p);

            /*
                Pair p = new Pair(pair.car);
                p.Info = pair.Info;
                return append(reverse(pair.cdr), p);*/
            }
        }
Beispiel #9
0
 public static Pair Cons(object obj, object p)
 {
     if (p != null)
         throw new Exception("dotted pairs not supported");
     Pair newPair = new Pair(obj);
     newPair.cdr = null;
     return newPair;
 }
Beispiel #10
0
 public static Pair Cons(object obj, Pair p)
 {
     Pair newPair = new Pair(obj);
     newPair.cdr = p;
     return newPair;
 }
Beispiel #11
0
        // TODO: new cons for each?
        public static Pair append(Pair ls1, Pair ls2)
        {
            if (ls1 == null)
                return ls2;
            else
            {
                Pair p = ls1.Clone();
                p.cdr = append(ls1.cdr, ls2);
                return p;

            /*				Pair p = Pair.Cons(ls1.car, append(ls1.cdr, ls2));
                p.Info = ls1.Info;
                return p;*/
            }
        }