interpl

Experiments with intepreters in Prolog
git clone https://ccx.te2000.cz/git/interpl
Log | Files | Refs | README

commit 8ed96060ed0b3d2e96e180c3b79b64b3d7e4f514
parent c26b54eaac4023ed0a54a934738053e8bb66569b
Author: Jan Pobrislo <ccx@te2000.cz>
Date:   Wed,  2 Sep 2026 11:26:24 +0000

Dot-expression syntax definition and tests.

Diffstat:
Adotexp.pl | 131+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adotexp_tests.pl | 199+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 330 insertions(+), 0 deletions(-)

diff --git a/dotexp.pl b/dotexp.pl @@ -0,0 +1,131 @@ +%% #!/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(list(LHS_DE), LHS_L), + dotexp_lists(list(RHS_DE), RHS_L). +dotexp_lists(list(DE), L) :- + maplist(dotexp_lists, DE, 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). + +single_list(L, list(L)) :- is_list(L). +single_list(dot(I, L, R), dot(I, L, 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). + +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, 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 @@ -0,0 +1,199 @@ +:- use_module(library(debug), [assertion/1]). + +:- use_module(dotexp). + +portray_long :- portray_depth(32). +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)]). + +test_one_result(Template, Goal, Output) :- + findall(Template, Goal, Results), + % write(Results), nl, + assertion(length(Results, 1)), + Results = [Output]. + +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). + +normalize(Expr_DE, Expr_L) :- + test_one_result(E, dotexp_lists(Expr_DE, E), Expr_L). + +:- 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])])])])])])])])]) + ). + +:- 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, [a], [c])). + +:- end_tests(dotexp_ir). + + +:- begin_tests(dotexp_lists). +test(de_l_atom) :- + normalize(a, Expression), + assertion(Expression =@= a). + +test(de_l_number) :- + normalize(0, Expression), + assertion(Expression =@= 0). + +test(de_l_number2) :- + normalize(31337, Expression), + assertion(Expression =@= 31337). + +test(de_l_list_empty) :- + normalize(list([]), Expression), + assertion(Expression =@= []). + +test(de_l_list_one_atom) :- + normalize(list([a]), Expression), + assertion(Expression =@= [a]). + +test(de_l_list_two_atoms) :- + normalize(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), + assertion(Expression =@= [[a, b], [c]]). + +test(de_l_atom_dot_atom_atom) :- + normalize(dot(b, [a], [c]), Expression), + assertion(Expression =@= [b, [a], c]). + +:- 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, [a], [c])). + +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]]]). + +:- end_tests(dotexp). +