, works with dummy input (2)

This commit is contained in:
Claudio Maggioni 2018-12-04 10:28:34 +01:00
parent 70323a222f
commit 136548ad2e
3 changed files with 34 additions and 1217 deletions

File diff suppressed because it is too large Load Diff

22
gui.rkt
View File

@ -14,13 +14,15 @@
[width EDITOR-WIDTH]
[height EDITOR-HEIGHT]))
; execute-content: () -> ProgState
; Returns the last state of execution of the program loaded in the editor
(define (execute-content)
; execute-content: ((ProgState) -> Any) -> ProgState
; Given a callback, calls the callback with the last state of execution of the
; program loaded in the editor
(define (execute-content done)
(execute
(program->prog-state
(string->program
(send editor get-flattened-text)))))
(send editor get-flattened-text)))
done (lambda (callback) (callback 50))))
; run-program: Button ControlEvent -> Nothing
; Given a button and an event, runs the program currently loaded in the editor.
@ -30,11 +32,13 @@
(send run-output set-field-background run-col)
; run the program
(define out (prog-state-output (execute-content)))
; set output and change color to ended
(send run-output set-value out)
(send run-output set-field-background end-col))
(execute-content
(lambda (ps)
(define out (prog-state-output ps))
; set output and change color to ended
(send run-output set-value out)
(send run-output set-field-background end-col))))
; Definition of the run button widget
(define run-btn (new button% [parent f]

View File

@ -222,16 +222,6 @@
(check-expect (exec-out (prog-state (list 65) 0 1 "" ".[->+<]" 0))
(prog-state (list 65) 0 1 "A" ".[->+<]" 1))
; char-at: String Nat -> 1String
; Given a string and an index, returns the 1String at the position pointed by
; index
(define (char-at s i)
(substring s i (add1 i)))
; Tests for char-at
(check-expect (char-at "malusa" 2) "l")
(check-error (char-at "another string" 300))
; WalkingDirection can be one of:
; - 'forward
; - 'backward
@ -317,6 +307,11 @@
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 5))
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 1))
; insert-in-tape: DataTape Byte Nat -> DataTape
(define (insert-in-tape dt val n)
(cond [(zero? n) (cons val (rest dt))]
[else (cons (first dt) (insert-in-tape (rest dt) val (sub1 n)))]))
; exec-in: ProgState ((Byte -> _) -> _) (ProgState -> _) -> _
; Given a ProgState, a function that takes a callback function requiring a Byte
; and a function which takes the new ProgState, calls done with the input
@ -325,7 +320,7 @@
(define (exec-in w get-input done)
(define (got-input byte)
(done (prog-state
(insert-in-tape byte (prog-state-dp w))
(insert-in-tape (prog-state-tape w) byte (prog-state-dp w))
(prog-state-dp w)
(prog-state-tape-len w)
(prog-state-output w)
@ -333,23 +328,23 @@
(add1 (prog-state-ip w)))))
(get-input got-input))
; execute: ProgState ((ProgState) -> _) -> ProgState
; execute: ProgState ((ProgState) -> _) ((Byte -> _) -> _) -> ProgState
; Given an initial ProgState state, calls done when the final ProgState is ready
; to execute the program.
(define (execute w done)
(local [(define program-len (string-length (prog-state-program w)))]
(cond [(>= (prog-state-ip w) program-len) (done w)]
[else
(define inst (char-at (prog-state-program w) (prog-state-ip w)))
(cond [(string=? inst ",") (exec-in w (lambd]
[else
(execute (cond [(string=? inst "+") (exec-add1 w)]
[(string=? inst "-") (exec-sub1 w)]
[(string=? inst "<") (exec-tape-left w)]
[(string=? inst ">") (exec-tape-right w)]
[(string=? inst "[") (exec-loop-start w)]
[(string=? inst "]") (exec-loop-end w)]
[(string=? inst ".") (exec-out w)]) done)])])))
(define (execute w done get-input)
(define program-len (string-length (prog-state-program w)))
(cond [(>= (prog-state-ip w) program-len) (done w)]
[else
(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 (cond [(char=? inst #\+) (exec-add1 w)]
[(char=? inst #\-) (exec-sub1 w)]
[(char=? inst #\<) (exec-tape-left w)]
[(char=? inst #\>) (exec-tape-right w)]
[(char=? inst #\[) (exec-loop-start w)]
[(char=? inst #\]) (exec-loop-end w)]
[(char=? inst #\.) (exec-out w)]) done get-input)])]))
; Tests for execute
(check-expect (execute (prog-state (list 0) 0 3 "" "" 0))