]>
Commit | Line | Data |
---|---|---|
8ff4c670 MG |
1 | #!/usr/bin/perl |
2 | use strict; | |
3 | use warnings; | |
4 | ||
0fea28cb | 5 | use Data::Dump::Sexp; |
04be023f | 6 | use Test::More tests => 32; |
0fea28cb MG |
7 | |
8 | BEGIN { use_ok('App::Scheme79asm::Compiler') }; | |
8ff4c670 MG |
9 | |
10 | sub is_sexp { | |
11 | my ($expr, $expected, $name) = @_; | |
0fea28cb | 12 | is dump_sexp($expr), $expected, $name; |
8ff4c670 MG |
13 | } |
14 | ||
15 | sub to_sexp { | |
16 | my ($string) = @_; | |
17 | scalar Data::SExpression->new({fold_lists => 0, use_symbol_class => 1})->read($string) | |
18 | } | |
19 | ||
20 | sub new { | |
21 | App::Scheme79asm::Compiler->new; | |
22 | } | |
23 | ||
24 | sub is_toplevel { | |
25 | my ($string, $expected) = @_; | |
26 | is_sexp new->process_toplevel(to_sexp $string), $expected, "process_toplevel $string"; | |
5b0ffaa8 | 27 | is_sexp new->compile_string($string), $expected, "compile_string $string"; |
8ff4c670 MG |
28 | } |
29 | ||
ab8f838f | 30 | is_sexp new->process_quoted(to_sexp '5'), '(SYMBOL 3)', 'process_quoted 5'; |
04be023f | 31 | is_sexp new->process_quoted(to_sexp 'NIL'), '(LIST 0)', 'process_quoted NIL'; |
ab8f838f MG |
32 | is_sexp new->process_quoted(to_sexp '()'), '(LIST 0)', 'process_quoted ()'; |
33 | is_sexp new->process_quoted(to_sexp '(5 foo)'), '(LIST (LIST (LIST 0) (SYMBOL 3)) (SYMBOL 4))', 'process_quoted (5 foo)'; | |
34 | is_sexp new->process_quoted(to_sexp '(((5)))'), '(LIST (LIST 0) (LIST (LIST 0) (LIST (LIST 0) (SYMBOL 3))))', 'process_quoted (((5)))'; | |
8ff4c670 | 35 | |
5b0ffaa8 MG |
36 | is_toplevel '()', '(LIST 0)'; |
37 | is_toplevel 'NIL', '(LIST 0)'; | |
38 | is_toplevel 'T', '(SYMBOL 2)'; | |
ab8f838f | 39 | is_toplevel '(quote 5)', '(SYMBOL 3)'; |
5b0ffaa8 | 40 | is_toplevel '(reverse-list \'a \'a \'b)', '(CALL (MORE (MORE (REVERSE-LIST 0) (SYMBOL 4)) (SYMBOL 3)) (SYMBOL 3))'; |
ab8f838f MG |
41 | is_toplevel '(if t \'(2 3) \'x)', '(IF (LIST (SYMBOL 5) (LIST (LIST (LIST 0) (SYMBOL 3)) (SYMBOL 4))) (SYMBOL 2))'; |
42 | is_toplevel '(car \'(1 2))', '(CALL (CAR 0) (LIST (LIST (LIST 0) (SYMBOL 3)) (SYMBOL 4)))'; | |
43 | is_toplevel '(lambda id (x) x)', '(PROC (VAR -2))'; | |
44 | is_toplevel '((lambda id (x) x) 5)', '(CALL (MORE (FUNCALL 0) (PROC (VAR -2))) (SYMBOL 3))'; | |
45 | is_toplevel '(lambda append (x y) (if (atom x) y (cons (car x) (append (cdr x) y))))', '(PROC (IF (LIST (CALL (MORE (CONS 0) (CALL (MORE (MORE (FUNCALL 0) (VAR -1)) (VAR -2)) (CALL (CDR 0) (VAR -3)))) (CALL (CAR 0) (VAR -3))) (VAR -2)) (CALL (ATOM 0) (VAR -3))))'; | |
5b0ffaa8 MG |
46 | |
47 | sub pp_roundtrip { | |
48 | my ($string) = @_; | |
0fea28cb MG |
49 | my $pp = uc dump_sexp(to_sexp $string); |
50 | is $pp, uc($string), "dump_sexp roundtrip $string"; | |
5b0ffaa8 MG |
51 | } |
52 | ||
53 | pp_roundtrip '()'; | |
54 | pp_roundtrip 't'; | |
55 | pp_roundtrip '(lambda append (x y) (if (atom x) y (cons (car x) (append (cdr x) y))))'; | |
56 | ||
57 | sub expect_error_like (&$) { | |
58 | my ($block, $error_re) = @_; | |
59 | my $name = "test error like /$error_re/"; | |
60 | my $result = eval { $block->(); 1 }; | |
61 | if ($result) { | |
62 | note 'Block did not throw an exception, failing test'; | |
63 | fail $name; | |
64 | } else { | |
65 | like $@, qr/$error_re/, $name; | |
66 | } | |
67 | } | |
68 | ||
69 | expect_error_like { new->process_quoted([]) } 'argument to process_quoted is not a scalar, cons, or nil'; | |
70 | expect_error_like { is_toplevel 'x' } 'Variable x not in environment'; | |
71 | expect_error_like { is_toplevel '(car)' } 'Cannot call primitive car with no arguments'; |