, 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

16
gui.rkt
View file

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

View file

@ -222,16 +222,6 @@
(check-expect (exec-out (prog-state (list 65) 0 1 "" ".[->+<]" 0)) (check-expect (exec-out (prog-state (list 65) 0 1 "" ".[->+<]" 0))
(prog-state (list 65) 0 1 "A" ".[->+<]" 1)) (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: ; WalkingDirection can be one of:
; - 'forward ; - 'forward
; - 'backward ; - 'backward
@ -317,6 +307,11 @@
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 5)) (prog-state '(1) 0 1 "" "[++--]++--+-[]" 5))
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 1)) (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 -> _) -> _ ; exec-in: ProgState ((Byte -> _) -> _) (ProgState -> _) -> _
; Given a ProgState, a function that takes a callback function requiring a Byte ; 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 ; 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 (exec-in w get-input done)
(define (got-input byte) (define (got-input byte)
(done (prog-state (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-dp w)
(prog-state-tape-len w) (prog-state-tape-len w)
(prog-state-output w) (prog-state-output w)
@ -333,23 +328,23 @@
(add1 (prog-state-ip w))))) (add1 (prog-state-ip w)))))
(get-input got-input)) (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 ; Given an initial ProgState state, calls done when the final ProgState is ready
; to execute the program. ; to execute the program.
(define (execute w done) (define (execute w done get-input)
(local [(define program-len (string-length (prog-state-program w)))] (define program-len (string-length (prog-state-program w)))
(cond [(>= (prog-state-ip w) program-len) (done w)] (cond [(>= (prog-state-ip w) program-len) (done w)]
[else [else
(define inst (char-at (prog-state-program w) (prog-state-ip w))) (define inst (string-ref (prog-state-program w) (prog-state-ip w)))
(cond [(string=? inst ",") (exec-in w (lambd] (cond [(char=? inst #\,)
[else (exec-in w get-input (lambda (ps) (execute ps done get-input)))]
(execute (cond [(string=? inst "+") (exec-add1 w)] [else (execute (cond [(char=? inst #\+) (exec-add1 w)]
[(string=? inst "-") (exec-sub1 w)] [(char=? inst #\-) (exec-sub1 w)]
[(string=? inst "<") (exec-tape-left w)] [(char=? inst #\<) (exec-tape-left w)]
[(string=? inst ">") (exec-tape-right w)] [(char=? inst #\>) (exec-tape-right w)]
[(string=? inst "[") (exec-loop-start w)] [(char=? inst #\[) (exec-loop-start w)]
[(string=? inst "]") (exec-loop-end w)] [(char=? inst #\]) (exec-loop-end w)]
[(string=? inst ".") (exec-out w)]) done)])]))) [(char=? inst #\.) (exec-out w)]) done get-input)])]))
; Tests for execute ; Tests for execute
(check-expect (execute (prog-state (list 0) 0 3 "" "" 0)) (check-expect (execute (prog-state (list 0) 0 3 "" "" 0))