implemented input function
This commit is contained in:
parent
45ba1c924a
commit
801ce49212
1 changed files with 31 additions and 11 deletions
|
@ -112,7 +112,8 @@
|
||||||
(prog-state (cons 0 '()) 0 1 "" p 0))
|
(prog-state (cons 0 '()) 0 1 "" p 0))
|
||||||
|
|
||||||
; Tests for program->prog-state
|
; 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
|
; tape-help: Tape DP (Byte -> Byte) -> Tape
|
||||||
; Given a tape and a data pointer, returns the same tape with the data in the
|
; Given a tape and a data pointer, returns the same tape with the data in the
|
||||||
|
@ -181,7 +182,8 @@
|
||||||
; exec-tape-right: ProgState -> ProgState
|
; exec-tape-right: ProgState -> ProgState
|
||||||
; Given a ProgState, returns a new ProgState with the > instruction executed
|
; Given a ProgState, returns a new ProgState with the > instruction executed
|
||||||
(define (exec-tape-right w)
|
(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
|
(prog-state
|
||||||
(if end-of-tape
|
(if end-of-tape
|
||||||
(append (prog-state-tape w) (list 0))
|
(append (prog-state-tape w) (list 0))
|
||||||
|
@ -272,7 +274,8 @@
|
||||||
; exec-loop-start: ProgState -> ProgState
|
; exec-loop-start: ProgState -> ProgState
|
||||||
; Given a ProgState, returns a new ProgState with the [ instruction executed
|
; Given a ProgState, returns a new ProgState with the [ instruction executed
|
||||||
(define (exec-loop-start w)
|
(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
|
||||||
(prog-state-tape w)
|
(prog-state-tape w)
|
||||||
(prog-state-dp w)
|
(prog-state-dp w)
|
||||||
|
@ -294,7 +297,8 @@
|
||||||
; exec-loop-end: ProgState -> ProgState
|
; exec-loop-end: ProgState -> ProgState
|
||||||
; Given a ProgState, returns a new ProgState with the ] instruction executed
|
; Given a ProgState, returns a new ProgState with the ] instruction executed
|
||||||
(define (exec-loop-end w)
|
(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
|
||||||
(prog-state-tape w)
|
(prog-state-tape w)
|
||||||
(prog-state-dp w)
|
(prog-state-dp w)
|
||||||
|
@ -313,9 +317,25 @@
|
||||||
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 5))
|
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 5))
|
||||||
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 1))
|
(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
|
; execute: ProgState -> ProgState
|
||||||
; Given an initial ProgState state, returns the final ProgState state executing the
|
; Given an initial ProgState state,
|
||||||
; program.
|
; returns the final ProgState state executing the program.
|
||||||
(define (execute w)
|
(define (execute w)
|
||||||
(local [(define program-len (string-length (prog-state-program w)))]
|
(local [(define program-len (string-length (prog-state-program w)))]
|
||||||
(cond [(>= (prog-state-ip w) program-len) w]
|
(cond [(>= (prog-state-ip w) program-len) w]
|
||||||
|
|
Loading…
Reference in a new issue