interpl

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

dotexp.pl (4947B)


      1 %% #!/usr/bin/env swipl
      2 :- module('syntax/dotexp',
      3 	[ dotexp//1
      4 	, dotexp/4
      5 	, dotexp_lists/2
      6 	, ast_dotexp/2
      7 	]).
      8 :- use_module(library(apply), [maplist/3]).
      9 :- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]).
     10 :- use_module(common, [blank//0, blanks//0, number//1]).
     11 :- use_module(r6rs, [abbrev//1, has_abbrev_prefix/1]).
     12 
     13 %%% Transformer from IR to equivalent S-Expressions
     14 dotexp_lists(Atomic, Atomic) :- atomic(Atomic), !.
     15 dotexp_lists(multi([A_DE, B_DE | Rest_DE]), [A_L, B_L | Rest_L]) :-  % at least 2 elements
     16 	% maplist(dotexp_lists_nomulti, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]).
     17 	maplist(dotexp_lists, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]).
     18 
     19 dotexp_lists(dot(Infix_DE, LHS_DE, RHS_DE), [Infix_L, LHS_L | RHS_L]) :-
     20 	dotexp_lists(Infix_DE, Infix_L),
     21 	dotexp_lists_dot_lhs(LHS_DE, LHS_L),
     22 	dotexp_lists(RHS_DE, RHS_L).
     23 dotexp_lists(list(DE), L) :-
     24 	maplist(dotexp_lists, DE, L).
     25 dotexp_lists(list([], Cons_DE), L) :-
     26 	dotexp_lists(Cons_DE, L).
     27 dotexp_lists(list([Value_DE|Rest_DE], Cons_DE), [Value_L|Rest_L]) :-
     28 	dotexp_lists(Value_DE, Value_L),
     29 	dotexp_lists(list(Rest_DE, Cons_DE), Rest_L).
     30 
     31 dotexp_lists_dot_lhs(list([Value]), LHS_L) :-
     32 	dotexp_lists(Value, LHS_L).
     33 
     34 dotexp_lists_dot_lhs(LHS_DE, LHS_L) :-
     35 	LHS_DE \= list([_]),
     36 	dotexp_lists(LHS_DE, LHS_L).
     37 
     38 %%% Transformer from AST to IR
     39 %%% IR is:
     40 %%%  multi(List) - parenthesised semicolon-separated sub-lists
     41 %%%  list(List) - parenthesised list
     42 %%%  list(List, Cons) - parenthesised list ending with explicit pair
     43 %%%  dot(Infix, LHS, RHS)  - dotted infix operator
     44 ast_dotexp(Atomic, Atomic) :- atomic(Atomic), !.
     45 ast_dotexp(s(Nodes), MultiOrSingle) :-
     46 	ast_dotexp_s(Nodes, DE),
     47 	multi_or_single(DE, MultiOrSingle).
     48 
     49 is_list_or_dot(list(L)) :- is_list(L).
     50 is_list_or_dot(list(L, _)) :- is_list(L).
     51 is_list_or_dot(dot(_I, L, R)) :-
     52 	is_list_or_dot(L),
     53 	is_list_or_dot(R).
     54 
     55 assertion_list_or_dot(X) :- assertion(is_list_or_dot(X)).
     56 
     57 multi_or_single([A], A) :-
     58 	assertion_list_or_dot(A).
     59 multi_or_single([A,B|Tail], multi([A,B|Tail])) :-
     60 	maplist(assertion_list_or_dot, [A,B|Tail]).
     61 
     62 ast_dotexp_s([], []).
     63 ast_dotexp_s([d(Nodes)|AST_Tail], [DE|DE_Tail]) :-
     64 	ast_dotexp_d(Nodes, DE),
     65 	ast_dotexp_s(AST_Tail, DE_Tail).
     66 
     67 ast_dotexp_d([ws(Node, Tail)], DE) :-
     68 	ast_dotexp_ws(Node, Tail, DE).
     69 ast_dotexp_d(AST, dot(Infix_DE, LHS_DE, RHS_DE)) :-
     70 	append(LHS_AST, [ws([Infix_AST|RHS_AST], Tail_AST)], AST),
     71 	ast_dotexp_d(LHS_AST, LHS_DE),
     72 	ast_dotexp(Infix_AST, Infix_DE),
     73 	ast_dotexp_ws(RHS_AST, Tail_AST, RHS_DE).
     74 
     75 ast_dotexp_ws(Nodes, -, list(DE)) :-
     76 	maplist(ast_dotexp, Nodes, DE).
     77 ast_dotexp_ws(Nodes, cons(Tail), list(DE_List, DE_Cons)) :-
     78 	maplist(ast_dotexp, Nodes, DE_List),
     79 	ast_dotexp(Tail, DE_Cons).
     80 
     81 %%% Parser from text to AST
     82 %%% AST is:
     83 %%%  s(List) - semicolon separated nodes
     84 %%%  d(List) - dot-separated nodes
     85 %%% ws(List, -) - whitespace-separated nodes
     86 %%% ws(List, cons(Node)) - whitespace-separated nodes ending with explicit pair
     87 dotexp(Value) --> blanks, value(Value).
     88 
     89 value(List) --> `(`, blanks, parenthesized(List), !.
     90 value(s([d([ws([AbbrevName, Datum], -)])])) -->
     91 	abbrev(AbbrevName),
     92 	value(Datum).
     93 value(Number) --> number(Number).
     94 value(Atom) -->
     95 	{ ground(Atom) }, !,
     96 	{ atom_codes(Atom, Codes) },
     97 	Codes.
     98 value(Atom) -->
     99 	{ var(Atom) }, !,
    100 	[Initial],
    101 	{ atom_start_code(Initial) },
    102 	atom_tail(Codes),
    103 	{ \+ has_abbrev_prefix([Initial|Codes]) },
    104 	{ atom_codes(Atom, [Initial|Codes]) }.
    105 
    106 atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail).
    107 atom_tail([]) --> { true }.
    108 
    109 punctuation(Code) :- member(Code, `().;[]{}`).
    110 atom_code(Code) :-
    111 	code_type(Code, graph), % non-whitespace / non-control
    112 	\+ punctuation(Code).
    113 
    114 atom_start_code(Code) :-
    115 	atom_code(Code), \+ code_type(Code, digit).
    116 
    117 values([Value|Rest]) -->
    118 	value(Value),
    119 	!,
    120 	blanks,
    121 	values(Rest).
    122 values([]) --> [].
    123 
    124 parenthesized(s(SemicolonSeparated)) --> semicolon_separated(SemicolonSeparated), `)`.
    125 
    126 semicolon_separated([d(Value)|Rest]) -->
    127 	dot_separated(Value),
    128 	semicolon_separated_next(Rest).
    129 semicolon_separated_next(Rest) -->
    130 	`;`,
    131 	!,
    132 	blanks,
    133 	semicolon_separated(Rest).
    134 semicolon_separated_next([]) --> [].
    135 
    136 dot_separated([ws(WSList, WSTail)|Rest]) -->
    137 	ws_separated(WSList, WSTail),
    138 	dot_separated_next(Rest).
    139 dot_separated_next(Rest) -->
    140 	`.`,
    141 	!,
    142 	blanks,
    143 	dot_separated(Rest).
    144 dot_separated_next([]) --> [].
    145 
    146 ws_separated([Value|Rest], Tail) -->
    147 	value(Value),
    148 	!,
    149 	ws_separated_next(Rest, Tail).
    150 ws_separated([], -) --> blanks.
    151 
    152 ws_separated_next([], cons(Tail)) -->
    153 	blank,
    154 	blanks,
    155 	`|`,
    156 	blank,
    157 	!,
    158 	blanks,
    159 	value(Tail),
    160 	blanks.
    161 ws_separated_next([Value|Rest], Tail) -->
    162 	blank,
    163 	blanks,
    164 	value(Value),
    165 	!,
    166 	ws_separated_next(Rest, Tail).
    167 ws_separated_next([],  -) --> blanks.
    168 
    169 % {|dotexp||foo|} syntax definition
    170 dotexp(Content, _, _, Result) :-
    171 	phrase_from_quasi_quotation(dotexp(AST), Content),
    172 	ast_dotexp(AST, IR),
    173 	dotexp_lists(IR, Result).
    174 :- quasi_quotation_syntax(dotexp).