interpl

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

r6rs.plt (5140B)


      1 :- module('syntax/r6rs+tests', []).
      2 :- use_module(library(debug), [assertion/1]).
      3 
      4 :- use_module('../test_helpers').
      5 :- use_module(r6rs).
      6 
      7 decode(Codes, Expression) :-
      8 	test_one_result(E, phrase(r6rs(E), Codes), Expression).
      9 
     10 encode_then_decode(Expression) :-
     11 	once(phrase(r6rs(Expression), Codes)),
     12 	decode(Codes, Decoded),
     13 	assertion(Expression =@= Decoded).
     14 
     15 % r6rs_reference_reader(`-123`, -123).
     16 % r6rs_reference_reader(`+123`, 123).
     17 r6rs_reference_reader(`...`, '...').
     18 r6rs_reference_reader(`(...)`, ['...']).
     19 r6rs_reference_reader(`(... foo ... baz)`, ['...', foo, '...', baz]).
     20 % r6rs_reference_reader(`.5`, 0.5).
     21 r6rs_reference_reader(`(1 2 3)`, [1, 2, 3]).
     22 r6rs_reference_reader(`foo`, 'foo').
     23 r6rs_reference_reader(`fOo`, 'fOo').
     24 r6rs_reference_reader(`[1 2 3]`, [1, 2, 3]).
     25 % r6rs_reference_reader(`h\\x65;llo`, 'hello').
     26 r6rs_reference_reader(`'foo`, [quote, foo]).
     27 r6rs_reference_reader(`\`foo`, [quasiquote, foo]).
     28 r6rs_reference_reader(`,foo`, [unquote, foo]).
     29 r6rs_reference_reader(`,@foo`, ['unquote-splicing', foo]).
     30 r6rs_reference_reader(`#'foo`, [syntax, foo]).
     31 r6rs_reference_reader(`#\`foo`, [quasisyntax, foo]).
     32 r6rs_reference_reader(`#,foo`, [unsyntax, foo]).
     33 r6rs_reference_reader(`#,@foo`, ['unsyntax-splicing', foo]).
     34 r6rs_reference_reader(`->foo`, '->foo').
     35 % r6rs_reference_reader(`(#t #f #b1001 #T #F #B1001)`, ['#t', '#f', 9, '#t', '#f', 9]).
     36 r6rs_reference_reader(`a@`, 'a@').
     37 r6rs_reference_reader(`(#\\Z #\\F)`, ['#\\Z', '#\\F']).
     38 r6rs_reference_reader(`(a ,b)`, [a, [unquote, b]]).
     39 r6rs_reference_reader(`(#\\A ,b)`, ['#\\A', [unquote, b]]).
     40 r6rs_reference_reader(`(#\\t ,b)`, ['#\\t', [unquote, b]]).
     41 r6rs_reference_reader(`(#t #f)`, ['#t', '#f']).
     42 
     43 % r6rs_reference_reader(`(1 #| foo bar |# 2 3)`, [1 2 3]).
     44 % r6rs_reference_reader(`(1 #| foo #| bar |# |# 2 3)`, [1 2 3]).
     45 % r6rs_reference_reader(`(1 #;(foo bar baz) 2 3)`, [1 2 3]).
     46 
     47 % r6rs_reference_reader(`#\\linefeed`, (integer->char 10)).
     48 % r6rs_reference_reader(`#\\x578`, (integer->char #x578)).
     49 % r6rs_reference_reader(`\"\\a\\b\\t\\n\\v\\f\\r\\\"\\\\\"`, (list->string (map integer->char '(7 8 9 #xA #xB #xC #xD #x22 #x5c)))).
     50 % r6rs_reference_reader(`\"\\x578;\\x123;\"`, (list->string (map integer->char '(#x578 #x123)))).
     51 % r6rs_reference_reader(`#vu8(1 2 3 4 5)`, (bytes=?) (u8-list->bytes '(1 2 3 4 5))).
     52 
     53 % (check
     54 %  (get-datum (make-string-input-port "\\x2e;reader.")) => (string->symbol ".reader."))
     55 % (check
     56 %  (get-datum (open-input-string (string (integer->char #xa0)))) => (eof-object))
     57 % (check
     58 %  (get-datum (open-input-string (string (integer->char #xa1)))) =>
     59 %  (string->symbol (string (integer->char #xa1))))
     60 
     61 r6rs_reference_reader_exception(`..`).
     62 r6rs_reference_reader_exception(`(1 2 3]`).
     63 r6rs_reference_reader_exception(`\"\\x578;\\x123\"`).
     64 r6rs_reference_reader_exception(`\"\\x578\\x123\"`).
     65 r6rs_reference_reader_exception(`#\\Alarm`).
     66 r6rs_reference_reader_exception(`h\\x65llo`).
     67 r6rs_reference_reader_exception(`@`).
     68 
     69 r6rs_reference_reader_exception(`(#\\Z#\\F)`).
     70 r6rs_reference_reader_exception(`->#`).
     71 r6rs_reference_reader_exception(`(a#b)`).
     72 r6rs_reference_reader_exception(`(a,b)`). % missing delimiter
     73 r6rs_reference_reader_exception(`(#\\A,b)`). % missing delimiter
     74 r6rs_reference_reader_exception(`(#\\t,b)`).
     75 r6rs_reference_reader_exception(`(#t#f)`).
     76 
     77 :- begin_tests('syntax/r6rs').
     78 test(atom) :-
     79 	decode(`a`, Expression),
     80 	assertion(Expression =@= a),
     81 	encode_then_decode(Expression).
     82 
     83 test(atom2) :-
     84 	decode(`hello-world!`, Expression),
     85 	assertion(Expression =@= 'hello-world!'),
     86 	encode_then_decode(Expression).
     87 
     88 test(number) :-
     89 	decode(`0`, Expression),
     90 	assertion(Expression =@= 0),
     91 	encode_then_decode(Expression).
     92 
     93 test(number2) :-
     94 	decode(`31337`, Expression),
     95 	assertion(Expression =@= 31337),
     96 	encode_then_decode(Expression).
     97 
     98 test(list_empty) :-
     99 	decode(`()`, Expression),
    100 	assertion(Expression =@= []),
    101 	encode_then_decode(Expression).
    102 
    103 test(list_one_atom) :-
    104 	decode(`(a)`, Expression),
    105 	assertion(Expression =@= [a]),
    106 	encode_then_decode(Expression).
    107 
    108 test(list_one_two_atoms) :-
    109 	decode(`(a b)`, Expression),
    110 	assertion(Expression =@= [a, b]),
    111 	encode_then_decode(Expression).
    112 
    113 test(def) :-
    114 	decode(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
    115 	assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
    116 	encode_then_decode(Expression).
    117 
    118 test(qq) :-
    119 	Expression = {|r6rs||(define proc (lambda (e f g) (lambda () e)))|},
    120 	assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]),
    121 	encode_then_decode(Expression).
    122 
    123 test(ref_reader_example, [forall(r6rs_reference_reader(Input, ExpectedOutput))]) :-
    124 	decode(Input, Expression),
    125 	assertion(Expression =@= ExpectedOutput),
    126 	encode_then_decode(Expression).
    127 
    128 test(cons) :-
    129 	decode(`(a . b)`, Expression),
    130 	assertion(Expression =@= [a|b]),
    131 	encode_then_decode(Expression).
    132 
    133 test(cons2) :-
    134 	decode(`[a b . c]`, Expression),
    135 	assertion(Expression =@= [a, b|c]),
    136 	encode_then_decode(Expression).
    137 
    138 test(cons3) :-
    139 	decode(`(a b . (c))`, Expression),
    140 	assertion(Expression =@= [a, b, c]),
    141 	encode_then_decode(Expression).
    142 
    143 :- end_tests('syntax/r6rs').
    144 % vim: ft=prolog noet sts=4 ts=4 sw=4