interpreter.plt (3658B)
1 :- module('interpreter+tests', []). 2 :- use_module(library(debug), [assertion/1]). 3 4 :- use_module(interpreter). 5 :- use_module(test_helpers). 6 :- use_module(syntax/sexp). 7 8 decode(Codes, Expression) :- 9 test_one_result(E, parse_input_expression(Codes, E), Expression). 10 11 apply_example(car, [{|sexp||(a b c)|}], {|sexp||a|}). 12 apply_example(car, [{|sexp||((a b c) x y z)|}], {|sexp||(a b c)|}). 13 apply_example(car, [{|sexp||(((hotdogs)) (and) (pickle) relish)|}], {|sexp||((hotdogs))|}). 14 apply_example(car, [{|sexp||((hotdogs))|}], {|sexp||(hotdogs)|}). 15 16 apply_example(cdr, [{|sexp||(a b c)|}], {|sexp||(b c)|}). 17 apply_example(cdr, [{|sexp||((a b c) x y z)|}], {|sexp||(x y z)|}). 18 apply_example(cdr, [{|sexp||(hamburger)|}], {|sexp||()|}). 19 apply_example(cdr, [{|sexp||((x) t r)|}], {|sexp||(t r)|}). 20 21 apply_example(Func, Args, ExpectedResult) :- 22 apply_example(CallCs, ResultCs), 23 decode(CallCs, [Func|Args]), 24 decode(ResultCs, ExpectedResult). 25 26 apply_example(`(cons peanut (butter and jelly))`, 27 `(peanut butter and jelly)`). 28 29 apply_example(`(cons ((help) this) (is very ((hard) to learn)))`, 30 `(((help) this) is very ((hard) to learn))`). 31 32 apply_example(`(cons (a b (c)) ())`, `((a b (c)))`). 33 34 apply_example(`(+ 0 0)`, `0`). 35 apply_example(`(+ 3 2)`, `5`). 36 37 :- begin_tests('interpreter+test_apply'). 38 test(apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :- 39 initial_state(State0), 40 primitive_arity(Func, Arity), 41 interp_apply(primitive(Func, Arity), Args, Result, State0, _State1), 42 assertion(Result =@= ExpectedResult). 43 44 test(apply_arity_error, [error(interp_error(arity, car))]) :- 45 Func=car, 46 initial_state(State0), 47 primitive_arity(Func, Arity), 48 interp_apply(primitive(Func, Arity), [[a], [b]], _Result, State0, _State1). 49 50 :- end_tests('interpreter+test_apply'). 51 :- begin_tests('interpreter+test_eval'). 52 53 quoted_args([], []). 54 quoted_args([H|T], [[quote, H]|QT]) :- quoted_args(T, QT). 55 56 test_eval(Call, ExpectedResult) :- 57 initial_state(State0), 58 test_eval(Call, ExpectedResult, State0, _State1). 59 test_eval(Call, ExpectedResult, State0, State1) :- 60 format('testing call: ~w → expecting ~w | ~w~n', [Call, ExpectedResult, State0]), 61 interp_eval(Call, Result, State0, State1), 62 assertion(Result =@= ExpectedResult). 63 64 eval_example(`(car (cdr (quote ((b) (x y) ((c))))))`, `(x y)`). 65 eval_example(`(cdr (cdr (quote ((b) (x y) ((c))))))`, `(((c)))`). 66 67 eval_example_decoded(Call, ExpectedResult) :- 68 eval_example(CallCodes, ResultCodes), 69 decode(CallCodes, Call), 70 decode(ResultCodes, ExpectedResult). 71 72 test(eval_quote) :- 73 test_eval({|sexp||(quote a)|}, a). 74 75 test(eval_quote2) :- 76 test_eval({|sexp||(quote ())|}, []). 77 78 test(eval_apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :- 79 quoted_args(Args, QArgs), 80 Call = [Func|QArgs], 81 test_eval(Call, ExpectedResult). 82 83 test(eval_example, [forall(eval_example_decoded(Call, ExpectedResult))]) :- 84 test_eval(Call, ExpectedResult). 85 86 eval_define_lat :- 87 DefLat = {|sexp|| 88 (define lat? 89 (lambda (l) 90 (cond 91 ((null? l) #t) 92 ((atom? (car l)) (lat? (cdr l))) 93 (else #f))))|}, 94 initial_state(State0), 95 interp_eval(DefLat, _, State0, State1), 96 lookup_variable_value('lat?', Proc, State1, _), 97 assertion(Proc = proc([l], _, _)), 98 Call0 = {|sexp||(lat? (quote ()))|}, 99 test_eval(Call0, '#t', State1, _), 100 Call1 = {|sexp||(lat? (quote (Jack Sprat could eat no chicken fat)))|}, 101 test_eval(Call1, '#t', State1, _), 102 Call2 = {|sexp||(lat? (quote ((Jack) Sprat could eat no chicken fat)))|}, 103 test_eval(Call2, '#f', State1, _). 104 test(eval_define_lat) :- eval_define_lat. 105 106 :- end_tests('interpreter+test_eval'). 107 108 % vim: ft=prolog noet sts=4 ts=4 sw=4