示例#1
0
                public void TestVector()
                {
                    Reader rdr = new Reader();
                    NLVector vec = (NLVector)rdr.ReadFirstForm("[]");
                    Assert.IsNotNull(vec);
                    Assert.AreEqual(0, vec.Contents.Length);

                    vec = (NLVector) rdr.ReadFirstForm("[1]");
                    Assert.IsNotNull(vec);
                    Assert.AreEqual(1, vec.Contents.Length);
                    long ival = (long)vec.Contents[0];
                    Assert.AreEqual(1, ival);

                    vec = (NLVector)rdr.ReadFirstForm("[ 1, 2]");
                    Assert.IsNotNull(vec);
                    Assert.AreEqual(2, vec.Contents.Length);

                    ival = (long)vec.Contents[0];
                    Assert.AreEqual(1, ival);

                    ival = (long)vec.Contents[1];
                    Assert.AreEqual(2, ival);

                    vec = (NLVector)rdr.ReadFirstForm(" [ [, 42 ] \"a\" ]");
                    Assert.IsNotNull(vec);
                    Assert.AreEqual(2, vec.Contents.Length);

                    NLVector vval = (NLVector)vec.Contents[0];
                    Assert.AreEqual(42, (long)vval.Contents[0]);

                    string sval = (string)vec.Contents[1];
                    Assert.AreEqual("a", sval);
                }
示例#2
0
文件: REPL.cs 项目: foogoof/nihilisp
                public static void Main()
                {
                    Reader arr = new Reader();
                    Evaluator eval = new Evaluator();
                    Greet();
                    do {
                        Console.Write("nihil> ");

                        string str = Console.ReadLine().Trim();
                        // FIXME: the really annoying echo problem

                        if (str.Length == 0) {
                            // Why yes, this *is* the hard way of printing an empty line.
                            str = "(System.Console.WriteLine)";
                        }

                        try {
                            object form = arr.ReadFirstForm(str.Trim());
                            if (form == null) {
                                throw new Exception("uh, couldn't read " + str);
                            }
                            form = eval.Evaluate(form);
                            if (form != null) {
                                Console.WriteLine(PrettyPrinter.Reformat(form));
                            }
                        } catch (ReaderException rex) {
                            Console.WriteLine("Reader exception: " + rex.Message);
                            Console.WriteLine(rex.StackTrace);
                        } catch (EvaluatorException eex) {
                            Console.WriteLine("Evaluator exception: " + eex.Message);
                            Console.WriteLine(eex.StackTrace);
                        } catch (Exception ex) {
                            Console.WriteLine("UNEXPECTED exception: " + ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                    } while (true);
                }
示例#3
0
                public void TestMap()
                {
                    Reader rdr = new Reader();
                    NLMap map = (NLMap)rdr.ReadFirstForm("{}");
                    Assert.AreEqual(0, map.Contents.Length);

                    map = (NLMap)rdr.ReadFirstForm("{ 42 \"everything\" }");
                    Assert.AreEqual(2, map.Contents.Length);
                    Assert.AreEqual(42, (long)map.Contents[0]);
                    Assert.AreEqual("everything", (string)map.Contents[1]);

                    map = (NLMap)rdr.ReadFirstForm("{ [0], :foo }");
                    Assert.AreEqual(2, map.Contents.Length);
                    Assert.AreEqual(0, (long)((NLVector)map.Contents[0]).Contents[0]);
                    Assert.AreEqual(":foo", ((NLKeyword)map.Contents[1]).ToString());

                    // Form equivalence not trivial
                    // NLMap map = (NLMap)form;
                    // Assert.AreEqual("everything", map.Get(NLInteger.Create("42")));
                }
示例#4
0
                public void TestSymbol()
                {
                    Reader rdr = new Reader();
                    NLSymbol sym = (NLSymbol)rdr.ReadFirstForm("foo");
                    Assert.AreEqual("foo", sym.ToString());

                    sym = (NLSymbol)rdr.ReadFirstForm("foo.bar");
                    Assert.AreEqual("foo", sym.Namespace);
                    Assert.AreEqual("bar", sym.FunctionName);
                }
示例#5
0
                public void TestList()
                {
                    Reader rdr = new Reader();
                    NLList list = (NLList)rdr.ReadFirstForm("( )");
                    Assert.IsNotNull(list);
                    Assert.AreEqual(0, list.Contents.Length);

                    list = (NLList)rdr.ReadFirstForm("(1)");
                    Assert.IsNotNull(list);
                    Assert.AreEqual(1, list.Contents.Length);
                    long ival = (long)list.Contents[0];
                    Assert.AreEqual(1, ival);

                    list = (NLList)rdr.ReadFirstForm("( 1, 2)");
                    Assert.IsNotNull(list);
                    Assert.AreEqual(2, list.Contents.Length);

                    ival = (long)list.Contents[0];
                    Assert.AreEqual(1, ival);

                    ival = (long)list.Contents[1];
                    Assert.AreEqual(2, ival);

                    list = (NLList)rdr.ReadFirstForm(" ( [, 42 ] \"a\" )");
                    Assert.IsNotNull(list);
                    Assert.AreEqual(2, list.Contents.Length);

                    NLVector vval = (NLVector)list.Contents[0];
                    Assert.AreEqual(42, (long)vval.Contents[0]);

                    string sval = (string)list.Contents[1];
                    Assert.AreEqual("a", sval);
                }