funstacker

pull/2/head
Matthew Butterick 8 years ago
parent 48c17bd852
commit 86ceb297e6

@ -1,26 +1,29 @@
#lang br/quicklang #lang br/quicklang
(provide read-syntax
(rename-out [stacker-module-begin #%module-begin])
+ *)
(define (read-syntax src-path in-port) (define (read-syntax path port)
(define args (port->list read in-port)) (define args (port->lines port))
(define module-datum `(module funstacker-mod br/demo/funstacker (define arg-datums (format-datums '~a args))
,@args)) (define module-datum `(module stacker-mod br/demo/funstacker
(handle-args ,@arg-datums)))
(datum->syntax #f module-datum)) (datum->syntax #f module-datum))
(provide read-syntax)
(define-macro (stacker-module-begin ARG ...) (define-macro (stacker-module-begin HANDLE-ARGS-EXPR)
#'(#%module-begin #'(#%module-begin
(define stack-result (display (first HANDLE-ARGS-EXPR))))
(for/fold ([stack empty]) (provide (rename-out [stacker-module-begin #%module-begin]))
([arg (in-list (list ARG ...))])
(push arg stack)))
(display (first stack-result))))
(define (push arg stack) (define (handle-args . args)
(if (number? arg) (for/fold ([stack empty])
(cons arg stack) ([arg (in-list (filter-not void? args))])
(cons (arg (first stack) (second stack)) (drop stack 2)))) (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 handle-args)
(provide + *)
(module+ test (module+ test
(require rackunit) (require rackunit)

Loading…
Cancel
Save