continuing to work on test-compiler.rkt
This commit is contained in:
parent
872ea81adc
commit
10e1d446e2
|
@ -16,8 +16,9 @@
|
|||
(: -compile (ExpressionCore Target Linkage -> (Listof Statement)))
|
||||
(define (-compile exp target linkage)
|
||||
(statements
|
||||
(compile exp
|
||||
(list (make-Prefix (find-toplevel-variables exp)))
|
||||
(compile (make-Top (make-Prefix (find-toplevel-variables exp))
|
||||
exp)
|
||||
(list)
|
||||
target
|
||||
linkage)))
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
[(quoted? exp)
|
||||
(make-Constant (text-of-quotation exp))]
|
||||
[(variable? exp)
|
||||
(make-Var exp)]
|
||||
(make-Var exp)]
|
||||
[(definition? exp)
|
||||
(make-Def (definition-variable exp)
|
||||
(parse (definition-value exp)))]
|
||||
|
@ -38,6 +38,7 @@
|
|||
(cond
|
||||
[(number? exp) #t]
|
||||
[(string? exp) #t]
|
||||
[(boolean? exp) #t]
|
||||
[else #f]))
|
||||
|
||||
(define (variable? exp) (symbol? exp))
|
||||
|
|
|
@ -5,33 +5,60 @@
|
|||
"compile.rkt"
|
||||
"parse.rkt")
|
||||
|
||||
|
||||
(define (run-compiler code)
|
||||
(compile (parse code) 'val 'next))
|
||||
|
||||
|
||||
;; Test out the compiler, using the simulator.
|
||||
(define-syntax (test stx)
|
||||
(syntax-case stx ()
|
||||
[(_ code exp)
|
||||
(with-syntax ([stx stx])
|
||||
(syntax/loc #'stx
|
||||
(let* ([a-machine (run (new-machine (compile (parse code) 'val 'next)))]
|
||||
[actual (machine-val a-machine)])
|
||||
(unless (equal? actual exp)
|
||||
(raise-syntax-error #f (format "Expected ~s, got ~s" exp actual)
|
||||
#'stx)))))]))
|
||||
(begin
|
||||
(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)
|
||||
(raise-syntax-error #f (format "Expected ~s, got ~s" exp actual)
|
||||
#'stx))
|
||||
(printf "ok. ~s steps\n\n" num-steps)))))]))
|
||||
|
||||
;; run: machine -> machine
|
||||
;; run: machine -> (machine number)
|
||||
;; Run the machine to completion.
|
||||
(define (run m)
|
||||
(cond
|
||||
[(can-step? m)
|
||||
(run (step m))]
|
||||
[else
|
||||
m]))
|
||||
(let loop ([m m]
|
||||
[steps 0])
|
||||
(cond
|
||||
[(can-step? m)
|
||||
(loop (step m) (add1 steps))]
|
||||
[else
|
||||
(values m steps)])))
|
||||
|
||||
|
||||
;; Atomic expressions
|
||||
(test 42 42)
|
||||
(test '(begin (define x 42)
|
||||
(+ x x))
|
||||
(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))
|
||||
84)
|
||||
|
||||
(test (begin (define x 6)
|
||||
(define y 7)
|
||||
(define z 8)
|
||||
(* x y z))
|
||||
(* 6 7 8))
|
||||
|
||||
|
||||
;(simulate (compile (parse '42) 'val 'next))
|
||||
;(compile (parse '(+ 3 4)) 'val 'next)
|
Loading…
Reference in New Issue
Block a user