funstacker
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))))
|
||||||
|
(provide (rename-out [stacker-module-begin #%module-begin]))
|
||||||
|
|
||||||
|
(define (handle-args . args)
|
||||||
(for/fold ([stack empty])
|
(for/fold ([stack empty])
|
||||||
([arg (in-list (list ARG ...))])
|
([arg (in-list (filter-not void? args))])
|
||||||
(push arg stack)))
|
(cond
|
||||||
(display (first stack-result))))
|
[(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)
|
||||||
|
|
||||||
(define (push arg stack)
|
(provide + *)
|
||||||
(if (number? arg)
|
|
||||||
(cons arg stack)
|
|
||||||
(cons (arg (first stack) (second stack)) (drop stack 2))))
|
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
(require rackunit)
|
(require rackunit)
|
||||||
|
Loading…
Reference in New Issue