dotexp.pl (3932B)
1 %% #!/usr/bin/env swipl 2 :- module(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(dcg/basics), [blanks//0, number//1]). 10 :- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]). 11 12 %%% Transformer from IR to equivalent S-Expressions 13 dotexp_lists(Atomic, Atomic) :- atomic(Atomic), !. 14 dotexp_lists(multi([A_DE, B_DE | Rest_DE]), [A_L, B_L | Rest_L]) :- % at least 2 elements 15 % maplist(dotexp_lists_nomulti, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]). 16 maplist(dotexp_lists, [A_DE, B_DE | Rest_DE], [A_L, B_L | Rest_L]). 17 18 dotexp_lists(dot(Infix_DE, LHS_DE, RHS_DE), [Infix_L, LHS_L | RHS_L]) :- 19 dotexp_lists(Infix_DE, Infix_L), 20 dotexp_lists_dot_lhs(LHS_DE, LHS_L), 21 dotexp_lists(RHS_DE, RHS_L). 22 dotexp_lists(list(DE), L) :- 23 maplist(dotexp_lists, DE, L). 24 25 dotexp_lists_dot_lhs(list([Value]), LHS_L) :- 26 dotexp_lists(Value, LHS_L). 27 28 dotexp_lists_dot_lhs(LHS_DE, LHS_L) :- 29 LHS_DE \= list([_]), 30 dotexp_lists(LHS_DE, LHS_L). 31 32 %%% Transformer from AST to IR 33 %%% IR is: 34 %%% multi(List) - parenthesised semicolon-separated sub-lists 35 %%% list(List) - parenthesised list 36 %%% dot(Infix, LHS, RHS) - dotted infix operator 37 ast_dotexp(Atomic, Atomic) :- atomic(Atomic), !. 38 ast_dotexp(s(Nodes), MultiOrSingle) :- 39 ast_dotexp_s(Nodes, DE), 40 multi_or_single(DE, MultiOrSingle). 41 42 is_list_or_dot(list(L)) :- is_list(L). 43 is_list_or_dot(dot(_I, L, R)) :- 44 is_list_or_dot(L), 45 is_list_or_dot(R). 46 47 assertion_list_or_dot(X) :- assertion(is_list_or_dot(X)). 48 49 multi_or_single([A], A) :- 50 assertion_list_or_dot(A). 51 multi_or_single([A,B|Tail], multi([A,B|Tail])) :- 52 maplist(assertion_list_or_dot, [A,B|Tail]). 53 54 ast_dotexp_s([], []). 55 ast_dotexp_s([d(Nodes)|AST_Tail], [DE|DE_Tail]) :- 56 ast_dotexp_d(Nodes, DE), 57 ast_dotexp_s(AST_Tail, DE_Tail). 58 59 ast_dotexp_d([ws(Node)], DE) :- 60 ast_dotexp_ws(Node, DE). 61 ast_dotexp_d(AST, dot(Infix_DE, LHS_DE, RHS_DE)) :- 62 append(LHS_AST, [ws([Infix_AST|RHS_AST])], AST), 63 ast_dotexp_d(LHS_AST, LHS_DE), 64 ast_dotexp(Infix_AST, Infix_DE), 65 ast_dotexp_ws(RHS_AST, RHS_DE). 66 67 ast_dotexp_ws(Nodes, list(DE)) :- 68 maplist(ast_dotexp, Nodes, DE). 69 70 %%% Parser from text to AST 71 %%% AST is: 72 %%% s(List) - semicolon separated nodes 73 %%% d(List) - dot-separated nodes 74 %%% ws(List) - whitespace-separated nodes 75 dotexp(Value) --> blanks, value(Value). 76 77 value(List) --> `(`, blanks, parenthesized(List), !. 78 value(Number) --> number(Number). 79 value(Atom) --> 80 { ground(Atom) }, !, 81 { atom_codes(Atom, Codes) }, 82 Codes. 83 value(Atom) --> 84 { var(Atom) }, !, 85 [Initial], 86 { atom_start_code(Initial) }, 87 atom_tail(Codes), 88 { atom_codes(Atom, [Initial|Codes]) }. 89 90 atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail). 91 atom_tail([]) --> { true }. 92 93 atom_code(Code) :- 94 code_type(Code, graph), % non-whitespace / non-control 95 Code \= 40, % open paren 96 Code \= 41, % close paren 97 Code \= 46, % dot 98 Code \= 59. % semicolon 99 100 atom_start_code(Code) :- 101 atom_code(Code), \+ code_type(Code, digit). 102 103 values([Value|Rest]) --> 104 value(Value), 105 !, 106 blanks, 107 values(Rest). 108 values([]) --> []. 109 110 parenthesized(s(SemicolonSeparated)) --> semicolon_separated(SemicolonSeparated), `)`. 111 112 semicolon_separated([d(Value)|Rest]) --> 113 dot_separated(Value), 114 semicolon_separated_next(Rest). 115 semicolon_separated_next(Rest) --> 116 `;`, 117 !, 118 blanks, 119 semicolon_separated(Rest). 120 semicolon_separated_next([]) --> []. 121 122 dot_separated([ws(Value)|Rest]) --> 123 ws_separated(Value), 124 dot_separated_next(Rest). 125 dot_separated_next(Rest) --> 126 `.`, 127 !, 128 blanks, 129 dot_separated(Rest). 130 dot_separated_next([]) --> []. 131 132 ws_separated([Value|Rest]) --> 133 value(Value), 134 !, 135 blanks, 136 ws_separated(Rest). 137 ws_separated([]) --> []. 138 139 % {|dotexp||foo|} syntax definition 140 dotexp(Content, _, _, Result) :- 141 phrase_from_quasi_quotation(dotexp(AST), Content), 142 ast_dotexp(AST, IR), 143 dotexp_lists(IR, Result). 144 :- quasi_quotation_syntax(dotexp).