예제 #1
0
 public void StringToList()
 {
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant('t'), Lisp.Constant('e'), Lisp.Constant('s'), Lisp.Constant('t')),
                            Lisp.List(Lisp.Symbol("string->list"), Lisp.Constant("test")));
     LispAssert.EvaluatesTo(Lisp.Nil,
                            Lisp.List(Lisp.Symbol("string->list"), Lisp.Constant("")));
 }
예제 #2
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)));
        }
예제 #3
0
 public void StringLength()
 {
     LispAssert.EvaluatesTo(Lisp.Constant(4),
                            Lisp.List(Lisp.Symbol("string-length"), Lisp.Constant("test")));
     LispAssert.EvaluatesTo(Lisp.Constant(0),
                            Lisp.List(Lisp.Symbol("string-length"), Lisp.Constant("")));
 }
예제 #4
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));
 }
예제 #5
0
 public void MakeString()
 {
     LispAssert.EvaluatesTo(Lisp.Constant("xxxxxxxxxx"),
                            Lisp.List(Lisp.Symbol("make-string"), Lisp.Constant(10), Lisp.Constant('x')));
     LispAssert.ThrowsWhenEvaluated(
         Lisp.List(Lisp.Symbol("make-string"), Lisp.Constant(9.3m), Lisp.Constant('x')));
     LispAssert.EvaluatesTo(Lisp.Constant(8),
                            Lisp.List(Lisp.Symbol("string-length"), Lisp.List(Lisp.Symbol("make-string"), Lisp.Constant(8))));
 }
예제 #6
0
 public void List()
 {
     LispAssert.EvaluatesTo(Lisp.Nil,
                            Lisp.List(Lisp.Symbol("list")));
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(1)),
                            Lisp.List(Lisp.Symbol("list"), Lisp.Constant(1)));
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Constant(1), Lisp.Constant(2)),
                            Lisp.List(Lisp.Symbol("list"), Lisp.Constant(1), Lisp.Constant(2)));
 }
예제 #7
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)));
 }
예제 #8
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)))));
 }
예제 #9
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)))));
 }
예제 #10
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()));
        }
예제 #11
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()));
        }
예제 #12
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()));
        }
예제 #13
0
 public void StringRef()
 {
     LispAssert.EvaluatesTo(Lisp.Constant('t'),
                            Lisp.List(Lisp.Symbol("string-ref"), Lisp.Constant("test"), Lisp.Constant(0)));
     LispAssert.EvaluatesTo(Lisp.Constant('s'),
                            Lisp.List(Lisp.Symbol("string-ref"), Lisp.Constant("test test test"), Lisp.Constant(12)));
     LispAssert.ThrowsWhenEvaluated(
         Lisp.List(Lisp.Symbol("string-ref"), Lisp.Constant("test"), Lisp.Constant(-1)));
     LispAssert.ThrowsWhenEvaluated(
         Lisp.List(Lisp.Symbol("string-ref"), Lisp.Constant("test"), Lisp.Constant(4)));
     LispAssert.ThrowsWhenEvaluated(
         Lisp.List(Lisp.Symbol("string-ref"), Lisp.Constant("test"), Lisp.Constant(2.2m)));
 }
예제 #14
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")));
        }
예제 #15
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()));
        }
예제 #16
0
        public void Subtract()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(-1),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(1)));
            LispAssert.EvaluatesTo(Lisp.Constant(-1),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(-4),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3)));

            LispAssert.EvaluatesTo(Lisp.Constant(-0.1m),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(3.1m), Lisp.Constant(2.1m), Lisp.Constant(1.1m)));
            LispAssert.EvaluatesTo(Lisp.Constant(0.1m),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(3.1m), Lisp.Constant(2), Lisp.Constant(1)));
            LispAssert.EvaluatesTo(Lisp.Constant(-0.1m),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(3), Lisp.Constant(2.1m), Lisp.Constant(1)));
            LispAssert.EvaluatesTo(Lisp.Constant(-0.1m),
                                   Lisp.List(Lisp.Symbol("-"), Lisp.Constant(3), Lisp.Constant(2), Lisp.Constant(1.1m)));
        }
예제 #17
0
        public void Add()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(1),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1)));
            LispAssert.EvaluatesTo(Lisp.Constant(3),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(6),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3)));

            LispAssert.EvaluatesTo(Lisp.Constant(6.3m),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1.1m), Lisp.Constant(2.1m), Lisp.Constant(3.1m)));
            LispAssert.EvaluatesTo(Lisp.Constant(6.1m),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1.1m), Lisp.Constant(2), Lisp.Constant(3)));
            LispAssert.EvaluatesTo(Lisp.Constant(6.1m),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1), Lisp.Constant(2.1m), Lisp.Constant(3)));
            LispAssert.EvaluatesTo(Lisp.Constant(6.1m),
                                   Lisp.List(Lisp.Symbol("+"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3.1m)));
        }
예제 #18
0
        public void Multiply()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(1),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(1)));
            LispAssert.EvaluatesTo(Lisp.Constant(2),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(6),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3)));
            LispAssert.EvaluatesTo(Lisp.Constant(24),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3), Lisp.Constant(4)));

            LispAssert.EvaluatesTo(Lisp.Constant(3.1m * 2.1m * 1.1m),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(3.1m), Lisp.Constant(2.1m), Lisp.Constant(1.1m)));
            LispAssert.EvaluatesTo(Lisp.Constant(2.1m * 3 * 4),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(2.1m), Lisp.Constant(3), Lisp.Constant(4)));
            LispAssert.EvaluatesTo(Lisp.Constant(2 * 3.1m * 4),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(2), Lisp.Constant(3.1m), Lisp.Constant(4)));
            LispAssert.EvaluatesTo(Lisp.Constant(2 * 3 * 4.1m),
                                   Lisp.List(Lisp.Symbol("*"), Lisp.Constant(2), Lisp.Constant(3), Lisp.Constant(4.1m)));
        }
예제 #19
0
        public void Divide()
        {
            LispAssert.EvaluatesTo(Lisp.Constant(1),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(1)));
            LispAssert.EvaluatesTo(Lisp.Constant(0.5m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(1), Lisp.Constant(2)));
            LispAssert.EvaluatesTo(Lisp.Constant(1m / 2m / 3m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3)));
            LispAssert.EvaluatesTo(Lisp.Constant(1m / 2m / 3m / 4m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(1), Lisp.Constant(2), Lisp.Constant(3), Lisp.Constant(4)));

            LispAssert.EvaluatesTo(Lisp.Constant(3.1m / 2.1m / 1.1m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(3.1m), Lisp.Constant(2.1m), Lisp.Constant(1.1m)));
            LispAssert.EvaluatesTo(Lisp.Constant(2.1m / 3m / 4m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(2.1m), Lisp.Constant(3), Lisp.Constant(4)));
            LispAssert.EvaluatesTo(Lisp.Constant(2m / 3.1m / 4m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(2), Lisp.Constant(3.1m), Lisp.Constant(4)));
            LispAssert.EvaluatesTo(Lisp.Constant(2m / 3m / 4.1m),
                                   Lisp.List(Lisp.Symbol("/"), Lisp.Constant(2), Lisp.Constant(3), Lisp.Constant(4.1m)));
        }
예제 #20
0
 public void Quote()
 {
     LispAssert.EvaluatesTo(Lisp.List(Lisp.Symbol("car"), Lisp.Nil),
                            Lisp.Quote(Lisp.List(Lisp.Symbol("car"), Lisp.Nil)));
 }