commit aff74889067b3d7a78d156141bc3714e590c36f9
parent 8ed96060ed0b3d2e96e180c3b79b64b3d7e4f514
Author: Jan Pobrislo <ccx@te2000.cz>
Date: Wed, 2 Sep 2026 12:03:36 +0000
Make nested dot/3 render correctly.
Diffstat:
2 files changed, 50 insertions(+), 19 deletions(-)
diff --git a/dotexp.pl b/dotexp.pl
@@ -17,8 +17,8 @@ dotexp_lists(multi([A_DE, B_DE | Rest_DE]), [A_L, B_L | Rest_L]) :- % at least
dotexp_lists(dot(Infix_DE, LHS_DE, RHS_DE), [Infix_L, LHS_L | RHS_L]) :-
dotexp_lists(Infix_DE, Infix_L),
- dotexp_lists(list(LHS_DE), LHS_L),
- dotexp_lists(list(RHS_DE), RHS_L).
+ dotexp_lists(LHS_DE, LHS_L),
+ dotexp_lists(RHS_DE, RHS_L).
dotexp_lists(list(DE), L) :-
maplist(dotexp_lists, DE, L).
@@ -32,11 +32,17 @@ ast_dotexp(s(Nodes), MultiOrSingle) :-
ast_dotexp_s(Nodes, DE),
multi_or_single(DE, MultiOrSingle).
-single_list(L, list(L)) :- is_list(L).
-single_list(dot(I, L, R), dot(I, L, R)).
+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).
-multi_or_single([A], DE) :- single_list(A, DE).
-multi_or_single([A,B|Tail], multi(DE)) :- maplist(single_list, [A,B|Tail], DE).
+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]) :-
@@ -51,7 +57,7 @@ ast_dotexp_d(AST, dot(Infix_DE, LHS_DE, RHS_DE)) :-
ast_dotexp(Infix_AST, Infix_DE),
ast_dotexp_ws(RHS_AST, RHS_DE).
-ast_dotexp_ws(Nodes, DE) :-
+ast_dotexp_ws(Nodes, list(DE)) :-
maplist(ast_dotexp, Nodes, DE).
%%% Parser from text to AST
diff --git a/dotexp_tests.pl b/dotexp_tests.pl
@@ -23,8 +23,13 @@ codes_to_ir(Codes, IR) :-
codes_to_ast(Codes, AST),
ast_to_ir(AST, IR).
-normalize(Expr_DE, Expr_L) :-
- test_one_result(E, dotexp_lists(Expr_DE, E), Expr_L).
+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).
@@ -70,6 +75,10 @@ test(dotexp_def) :-
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).
@@ -109,44 +118,52 @@ test(dotexp_multi_list_two_and_one_atom) :-
test(dotexp_atom_dot_atom_atom) :-
ast_to_ir(s([d([ws([a]), ws([b, c])])]), Expression),
- assertion(Expression =@= dot(b, [a], [c])).
+ 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) :-
- normalize(a, Expression),
+ ir_to_sexp(a, Expression),
assertion(Expression =@= a).
test(de_l_number) :-
- normalize(0, Expression),
+ ir_to_sexp(0, Expression),
assertion(Expression =@= 0).
test(de_l_number2) :-
- normalize(31337, Expression),
+ ir_to_sexp(31337, Expression),
assertion(Expression =@= 31337).
test(de_l_list_empty) :-
- normalize(list([]), Expression),
+ ir_to_sexp(list([]), Expression),
assertion(Expression =@= []).
test(de_l_list_one_atom) :-
- normalize(list([a]), Expression),
+ ir_to_sexp(list([a]), Expression),
assertion(Expression =@= [a]).
test(de_l_list_two_atoms) :-
- normalize(list([a, b]), Expression),
+ ir_to_sexp(list([a, b]), Expression),
assertion(Expression =@= [a, b]).
test(de_l_multi_list_two_and_one_atom) :-
- normalize(multi([list([a, b]), list([c])]), Expression),
+ ir_to_sexp(multi([list([a, b]), list([c])]), Expression),
assertion(Expression =@= [[a, b], [c]]).
test(de_l_atom_dot_atom_atom) :-
- normalize(dot(b, [a], [c]), Expression),
+ ir_to_sexp(dot(b, list([a]), list([c])), Expression),
assertion(Expression =@= [b, [a], c]).
+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).
@@ -185,7 +202,7 @@ test(dotexp_multi_list_two_and_one_atom) :-
test(dotexp_atom_dot_atom_atom) :-
codes_to_ir(`(a .b c)`, Expression),
- assertion(Expression =@= dot(b, [a], [c])).
+ assertion(Expression =@= dot(b, list([a]), list([c]))).
test(dotexp_def) :-
codes_to_ir(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
@@ -195,5 +212,13 @@ 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(dotexp_qq2) :-
+ Expression = {|dotexp||(numbers .choose .as a)|},
+ assertion(Expression =@= [as, [choose, [numbers]], a]).
+
:- end_tests(dotexp).