handle negative numbers, for / next
parent
d0ced4022f
commit
12bc37145a
@ -0,0 +1,9 @@
|
||||
#lang br
|
||||
(provide (matching-identifiers-out #rx"^b-" (all-defined-out)))
|
||||
|
||||
(define (b-sum . nums) (apply + nums))
|
||||
|
||||
(define (b-num-expr expr)
|
||||
(if (integer? expr) (inexact->exact expr) expr))
|
||||
|
||||
(define (b-negative num) (- num))
|
@ -0,0 +1,56 @@
|
||||
#lang br
|
||||
(require "structs.rkt" "misc.rkt" "line.rkt")
|
||||
(provide (matching-identifiers-out #rx"^b-" (all-defined-out)))
|
||||
|
||||
(define (b-end) (raise (end-program-signal)))
|
||||
(define (b-goto num-expr) (raise (change-line-signal num-expr)))
|
||||
|
||||
(define gosub-ccs empty)
|
||||
|
||||
(define (b-gosub num-expr)
|
||||
(let/cc gosub-cc
|
||||
(push! gosub-ccs gosub-cc)
|
||||
(b-goto num-expr)))
|
||||
|
||||
(define (b-return)
|
||||
(unless (pair? gosub-ccs)
|
||||
(raise (line-error "return without gosub")))
|
||||
(define top-return-k (pop! gosub-ccs))
|
||||
(top-return-k))
|
||||
|
||||
(define (in-closed-interval? x left right)
|
||||
(define cmp (if (< left right) <= >=))
|
||||
(cmp left x right))
|
||||
|
||||
(define-macro-cases b-for
|
||||
[(_ ID START END) #'(b-for ID START END 1)]
|
||||
[(_ ID START END STEP)
|
||||
#'(b-let ID (let/cc top-of-loop-cc
|
||||
(push-thunk!
|
||||
(cons 'ID
|
||||
(λ ()
|
||||
(define next-val (+ ID STEP))
|
||||
(if (next-val . in-closed-interval? . START END)
|
||||
(top-of-loop-cc next-val)
|
||||
(remove-thunk! 'ID)))))
|
||||
START))])
|
||||
|
||||
(define for-thunks (make-parameter empty))
|
||||
|
||||
(define (push-thunk! thunk)
|
||||
(for-thunks (cons thunk (for-thunks))))
|
||||
|
||||
(define (remove-thunk! id-sym)
|
||||
(for-thunks (remq (assq id-sym (for-thunks)) (for-thunks))))
|
||||
|
||||
(define-macro (b-next ID ...) #'(do-next 'ID ...))
|
||||
|
||||
(define (do-next [id-sym #f])
|
||||
(when (empty? (for-thunks))
|
||||
(raise-line-error "next without for"))
|
||||
(define for-thunk
|
||||
(cdr (if id-sym
|
||||
(or (assq id-sym (for-thunks))
|
||||
(raise-line-error "next without for"))
|
||||
(car (for-thunks)))))
|
||||
(for-thunk))
|
@ -1,3 +0,0 @@
|
||||
#lang br
|
||||
(provide b-gosub b-return)
|
||||
|
@ -0,0 +1,19 @@
|
||||
#lang br
|
||||
(require "structs.rkt")
|
||||
(provide (all-defined-out))
|
||||
|
||||
(define-macro (b-line NUM STATEMENT ...)
|
||||
(with-pattern ([LINE-NUM (prefix-id "line-" #'NUM
|
||||
#:source #'NUM)])
|
||||
(syntax/loc caller-stx
|
||||
(define (LINE-NUM)
|
||||
(with-handlers ([line-error? (λ (le) (handle-line-error NUM le))])
|
||||
(void) STATEMENT ...)))))
|
||||
|
||||
(define (handle-line-error num le)
|
||||
(error (format "error in line ~a: ~a" num (line-error-msg le))))
|
||||
|
||||
(define (raise-line-error line-error-or-str)
|
||||
(raise (if (string? line-error-or-str)
|
||||
(line-error line-error-or-str)
|
||||
line-error-or-str)))
|
@ -0,0 +1,7 @@
|
||||
#lang br
|
||||
(provide (matching-identifiers-out #rx"^b-" (all-defined-out)))
|
||||
|
||||
(define (b-rem val) (void))
|
||||
(define (b-print [val ""]) (displayln val))
|
||||
(define-macro (b-let ID VAL)
|
||||
#'(set! ID VAL))
|
@ -0,0 +1,24 @@
|
||||
#lang br
|
||||
(require "line.rkt" "structs.rkt")
|
||||
(provide run)
|
||||
|
||||
(define (run line-table)
|
||||
(define line-vec
|
||||
(list->vector (sort (hash-keys line-table) <)))
|
||||
(with-handlers ([end-program-signal? (λ (exn-val) (void))])
|
||||
(for/fold ([line-idx 0])
|
||||
([i (in-naturals)]
|
||||
#:break (>= line-idx (vector-length line-vec)))
|
||||
(define line-num (vector-ref line-vec line-idx))
|
||||
(define line-func (hash-ref line-table line-num))
|
||||
(with-handlers
|
||||
([change-line-signal?
|
||||
(λ (cls)
|
||||
(define clsv (change-line-signal-val cls))
|
||||
(or
|
||||
(and (exact-positive-integer? clsv)
|
||||
(vector-member clsv line-vec))
|
||||
(handle-line-error line-num
|
||||
(line-error (format "line ~a not found" clsv)))))])
|
||||
(line-func)
|
||||
(add1 line-idx)))))
|
@ -0,0 +1,3 @@
|
||||
#lang br
|
||||
(provide current-basic-port)
|
||||
(define current-basic-port (make-parameter #f))
|
@ -0,0 +1,8 @@
|
||||
#lang basic-demo-2
|
||||
10 for a = 1 to 3
|
||||
20 print a
|
||||
21 for b = 103 to 101 step -1
|
||||
22 print b
|
||||
23 next b
|
||||
30 next a
|
||||
40 print "yay"
|
@ -0,0 +1,3 @@
|
||||
#lang br
|
||||
(require basic-demo-2/sample-var)
|
||||
(* a a)
|
@ -0,0 +1,9 @@
|
||||
#lang basic-demo
|
||||
30 rem print 'ignored'
|
||||
35
|
||||
50 print "never gets here"
|
||||
40 end
|
||||
60 print 'three' : print 1.0 + 3
|
||||
70 goto 11. + 18.5 + .5
|
||||
10 print "one"
|
||||
20 print : goto 60.0 : end
|
@ -0,0 +1,7 @@
|
||||
#lang br
|
||||
(provide (all-defined-out))
|
||||
|
||||
(struct end-program-signal ())
|
||||
(struct change-line-signal (val))
|
||||
|
||||
(struct line-error (msg))
|
Loading…
Reference in New Issue