|
| 1 | +#lang racket |
| 2 | +(require "../compile.rkt" "../asm/interp.rkt" rackunit) |
| 3 | + |
| 4 | +(define (run e) |
| 5 | + (asm-interp (compile e))) |
| 6 | + |
| 7 | +(check-equal? (run 7) 7) |
| 8 | +(check-equal? (run -8) -8) |
| 9 | +(check-equal? (run '(add1 (add1 7))) 9) |
| 10 | +(check-equal? (run '(add1 (sub1 7))) 7) |
| 11 | + |
| 12 | +;; Con examples |
| 13 | +(check-equal? (run '(if (zero? 0) 1 2)) 1) |
| 14 | +(check-equal? (run '(if (zero? 1) 1 2)) 2) |
| 15 | +(check-equal? (run '(if (zero? -7) 1 2)) 2) |
| 16 | +(check-equal? (run '(if (zero? 0) |
| 17 | + (if (zero? 1) 1 2) |
| 18 | + 7)) |
| 19 | + 2) |
| 20 | +(check-equal? (run '(if (zero? (if (zero? 0) 1 0)) |
| 21 | + (if (zero? 1) 1 2) |
| 22 | + 7)) |
| 23 | + 7) |
| 24 | + |
| 25 | +;; Fraud examples |
| 26 | +(check-equal? (run '(let ((x 7)) x)) 7) |
| 27 | +(check-equal? (run '(let ((x 7)) 2)) 2) |
| 28 | +(check-equal? (run '(let ((x 7)) (add1 x))) 8) |
| 29 | +(check-equal? (run '(let ((x (add1 7))) x)) 8) |
| 30 | +(check-equal? (run '(let ((x 7)) (let ((y 2)) x))) 7) |
| 31 | +(check-equal? (run '(let ((x 7)) (let ((x 2)) x))) 2) |
| 32 | +(check-equal? (run '(let ((x 7)) (let ((x (add1 x))) x))) 8) |
| 33 | + |
| 34 | +(check-equal? (run '(let ((x 0)) |
| 35 | + (if (zero? x) 7 8))) |
| 36 | + 7) |
| 37 | +(check-equal? (run '(let ((x 1)) |
| 38 | + (add1 (if (zero? x) 7 8)))) |
| 39 | + 9) |
| 40 | + |
| 41 | +;; Hustle example |
| 42 | +(check-equal? (run '(box 8)) (box 8)) |
| 43 | +(check-equal? (run '(unbox (box 8))) 8) |
| 44 | +(check-equal? (run '(box (box 8))) (box (box 8))) |
| 45 | +(check-equal? (run '(unbox (unbox (box (box 8))))) 8) |
| 46 | +(check-equal? (run '(cons 3 4)) (cons 3 4)) |
| 47 | +(check-equal? (run '(car (cons 3 4))) 3) |
| 48 | +(check-equal? (run '(cdr (cons 3 4))) 4) |
| 49 | +(check-equal? (run '(let ((x (box 3))) (unbox x))) 3) |
| 50 | +(check-equal? (run '(unbox (let ((x (box 3))) x))) 3) |
| 51 | +(check-equal? (run '(unbox (if (zero? 0) (box 3) (box 4)))) 3) |
| 52 | +(check-equal? (run '(cons 1 (cons 2 (cons 3 '())))) (list 1 2 3)) |
0 commit comments