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 ; calls done when the final ProgState has been
; computed by executing the program ; computed by executing the program
(define (execute prog done get-input) (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`. ; Helper function for `execute`.
; Given an initial ProgState state, calls done ; Given an initial ProgState state, and the previous state,
; when the final ProgState is ready. ; calls done when the final ProgState is ready.
(define (execute-help w) (define (execute-help w prev)
(cond [(>= (prog-state-ip w) program-len) (done w)] (cond [(eq? w #f) (done prev)]
[else (execute-instr w execute-help get-input)])) [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 ; Tests for execute
(test-begin (test-begin