From 99ebc4f804dbfc04ce2a9c42b639ed728efdf17c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 21 Jul 2016 14:21:48 -0700 Subject: [PATCH] add `format-datums` --- beautiful-racket-lib/br/datum.rkt | 6 +++++- beautiful-racket/br/demo/stacker.rkt | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/beautiful-racket-lib/br/datum.rkt b/beautiful-racket-lib/br/datum.rkt index 192fd57..32013ec 100644 --- a/beautiful-racket-lib/br/datum.rkt +++ b/beautiful-racket-lib/br/datum.rkt @@ -26,6 +26,9 @@ (syntax->datum arg) arg)) args)))) +(define (format-datums datum-template args) + (map (λ(arg) (format-datum datum-template arg)) args)) + (module+ test (require rackunit syntax/datum) (check-equal? (string->datum "foo") 'foo) @@ -36,4 +39,5 @@ (check-equal? (format-datum '(~a-bar-~a) #'foo #'zam) '(foo-bar-zam)) (check-equal? (format-datum (datum (~a-bar-~a)) "foo" "zam") '(foo-bar-zam)) (check-equal? (format-datum '~a "foo") 'foo) - (check-equal? (format-datum (datum ~a) "foo") 'foo)) + (check-equal? (format-datum (datum ~a) "foo") 'foo) + (check-equal? (format-datums '(put ~a) '("foo" "zam")) '((put foo) (put zam)))) diff --git a/beautiful-racket/br/demo/stacker.rkt b/beautiful-racket/br/demo/stacker.rkt index 8afc511..3c8a45a 100644 --- a/beautiful-racket/br/demo/stacker.rkt +++ b/beautiful-racket/br/demo/stacker.rkt @@ -1,15 +1,16 @@ #lang br/quicklang (define (read-syntax src-path in-port) - (define arg-datums (port->list read in-port)) + (define args (port->list read in-port)) + (define arg-datums (format-datums '(handle ~a) args)) (define module-datum `(module mod-name br/demo/stacker ,@arg-datums)) (datum->syntax #f module-datum)) (provide read-syntax) -(define-macro (stacker-module-begin ARG-STX ...) +(define-macro (stacker-module-begin ARG-DATUM-STX ...) #'(#%module-begin - (put ARG-STX) ... + ARG-DATUM-STX ... (display (pop-stack!)))) (provide (rename-out [stacker-module-begin #%module-begin])) @@ -23,14 +24,14 @@ (define (push-stack! item) (set! stack (cons item stack))) -(define (put arg) +(define (handle arg) (cond [(number? arg) (push-stack! arg)] [else (define op-result (arg (pop-stack!) (pop-stack!))) (push-stack! op-result)])) -(provide + *) +(provide handle + *) (module+ test (require rackunit)