Beispiel #1
0
        public static void runfile(string cfun0, TextReader tr0, bool prmpt)
        {
            ExtendedReader tr   = new ExtendedReader(tr0);
            Symbol         cfun = _ss(cfun0);

            while (true)
            {
                if (prmpt)
                {
                    con.Write(prompt); con.Flush();
                }
                Object o = aread(tr);
                if (o == null)
                {
                    return;
                }
                try
                {
                    Object result =
                        apply(cfun,
                              new Object[] { o });
                    if (prmpt)
                    {
                        con.WriteLine(rprompt + print(result));
                        con.Flush();
                    }
                }
                catch (Exception e)
                {
                    con.WriteLine("Exception " + e + "\nin: \n" + print(o));
                    con.Flush();
                }
            }
        }
Beispiel #2
0
        public static void bootfile(string nm, string on)
        {
            ExtendedReader tr = new ExtendedReader(new StreamReader(nm));

            Runtime.xpath = Path.GetDirectoryName(nm);
            StreamWriter wr = new StreamWriter(on);

            while (true)
            {
                Object o = Runtime.aread(tr);
                if (o == null)
                {
                    wr.Close();
                    tr.Close();
                    return;
                }

                Object result =
                    Runtime.apply(Symbol.make("read-compile-eval-dump"),
                                  new Object[] { o });
                if (result != null)
                {
                    wr.WriteLine(result);
                }
            }
        }
Beispiel #3
0
        public static Object read(string str)
        {
            ExtendedReader r = new ExtendedReader(new System.IO.StringReader(str));
            Object         p = aread(r);

            r.Close();
            return(p);
        }
Beispiel #4
0
        public static Object eval0(string str)
        {
            ExtendedReader r      = new ExtendedReader(new System.IO.StringReader(str));
            Object         o      = aread(r);
            Object         result = (Precompiler.compile(o).run(null, null));

            return(result);
        }
Beispiel #5
0
        public static Object eval(string str)
        {
            ExtendedReader r      = new ExtendedReader(new System.IO.StringReader(str));
            Object         o      = aread(r);
            Object         result =
                apply(_read_compile_eval,
                      new Object[] { o });

            return(result);
        }
Beispiel #6
0
 public static Object aread(ExtendedReader rdr)
 {
     if (_areadstr == null)
     {
         return(Reader.process(Reader.aread(rdr)));
     }
     else
     {
         return(apply(_ss(_areadstr), new Object[] { rdr }));
     }
 }
Beispiel #7
0
        public static Object meval(string str)
        {
            Object         result = null;
            ExtendedReader tr     = new ExtendedReader(new System.IO.StringReader(str));

            while (true)
            {
                Object o = aread(tr);
                if (o == null)
                {
                    return(result);
                }
                result =
                    apply(_read_compile_eval,
                          new Object[] { o });
            }
        }
Beispiel #8
0
        public static Object aread(ExtendedReader r)
        {
            Object prev_p = null;
            Stack  top    = new Stack();

            char[]  c        = new char[1];
            bool    rd       = true;
            bool    annotate = r.annotate;
            int     state    = 0;
            string  s        = "";
            pospair strt     = null;
            pospair crnt     = null;

            System.Collections.Stack st  = new System.Collections.Stack();
            System.Collections.Stack pos = new System.Collections.Stack();
            while (true)
            {
                char ch;
                if (!rd)
                {
                    if (top.Count > 0)
                    {
                        return(top.Pop());
                    }
                    else
                    {
                        return(null);
                    }
                }
                int n = r.Read(c, 0, 1);
                if (annotate)
                {
                    crnt = new pospair(r.file, r.linepos, r.cpos);
                }
                if (n == 0)
                {
                    rd = false; // stop
                    ch = ' ';   // simulate end of something
                    goto cont;
                }
                ch = c[0]; //Console.Write(ch);
cont:

                if (state == 0)
                {
                    switch (ch)
                    {
                    case '(':
                    {
                        // start a new list;
                        Pair p = new Pair(null, null);

                        top.Push(p);
                        st.Push(p);
                        pos.Push(0); // will fill car first
                        prev_p = p;
                        break;
                    }

                    case ')':
                    {
                        // pop a list
                        if (top.Count < 1)
                        {
                            throw new
                                  Meta.Scripting.MBaseException(new Pair(Symbol.make("READER-ERROR-AT:"),
                                                                         prev_p));
                        }
                        Object p = top.Pop();
                        if (p is Pair)
                        {
                            Pair pp = (Pair)p;
                            if (pp.car == null && pp.cdr == null)
                            {
                                p = null;
                            }
                        }
                        st.Pop();  // discard it
                        pos.Pop(); //discard it
                        if (!popper(st, pos, p))
                        {
                            return(p);
                        }
                        if (p != null)
                        {
                            prev_p = p;
                        }
                        state = 0;
                        break;
                    }

                    case '.':
                    {
                        s = "" + ch; strt = crnt; state = 6;
                        break;
                    }

                    case ';':
                    {
                        state = 4; // comment started
                        break;
                    }

                    case ' ':
                    case '\n':
                    case '\t':
                    case '\r':
                    case '\f':
                    { // just skip
                        break;
                    }

                    case '\"': // start a string literal
                    {
                        s     = "" + ch; strt = crnt;
                        state = 2;
                        break;
                    }

                    case '\'':
                    case '`': // immediate
                    {
                        s = "" + ch; strt = crnt;
                        if (!popper(st, pos, s))
                        {
                            return(s);
                        }
                        s = "";
                        break;
                    }

                    case ',': // delay
                    {
                        s     = "" + ch; strt = crnt;
                        state = 5;
                        break;
                    }

                    case '&': // another delayed reader
                    {
                        s     = "" + ch; strt = crnt;
                        state = 7;
                        break;
                    }

                    default:             // all other chars starts atoms
                    {
                        s     = "" + ch; // start a new one;
                        strt  = crnt;
                        state = 1;       // atom processing
                        break;
                    }
                    }
                }
                else if (state == 1)
                {
                    if (ch == '(' || ch == ')' || Char.IsWhiteSpace(ch) || ch == ';')
                    {
                        // atom ends here, sorry
                        if (annotate)
                        {
                            strt.s = s;
                            if (!popper(st, pos, strt))
                            {
                                return(strt);
                            }
                        }
                        else
                        {
                            if (!popper(st, pos, s))
                            {
                                return(s);
                            }
                        }
                        if (s != null)
                        {
                            prev_p = s;
                        }
                        s     = "";
                        state = 0;
                        goto cont; // now process the termination character in a new state
                    }
                    else
                    {
                        s = s + ch; // add a char to an atom
                    }
                }
                else if (state == 2)
                {
                    if (ch == '\\')
                    {
                        state = 3;
                    }
                    else if (ch == '\"')
                    {
                        s     = s + ch;
                        state = 1;
                        ch    = ' '; // simulate an end of atom
                        goto cont;
                    }
                    else
                    {
                        s = s + ch;
                    }
                }
                else if (state == 3)
                {
                    if (ch == 'n')
                    {
                        ch = '\n';
                    }
                    else if (ch == 't')
                    {
                        ch = '\t';
                    }
                    s = s + ch; state = 2;
                }
                else if (state == 4)
                {
                    if (ch == '\n')
                    {
                        state = 0;       // comment line finished
                    }
                }
                else if (state == 5)
                {
                    if (ch == '@')
                    {
                        s += ch;
                    }

                    if (!popper(st, pos, s))
                    {
                        return(s);
                    }
                    state = 0;
                    if (ch != '@')
                    {
                        goto cont;      // otherwise read next char
                    }
                }
                else if (state == 6)
                {
                    if (ch == '.')
                    {
                        s += ch; state = 1;
                    }
                    else
                    {
                        state = 0;
                        pos.Pop();
                        pos.Push(1); // will fill cdr, not a new cons cell
                        goto cont;
                    }
                }
                else if (state == 7)
                {
                    if (ch == '\"')
                    {
                        s    += ch;
                        state = 2;
                    }
                    else
                    {
                        state = 1;
                        goto cont;
                    }
                }
            }
        }