Chores needed for stepper functionality

This commit is contained in:
Claudio Maggioni 2018-12-11 14:07:01 +01:00
parent 68f7133999
commit d2064f326c
2 changed files with 35 additions and 27 deletions

18
gui.rkt
View file

@ -64,13 +64,19 @@
; run the program ; run the program
(execute-content (execute-content
(lambda (ps) (lambda (ps)
(define out (prog-state-output ps)) (update-state-gui ps)
; update data tape inspector
(make-tape-cells ps)
; set output and change color to ended
(send RUN-OUTPUT set-value out)
(send RUN-OUTPUT set-field-background end-col)))) (send RUN-OUTPUT set-field-background end-col))))
; update-state-gui: ProgState -> _
; Given a prog-state, updates the data tape inspector and the output window with
; the contents of that prog-state.
(define (update-state-gui ps)
(define out (prog-state-output ps))
; update data tape inspector
(make-tape-cells ps)
; set output and change color to ended
(send RUN-OUTPUT set-value out))
; Definition of the run button widget ; Definition of the run button widget
(define RUN-BTN (new button% [parent F] (define RUN-BTN (new button% [parent F]
[label "Run"] [label "Run"]
@ -218,7 +224,7 @@
(define INPUT-DONE-BTN (new button% (define INPUT-DONE-BTN (new button%
[parent input-panel] [parent input-panel]
[label "Confirm"] [label "Confirm"]
[callback (lambda (btn ev) (input-done-cb btn ev))])) [callback (lambda (b ev) (input-done-cb b ev))]))
; The run output text object ; The run output text object
(define RUN-OUTPUT (new text-field% (define RUN-OUTPUT (new text-field%

View file

@ -8,14 +8,8 @@
racket/struct racket/struct
rackunit) rackunit)
(provide prog-state (provide (struct-out prog-state)
prog-state? execute-instr
prog-state-tape
prog-state-tape-len
prog-state-dp
prog-state-output
prog-state-program
prog-state-ip
execute execute
string->program string->program
program->prog-state) program->prog-state)
@ -379,12 +373,12 @@
(lambda (done) (done 50)) (lambda (done) (done 50))
(lambda (w) (check-equal? w (prog-state (list 50) 0 1 "," "" 1 #f))))) (lambda (w) (check-equal? w (prog-state (list 50) 0 1 "," "" 1 #f)))))
; execute: ProgState (ProgState -> _) ((Byte -> _) -> _) -> _ ; execute-instr: ProgState (ProgState -> _) ((Byte -> _) -> _) -> _
; Given an initial ProgState state, ; Given an initial ProgState state, an async function to get input and a "done"
; calls done when the final ProgState has been ; callback, calls done with the ProgState updated after executing one
; computed by executing the program ; instruction.
(define (execute prog done get-input) (define (execute-instr prog done get-input)
; execute-sync: Char ProgState -> ProgState ; execute-sync: Char ProgState -> ProgState
; Given a synchronous instruction as a Char and the current ProgState, ; Given a synchronous instruction as a Char and the current ProgState,
; returns the new ProgState by executing the instruction. ; returns the new ProgState by executing the instruction.
@ -396,6 +390,20 @@
[(char=? inst #\[) (exec-loop-start w)] [(char=? inst #\[) (exec-loop-start w)]
[(char=? inst #\]) (exec-loop-end w)] [(char=? inst #\]) (exec-loop-end w)]
[(char=? inst #\.) (exec-out w)])) [(char=? inst #\.) (exec-out w)]))
; Fetch current instruction
(define inst (string-ref (prog-state-program prog) (prog-state-ip prog)))
; Execute asynchr. if ",", otherwise call callback with results of
; execute-sync
(cond [(char=? inst #\,) (exec-in prog get-input done)]
[else (done (execute-sync inst prog))]))
; execute: ProgState (ProgState -> _) ((Byte -> _) -> _) -> _
; Given an initial ProgState state,
; calls done when the final ProgState has been
; computed by executing the program
(define (execute prog done get-input)
; The program length in characters ; The program length in characters
(define program-len (string-length (prog-state-program prog))) (define program-len (string-length (prog-state-program prog)))
@ -406,13 +414,7 @@
; when the final ProgState is ready. ; when the final ProgState is ready.
(define (execute-help w) (define (execute-help w)
(cond [(>= (prog-state-ip w) program-len) (done w)] (cond [(>= (prog-state-ip w) program-len) (done w)]
[else [else (execute-instr w execute-help get-input)]))
; Fetch current instruction
(define inst (string-ref (prog-state-program w) (prog-state-ip w)))
(cond [(char=? inst #\,)
(exec-in w get-input
(lambda (ps) (execute ps done get-input)))]
[else (execute (execute-sync inst w) done get-input)])]))
(execute-help prog)) (execute-help prog))