Made execute more efficiently

This commit is contained in:
Claudio Maggioni 2018-12-12 10:21:45 +01:00
parent 8a1af32dcb
commit 0034de018b

View file

@ -407,19 +407,17 @@
; calls done when the final ProgState has been
; computed by executing the program
(define (execute prog done get-input)
; The program length in characters
(define program-len (string-length (prog-state-program prog)))
; execute-help: ProgState -> _
; execute-help: ProgState ProgState -> _
; Helper function for `execute`.
; Given an initial ProgState state, calls done
; when the final ProgState is ready.
(define (execute-help w)
(cond [(>= (prog-state-ip w) program-len) (done w)]
[else (execute-instr w execute-help get-input)]))
; Given an initial ProgState state, and the previous state,
; calls done when the final ProgState is ready.
(define (execute-help w prev)
(cond [(eq? w #f) (done prev)]
[else (execute-instr w (lambda (new) (execute-help new w))
get-input)]))
(execute-help prog))
(execute-instr prog (lambda (new) (execute-help new prog)) get-input))
; Tests for execute
(test-begin