r6rs.pl (2150B)
1 :- module('syntax/r6rs', 2 [ r6rs//1 3 , r6rs/4 4 , abbrev/2 5 , abbrev//1 6 , has_abbrev_prefix/1 7 ]). 8 :- use_module(library(quasi_quotations), [phrase_from_quasi_quotation/2, quasi_quotation_syntax/1]). 9 :- use_module(common, [blank//0, blanks//0, number//1]). 10 11 abbrev(quote, `'`). 12 abbrev(quasiquote, `\``). 13 abbrev(unquote, `,`). 14 abbrev('unquote-splicing', `,@`). 15 abbrev(syntax, `#'`). 16 abbrev(quasisyntax, `#\``). 17 abbrev(unsyntax, `#,`). 18 abbrev('unsyntax-splicing', `#,@`). 19 20 has_abbrev_prefix(Codes) :- 21 abbrev(_, ACodes), 22 append(ACodes, _, Codes). 23 24 abbrev('unsyntax-splicing') --> `#,@`, !. 25 abbrev('unquote-splicing') --> `,@`, !. 26 abbrev(quote) --> `'`. 27 abbrev(quasiquote) --> `\``. 28 abbrev(unquote) --> `,`. 29 abbrev(syntax) --> `#'`. 30 abbrev(quasisyntax) --> `#\``. 31 abbrev(unsyntax) --> `#,`. 32 33 r6rs(Value) --> blanks, value(Value). 34 35 value([AbbrevName, Datum]) --> 36 abbrev(AbbrevName), 37 value(Datum). 38 39 value(List) --> list(List). 40 value(Number) --> number(Number). 41 value(Atom) --> 42 { nonvar(Atom), atomic(Atom) }, !, 43 { atom_codes(Atom, Codes) }, 44 Codes. 45 value(Atom) --> 46 { var(Atom) }, !, 47 [Initial], 48 { atom_start_code(Initial) }, 49 atom_tail(Codes), 50 { \+ has_abbrev_prefix([Initial|Codes]) }, 51 { atom_codes(Atom, [Initial|Codes]) }. 52 53 atom_tail([Code|Tail]) --> [Code], { atom_code(Code) }, !, atom_tail(Tail). 54 atom_tail([]) --> { true }. 55 56 punctuation(Code) :- member(Code, `()[]{}`). 57 atom_code(Code) :- 58 code_type(Code, graph), % non-whitespace / non-control 59 \+ punctuation(Code). 60 61 atom_start_code(Code) :- 62 atom_code(Code), \+ code_type(Code, digit). 63 64 list(List) --> `(`, blanks, list_first(List, `)`). 65 list(List) --> `[`, blanks, list_first(List, `]`). 66 % list(List) --> `{`, blanks, list_first(List, `}`). 67 68 list_first([], End) --> End. 69 list_first([Value|Rest], End) --> value(Value), list_more(Rest, End). 70 71 list_more([], End) --> blanks, End. 72 list_more(Value, End) --> 73 blank, 74 blanks, 75 `.`, 76 blank, 77 !, 78 blanks, 79 value(Value), 80 blanks, 81 End. 82 list_more([Value|Rest], End) --> 83 blank, 84 blanks, 85 value(Value), 86 list_more(Rest, End). 87 88 r6rs(Content, _, _, Result) :- 89 phrase_from_quasi_quotation(r6rs(Result), Content). 90 :- quasi_quotation_syntax(r6rs). 91