update funstacker

pull/2/head
Matthew Butterick 8 years ago
parent 86ceb297e6
commit 6af77e0ce5

@ -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"))

@ -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 + *)

@ -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]

Loading…
Cancel
Save