diff --git a/beautiful-racket/br/demo/funstacker-h2.rkt b/beautiful-racket/br/demo/funstacker-h2.rkt new file mode 100644 index 0000000..9d8cfcb --- /dev/null +++ b/beautiful-racket/br/demo/funstacker-h2.rkt @@ -0,0 +1,33 @@ +#lang br/quicklang + +(define (read-syntax path port) + (define args (port->lines port)) + (define arg-datums (filter-not void? (format-datums '~a args))) + (define module-datum `(module stacker-mod br/demo/funstacker + (nestify null ,@arg-datums))) + (datum->syntax #f module-datum)) +(provide read-syntax) + +(define-macro (stacker-module-begin HANDLE-ARGS-EXPR) + #'(#%module-begin + (display (first HANDLE-ARGS-EXPR)))) +(provide (rename-out [stacker-module-begin #%module-begin])) + +(require (for-syntax sugar/debug)) +(define-macro-cases nestify + [(nestify ARG0) #'ARG0] + [(nestify ARG0 ARG1 ARG ...) #'(nestify (h3 ARG0 ARG1) ARG ...)]) +(provide nestify) + +(define (h3 stack arg) + (cond + [(number? arg) (cons arg stack)] + [(or (equal? * arg) (equal? + arg)) + (define op-result (arg (first stack) (second stack))) + (cons op-result (drop stack 2))])) + +(provide + * null) + +(module+ test + (require rackunit) + #;(check-equal? (with-output-to-string (λ () (dynamic-require "funstacker-test.rkt" #f))) "36")) \ No newline at end of file diff --git a/beautiful-racket/br/demo/funstacker.rkt b/beautiful-racket/br/demo/funstacker.rkt index ce286cf..97c9589 100644 --- a/beautiful-racket/br/demo/funstacker.rkt +++ b/beautiful-racket/br/demo/funstacker.rkt @@ -8,19 +8,20 @@ (datum->syntax #f module-datum)) (provide read-syntax) -(define-macro (stacker-module-begin HANDLE-ARGS-EXPR) +(define-macro (funstacker-module-begin HANDLE-ARGS-EXPR) #'(#%module-begin (display (first HANDLE-ARGS-EXPR)))) -(provide (rename-out [stacker-module-begin #%module-begin])) +(provide (rename-out [funstacker-module-begin #%module-begin])) (define (handle-args . args) - (for/fold ([stack empty]) - ([arg (in-list (filter-not void? args))]) + (for/fold ([stack-acc empty]) + ([arg (filter-not void? args)]) (cond - [(number? arg) (cons arg stack)] + [(number? arg) (cons arg stack-acc)] [(or (equal? * arg) (equal? + arg)) - (define op-result (arg (first stack) (second stack))) - (cons op-result (drop stack 2))]))) + (define op-result + (arg (first stack-acc) (second stack-acc))) + (cons op-result (drop stack-acc 2))]))) (provide handle-args) (provide + *) diff --git a/beautiful-racket/br/demo/stackerizer-test copy.rkt b/beautiful-racket/br/demo/stackerizer-test.rkt similarity index 100% rename from beautiful-racket/br/demo/stackerizer-test copy.rkt rename to beautiful-racket/br/demo/stackerizer-test.rkt diff --git a/beautiful-racket/br/demo/stackerizer.rkt b/beautiful-racket/br/demo/stackerizer.rkt index f6637ed..0da730a 100644 --- a/beautiful-racket/br/demo/stackerizer.rkt +++ b/beautiful-racket/br/demo/stackerizer.rkt @@ -3,7 +3,7 @@ (define-macro (stackerizer-module-begin EXPR ...) #'(#%module-begin - (stackerize EXPR ...))) + (for-each displayln (reverse (flatten EXPR ...))))) (provide (rename-out [stackerizer-module-begin #%module-begin])) #| @@ -18,9 +18,6 @@ [(+ EXPR0 EXPR ...) #'(list '+ EXPR0 (+ EXPR ...))]) |# -(define (stackerize . args) - (for-each displayln (reverse (flatten args)))) - (define-macro (define-op-macro OP-NAME IDENTITY-VAL) #'(define-macro-cases OP-NAME [(OP-NAME) #'IDENTITY-VAL]