Exemplo n.º 1
0
        public void If()
        {
            ILispEnvironment environment = new GlobalEnvironment();

            environment.Set(new Symbol("kablooey"), new NativeLambda {
                Body = parameters => { throw new LispException("kablooey!"); }
            });

            // Ensure only the "then" clause is evaluated on a true condition.
            LispAssert.EvaluatesTo(Lisp.Constant(1),
                                   Lisp.List(Lisp.Symbol("if"), Lisp.Constant(true), Lisp.Constant(1), Lisp.List(Lisp.Symbol("kablooey"))),
                                   environment);
            LispAssert.ThrowsWhenEvaluated(
                Lisp.List(Lisp.Symbol("if"), Lisp.Constant(true), Lisp.List(Lisp.Symbol("kablooey")), Lisp.Constant(1)),
                environment);

            // Ensure only the "else" clause is evaluated on a false condition.
            LispAssert.EvaluatesTo(Lisp.Constant(1),
                                   Lisp.List(Lisp.Symbol("if"), Lisp.Constant(false), Lisp.List(Lisp.Symbol("kablooey")), Lisp.Constant(1)),
                                   environment);
            LispAssert.ThrowsWhenEvaluated(
                Lisp.List(Lisp.Symbol("if"), Lisp.Constant(false), Lisp.Constant(1), Lisp.List(Lisp.Symbol("kablooey"))),
                environment);

            // Ensure the test condition clause is evaluated.  In this case, a non-empty list would be true if not
            // evaluated, but evaluating the expression will produce false.
            LispAssert.EvaluatesTo(Lisp.Constant(2),
                                   Lisp.List(Lisp.Symbol("if"), Lisp.List(Lisp.Symbol("number?"), Lisp.Symbol("car")), Lisp.Constant(1), Lisp.Constant(2)));

            // Test truth / falsiness of non-boolean - only #f and () should count as false.
            LispAssert.EvaluatesTo(Lisp.Constant(2), Lisp.List(Lisp.Symbol("if"), Lisp.Nil, Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(1), Lisp.List(Lisp.Symbol("if"), Lisp.Quote(Lisp.List(Lisp.Constant(false))), Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(1), Lisp.List(Lisp.Symbol("if"), Lisp.Constant(""), Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(1), Lisp.List(Lisp.Symbol("if"), Lisp.Symbol("cdr"), Lisp.Constant(1), Lisp.Constant(2)));
        }
Exemplo n.º 2
0
 public void ListToString()
 {
     LispAssert.EvaluatesTo(Lisp.Constant("test"),
                            Lisp.List(Lisp.Symbol("list->string"), Lisp.Quote(Lisp.List(Lisp.Constant('t'), Lisp.Constant('e'), Lisp.Constant('s'), Lisp.Constant('t')))));
     LispAssert.EvaluatesTo(Lisp.Constant(""),
                            Lisp.List(Lisp.Symbol("list->string"), Lisp.Nil));
 }
Exemplo n.º 3
0
 public void Cons()
 {
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(1)),
                            Lisp.List(Lisp.Symbol("cons"), Lisp.Constant(1), Lisp.Nil));
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(1), Lisp.Constant(2)),
                            Lisp.List(Lisp.Symbol("cons"), Lisp.Constant(1), Lisp.Quote(Lisp.List(Lisp.Constant(2)))));
     LispAssert.EvaluatesTo(Lisp.Cons(Lisp.Constant(1), Lisp.Constant(2)),
                            Lisp.List(Lisp.Symbol("cons"), Lisp.Constant(1), Lisp.Constant(2)));
 }
Exemplo n.º 4
0
 public void Length()
 {
     LispAssert.EvaluatesTo(Lisp.Constant(0),
                            Lisp.List(Lisp.Symbol("length"), Lisp.Nil));
     LispAssert.EvaluatesTo(Lisp.Constant(1),
                            Lisp.List(Lisp.Symbol("length"), Lisp.Quote(Lisp.List(Lisp.Constant(1)))));
     LispAssert.EvaluatesTo(Lisp.Constant(2),
                            Lisp.List(Lisp.Symbol("length"), Lisp.Quote(Lisp.List(Lisp.Constant(1), Lisp.Constant(2)))));
     LispAssert.EvaluatesTo(Lisp.Constant(5),
                            Lisp.List(Lisp.Symbol("length"), Lisp.Quote(Lisp.List(Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3), Lisp.Constant(4), Lisp.Constant(5)))));
 }
Exemplo n.º 5
0
 public void Reverse()
 {
     LispAssert.EvaluatesTo(Lisp.Nil,
                            Lisp.List(Lisp.Symbol("reverse"), Lisp.Nil));
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(1)),
                            Lisp.List(Lisp.Symbol("reverse"), Lisp.Quote(Lisp.List(Lisp.Constant(1)))));
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(2), Lisp.Constant(1)),
                            Lisp.List(Lisp.Symbol("reverse"), Lisp.Quote(Lisp.List(Lisp.Constant(1), Lisp.Constant(2)))));
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(5), Lisp.Constant(4), Lisp.Constant(3), Lisp.Constant(2), Lisp.Constant(1)),
                            Lisp.List(Lisp.Symbol("reverse"), Lisp.Quote(Lisp.List(Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3), Lisp.Constant(4), Lisp.Constant(5)))));
 }
Exemplo n.º 6
0
        public void Cdr()
        {
            LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(2), Lisp.Constant(3)),
                                   Lisp.List(Lisp.Symbol("cdr"), Lisp.Quote(Lisp.List(Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3)))));
            LispAssert.EvaluatesTo(Lisp.Nil,
                                   Lisp.List(Lisp.Symbol("cdr"), Lisp.Quote(Lisp.List(Lisp.Constant(1)))));
            LispAssert.EvaluatesTo(Lisp.Constant(2),
                                   Lisp.List(Lisp.Symbol("cdr"), Lisp.Quote(Lisp.Cons(Lisp.Constant(1), Lisp.Constant(2)))));

            LispAssert.ThrowsWhenEvaluated(Lisp.List(Lisp.Symbol("cdr"), Lisp.Nil));
            LispAssert.ThrowsWhenEvaluated <TypeMismatchException>(Lisp.List(Lisp.Symbol("cdr"), Lisp.Constant(3)));
            LispAssert.ThrowsWhenEvaluated <SignatureMismatchException>(Lisp.List(Lisp.Symbol("cdr")));
        }
Exemplo n.º 7
0
        public void StringP()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(true), Lisp.List(Lisp.Symbol("string?"), Lisp.Constant("test")));

            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("string?"), Lisp.Constant(true)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("string?"), Lisp.Constant('x')));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("string?"), Nil.Instance));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("string?"), Lisp.Constant(5)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("string?"), Lisp.Quote(Lisp.Symbol("test"))));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("string?"), new UndefinedValue()));
        }
Exemplo n.º 8
0
        public void NumberP()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(true), Lisp.List(Lisp.Symbol("number?"), Lisp.Constant(5)));
            LispAssert.EvaluatesTo(Lisp.Constant(true), Lisp.List(Lisp.Symbol("number?"), Lisp.Constant(9.95m)));

            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("number?"), Lisp.Constant(true)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("number?"), Lisp.Constant('x')));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("number?"), Nil.Instance));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("number?"), Lisp.Constant("test")));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("number?"), Lisp.Quote(Lisp.Symbol("test"))));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("number?"), new UndefinedValue()));
        }
Exemplo n.º 9
0
        public void ListP()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(true), Lisp.List(Lisp.Symbol("list?"), Lisp.Nil));
            LispAssert.EvaluatesTo(Lisp.Constant(true), Lisp.List(Lisp.Symbol("list?"), Lisp.Quote(Lisp.List(Lisp.Constant(1), Lisp.Symbol("a")))));

            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("list?"), Lisp.Constant(true)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("list?"), Lisp.Constant('x')));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("list?"), Lisp.Constant(5)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("list?"), Lisp.Constant("test")));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("list?"), Lisp.Quote(Lisp.Symbol("test"))));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("list?"), new UndefinedValue()));
        }
Exemplo n.º 10
0
        public void LambdaP()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(true), Lisp.List(Lisp.Symbol("lambda?"), Lisp.Symbol("lambda?")));
            // TODO: add test for user-defined function

            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), Lisp.Constant(true)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), Lisp.Constant('x')));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), Nil.Instance));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), Lisp.Constant(5)));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), Lisp.Constant("test")));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), Lisp.Quote(Lisp.Symbol("test"))));
            LispAssert.EvaluatesTo(Lisp.Constant(false), Lisp.List(Lisp.Symbol("lambda?"), new UndefinedValue()));
        }
Exemplo n.º 11
0
 public void Quote()
 {
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Symbol("car"), Lisp.Nil),
                            Lisp.Quote(Lisp.List(Lisp.Symbol("car"), Lisp.Nil)));
 }