the tightening
parent
afa9af72d8
commit
aa43154777
@ -1,46 +1,54 @@
|
|||||||
#lang br/quicklang
|
#lang br/quicklang
|
||||||
|
(provide (rename-out [b-module-begin #%module-begin])
|
||||||
|
(matching-identifiers-out #rx"^b-" (all-defined-out)))
|
||||||
|
|
||||||
(define-macro (b-module-begin (b-program LINE ...))
|
(define-macro (b-module-begin (b-program LINE ...))
|
||||||
(with-pattern ([(LINE-NUM ...)
|
(with-pattern ([(LINE-NUM ...)
|
||||||
(filter-stx-prop 'b-line-number (syntax-flatten #'(LINE ...)))]
|
(filter-stx-prop 'b-line-number (stx-flatten #'(LINE ...)))]
|
||||||
[(LINE-ID ...) (syntax-map (λ (stx) (prefix-id "line-" stx)) #'(LINE-NUM ...))])
|
[(LINE-ID ...) (prefix-ids "line-" #'(LINE-NUM ...))])
|
||||||
#'(#%module-begin
|
#'(#%module-begin
|
||||||
LINE ...
|
LINE ...
|
||||||
(define line-table (apply hasheqv (append (list LINE-NUM LINE-ID) ...)))
|
(define line-table
|
||||||
|
(apply hasheqv (append (list LINE-NUM LINE-ID) ...)))
|
||||||
(run line-table))))
|
(run line-table))))
|
||||||
(provide (rename-out [b-module-begin #%module-begin]))
|
|
||||||
|
|
||||||
(define-macro (b-line LINE-NUMBER STATEMENT)
|
(define-macro (b-line LINE-NUMBER STATEMENT ...)
|
||||||
(with-pattern ([LINE-NUMBER-ID (prefix-id "line-" #'LINE-NUMBER
|
(with-pattern ([LINE-NUMBER-ID (prefix-id "line-" #'LINE-NUMBER
|
||||||
#:source #'LINE-NUMBER)]
|
#:source #'LINE-NUMBER)]
|
||||||
[ORIG-LOC caller-stx])
|
[ORIG-LOC caller-stx])
|
||||||
(syntax/loc caller-stx (define (LINE-NUMBER-ID #:srcloc? [srcloc #f])
|
(syntax/loc caller-stx
|
||||||
(if srcloc
|
(define (LINE-NUMBER-ID #:srcloc? [srcloc #f])
|
||||||
(syntax-srcloc #'ORIG-LOC)
|
(if srcloc
|
||||||
STATEMENT)))))
|
(syntax-srcloc #'ORIG-LOC)
|
||||||
|
(begin STATEMENT ...))))))
|
||||||
|
|
||||||
(define (b-statement stmt) stmt)
|
(define b-rem void)
|
||||||
(define (b-rem str) #f)
|
(define (b-print [val ""]) (displayln val))
|
||||||
(define (b-print str) (displayln str))
|
(define (b-sum . nums) (apply + nums))
|
||||||
(define (b-goto line-number) line-number)
|
(define (b-expr expr)
|
||||||
|
(if (integer? expr) (inexact->exact expr) expr))
|
||||||
|
|
||||||
(define-exn end-program-signal exn:fail)
|
(struct $program-end-signal ())
|
||||||
(define (b-end) (raise-end-program-signal))
|
(define (b-end) (raise ($program-end-signal)))
|
||||||
|
|
||||||
(provide b-line b-statement b-rem b-print b-goto b-end)
|
(struct $change-line-signal (num))
|
||||||
|
(define (b-goto expr) (raise ($change-line-signal expr)))
|
||||||
|
|
||||||
(define-exn-srcloc line-not-found exn:fail)
|
(define-exn line-not-found exn:fail)
|
||||||
|
|
||||||
(define (run line-table)
|
(define (run line-table)
|
||||||
(define line-vec (list->vector (sort (hash-keys line-table) <)))
|
(define line-vec (list->vector (sort (hash-keys line-table) <)))
|
||||||
(with-handlers ([end-program-signal? void])
|
(with-handlers ([$program-end-signal? void])
|
||||||
(for/fold ([line-idx 0])
|
(for/fold ([line-idx 0])
|
||||||
([i (in-naturals)])
|
([i (in-naturals)])
|
||||||
(unless (< line-idx (vector-length line-vec)) (b-end))
|
(unless (< line-idx (vector-length line-vec)) (b-end))
|
||||||
(define line-num (vector-ref line-vec line-idx))
|
(define line-num (vector-ref line-vec line-idx))
|
||||||
(define line-proc (hash-ref line-table line-num))
|
(define line-proc (hash-ref line-table line-num))
|
||||||
(define line-result (line-proc))
|
(with-handlers ([$change-line-signal?
|
||||||
(if (exact-positive-integer? line-result)
|
(λ (cls)
|
||||||
(or (vector-member line-result line-vec)
|
(or
|
||||||
(raise-line-not-found (line-proc #:srcloc? #t)))
|
(and (exact-positive-integer? ($change-line-signal-num cls))
|
||||||
(add1 line-idx)))))
|
(vector-member ($change-line-signal-num cls) line-vec))
|
||||||
|
(raise-line-not-found (line-proc #:srcloc? #t))))])
|
||||||
|
(line-proc)
|
||||||
|
(add1 line-idx)))))
|
||||||
|
@ -1,20 +1,15 @@
|
|||||||
#lang brag
|
#lang brag
|
||||||
|
|
||||||
b-program : b-line*
|
b-program : b-line*
|
||||||
|
b-line : @b-line-number b-statement (/":" b-statement)*
|
||||||
b-line: @b-line-number b-statement
|
b-line-number : INTEGER
|
||||||
|
@b-statement : b-rem
|
||||||
b-line-number : NUMBER
|
| b-print
|
||||||
|
| b-goto
|
||||||
b-statement: b-rem
|
| b-end
|
||||||
| b-print
|
|
||||||
| b-goto
|
|
||||||
| b-end
|
|
||||||
|
|
||||||
b-rem : REM
|
b-rem : REM
|
||||||
|
b-print : /"print" (STRING | b-expr)*
|
||||||
b-print : /"print" STRING
|
b-goto : /"goto" b-expr
|
||||||
|
b-expr : b-sum
|
||||||
b-goto : /"goto" NUMBER
|
b-sum : (b-number /"+")* b-number
|
||||||
|
@b-number : INTEGER | DECIMAL
|
||||||
b-end : /"end"
|
b-end : /"end"
|
@ -1,8 +1,8 @@
|
|||||||
#lang basic-demo
|
#lang basic-demo
|
||||||
30 rem print "nope"
|
30 rem print "statement ignored"
|
||||||
40 end
|
40 end
|
||||||
50 print "never"
|
50 print "never gets here"
|
||||||
60 print "second"
|
60 print "two" : print 1.2 + 1.8
|
||||||
70 goto 30
|
70 goto 11 + 10.5 + 8.5
|
||||||
10 print "first"
|
10 print "one"
|
||||||
20 goto 60
|
20 print : goto 60 : end
|
Loading…
Reference in New Issue