From d2064f326c5901c7892e75bb390a0b6aae18101e Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 11 Dec 2018 14:07:01 +0100 Subject: [PATCH] Chores needed for stepper functionality --- gui.rkt | 18 ++++++++++++------ interpreter.rkt | 44 +++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/gui.rkt b/gui.rkt index 00d02e3..14dff48 100644 --- a/gui.rkt +++ b/gui.rkt @@ -64,13 +64,19 @@ ; run the program (execute-content (lambda (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) + (update-state-gui ps) (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 (define RUN-BTN (new button% [parent F] [label "Run"] @@ -218,7 +224,7 @@ (define INPUT-DONE-BTN (new button% [parent input-panel] [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 (define RUN-OUTPUT (new text-field% diff --git a/interpreter.rkt b/interpreter.rkt index 968cf15..28493e7 100644 --- a/interpreter.rkt +++ b/interpreter.rkt @@ -8,14 +8,8 @@ racket/struct rackunit) -(provide prog-state - prog-state? - prog-state-tape - prog-state-tape-len - prog-state-dp - prog-state-output - prog-state-program - prog-state-ip +(provide (struct-out prog-state) + execute-instr execute string->program program->prog-state) @@ -379,12 +373,12 @@ (lambda (done) (done 50)) (lambda (w) (check-equal? w (prog-state (list 50) 0 1 "," "" 1 #f))))) -; 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) - +; execute-instr: ProgState (ProgState -> _) ((Byte -> _) -> _) -> _ +; Given an initial ProgState state, an async function to get input and a "done" +; callback, calls done with the ProgState updated after executing one +; instruction. +(define (execute-instr prog done get-input) + ; execute-sync: Char ProgState -> ProgState ; Given a synchronous instruction as a Char and the current ProgState, ; returns the new ProgState by executing the instruction. @@ -396,6 +390,20 @@ [(char=? inst #\[) (exec-loop-start w)] [(char=? inst #\]) (exec-loop-end 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 (define program-len (string-length (prog-state-program prog))) @@ -406,13 +414,7 @@ ; when the final ProgState is ready. (define (execute-help w) (cond [(>= (prog-state-ip w) program-len) (done w)] - [else - ; 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)])])) + [else (execute-instr w execute-help get-input)])) (execute-help prog))