copy edits

pull/10/head
Matthew Butterick 7 years ago
parent d4d5ea638d
commit 50bd8e6301

@ -10,37 +10,43 @@
(define return-ccs empty) (define return-ccs empty)
(define (b-gosub num-expr) (define (b-gosub num-expr)
(let/cc here-cc (let/cc this-cc
(push! return-ccs here-cc) (push! return-ccs this-cc)
(b-goto num-expr))) (b-goto num-expr)))
(define (b-return) (define (b-return)
(unless (pair? return-ccs) (unless (not (empty? return-ccs))
(raise-line-error "return without gosub")) (raise-line-error "return without gosub"))
(define top-return-cc (pop! return-ccs)) (define top-cc (pop! return-ccs))
(top-return-cc (void))) (top-cc (void)))
(define thunk-table (make-hasheq)) (define next-funcs (make-hasheq))
(define-macro-cases b-for (define-macro-cases b-for
[(_ LOOP-ID START END) #'(b-for LOOP-ID START END 1)] [(_ LOOP-ID START END) #'(b-for LOOP-ID START END 1)]
[(_ LOOP-ID START END STEP) [(_ LOOP-ID START END STEP)
#'(b-let LOOP-ID (let/cc loop-cc #'(b-let LOOP-ID
(hash-set! thunk-table (let/cc loop-cc
'LOOP-ID (hash-set! next-funcs
(λ () 'LOOP-ID
(define next-val (+ LOOP-ID STEP)) (λ ()
(if (next-val . in-closed-interval? . START END) (define next-val
(loop-cc next-val) (+ LOOP-ID STEP))
(hash-remove! thunk-table 'LOOP-ID)))) (if (next-val
START))]) . in-closed-interval? .
START END)
(loop-cc next-val)
(hash-remove! next-funcs
'LOOP-ID))))
START))])
(define (in-closed-interval? x start end) (define (in-closed-interval? x start end)
((if (< start end) <= >=) start x end)) ((if (< start end) <= >=) start x end))
(define-macro (b-next LOOP-ID) (define-macro (b-next LOOP-ID)
#'(begin #'(begin
(unless (hash-has-key? thunk-table 'LOOP-ID) (unless (hash-has-key? next-funcs 'LOOP-ID)
(raise-line-error "next without for")) (raise-line-error
(define thunk (hash-ref thunk-table 'LOOP-ID)) (format "`next ~a` without for" 'LOOP-ID)))
(thunk))) (define func (hash-ref next-funcs 'LOOP-ID))
(func)))

@ -19,8 +19,7 @@ b-input : /"input" b-id
b-gosub : /"gosub" b-expr b-gosub : /"gosub" b-expr
b-return : /"return" b-return : /"return"
b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr] 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-expr : b-or-expr
b-or-expr : [b-or-expr "or"] b-and-expr b-or-expr : [b-or-expr "or"] b-and-expr
b-and-expr : [b-and-expr "and"] b-not-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-product : [b-product ("*"|"/"|"mod")] b-neg
b-neg : ["-"] b-expt b-neg : ["-"] b-expt
b-expt : [b-expt ("^")] b-value b-expt : [b-expt ("^")] b-value
@b-value : b-number | b-id | /"(" b-expr /")" @b-value : b-number | b-id | /"(" b-expr /")"
@b-number : INTEGER | DECIMAL @b-number : INTEGER | DECIMAL
Loading…
Cancel
Save