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))); }
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)))); }
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))); }
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"))); }