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

@ -408,18 +408,16 @@
; 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 ; execute-help: ProgState ProgState -> _
(define program-len (string-length (prog-state-program prog)))
; execute-help: 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