continuing to work on test-compiler.rkt

This commit is contained in:
Danny Yoo 2011-03-07 18:46:11 -05:00
parent 872ea81adc
commit 10e1d446e2
3 changed files with 45 additions and 16 deletions

View File

@ -16,8 +16,9 @@
(: -compile (ExpressionCore Target Linkage -> (Listof Statement))) (: -compile (ExpressionCore Target Linkage -> (Listof Statement)))
(define (-compile exp target linkage) (define (-compile exp target linkage)
(statements (statements
(compile exp (compile (make-Top (make-Prefix (find-toplevel-variables exp))
(list (make-Prefix (find-toplevel-variables exp))) exp)
(list)
target target
linkage))) linkage)))

View File

@ -38,6 +38,7 @@
(cond (cond
[(number? exp) #t] [(number? exp) #t]
[(string? exp) #t] [(string? exp) #t]
[(boolean? exp) #t]
[else #f])) [else #f]))
(define (variable? exp) (symbol? exp)) (define (variable? exp) (symbol? exp))

View File

@ -5,33 +5,60 @@
"compile.rkt" "compile.rkt"
"parse.rkt") "parse.rkt")
(define (run-compiler code)
(compile (parse code) 'val 'next))
;; Test out the compiler, using the simulator. ;; Test out the compiler, using the simulator.
(define-syntax (test stx) (define-syntax (test stx)
(syntax-case stx () (syntax-case stx ()
[(_ code exp) [(_ code exp)
(with-syntax ([stx stx]) (with-syntax ([stx stx])
(syntax/loc #'stx (syntax/loc #'stx
(let* ([a-machine (run (new-machine (compile (parse code) 'val 'next)))] (begin
[actual (machine-val a-machine)]) (printf "Running ~s...\n" 'code)
(let*-values([(a-machine num-steps)
(run (new-machine (run-compiler 'code)))]
[(actual) (machine-val a-machine)])
(unless (equal? actual exp) (unless (equal? actual exp)
(raise-syntax-error #f (format "Expected ~s, got ~s" exp actual) (raise-syntax-error #f (format "Expected ~s, got ~s" exp actual)
#'stx)))))])) #'stx))
(printf "ok. ~s steps\n\n" num-steps)))))]))
;; run: machine -> machine ;; run: machine -> (machine number)
;; Run the machine to completion. ;; Run the machine to completion.
(define (run m) (define (run m)
(let loop ([m m]
[steps 0])
(cond (cond
[(can-step? m) [(can-step? m)
(run (step m))] (loop (step m) (add1 steps))]
[else [else
m])) (values m steps)])))
;; Atomic expressions
(test 42 42) (test 42 42)
(test '(begin (define x 42) (test "hello world" "hello world")
(test #t true)
(test #f false)
;; quoted
(test '(+ 3 4)
'(+ 3 4))
;; Simple definitions
(test (begin (define x 42)
(+ x x)) (+ x x))
84) 84)
(test (begin (define x 6)
(define y 7)
(define z 8)
(* x y z))
(* 6 7 8))
;(simulate (compile (parse '42) 'val 'next)) ;(simulate (compile (parse '42) 'val 'next))
;(compile (parse '(+ 3 4)) 'val 'next) ;(compile (parse '(+ 3 4)) 'val 'next)