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