Open DrRacket. Start a new file called "funstacker.rkt" and save it in a convenient location. Paste in the source code that we had at the end of the stacker tutorial, with one change: for our module-datum, change the expander name from "stacker.rkt" to "funstacker.rkt":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #lang br/quicklang (define (read-syntax path port) (define src-lines (port->lines port)) (define src-datums (format-datums '(handle ~a) src-lines)) (define module-datum `(module stacker-mod "funstacker.rkt" ,@src-datums)) (datum->syntax #f module-datum)) (provide read-syntax) (define-macro (stacker-module-begin HANDLE-EXPR ...) #'(#%module-begin HANDLE-EXPR ... (display (first stack)))) (provide (rename-out [stacker-module-begin #%module-begin])) (define stack empty) (define (pop-stack!) (define arg (first stack)) (set! stack (rest stack)) arg) (define (push-stack! arg) (set! stack (cons arg stack))) (define (handle [arg #f]) (cond [(number? arg) (push-stack! arg)] [(or (equal? * arg) (equal? + arg)) (define op-result (arg (pop-stack!) (pop-stack!))) (push-stack! op-result)])) (provide handle) (provide + *) |
In the same directory, create a second file called "funstacker-test.rkt", and paste in our stacker demo program, but with an updated #lang line:
We should be able to run this file and get 36, same as usual.
Now we’re ready to break things.