diff --git a/interpreter.rkt b/interpreter.rkt index 8f7dab5..3c32607 100644 --- a/interpreter.rkt +++ b/interpreter.rkt @@ -112,7 +112,8 @@ (prog-state (cons 0 '()) 0 1 "" p 0)) ; Tests for program->prog-state -(check-expect (program->prog-state "[->+<]") (prog-state (list 0) 0 1 "" "[->+<]" 0)) +(check-expect + (program->prog-state "[->+<]") (prog-state (list 0) 0 1 "" "[->+<]" 0)) ; tape-help: Tape DP (Byte -> Byte) -> Tape ; Given a tape and a data pointer, returns the same tape with the data in the @@ -177,15 +178,16 @@ (check-error (exec-tape-left (prog-state (list 1 2 3) 0 3 "" "<" 0))) (check-expect (exec-tape-left (prog-state (list 1 2 3) 2 3 "" "<" 0)) (prog-state (list 1 2 3) 1 3 "" "<" 1)) - + ; exec-tape-right: ProgState -> ProgState ; Given a ProgState, returns a new ProgState with the > instruction executed (define (exec-tape-right w) - (local [(define end-of-tape (= (prog-state-dp w) (sub1 (prog-state-tape-len w))))] + (local [(define end-of-tape + (= (prog-state-dp w) (sub1 (prog-state-tape-len w))))] (prog-state (if end-of-tape (append (prog-state-tape w) (list 0)) - (prog-state-tape w)) + (prog-state-tape w)) (add1 (prog-state-dp w)) (if end-of-tape (add1 (prog-state-tape-len w)) @@ -243,7 +245,7 @@ (define brkt (if (symbol=? wd 'forward) #\] #\[)) (define oppos-brkt (if (symbol=? wd 'forward) #\[ #\])) (define upd-start (if (symbol=? wd 'forward) add1 sub1)) - + ; fm-helper Nat Nat -> Nat ; Given a starting position and a nest accumulator, returns the position of ; the matching bracket waking in the direction provided by updating the @@ -272,7 +274,8 @@ ; exec-loop-start: ProgState -> ProgState ; Given a ProgState, returns a new ProgState with the [ instruction executed (define (exec-loop-start w) - (local [(define jump (zero? (list-ref (prog-state-tape w) (prog-state-dp w))))] + (local [(define jump + (zero? (list-ref (prog-state-tape w) (prog-state-dp w))))] (prog-state (prog-state-tape w) (prog-state-dp w) @@ -280,7 +283,7 @@ (prog-state-output w) (prog-state-program w) (add1 (if jump - (find-matching (prog-state-program w) (prog-state-ip w) 'forward) + (find-matching (prog-state-program w) (prog-state-ip w) 'forward) (prog-state-ip w)))))) ; Tests for exec-loop-start @@ -294,7 +297,8 @@ ; exec-loop-end: ProgState -> ProgState ; Given a ProgState, returns a new ProgState with the ] instruction executed (define (exec-loop-end w) - (local [(define jump (not (zero? (list-ref (prog-state-tape w) (prog-state-dp w)))))] + (local [(define jump + (not (zero? (list-ref (prog-state-tape w) (prog-state-dp w)))))] (prog-state (prog-state-tape w) (prog-state-dp w) @@ -302,7 +306,7 @@ (prog-state-output w) (prog-state-program w) (add1 (if jump - (find-matching (prog-state-program w) (prog-state-ip w) 'backward) + (find-matching (prog-state-program w) (prog-state-ip w) 'backward) (prog-state-ip w)))))) ; Tests for exec-loop-end @@ -313,9 +317,25 @@ (prog-state '(1) 0 1 "" "[++--]++--+-[]" 5)) (prog-state '(1) 0 1 "" "[++--]++--+-[]" 1)) +; exec-in: ProgState ((Byte -> _) -> _) (ProgState -> _) -> _ +; Given a ProgState, a function that takes a callback function requiring a Byte +; and a function which takes the new ProgState, calls done with the input +; provided by get-input (provided by the call to the callback given in +; get-input). +(define (exec-in w get-input done) + (define (got-input byte) + (done (prog-state + (insert-in-tape byte (prog-state-dp w)) + (prog-state-dp w) + (prog-state-tape-len w) + (prog-state-output w) + (prog-state-program w) + (add1 (prog-state-ip w))))) + (get-input got-input)) + ; execute: ProgState -> ProgState -; Given an initial ProgState state, returns the final ProgState state executing the -; program. +; Given an initial ProgState state, +; returns the final ProgState state executing the program. (define (execute w) (local [(define program-len (string-length (prog-state-program w)))] (cond [(>= (prog-state-ip w) program-len) w]