commit ee73dbbbc5f317442a338b192d4a96a09760de2f
parent ad49f09993ef3c417559ce69de157c87dc68c5a6
Author: Jan Pobrislo <ccx@te2000.cz>
Date: Sun, 13 Sep 2026 16:09:00 +0000
Implement abbreviations from r6rs, rename test files to .plt, enforce module naming convention.
Diffstat:
| M | Makefile | | | 2 | +- |
| D | dotexp.pl | | | 144 | ------------------------------------------------------------------------------- |
| D | dotexp_tests.pl | | | 226 | ------------------------------------------------------------------------------- |
| A | interp_errors.plt | | | 9 | +++++++++ |
| M | interpreter.pl | | | 64 | +--------------------------------------------------------------- |
| A | interpreter.plt | | | 108 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules.plt | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
| A | syntax/common.pl | | | 32 | ++++++++++++++++++++++++++++++++ |
| A | syntax/dotexp.pl | | | 156 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | syntax/dotexp.plt | | | 252 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | syntax/r6rs.pl | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | syntax/r6rs.plt | | | 129 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | syntax/sexp.pl | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
| A | syntax/sexp.plt | | | 62 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | test_helpers.pl | | | 2 | ++ |
| A | test_runner.pl | | | 20 | ++++++++++++++++++++ |
| D | tests.pl | | | 164 | ------------------------------------------------------------------------------- |
17 files changed, 936 insertions(+), 598 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,2 +1,2 @@
tests:
- swipl -g run_tests -t halt *tests.pl
+ swipl test_runner.pl
diff --git a/dotexp.pl b/dotexp.pl
@@ -1,144 +0,0 @@
-%% #!/usr/bin/env swipl
-:- module(dotexp,
- [ dotexp//1
- , dotexp/4
- , dotexp_lists/2
- , ast_dotexp/2
- ]).
-:- use_module(library(apply), [maplist/3]).
-:- use_module(library(dcg/basics), [blanks//0, number//1]).
-:- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]).
-
-%%% Transformer from IR to equivalent S-Expressions
-dotexp_lists(Atomic, Atomic) :- atomic(Atomic), !.
-dotexp_lists(multi([A_DE, B_DE | Rest_DE]), [A_L, B_L | Rest_L]) :- % at least 2 elements
- % maplist(dotexp_lists_nomulti, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]).
- maplist(dotexp_lists, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]).
-
-dotexp_lists(dot(Infix_DE, LHS_DE, RHS_DE), [Infix_L, LHS_L | RHS_L]) :-
- dotexp_lists(Infix_DE, Infix_L),
- dotexp_lists_dot_lhs(LHS_DE, LHS_L),
- dotexp_lists(RHS_DE, RHS_L).
-dotexp_lists(list(DE), L) :-
- maplist(dotexp_lists, DE, L).
-
-dotexp_lists_dot_lhs(list([Value]), LHS_L) :-
- dotexp_lists(Value, LHS_L).
-
-dotexp_lists_dot_lhs(LHS_DE, LHS_L) :-
- LHS_DE \= list([_]),
- dotexp_lists(LHS_DE, LHS_L).
-
-%%% Transformer from AST to IR
-%%% IR is:
-%%% multi(List) - parenthesised semicolon-separated sub-lists
-%%% list(List) - parenthesised list
-%%% dot(Infix, LHS, RHS) - dotted infix operator
-ast_dotexp(Atomic, Atomic) :- atomic(Atomic), !.
-ast_dotexp(s(Nodes), MultiOrSingle) :-
- ast_dotexp_s(Nodes, DE),
- multi_or_single(DE, MultiOrSingle).
-
-is_list_or_dot(list(L)) :- is_list(L).
-is_list_or_dot(dot(_I, L, R)) :-
- is_list_or_dot(L),
- is_list_or_dot(R).
-
-assertion_list_or_dot(X) :- assertion(is_list_or_dot(X)).
-
-multi_or_single([A], A) :-
- assertion_list_or_dot(A).
-multi_or_single([A,B|Tail], multi([A,B|Tail])) :-
- maplist(assertion_list_or_dot, [A,B|Tail]).
-
-ast_dotexp_s([], []).
-ast_dotexp_s([d(Nodes)|AST_Tail], [DE|DE_Tail]) :-
- ast_dotexp_d(Nodes, DE),
- ast_dotexp_s(AST_Tail, DE_Tail).
-
-ast_dotexp_d([ws(Node)], DE) :-
- ast_dotexp_ws(Node, DE).
-ast_dotexp_d(AST, dot(Infix_DE, LHS_DE, RHS_DE)) :-
- append(LHS_AST, [ws([Infix_AST|RHS_AST])], AST),
- ast_dotexp_d(LHS_AST, LHS_DE),
- ast_dotexp(Infix_AST, Infix_DE),
- ast_dotexp_ws(RHS_AST, RHS_DE).
-
-ast_dotexp_ws(Nodes, list(DE)) :-
- maplist(ast_dotexp, Nodes, DE).
-
-%%% Parser from text to AST
-%%% AST is:
-%%% s(List) - semicolon separated nodes
-%%% d(List) - dot-separated nodes
-%%% ws(List) - whitespace-separated nodes
-dotexp(Value) --> blanks, value(Value).
-
-value(List) --> `(`, blanks, parenthesized(List), !.
-value(Number) --> number(Number).
-value(Atom) -->
- { ground(Atom) }, !,
- { atom_codes(Atom, Codes) },
- Codes.
-value(Atom) -->
- { var(Atom) }, !,
- [Initial],
- { atom_start_code(Initial) },
- atom_tail(Codes),
- { atom_codes(Atom, [Initial|Codes]) }.
-
-atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail).
-atom_tail([]) --> { true }.
-
-atom_code(Code) :-
- code_type(Code, graph), % non-whitespace / non-control
- Code \= 40, % open paren
- Code \= 41, % close paren
- Code \= 46, % dot
- Code \= 59. % semicolon
-
-atom_start_code(Code) :-
- atom_code(Code), \+ code_type(Code, digit).
-
-values([Value|Rest]) -->
- value(Value),
- !,
- blanks,
- values(Rest).
-values([]) --> [].
-
-parenthesized(s(SemicolonSeparated)) --> semicolon_separated(SemicolonSeparated), `)`.
-
-semicolon_separated([d(Value)|Rest]) -->
- dot_separated(Value),
- semicolon_separated_next(Rest).
-semicolon_separated_next(Rest) -->
- `;`,
- !,
- blanks,
- semicolon_separated(Rest).
-semicolon_separated_next([]) --> [].
-
-dot_separated([ws(Value)|Rest]) -->
- ws_separated(Value),
- dot_separated_next(Rest).
-dot_separated_next(Rest) -->
- `.`,
- !,
- blanks,
- dot_separated(Rest).
-dot_separated_next([]) --> [].
-
-ws_separated([Value|Rest]) -->
- value(Value),
- !,
- blanks,
- ws_separated(Rest).
-ws_separated([]) --> [].
-
-% {|dotexp||foo|} syntax definition
-dotexp(Content, _, _, Result) :-
- phrase_from_quasi_quotation(dotexp(AST), Content),
- ast_dotexp(AST, IR),
- dotexp_lists(IR, Result).
-:- quasi_quotation_syntax(dotexp).
diff --git a/dotexp_tests.pl b/dotexp_tests.pl
@@ -1,226 +0,0 @@
-:- use_module(library(debug), [assertion/1]).
-
-:- use_module(dotexp).
-:- use_module(test_helpers).
-
-codes_to_ast(Codes, Result) :-
- test_one_result(E, phrase(dotexp(E), Codes), Result).
-
-ast_to_ir(AST, Result) :-
- test_one_result(IR, ast_dotexp(AST, IR), Result).
-
-codes_to_ir(Codes, IR) :-
- codes_to_ast(Codes, AST),
- ast_to_ir(AST, IR).
-
-ir_to_sexp(IR, Result) :-
- test_one_result(E, dotexp_lists(IR, E), Result).
-
-codes_to_sexp(Codes, Result) :-
- codes_to_ast(Codes, AST),
- ast_to_ir(AST, IR),
- ir_to_sexp(IR, Result).
-
-:- begin_tests(dotexp_ast).
-
-test(dotexp_atom) :-
- codes_to_ast(`a`, Expression),
- assertion(Expression =@= a).
-
-test(dotexp_atom2) :-
- codes_to_ast(`hello-world!`, Expression),
- assertion(Expression =@= 'hello-world!').
-
-test(dotexp_number) :-
- codes_to_ast(`0`, Expression),
- assertion(Expression =@= 0).
-
-test(dotexp_number2) :-
- codes_to_ast(`31337`, Expression),
- assertion(Expression =@= 31337).
-
-test(dotexp_list_empty) :-
- codes_to_ast(`()`, Expression),
- assertion(Expression =@= s([d([ws([])])])).
-
-test(dotexp_list_one_atom) :-
- codes_to_ast(`(a)`, Expression),
- assertion(Expression =@= s([d([ws([a])])])).
-
-test(dotexp_list_two_atoms) :-
- codes_to_ast(`(a b)`, Expression),
- assertion(Expression =@= s([d([ws([a, b])])])).
-
-test(dotexp_multi_list_two_and_one_atom) :-
- codes_to_ast(`(a b; c)`, Expression),
- assertion(Expression =@= s([d([ws([a, b])]), d([ws([c])])])).
-
-test(dotexp_atom_dot_atom_atom) :-
- codes_to_ast(`(a .b c)`, Expression),
- assertion(Expression =@= s([d([ws([a]), ws([b, c])])])).
-
-test(dotexp_def) :-
- codes_to_ast(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
- assertion(Expression =@=
- s([d([ws([define, proc, s([d([ws([lambda, s([d([ws([e, f, g])])]), s([d([ws([lambda, s([d([ws([])])]), e])])])])])])])])])
- ).
-
-test(numbers_choose_as_a) :-
- codes_to_ast(`(numbers .choose .as a)`, Expression),
- assertion(Expression =@= s([d([ws([numbers]), ws([choose]), ws([as, a])])])).
-
-:- end_tests(dotexp_ast).
-
-
-:- begin_tests(dotexp_ir).
-
-test(dotexp_atom) :-
- ast_to_ir(a, Expression),
- assertion(Expression =@= a).
-
-test(dotexp_atom2) :-
- ast_to_ir('hello-world!', Expression),
- assertion(Expression =@= 'hello-world!').
-
-test(dotexp_number) :-
- ast_to_ir(0, Expression),
- assertion(Expression =@= 0).
-
-test(dotexp_number2) :-
- ast_to_ir(31337, Expression),
- assertion(Expression =@= 31337).
-
-test(dotexp_list_empty) :-
- ast_to_ir(s([d([ws([])])]), Expression),
- assertion(Expression =@= list([])).
-
-test(dotexp_list_one_atom) :-
- ast_to_ir(s([d([ws([a])])]), Expression),
- assertion(Expression =@= list([a])).
-
-test(dotexp_list_two_atoms) :-
- ast_to_ir(s([d([ws([a, b])])]), Expression),
- assertion(Expression =@= list([a, b])).
-
-test(dotexp_multi_list_two_and_one_atom) :-
- ast_to_ir(s([d([ws([a, b])]), d([ws([c])])]), Expression),
- assertion(Expression =@= multi([list([a, b]), list([c])])).
-
-test(dotexp_atom_dot_atom_atom) :-
- ast_to_ir(s([d([ws([a]), ws([b, c])])]), Expression),
- assertion(Expression =@= dot(b, list([a]), list([c]))).
-
-test(numbers_choose_as_a) :-
- ast_to_ir(s([d([ws([numbers]), ws([choose]), ws([as, a])])]), Expression),
- assertion(Expression =@= dot(as, dot(choose, list([numbers]), list([])), list([a]))).
-
-:- end_tests(dotexp_ir).
-
-
-:- begin_tests(dotexp_lists).
-test(de_l_atom) :-
- ir_to_sexp(a, Expression),
- assertion(Expression =@= a).
-
-test(de_l_number) :-
- ir_to_sexp(0, Expression),
- assertion(Expression =@= 0).
-
-test(de_l_number2) :-
- ir_to_sexp(31337, Expression),
- assertion(Expression =@= 31337).
-
-test(de_l_list_empty) :-
- ir_to_sexp(list([]), Expression),
- assertion(Expression =@= []).
-
-test(de_l_list_one_atom) :-
- ir_to_sexp(list([a]), Expression),
- assertion(Expression =@= [a]).
-
-test(de_l_list_two_atoms) :-
- ir_to_sexp(list([a, b]), Expression),
- assertion(Expression =@= [a, b]).
-
-test(de_l_multi_list_two_and_one_atom) :-
- ir_to_sexp(multi([list([a, b]), list([c])]), Expression),
- assertion(Expression =@= [[a, b], [c]]).
-
-test(de_l_atom_dot_atom_atom) :-
- ir_to_sexp(dot(b, list([a]), list([c])), Expression),
- assertion(Expression =@= [b, a, c]).
-
-test(de_l_atom_atom_dot_atom_atom) :-
- ir_to_sexp(dot(c, list([a, b]), list([d])), Expression),
- assertion(Expression =@= [c, [a, b], d]).
-
-test(numbers_choose_as_a) :-
- ir_to_sexp(dot(as, dot(choose, list([numbers]), list([])), list([a])), Expression),
- assertion(Expression =@= [as, [choose, numbers], a]).
-
-:- end_tests(dotexp_lists).
-
-:- begin_tests(dotexp).
-
-test(dotexp_atom) :-
- codes_to_ir(`a`, Expression),
- assertion(Expression =@= a).
-
-test(dotexp_atom2) :-
- codes_to_ir(`hello-world!`, Expression),
- assertion(Expression =@= 'hello-world!').
-
-test(dotexp_number) :-
- codes_to_ir(`0`, Expression),
- assertion(Expression =@= 0).
-
-test(dotexp_number2) :-
- codes_to_ir(`31337`, Expression),
- assertion(Expression =@= 31337).
-
-test(dotexp_list_empty) :-
- codes_to_ir(`()`, Expression),
- assertion(Expression =@= list([])).
-
-test(dotexp_list_one_atom) :-
- codes_to_ir(`(a)`, Expression),
- assertion(Expression =@= list([a])).
-
-test(dotexp_list_two_atoms) :-
- codes_to_ir(`(a b)`, Expression),
- assertion(Expression =@= list([a, b])).
-
-test(dotexp_multi_list_two_and_one_atom) :-
- codes_to_ir(`(a b; c)`, Expression),
- assertion(Expression =@= multi([list([a, b]), list([c])])).
-
-test(dotexp_atom_dot_atom_atom) :-
- codes_to_ir(`(a .b c)`, Expression),
- assertion(Expression =@= dot(b, list([a]), list([c]))).
-
-test(dotexp_atom_atom_dot_atom_atom) :-
- codes_to_ir(`(a b .c d)`, Expression),
- assertion(Expression =@= dot(c, list([a, b]), list([d]))).
-
-test(dotexp_def) :-
- codes_to_ir(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
- assertion(Expression =@= list([define, proc, list([lambda, list([e, f, g]), list([lambda, list([]), e])])])).
-
-test(dotexp_qq) :-
- Expression = {|dotexp||(define proc (lambda (e f g) (lambda () e)))|},
- assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]).
-
-test(numbers_choose_as_a) :-
- codes_to_sexp(`(numbers .choose .as a)`, Expression),
- assertion(Expression =@= [as, [choose, numbers], a]).
-
-test(par_numbers_choose_as_a) :-
- codes_to_sexp(`((numbers) .choose .as a)`, Expression),
- assertion(Expression =@= [as, [choose, [numbers]], a]).
-
-test(dotexp_qq2) :-
- Expression = {|dotexp||(numbers .choose .as a)|},
- assertion(Expression =@= [as, [choose, numbers], a]).
-
-:- end_tests(dotexp).
-
diff --git a/interp_errors.plt b/interp_errors.plt
@@ -0,0 +1,9 @@
+:- module('interp_errors+tests', []).
+:- begin_tests(interp_errors).
+
+test(read_error) :-
+ message_to_string(error(read_error, _), Message),
+ assertion(Message =@= "Failed to read line.").
+
+:- end_tests(interp_errors).
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/interpreter.pl b/interpreter.pl
@@ -1,8 +1,6 @@
%% #!/usr/bin/env swipl
:- module(interpreter,
[ parse_input_expression/2
- , sexp//1
- , sexp/4
, interp_apply/5
, interp_eval/4
, lookup_variable_value/4
@@ -11,12 +9,12 @@
]).
:- use_module(library(debug), [assertion/1]).
:- use_module(library(dcg/basics), [blanks//0, number//1]).
-:- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]).
:- use_module(library(assoc), [get_assoc/3, put_assoc/4]).
:- use_module(library(pairs), [pairs_keys_values/3]).
:- use_module(interp_errors).
:- use_module(interp_state).
+:- use_module(syntax/sexp).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% conditionally throw errors
@@ -273,69 +271,9 @@ interp_loop(State0, InputStream) :-
% grammar {{{1
-blank_code(0x20). % SPACE
-blank_code(0x9). % ASCII HT '\t' (horizontal tab)
-blank_code(0xa). % ASCII LF '\n' (new line)
-blank_code(0xb). % ASCII VT '\v' (vertical tab)
-blank_code(0xc). % ASCII FF '\f' (form feed)
-blank_code(0xd). % ASCII CR '\r' (carriage ret)
-blank_code(0x85). % NEL (next line)
-blank_code(0x2000). % EN QUAD
-blank_code(0x2001). % EM QUAD
-blank_code(0x2002). % EN SPACE
-blank_code(0x2003). % EM SPACE
-blank_code(0x2004). % THREE-PER-EM SPACE
-blank_code(0x2005). % FOUR-PER-EM SPACE
-blank_code(0x2006). % SIX-PER-EM SPACE
-blank_code(0x2008). % PUNCTUATION SPACE
-blank_code(0x2009). % THIN SPACE
-blank_code(0x200a). % HAIR SPACE
-blank_code(0x2028). % LINE SEPARATOR
-blank_code(0x2029). % PARAGRAPH SEPARATOR
-blank_code(0x205f). % MEDIUM MATHEMATICAL SPACE
-blank_code(0x3000). % IDEOGRAPHIC SPACE
-
-blank --> [C], { blank_code(C) }.
-
parse_input_expression(Codes, Expression) :-
phrase(sexp(Expression), Codes).
-sexp(Value) --> blanks, sexp_value(Value).
-
-sexp_value(List) --> `(`, blanks, sexp_list(List).
-sexp_value(Number) --> {when(nonvar(Number), number(Number))}, number(Number).
-sexp_value(Atom) -->
- { nonvar(Atom), atomic(Atom) }, !,
- { atom_codes(Atom, Codes) },
- Codes.
-sexp_value(Atom) -->
- { var(Atom) }, !,
- [Initial],
- { atom_start_code(Initial) },
- atom_tail(Codes),
- { atom_codes(Atom, [Initial|Codes]) }.
-
-atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail).
-atom_tail([]) --> { true }.
-
-atom_code(Code) :-
- code_type(Code, graph), % non-whitespace / non-control
- Code \= 40, % open paren
- Code \= 41. % close paren
-
-atom_start_code(Code) :-
- atom_code(Code), \+ code_type(Code, digit).
-
-sexp_list([]) --> `)`.
-sexp_list([Value|Rest]) --> sexp_value(Value), sexp_list_more(Rest).
-
-sexp_list_more([]) --> `)`.
-sexp_list_more([Value|Rest]) --> blank, blanks, sexp_value(Value), sexp_list_more(Rest).
-
-sexp(Content, _, _, Result) :-
- phrase_from_quasi_quotation(sexp(Result), Content).
-:- quasi_quotation_syntax(sexp).
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Interpreter wrapper
diff --git a/interpreter.plt b/interpreter.plt
@@ -0,0 +1,108 @@
+:- module('interpreter+tests', []).
+:- use_module(library(debug), [assertion/1]).
+
+:- use_module(interpreter).
+:- use_module(test_helpers).
+:- use_module(syntax/sexp).
+
+decode(Codes, Expression) :-
+ test_one_result(E, parse_input_expression(Codes, E), Expression).
+
+apply_example(car, [{|sexp||(a b c)|}], {|sexp||a|}).
+apply_example(car, [{|sexp||((a b c) x y z)|}], {|sexp||(a b c)|}).
+apply_example(car, [{|sexp||(((hotdogs)) (and) (pickle) relish)|}], {|sexp||((hotdogs))|}).
+apply_example(car, [{|sexp||((hotdogs))|}], {|sexp||(hotdogs)|}).
+
+apply_example(cdr, [{|sexp||(a b c)|}], {|sexp||(b c)|}).
+apply_example(cdr, [{|sexp||((a b c) x y z)|}], {|sexp||(x y z)|}).
+apply_example(cdr, [{|sexp||(hamburger)|}], {|sexp||()|}).
+apply_example(cdr, [{|sexp||((x) t r)|}], {|sexp||(t r)|}).
+
+apply_example(Func, Args, ExpectedResult) :-
+ apply_example(CallCs, ResultCs),
+ decode(CallCs, [Func|Args]),
+ decode(ResultCs, ExpectedResult).
+
+apply_example(`(cons peanut (butter and jelly))`,
+ `(peanut butter and jelly)`).
+
+apply_example(`(cons ((help) this) (is very ((hard) to learn)))`,
+ `(((help) this) is very ((hard) to learn))`).
+
+apply_example(`(cons (a b (c)) ())`, `((a b (c)))`).
+
+apply_example(`(+ 0 0)`, `0`).
+apply_example(`(+ 3 2)`, `5`).
+
+:- begin_tests('interpreter+test_apply').
+test(apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :-
+ initial_state(State0),
+ primitive_arity(Func, Arity),
+ interp_apply(primitive(Func, Arity), Args, Result, State0, _State1),
+ assertion(Result =@= ExpectedResult).
+
+test(apply_arity_error, [error(interp_error(arity, car))]) :-
+ Func=car,
+ initial_state(State0),
+ primitive_arity(Func, Arity),
+ interp_apply(primitive(Func, Arity), [[a], [b]], _Result, State0, _State1).
+
+:- end_tests('interpreter+test_apply').
+:- begin_tests('interpreter+test_eval').
+
+quoted_args([], []).
+quoted_args([H|T], [[quote, H]|QT]) :- quoted_args(T, QT).
+
+test_eval(Call, ExpectedResult) :-
+ initial_state(State0),
+ test_eval(Call, ExpectedResult, State0, _State1).
+test_eval(Call, ExpectedResult, State0, State1) :-
+ format('testing call: ~w → expecting ~w | ~w~n', [Call, ExpectedResult, State0]),
+ interp_eval(Call, Result, State0, State1),
+ assertion(Result =@= ExpectedResult).
+
+eval_example(`(car (cdr (quote ((b) (x y) ((c))))))`, `(x y)`).
+eval_example(`(cdr (cdr (quote ((b) (x y) ((c))))))`, `(((c)))`).
+
+eval_example_decoded(Call, ExpectedResult) :-
+ eval_example(CallCodes, ResultCodes),
+ decode(CallCodes, Call),
+ decode(ResultCodes, ExpectedResult).
+
+test(eval_quote) :-
+ test_eval({|sexp||(quote a)|}, a).
+
+test(eval_quote2) :-
+ test_eval({|sexp||(quote ())|}, []).
+
+test(eval_apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :-
+ quoted_args(Args, QArgs),
+ Call = [Func|QArgs],
+ test_eval(Call, ExpectedResult).
+
+test(eval_example, [forall(eval_example_decoded(Call, ExpectedResult))]) :-
+ test_eval(Call, ExpectedResult).
+
+eval_define_lat :-
+ DefLat = {|sexp||
+(define lat?
+ (lambda (l)
+ (cond
+ ((null? l) #t)
+ ((atom? (car l)) (lat? (cdr l)))
+ (else #f))))|},
+ initial_state(State0),
+ interp_eval(DefLat, _, State0, State1),
+ lookup_variable_value('lat?', Proc, State1, _),
+ assertion(Proc = proc([l], _, _)),
+ Call0 = {|sexp||(lat? (quote ()))|},
+ test_eval(Call0, '#t', State1, _),
+ Call1 = {|sexp||(lat? (quote (Jack Sprat could eat no chicken fat)))|},
+ test_eval(Call1, '#t', State1, _),
+ Call2 = {|sexp||(lat? (quote ((Jack) Sprat could eat no chicken fat)))|},
+ test_eval(Call2, '#f', State1, _).
+test(eval_define_lat) :- eval_define_lat.
+
+:- end_tests('interpreter+test_eval').
+
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/modules.plt b/modules.plt
@@ -0,0 +1,45 @@
+:- module('modules+tests', []).
+:- use_module(library(debug), [assertion/1]).
+
+user_module_path(Module, Path) :-
+ module_property(Module, class(user)),
+ module_property(Module, file(Path)).
+
+test_module_path(Module, Path) :-
+ module_property(Module, class(test)),
+ module_property(Module, file(Path)).
+
+check_user_module_name(pl, BaseName, Module) :-
+ assertion(Module =@= BaseName).
+
+check_user_module_name(plt, BaseName, Module) :-
+ atom_concat(BaseName, '+tests', ExpectedName),
+ assertion(Module =@= ExpectedName).
+
+check_test_module_name(BaseName, Module) :-
+ atomic_list_concat([BeforePlus|_], +, Module),
+ atom_concat(plunit_, BaseName, ExpectedName),
+ assertion(BeforePlus =@= ExpectedName).
+
+:- begin_tests('modules+test_names').
+
+test(user_module, [forall(user_module_path(Module, Path))]) :-
+ working_directory(CWD, CWD),
+ ( directory_file_path(CWD, Relative, Path)
+ -> file_name_extension(BaseName, Ext, Relative),
+ assertion(member(Ext, [pl, plt])),
+ check_user_module_name(Ext, BaseName, Module)
+ ; assertion(directory_file_path(CWD, _, Path))
+ ).
+
+test(test_module, [forall(test_module_path(Module, Path))]) :-
+ working_directory(CWD, CWD),
+ ( directory_file_path(CWD, Relative, Path)
+ -> file_name_extension(BaseName, Ext, Relative),
+ assertion(Ext =@= plt),
+ check_test_module_name(BaseName, Module)
+ ; assertion(directory_file_path(CWD, _, Path))
+ ).
+
+:- end_tests('modules+test_names').
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/syntax/common.pl b/syntax/common.pl
@@ -0,0 +1,32 @@
+:- module('syntax/common',
+ [ blank_code/1
+ , blank//0
+ , number//1
+ ]).
+:- reexport(library(dcg/basics), [blanks//0]).
+
+blank_code(0x20). % SPACE
+blank_code(0x9). % ASCII HT '\t' (horizontal tab)
+blank_code(0xa). % ASCII LF '\n' (new line)
+blank_code(0xb). % ASCII VT '\v' (vertical tab)
+blank_code(0xc). % ASCII FF '\f' (form feed)
+blank_code(0xd). % ASCII CR '\r' (carriage ret)
+blank_code(0x85). % NEL (next line)
+blank_code(0x2000). % EN QUAD
+blank_code(0x2001). % EM QUAD
+blank_code(0x2002). % EN SPACE
+blank_code(0x2003). % EM SPACE
+blank_code(0x2004). % THREE-PER-EM SPACE
+blank_code(0x2005). % FOUR-PER-EM SPACE
+blank_code(0x2006). % SIX-PER-EM SPACE
+blank_code(0x2008). % PUNCTUATION SPACE
+blank_code(0x2009). % THIN SPACE
+blank_code(0x200a). % HAIR SPACE
+blank_code(0x2028). % LINE SEPARATOR
+blank_code(0x2029). % PARAGRAPH SEPARATOR
+blank_code(0x205f). % MEDIUM MATHEMATICAL SPACE
+blank_code(0x3000). % IDEOGRAPHIC SPACE
+
+blank --> [C], { blank_code(C) }.
+
+number(Number) --> {when(nonvar(Number), number(Number))}, dcg_basics:number(Number).
diff --git a/syntax/dotexp.pl b/syntax/dotexp.pl
@@ -0,0 +1,156 @@
+%% #!/usr/bin/env swipl
+:- module('syntax/dotexp',
+ [ dotexp//1
+ , dotexp/4
+ , dotexp_lists/2
+ , ast_dotexp/2
+ ]).
+:- use_module(library(apply), [maplist/3]).
+:- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]).
+:- use_module(common, [blank//0, blanks//0, number//1]).
+:- use_module(r6rs, [abbrev//1, has_abbrev_prefix/1]).
+
+%%% Transformer from IR to equivalent S-Expressions
+dotexp_lists(Atomic, Atomic) :- atomic(Atomic), !.
+dotexp_lists(multi([A_DE, B_DE | Rest_DE]), [A_L, B_L | Rest_L]) :- % at least 2 elements
+ % maplist(dotexp_lists_nomulti, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]).
+ maplist(dotexp_lists, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]).
+
+dotexp_lists(dot(Infix_DE, LHS_DE, RHS_DE), [Infix_L, LHS_L | RHS_L]) :-
+ dotexp_lists(Infix_DE, Infix_L),
+ dotexp_lists_dot_lhs(LHS_DE, LHS_L),
+ dotexp_lists(RHS_DE, RHS_L).
+dotexp_lists(list(DE), L) :-
+ maplist(dotexp_lists, DE, L).
+
+dotexp_lists_dot_lhs(list([Value]), LHS_L) :-
+ dotexp_lists(Value, LHS_L).
+
+dotexp_lists_dot_lhs(LHS_DE, LHS_L) :-
+ LHS_DE \= list([_]),
+ dotexp_lists(LHS_DE, LHS_L).
+
+%%% Transformer from AST to IR
+%%% IR is:
+%%% multi(List) - parenthesised semicolon-separated sub-lists
+%%% list(List) - parenthesised list
+%%% dot(Infix, LHS, RHS) - dotted infix operator
+ast_dotexp(Atomic, Atomic) :- atomic(Atomic), !.
+ast_dotexp(s(Nodes), MultiOrSingle) :-
+ ast_dotexp_s(Nodes, DE),
+ multi_or_single(DE, MultiOrSingle).
+
+is_list_or_dot(list(L)) :- is_list(L).
+is_list_or_dot(dot(_I, L, R)) :-
+ is_list_or_dot(L),
+ is_list_or_dot(R).
+
+assertion_list_or_dot(X) :- assertion(is_list_or_dot(X)).
+
+multi_or_single([A], A) :-
+ assertion_list_or_dot(A).
+multi_or_single([A,B|Tail], multi([A,B|Tail])) :-
+ maplist(assertion_list_or_dot, [A,B|Tail]).
+
+ast_dotexp_s([], []).
+ast_dotexp_s([d(Nodes)|AST_Tail], [DE|DE_Tail]) :-
+ ast_dotexp_d(Nodes, DE),
+ ast_dotexp_s(AST_Tail, DE_Tail).
+
+ast_dotexp_d([ws(Node)], DE) :-
+ ast_dotexp_ws(Node, DE).
+ast_dotexp_d(AST, dot(Infix_DE, LHS_DE, RHS_DE)) :-
+ append(LHS_AST, [ws([Infix_AST|RHS_AST])], AST),
+ ast_dotexp_d(LHS_AST, LHS_DE),
+ ast_dotexp(Infix_AST, Infix_DE),
+ ast_dotexp_ws(RHS_AST, RHS_DE).
+
+ast_dotexp_ws(Nodes, list(DE)) :-
+ maplist(ast_dotexp, Nodes, DE).
+
+%%% Parser from text to AST
+%%% AST is:
+%%% s(List) - semicolon separated nodes
+%%% d(List) - dot-separated nodes
+%%% ws(List) - whitespace-separated nodes
+dotexp(Value) --> blanks, value(Value).
+
+value(List) --> `(`, blanks, parenthesized(List), !.
+value(s([d([ws([AbbrevName, Datum])])])) -->
+ abbrev(AbbrevName),
+ value(Datum).
+value(Number) --> number(Number).
+value(Atom) -->
+ { ground(Atom) }, !,
+ { atom_codes(Atom, Codes) },
+ Codes.
+value(Atom) -->
+ { var(Atom) }, !,
+ [Initial],
+ { atom_start_code(Initial) },
+ atom_tail(Codes),
+ { \+ has_abbrev_prefix([Initial|Codes]) },
+ { atom_codes(Atom, [Initial|Codes]) }.
+
+atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail).
+atom_tail([]) --> { true }.
+
+atom_code(Code) :-
+ code_type(Code, graph), % non-whitespace / non-control
+ Code \= 40, % open paren
+ Code \= 41, % close paren
+ Code \= 46, % dot
+ Code \= 59. % semicolon
+
+atom_start_code(Code) :-
+ atom_code(Code), \+ code_type(Code, digit).
+
+values([Value|Rest]) -->
+ value(Value),
+ !,
+ blanks,
+ values(Rest).
+values([]) --> [].
+
+parenthesized(s(SemicolonSeparated)) --> semicolon_separated(SemicolonSeparated), `)`.
+
+semicolon_separated([d(Value)|Rest]) -->
+ dot_separated(Value),
+ semicolon_separated_next(Rest).
+semicolon_separated_next(Rest) -->
+ `;`,
+ !,
+ blanks,
+ semicolon_separated(Rest).
+semicolon_separated_next([]) --> [].
+
+dot_separated([ws(Value)|Rest]) -->
+ ws_separated(Value),
+ dot_separated_next(Rest).
+dot_separated_next(Rest) -->
+ `.`,
+ !,
+ blanks,
+ dot_separated(Rest).
+dot_separated_next([]) --> [].
+
+ws_separated([Value|Rest]) -->
+ value(Value),
+ !,
+ ws_separated_next(Rest).
+ws_separated([]) --> blanks.
+
+ws_separated_next([Value|Rest]) -->
+ blank,
+ blanks,
+ value(Value),
+ !,
+ ws_separated_next(Rest).
+ws_separated_next([]) --> blanks.
+
+% {|dotexp||foo|} syntax definition
+dotexp(Content, _, _, Result) :-
+ phrase_from_quasi_quotation(dotexp(AST), Content),
+ ast_dotexp(AST, IR),
+ dotexp_lists(IR, Result).
+:- quasi_quotation_syntax(dotexp).
diff --git a/syntax/dotexp.plt b/syntax/dotexp.plt
@@ -0,0 +1,252 @@
+:- module('syntax/dotexp+tests', []).
+:- use_module(library(debug), [assertion/1]).
+
+:- use_module(dotexp).
+:- use_module('../test_helpers').
+
+codes_to_ast(Codes, Result) :-
+ test_one_result(E, phrase(dotexp(E), Codes), Result).
+
+ast_to_ir(AST, Result) :-
+ test_one_result(IR, ast_dotexp(AST, IR), Result).
+
+codes_to_ir(Codes, IR) :-
+ codes_to_ast(Codes, AST),
+ ast_to_ir(AST, IR).
+
+ir_to_sexp(IR, Result) :-
+ test_one_result(E, dotexp_lists(IR, E), Result).
+
+codes_to_sexp(Codes, Result) :-
+ codes_to_ast(Codes, AST),
+ ast_to_ir(AST, IR),
+ ir_to_sexp(IR, Result).
+
+:- begin_tests('syntax/dotexp+test_ast').
+
+test(dotexp_atom) :-
+ codes_to_ast(`a`, Expression),
+ assertion(Expression =@= a).
+
+test(dotexp_atom2) :-
+ codes_to_ast(`hello-world!`, Expression),
+ assertion(Expression =@= 'hello-world!').
+
+test(dotexp_number) :-
+ codes_to_ast(`0`, Expression),
+ assertion(Expression =@= 0).
+
+test(dotexp_number2) :-
+ codes_to_ast(`31337`, Expression),
+ assertion(Expression =@= 31337).
+
+test(dotexp_list_empty) :-
+ codes_to_ast(`()`, Expression),
+ assertion(Expression =@= s([d([ws([])])])).
+
+test(dotexp_list_one_atom) :-
+ codes_to_ast(`(a)`, Expression),
+ assertion(Expression =@= s([d([ws([a])])])).
+
+test(dotexp_list_two_atoms) :-
+ codes_to_ast(`(a b)`, Expression),
+ assertion(Expression =@= s([d([ws([a, b])])])).
+
+test(dotexp_multi_list_two_and_one_atom) :-
+ codes_to_ast(`(a b; c)`, Expression),
+ assertion(Expression =@= s([d([ws([a, b])]), d([ws([c])])])).
+
+test(dotexp_atom_dot_atom_atom) :-
+ codes_to_ast(`(a .b c)`, Expression),
+ assertion(Expression =@= s([d([ws([a]), ws([b, c])])])).
+
+test(dotexp_def) :-
+ codes_to_ast(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
+ assertion(Expression =@=
+ s([d([ws([define, proc, s([d([ws([lambda, s([d([ws([e, f, g])])]), s([d([ws([lambda, s([d([ws([])])]), e])])])])])])])])])
+ ).
+
+test(numbers_choose_as_a) :-
+ codes_to_ast(`(numbers .choose .as a)`, Expression),
+ assertion(Expression =@= s([d([ws([numbers]), ws([choose]), ws([as, a])])])).
+
+test(dotexp_quote) :-
+ codes_to_ast(`'()`, Expression),
+ assertion(Expression =@= s([d([ws([quote, s([d([ws([])])])])])])).
+
+test(dotexp_quote2) :-
+ codes_to_ast(`'a`, Expression),
+ assertion(Expression =@= s([d([ws([quote, a])])])).
+
+test(dotexp_unquote) :-
+ codes_to_ast(`\`(a ,b)`, Expression),
+ assertion(Expression =@= s([d([ws([quasiquote, s([d([ws([a, s([d([ws([unquote, b])])])])])])])])])).
+
+:- end_tests('syntax/dotexp+test_ast').
+
+
+:- begin_tests('syntax/dotexp+test_ir').
+
+test(dotexp_atom) :-
+ ast_to_ir(a, Expression),
+ assertion(Expression =@= a).
+
+test(dotexp_atom2) :-
+ ast_to_ir('hello-world!', Expression),
+ assertion(Expression =@= 'hello-world!').
+
+test(dotexp_number) :-
+ ast_to_ir(0, Expression),
+ assertion(Expression =@= 0).
+
+test(dotexp_number2) :-
+ ast_to_ir(31337, Expression),
+ assertion(Expression =@= 31337).
+
+test(dotexp_list_empty) :-
+ ast_to_ir(s([d([ws([])])]), Expression),
+ assertion(Expression =@= list([])).
+
+test(dotexp_list_one_atom) :-
+ ast_to_ir(s([d([ws([a])])]), Expression),
+ assertion(Expression =@= list([a])).
+
+test(dotexp_list_two_atoms) :-
+ ast_to_ir(s([d([ws([a, b])])]), Expression),
+ assertion(Expression =@= list([a, b])).
+
+test(dotexp_multi_list_two_and_one_atom) :-
+ ast_to_ir(s([d([ws([a, b])]), d([ws([c])])]), Expression),
+ assertion(Expression =@= multi([list([a, b]), list([c])])).
+
+test(dotexp_atom_dot_atom_atom) :-
+ ast_to_ir(s([d([ws([a]), ws([b, c])])]), Expression),
+ assertion(Expression =@= dot(b, list([a]), list([c]))).
+
+test(numbers_choose_as_a) :-
+ ast_to_ir(s([d([ws([numbers]), ws([choose]), ws([as, a])])]), Expression),
+ assertion(Expression =@= dot(as, dot(choose, list([numbers]), list([])), list([a]))).
+
+:- end_tests('syntax/dotexp+test_ir').
+
+
+:- begin_tests('syntax/dotexp+test_ir_to_sexp').
+test(de_l_atom) :-
+ ir_to_sexp(a, Expression),
+ assertion(Expression =@= a).
+
+test(de_l_number) :-
+ ir_to_sexp(0, Expression),
+ assertion(Expression =@= 0).
+
+test(de_l_number2) :-
+ ir_to_sexp(31337, Expression),
+ assertion(Expression =@= 31337).
+
+test(de_l_list_empty) :-
+ ir_to_sexp(list([]), Expression),
+ assertion(Expression =@= []).
+
+test(de_l_list_one_atom) :-
+ ir_to_sexp(list([a]), Expression),
+ assertion(Expression =@= [a]).
+
+test(de_l_list_two_atoms) :-
+ ir_to_sexp(list([a, b]), Expression),
+ assertion(Expression =@= [a, b]).
+
+test(de_l_multi_list_two_and_one_atom) :-
+ ir_to_sexp(multi([list([a, b]), list([c])]), Expression),
+ assertion(Expression =@= [[a, b], [c]]).
+
+test(de_l_atom_dot_atom_atom) :-
+ ir_to_sexp(dot(b, list([a]), list([c])), Expression),
+ assertion(Expression =@= [b, a, c]).
+
+test(de_l_atom_atom_dot_atom_atom) :-
+ ir_to_sexp(dot(c, list([a, b]), list([d])), Expression),
+ assertion(Expression =@= [c, [a, b], d]).
+
+test(numbers_choose_as_a) :-
+ ir_to_sexp(dot(as, dot(choose, list([numbers]), list([])), list([a])), Expression),
+ assertion(Expression =@= [as, [choose, numbers], a]).
+
+:- end_tests('syntax/dotexp+test_ir_to_sexp').
+
+:- begin_tests('syntax/dotexp+test_other').
+
+test(dotexp_atom) :-
+ codes_to_ir(`a`, Expression),
+ assertion(Expression =@= a).
+
+test(dotexp_atom2) :-
+ codes_to_ir(`hello-world!`, Expression),
+ assertion(Expression =@= 'hello-world!').
+
+test(dotexp_number) :-
+ codes_to_ir(`0`, Expression),
+ assertion(Expression =@= 0).
+
+test(dotexp_number2) :-
+ codes_to_ir(`31337`, Expression),
+ assertion(Expression =@= 31337).
+
+test(dotexp_list_empty) :-
+ codes_to_ir(`()`, Expression),
+ assertion(Expression =@= list([])).
+
+test(dotexp_list_one_atom) :-
+ codes_to_ir(`(a)`, Expression),
+ assertion(Expression =@= list([a])).
+
+test(dotexp_list_two_atoms) :-
+ codes_to_ir(`(a b)`, Expression),
+ assertion(Expression =@= list([a, b])).
+
+test(dotexp_multi_list_two_and_one_atom) :-
+ codes_to_ir(`(a b; c)`, Expression),
+ assertion(Expression =@= multi([list([a, b]), list([c])])).
+
+test(dotexp_atom_dot_atom_atom) :-
+ codes_to_ir(`(a .b c)`, Expression),
+ assertion(Expression =@= dot(b, list([a]), list([c]))).
+
+test(dotexp_atom_atom_dot_atom_atom) :-
+ codes_to_ir(`(a b .c d)`, Expression),
+ assertion(Expression =@= dot(c, list([a, b]), list([d]))).
+
+test(dotexp_def) :-
+ codes_to_ir(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
+ assertion(Expression =@= list([define, proc, list([lambda, list([e, f, g]), list([lambda, list([]), e])])])).
+
+test(dotexp_qq) :-
+ Expression = {|dotexp||(define proc (lambda (e f g) (lambda () e)))|},
+ assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]).
+
+test(numbers_choose_as_a) :-
+ codes_to_sexp(`(numbers .choose .as a)`, Expression),
+ assertion(Expression =@= [as, [choose, numbers], a]).
+
+test(par_numbers_choose_as_a) :-
+ codes_to_sexp(`((numbers) .choose .as a)`, Expression),
+ assertion(Expression =@= [as, [choose, [numbers]], a]).
+
+test(dotexp_qq2) :-
+ Expression = {|dotexp||(numbers .choose .as a)|},
+ assertion(Expression =@= [as, [choose, numbers], a]).
+
+test(dotexp_quote) :-
+ codes_to_sexp(`'()`, Expression),
+ assertion(Expression =@= [quote, []]).
+
+test(dotexp_quote2) :-
+ codes_to_sexp(`'a`, Expression),
+ assertion(Expression =@= [quote, a]).
+
+test(dotexp_unquote) :-
+ codes_to_sexp(`\`(a ,b)`, Expression),
+ assertion(Expression =@= [quasiquote, [a, [unquote, b]]]).
+
+:- end_tests('syntax/dotexp+test_other').
+
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/syntax/r6rs.pl b/syntax/r6rs.pl
@@ -0,0 +1,77 @@
+:- module('syntax/r6rs',
+ [ r6rs//1
+ , r6rs/4
+ , abbrev/2
+ , abbrev//1
+ , has_abbrev_prefix/1
+ ]).
+:- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]).
+:- use_module(common, [blank//0, blanks//0, number//1]).
+
+abbrev(quote, `'`).
+abbrev(quasiquote, `\``).
+abbrev(unquote, `,`).
+abbrev('unquote-splicing', `,@`).
+abbrev(syntax, `#'`).
+abbrev(quasisyntax, `#\``).
+abbrev(unsyntax, `#,`).
+abbrev('unsyntax-splicing', `#,@`).
+
+has_abbrev_prefix(Codes) :-
+ abbrev(_, ACodes),
+ append(ACodes, _, Codes).
+
+abbrev('unsyntax-splicing') --> `#,@`, !.
+abbrev('unquote-splicing') --> `,@`, !.
+abbrev(quote) --> `'`.
+abbrev(quasiquote) --> `\``.
+abbrev(unquote) --> `,`.
+abbrev(syntax) --> `#'`.
+abbrev(quasisyntax) --> `#\``.
+abbrev(unsyntax) --> `#,`.
+
+r6rs(Value) --> blanks, value(Value).
+
+value([AbbrevName, Datum]) -->
+ abbrev(AbbrevName),
+ value(Datum).
+
+value(List) --> list(List).
+value(Number) --> number(Number).
+value(Atom) -->
+ { nonvar(Atom), atomic(Atom) }, !,
+ { atom_codes(Atom, Codes) },
+ Codes.
+value(Atom) -->
+ { var(Atom) }, !,
+ [Initial],
+ { atom_start_code(Initial) },
+ atom_tail(Codes),
+ { \+ has_abbrev_prefix([Initial|Codes]) },
+ { atom_codes(Atom, [Initial|Codes]) }.
+
+atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail).
+atom_tail([]) --> { true }.
+
+atom_code(Code) :-
+ code_type(Code, graph), % non-whitespace / non-control
+ Code \= 40, % open paren
+ Code \= 41. % close paren
+
+atom_start_code(Code) :-
+ atom_code(Code), \+ code_type(Code, digit).
+
+list(List) --> `(`, blanks, list_first(List, `)`).
+list(List) --> `[`, blanks, list_first(List, `]`).
+list(List) --> `{`, blanks, list_first(List, `}`).
+
+list_first([], End) --> End.
+list_first([Value|Rest], End) --> value(Value), list_more(Rest, End).
+
+list_more([], End) --> End.
+list_more([Value|Rest], End) --> blank, blanks, value(Value), list_more(Rest, End).
+
+r6rs(Content, _, _, Result) :-
+ phrase_from_quasi_quotation(r6rs(Result), Content).
+:- quasi_quotation_syntax(r6rs).
+
diff --git a/syntax/r6rs.plt b/syntax/r6rs.plt
@@ -0,0 +1,129 @@
+:- module('syntax/r6rs+tests', []).
+:- use_module(library(debug), [assertion/1]).
+
+:- use_module('../test_helpers').
+:- use_module(r6rs).
+
+decode(Codes, Expression) :-
+ test_one_result(E, phrase(r6rs(E), Codes), Expression).
+
+encode_then_decode(Expression) :-
+ once(phrase(r6rs(Expression), Codes)),
+ decode(Codes, Decoded),
+ assertion(Expression =@= Decoded).
+
+% r6rs_reference_reader(`-123`, -123).
+% r6rs_reference_reader(`+123`, 123).
+r6rs_reference_reader(`...`, '...').
+r6rs_reference_reader(`(...)`, ['...']).
+r6rs_reference_reader(`(... foo ... baz)`, ['...', foo, '...', baz]).
+% r6rs_reference_reader(`.5`, 0.5).
+r6rs_reference_reader(`(1 2 3)`, [1, 2, 3]).
+r6rs_reference_reader(`foo`, 'foo').
+r6rs_reference_reader(`fOo`, 'fOo').
+r6rs_reference_reader(`[1 2 3]`, [1, 2, 3]).
+% r6rs_reference_reader(`h\\x65;llo`, 'hello').
+r6rs_reference_reader(`'foo`, [quote, foo]).
+r6rs_reference_reader(`\`foo`, [quasiquote, foo]).
+r6rs_reference_reader(`,foo`, [unquote, foo]).
+r6rs_reference_reader(`,@foo`, ['unquote-splicing', foo]).
+r6rs_reference_reader(`#'foo`, [syntax, foo]).
+r6rs_reference_reader(`#\`foo`, [quasisyntax, foo]).
+r6rs_reference_reader(`#,foo`, [unsyntax, foo]).
+r6rs_reference_reader(`#,@foo`, ['unsyntax-splicing', foo]).
+r6rs_reference_reader(`->foo`, '->foo').
+% r6rs_reference_reader(`(#t #f #b1001 #T #F #B1001)`, ['#t', '#f', 9, '#t', '#f', 9]).
+r6rs_reference_reader(`a@`, 'a@').
+r6rs_reference_reader(`(#\\Z #\\F)`, ['#\\Z', '#\\F']).
+r6rs_reference_reader(`(a ,b)`, [a, [unquote, b]]).
+r6rs_reference_reader(`(#\\A ,b)`, ['#\\A', [unquote, b]]).
+r6rs_reference_reader(`(#\\t ,b)`, ['#\\t', [unquote, b]]).
+r6rs_reference_reader(`(#t #f)`, ['#t', '#f']).
+
+% r6rs_reference_reader(`(1 #| foo bar |# 2 3)`, [1 2 3]).
+% r6rs_reference_reader(`(1 #| foo #| bar |# |# 2 3)`, [1 2 3]).
+% r6rs_reference_reader(`(1 #;(foo bar baz) 2 3)`, [1 2 3]).
+
+% r6rs_reference_reader(`#\\linefeed`, (integer->char 10)).
+% r6rs_reference_reader(`#\\x578`, (integer->char #x578)).
+% r6rs_reference_reader(`\"\\a\\b\\t\\n\\v\\f\\r\\\"\\\\\"`, (list->string (map integer->char '(7 8 9 #xA #xB #xC #xD #x22 #x5c)))).
+% r6rs_reference_reader(`\"\\x578;\\x123;\"`, (list->string (map integer->char '(#x578 #x123)))).
+% r6rs_reference_reader(`#vu8(1 2 3 4 5)`, (bytes=?) (u8-list->bytes '(1 2 3 4 5))).
+
+% (check
+% (get-datum (make-string-input-port "\\x2e;reader.")) => (string->symbol ".reader."))
+% (check
+% (get-datum (open-input-string (string (integer->char #xa0)))) => (eof-object))
+% (check
+% (get-datum (open-input-string (string (integer->char #xa1)))) =>
+% (string->symbol (string (integer->char #xa1))))
+
+r6rs_reference_reader_exception(`..`).
+r6rs_reference_reader_exception(`(1 2 3]`).
+r6rs_reference_reader_exception(`\"\\x578;\\x123\"`).
+r6rs_reference_reader_exception(`\"\\x578\\x123\"`).
+r6rs_reference_reader_exception(`#\\Alarm`).
+r6rs_reference_reader_exception(`h\\x65llo`).
+r6rs_reference_reader_exception(`@`).
+
+r6rs_reference_reader_exception(`(#\\Z#\\F)`).
+r6rs_reference_reader_exception(`->#`).
+r6rs_reference_reader_exception(`(a#b)`).
+r6rs_reference_reader_exception(`(a,b)`). % missing delimiter
+r6rs_reference_reader_exception(`(#\\A,b)`). % missing delimiter
+r6rs_reference_reader_exception(`(#\\t,b)`).
+r6rs_reference_reader_exception(`(#t#f)`).
+
+:- begin_tests('syntax/r6rs').
+test(atom) :-
+ decode(`a`, Expression),
+ assertion(Expression =@= a),
+ encode_then_decode(Expression).
+
+test(atom2) :-
+ decode(`hello-world!`, Expression),
+ assertion(Expression =@= 'hello-world!'),
+ encode_then_decode(Expression).
+
+test(number) :-
+ decode(`0`, Expression),
+ assertion(Expression =@= 0),
+ encode_then_decode(Expression).
+
+test(number2) :-
+ decode(`31337`, Expression),
+ assertion(Expression =@= 31337),
+ encode_then_decode(Expression).
+
+test(list_empty) :-
+ decode(`()`, Expression),
+ assertion(Expression =@= []),
+ encode_then_decode(Expression).
+
+test(list_one_atom) :-
+ decode(`(a)`, Expression),
+ assertion(Expression =@= [a]),
+ encode_then_decode(Expression).
+
+test(list_one_two_atoms) :-
+ decode(`(a b)`, Expression),
+ assertion(Expression =@= [a, b]),
+ encode_then_decode(Expression).
+
+test(def) :-
+ decode(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
+ assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
+ encode_then_decode(Expression).
+
+test(qq) :-
+ Expression = {|r6rs||(define proc (lambda (e f g) (lambda () e)))|},
+ assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
+ encode_then_decode(Expression).
+
+test(ref_reader_example, [forall(r6rs_reference_reader(Input, ExpectedOutput))]) :-
+ decode(Input, Expression),
+ assertion(Expression =@= ExpectedOutput),
+ encode_then_decode(Expression).
+
+:- end_tests('syntax/r6rs').
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/syntax/sexp.pl b/syntax/sexp.pl
@@ -0,0 +1,42 @@
+:- module('syntax/sexp',
+ [ sexp//1
+ , sexp/4
+ ]).
+:- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]).
+:- use_module(common, [blank//0, blanks//0, number//1]).
+
+sexp(Value) --> blanks, sexp_value(Value).
+
+sexp_value(List) --> `(`, blanks, sexp_list(List).
+sexp_value(Number) --> number(Number).
+sexp_value(Atom) -->
+ { nonvar(Atom), atomic(Atom) }, !,
+ { atom_codes(Atom, Codes) },
+ Codes.
+sexp_value(Atom) -->
+ { var(Atom) }, !,
+ [Initial],
+ { atom_start_code(Initial) },
+ atom_tail(Codes),
+ { atom_codes(Atom, [Initial|Codes]) }.
+
+atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail).
+atom_tail([]) --> { true }.
+
+atom_code(Code) :-
+ code_type(Code, graph), % non-whitespace / non-control
+ Code \= 40, % open paren
+ Code \= 41. % close paren
+
+atom_start_code(Code) :-
+ atom_code(Code), \+ code_type(Code, digit).
+
+sexp_list([]) --> `)`.
+sexp_list([Value|Rest]) --> sexp_value(Value), sexp_list_more(Rest).
+
+sexp_list_more([]) --> `)`.
+sexp_list_more([Value|Rest]) --> blank, blanks, sexp_value(Value), sexp_list_more(Rest).
+
+sexp(Content, _, _, Result) :-
+ phrase_from_quasi_quotation(sexp(Result), Content).
+:- quasi_quotation_syntax(sexp).
diff --git a/syntax/sexp.plt b/syntax/sexp.plt
@@ -0,0 +1,62 @@
+:- module('syntax/sexp+tests', []).
+:- use_module('..'/test_helpers).
+:- use_module(sexp).
+
+decode(Codes, Expression) :-
+ test_one_result(E, phrase(sexp(E), Codes), Expression).
+
+encode_then_decode(Expression) :-
+ once(phrase(sexp(Expression), Codes)),
+ decode(Codes, Decoded),
+ assertion(Expression =@= Decoded).
+
+
+:- begin_tests('syntax/sexp').
+
+test(sexp_atom) :-
+ decode(`a`, Expression),
+ assertion(Expression =@= a),
+ encode_then_decode(Expression).
+
+test(sexp_atom2) :-
+ decode(`hello-world!`, Expression),
+ assertion(Expression =@= 'hello-world!'),
+ encode_then_decode(Expression).
+
+test(sexp_number) :-
+ decode(`0`, Expression),
+ assertion(Expression =@= 0),
+ encode_then_decode(Expression).
+
+test(sexp_number2) :-
+ decode(`31337`, Expression),
+ assertion(Expression =@= 31337),
+ encode_then_decode(Expression).
+
+test(sexp_list_empty) :-
+ decode(`()`, Expression),
+ assertion(Expression =@= []),
+ encode_then_decode(Expression).
+
+test(sexp_list_one_atom) :-
+ decode(`(a)`, Expression),
+ assertion(Expression =@= [a]),
+ encode_then_decode(Expression).
+
+test(sexp_list_one_two_atoms) :-
+ decode(`(a b)`, Expression),
+ assertion(Expression =@= [a, b]),
+ encode_then_decode(Expression).
+
+test(sexp_def) :-
+ decode(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
+ assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
+ encode_then_decode(Expression).
+
+test(sexp_qq) :-
+ Expression = {|sexp||(define proc (lambda (e f g) (lambda () e)))|},
+ assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
+ encode_then_decode(Expression).
+
+:- end_tests('syntax/sexp').
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/test_helpers.pl b/test_helpers.pl
@@ -12,6 +12,8 @@ portray_depth(Depth) :-
set_prolog_flag(answer_write_options, [quoted(true), portrayed(true), max_depth(Depth), spacing(next_argument)]),
set_prolog_flag(debugger_write_options, [quoted(true), portrayed(true), max_depth(Depth), spacing(next_argument)]).
+
+:- meta_predicate(test_one_result(?, 0, ?)).
test_one_result(Template, Goal, Output) :-
findall(Template, Goal, Results),
% write(Results), nl,
diff --git a/test_runner.pl b/test_runner.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/env swipl
+:- module(test_runner, []).
+:- use_module(library(filesex), [directory_member/3]).
+
+find_test_files(Files) :-
+ setof(Filename, (
+ directory_member('.', Filename, [extensions([plt]), recursive(true), exclude_directory('.git')]),
+ \+ exists_directory(Filename)
+ ), Files).
+
+load_test_file(Filename) :- use_module(Filename, []).
+
+load_tests :-
+ find_test_files(Files),
+ maplist(load_test_file, Files).
+
+:- initialization(load_tests, now).
+:- initialization(run_tests, main).
+
+% vim: ft=prolog noet sts=4 ts=4 sw=4
diff --git a/tests.pl b/tests.pl
@@ -1,164 +0,0 @@
-:- use_module(library(debug), [assertion/1]).
-
-:- use_module(interpreter).
-:- use_module(test_helpers).
-
-decode(Codes, Expression) :-
- test_one_result(E, parse_input_expression(Codes, E), Expression).
-
-encode_then_decode(Expression) :-
- once(parse_input_expression(Codes, Expression)),
- decode(Codes, Decoded),
- assertion(Expression =@= Decoded).
-
-:- begin_tests(errors).
-%:- use_module('$messages', [message_to_string/2]).
-test(read_error) :-
- message_to_string(error(read_error, _), Message),
- assertion(Message =@= "Failed to read line.").
-:- end_tests(errors).
-:- begin_tests(sexp).
-
-test(sexp_atom) :-
- decode(`a`, Expression),
- assertion(Expression =@= a),
- encode_then_decode(Expression).
-
-test(sexp_atom2) :-
- decode(`hello-world!`, Expression),
- assertion(Expression =@= 'hello-world!'),
- encode_then_decode(Expression).
-
-test(sexp_number) :-
- decode(`0`, Expression),
- assertion(Expression =@= 0),
- encode_then_decode(Expression).
-
-test(sexp_number2) :-
- decode(`31337`, Expression),
- assertion(Expression =@= 31337),
- encode_then_decode(Expression).
-
-test(sexp_list_empty) :-
- decode(`()`, Expression),
- assertion(Expression =@= []),
- encode_then_decode(Expression).
-
-test(sexp_list_one_atom) :-
- decode(`(a)`, Expression),
- assertion(Expression =@= [a]),
- encode_then_decode(Expression).
-
-test(sexp_list_one_two_atoms) :-
- decode(`(a b)`, Expression),
- assertion(Expression =@= [a, b]),
- encode_then_decode(Expression).
-
-test(sexp_def) :-
- decode(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
- assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
- encode_then_decode(Expression).
-
-test(sexp_qq) :-
- Expression = {|sexp||(define proc (lambda (e f g) (lambda () e)))|},
- assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
- encode_then_decode(Expression).
-
-:- end_tests(sexp).
-
-apply_example(car, [{|sexp||(a b c)|}], {|sexp||a|}).
-apply_example(car, [{|sexp||((a b c) x y z)|}], {|sexp||(a b c)|}).
-apply_example(car, [{|sexp||(((hotdogs)) (and) (pickle) relish)|}], {|sexp||((hotdogs))|}).
-apply_example(car, [{|sexp||((hotdogs))|}], {|sexp||(hotdogs)|}).
-
-apply_example(cdr, [{|sexp||(a b c)|}], {|sexp||(b c)|}).
-apply_example(cdr, [{|sexp||((a b c) x y z)|}], {|sexp||(x y z)|}).
-apply_example(cdr, [{|sexp||(hamburger)|}], {|sexp||()|}).
-apply_example(cdr, [{|sexp||((x) t r)|}], {|sexp||(t r)|}).
-
-apply_example(Func, Args, ExpectedResult) :-
- apply_example(CallCs, ResultCs),
- decode(CallCs, [Func|Args]),
- decode(ResultCs, ExpectedResult).
-
-apply_example(`(cons peanut (butter and jelly))`,
- `(peanut butter and jelly)`).
-
-apply_example(`(cons ((help) this) (is very ((hard) to learn)))`,
- `(((help) this) is very ((hard) to learn))`).
-
-apply_example(`(cons (a b (c)) ())`, `((a b (c)))`).
-
-apply_example(`(+ 0 0)`, `0`).
-apply_example(`(+ 3 2)`, `5`).
-
-:- begin_tests(primitive_apply).
-test(apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :-
- initial_state(State0),
- primitive_arity(Func, Arity),
- interp_apply(primitive(Func, Arity), Args, Result, State0, _State1),
- assertion(Result =@= ExpectedResult).
-
-test(apply_arity_error, [error(interp_error(arity, car))]) :-
- Func=car,
- initial_state(State0),
- primitive_arity(Func, Arity),
- interp_apply(primitive(Func, Arity), [[a], [b]], _Result, State0, _State1).
-
-:- end_tests(primitive_apply).
-:- begin_tests(eval).
-
-quoted_args([], []).
-quoted_args([H|T], [[quote, H]|QT]) :- quoted_args(T, QT).
-
-test_eval(Call, ExpectedResult) :-
- initial_state(State0),
- test_eval(Call, ExpectedResult, State0, _State1).
-test_eval(Call, ExpectedResult, State0, State1) :-
- format('testing call: ~w → expecting ~w | ~w~n', [Call, ExpectedResult, State0]),
- interp_eval(Call, Result, State0, State1),
- assertion(Result =@= ExpectedResult).
-
-eval_example(`(car (cdr (quote ((b) (x y) ((c))))))`, `(x y)`).
-eval_example(`(cdr (cdr (quote ((b) (x y) ((c))))))`, `(((c)))`).
-
-eval_example_decoded(Call, ExpectedResult) :-
- eval_example(CallCodes, ResultCodes),
- decode(CallCodes, Call),
- decode(ResultCodes, ExpectedResult).
-
-test(eval_quote) :-
- test_eval({|sexp||(quote a)|}, a).
-
-test(eval_quote2) :-
- test_eval({|sexp||(quote ())|}, []).
-
-test(eval_apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :-
- quoted_args(Args, QArgs),
- Call = [Func|QArgs],
- test_eval(Call, ExpectedResult).
-
-test(eval_example, [forall(eval_example_decoded(Call, ExpectedResult))]) :-
- test_eval(Call, ExpectedResult).
-
-eval_define_lat :-
- DefLat = {|sexp||
-(define lat?
- (lambda (l)
- (cond
- ((null? l) #t)
- ((atom? (car l)) (lat? (cdr l)))
- (else #f))))|},
- initial_state(State0),
- interp_eval(DefLat, _, State0, State1),
- lookup_variable_value('lat?', Proc, State1, _),
- assertion(Proc = proc([l], _, _)),
- Call0 = {|sexp||(lat? (quote ()))|},
- test_eval(Call0, '#t', State1, _),
- Call1 = {|sexp||(lat? (quote (Jack Sprat could eat no chicken fat)))|},
- test_eval(Call1, '#t', State1, _),
- Call2 = {|sexp||(lat? (quote ((Jack) Sprat could eat no chicken fat)))|},
- test_eval(Call2, '#f', State1, _).
-test(eval_define_lat) :- eval_define_lat.
-
-:- end_tests(eval).