From 50bd8e630157a081a6c252a07ff00e085826a45b Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 17 Feb 2017 15:59:15 -0800 Subject: [PATCH] copy edits --- beautiful-racket-demo/basic-demo-2/go.rkt | 44 +++++++++++-------- beautiful-racket-demo/basic-demo-2/parser.rkt | 4 +- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/beautiful-racket-demo/basic-demo-2/go.rkt b/beautiful-racket-demo/basic-demo-2/go.rkt index 2edf9ba..d29f19d 100644 --- a/beautiful-racket-demo/basic-demo-2/go.rkt +++ b/beautiful-racket-demo/basic-demo-2/go.rkt @@ -10,37 +10,43 @@ (define return-ccs empty) (define (b-gosub num-expr) - (let/cc here-cc - (push! return-ccs here-cc) + (let/cc this-cc + (push! return-ccs this-cc) (b-goto num-expr))) (define (b-return) - (unless (pair? return-ccs) + (unless (not (empty? return-ccs)) (raise-line-error "return without gosub")) - (define top-return-cc (pop! return-ccs)) - (top-return-cc (void))) + (define top-cc (pop! return-ccs)) + (top-cc (void))) -(define thunk-table (make-hasheq)) +(define next-funcs (make-hasheq)) (define-macro-cases b-for [(_ LOOP-ID START END) #'(b-for LOOP-ID START END 1)] [(_ LOOP-ID START END STEP) - #'(b-let LOOP-ID (let/cc loop-cc - (hash-set! thunk-table - 'LOOP-ID - (λ () - (define next-val (+ LOOP-ID STEP)) - (if (next-val . in-closed-interval? . START END) - (loop-cc next-val) - (hash-remove! thunk-table 'LOOP-ID)))) - START))]) + #'(b-let LOOP-ID + (let/cc loop-cc + (hash-set! next-funcs + 'LOOP-ID + (λ () + (define next-val + (+ LOOP-ID STEP)) + (if (next-val + . in-closed-interval? . + START END) + (loop-cc next-val) + (hash-remove! next-funcs + 'LOOP-ID)))) + START))]) (define (in-closed-interval? x start end) ((if (< start end) <= >=) start x end)) (define-macro (b-next LOOP-ID) #'(begin - (unless (hash-has-key? thunk-table 'LOOP-ID) - (raise-line-error "next without for")) - (define thunk (hash-ref thunk-table 'LOOP-ID)) - (thunk))) \ No newline at end of file + (unless (hash-has-key? next-funcs 'LOOP-ID) + (raise-line-error + (format "`next ~a` without for" 'LOOP-ID))) + (define func (hash-ref next-funcs 'LOOP-ID)) + (func))) \ No newline at end of file diff --git a/beautiful-racket-demo/basic-demo-2/parser.rkt b/beautiful-racket-demo/basic-demo-2/parser.rkt index a5d9e1b..646d3e0 100644 --- a/beautiful-racket-demo/basic-demo-2/parser.rkt +++ b/beautiful-racket-demo/basic-demo-2/parser.rkt @@ -19,8 +19,7 @@ b-input : /"input" b-id b-gosub : /"gosub" b-expr b-return : /"return" b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr] -b-next : /"next" [b-id] - +b-next : /"next" b-id b-expr : b-or-expr b-or-expr : [b-or-expr "or"] b-and-expr b-and-expr : [b-and-expr "and"] b-not-expr @@ -30,6 +29,5 @@ b-sum : [b-sum ("+"|"-")] b-product b-product : [b-product ("*"|"/"|"mod")] b-neg b-neg : ["-"] b-expt b-expt : [b-expt ("^")] b-value - @b-value : b-number | b-id | /"(" b-expr /")" @b-number : INTEGER | DECIMAL \ No newline at end of file