From 6af77e0ce51510dbbd52025ead52cb016e7b6918 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 5 Aug 2016 13:47:35 -0700 Subject: [PATCH] update funstacker --- beautiful-racket/br/demo/funstacker-h2.rkt | 33 +++++++++++++++++++ beautiful-racket/br/demo/funstacker.rkt | 15 +++++---- ...zer-test copy.rkt => stackerizer-test.rkt} | 0 beautiful-racket/br/demo/stackerizer.rkt | 5 +-- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 beautiful-racket/br/demo/funstacker-h2.rkt rename beautiful-racket/br/demo/{stackerizer-test copy.rkt => stackerizer-test.rkt} (100%) 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]