updated world structure

added error-code function and fixed tests accordingly
This commit is contained in:
Tommaso Rodolfo Masera 2018-12-05 16:15:52 +01:00
parent 854949641c
commit 6edbb8bf76

View file

@ -41,6 +41,10 @@
; A InstructionPointer (IP) is a NonNegInt ; A InstructionPointer (IP) is a NonNegInt
; Interpretation: a pointer to the instruction to execute. ; Interpretation: a pointer to the instruction to execute.
; An ErrorCode is one of:
; - 'error1
; Interp: an error code for the bf interpreter.
; A ProgState is a (prog-state tape dp output program ip) where: ; A ProgState is a (prog-state tape dp output program ip) where:
; - tape: Tape ; - tape: Tape
; - dp: DataPointer ; - dp: DataPointer
@ -48,8 +52,9 @@
; - output: String ; - output: String
; - program: Program ; - program: Program
; - ip: InstructionPointer ; - ip: InstructionPointer
; - error: Option<ErrorCode>
; Interpretation: the current state of execution of a brainf*ck program. ; Interpretation: the current state of execution of a brainf*ck program.
(struct prog-state (tape dp tape-len output program ip) (struct prog-state (tape dp tape-len output program ip error)
#:transparent #:transparent
#:methods gen:custom-write #:methods gen:custom-write
[(define write-proc [(define write-proc
@ -60,7 +65,8 @@
(prog-state-tape-len w) (prog-state-tape-len w)
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(prog-state-ip w)))))]) (prog-state-ip w)
(prog-state-error w)))))])
; Template function for ProgState ; Template function for ProgState
#;(define (fn-for-prog-state w) #;(define (fn-for-prog-state w)
@ -69,7 +75,8 @@
#; (prog-state-tape-len w) #; (prog-state-tape-len w)
#; (prog-state-output w) #; (prog-state-output w)
#; (prog-state-program w) #; (prog-state-program w)
#; (prog-state-ip w))) #; (prog-state-ip w)
#; (prog-state-error w)))
; string->program: String -> Program ; string->program: String -> Program
; Given a string, returns a bf program without any invalid character ; Given a string, returns a bf program without any invalid character
@ -109,11 +116,11 @@
; program->prog-state: Program -> ProgState ; program->prog-state: Program -> ProgState
; Given a program, returns the corresponding initial ProgState state. ; Given a program, returns the corresponding initial ProgState state.
(define (program->prog-state p) (define (program->prog-state p)
(prog-state (cons 0 '()) 0 1 "" p 0)) (prog-state (cons 0 '()) 0 1 "" p 0 #f))
; Tests for program->prog-state ; Tests for program->prog-state
(check-equal? (check-equal?
(program->prog-state "[->+<]") (prog-state (list 0) 0 1 "" "[->+<]" 0)) (program->prog-state "[->+<]") (prog-state (list 0) 0 1 "" "[->+<]" 0 #f))
; tape-help: Tape DP (Byte -> Byte) -> Tape ; tape-help: Tape DP (Byte -> Byte) -> Tape
; Given a tape and a data pointer, returns the same tape with the data in the ; Given a tape and a data pointer, returns the same tape with the data in the
@ -126,6 +133,18 @@
(check-equal? (tape-help (list 0) 0 add1-byte) (list 1)) (check-equal? (tape-help (list 0) 0 add1-byte) (list 1))
(check-equal? (tape-help (list 0 1 2 3) 2 sub1-byte) (list 0 1 1 3)) (check-equal? (tape-help (list 0 1 2 3) 2 sub1-byte) (list 0 1 1 3))
; error-code: Option<Symbol> -> String
; Given an index symbol, returns the corresponding error.
(define (error-code sym)
(cond [(eq? sym #f) ""]
[(symbol=? sym 'error1)
"ERROR: '<' not working. Cannot access negative tape positions."]))
; Tests for error-code
(check-equal? (error-code #f) "")
(check-equal? (error-code 'error1)
"ERROR: '<' not working. Cannot access negative tape positions.")
; exec-add1: ProgState -> ProgState ; exec-add1: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the + instruction executed ; Given a ProgState, returns a new ProgState with the + instruction executed
(define (exec-add1 w) (define (exec-add1 w)
@ -134,15 +153,16 @@
(prog-state-tape-len w) (prog-state-tape-len w)
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (prog-state-ip w)))) (add1 (prog-state-ip w))
(prog-state-error w)))
; Tests for exec-add1 ; Tests for exec-add1
(check-equal? (exec-add1 (prog-state (check-equal? (exec-add1 (prog-state
(list 1 2 3 4 5 6 7) 3 7 "" "+" 0)) (list 1 2 3 4 5 6 7) 3 7 "" "+" 0 #f))
(prog-state (list 1 2 3 5 5 6 7) 3 7 "" "+" 1)) (prog-state (list 1 2 3 5 5 6 7) 3 7 "" "+" 1 #f))
(check-equal? (exec-add1 (prog-state (check-equal? (exec-add1 (prog-state
(list 255 1 2 3) 0 4 "" "+" 0)) (list 255 1 2 3) 0 4 "" "+" 0 #f))
(prog-state (list 0 1 2 3) 0 4 "" "+" 1)) (prog-state (list 0 1 2 3) 0 4 "" "+" 1 #f))
; exec-sub1: ProgState -> ProgState ; exec-sub1: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the - instruction executed ; Given a ProgState, returns a new ProgState with the - instruction executed
@ -152,32 +172,39 @@
(prog-state-tape-len w) (prog-state-tape-len w)
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (prog-state-ip w)))) (add1 (prog-state-ip w))
(prog-state-error w)))
; Tests for exec-sub1 ; Tests for exec-sub1
(check-equal? (exec-sub1 (prog-state (check-equal? (exec-sub1 (prog-state
(list 1 2 3 4 5 6 7) 3 7 "" "-" 0)) (list 1 2 3 4 5 6 7) 3 7 "" "-" 0 #f))
(prog-state (list 1 2 3 3 5 6 7) 3 7 "" "-" 1)) (prog-state (list 1 2 3 3 5 6 7) 3 7 "" "-" 1 #f))
(check-equal? (exec-sub1 (prog-state (check-equal? (exec-sub1 (prog-state
(list 0 1 2 3) 0 4 "" "-" 0)) (list 0 1 2 3) 0 4 "" "-" 0 #f))
(prog-state (list 255 1 2 3) 0 4 "" "-" 1)) (prog-state (list 255 1 2 3) 0 4 "" "-" 1 #f))
; exec-tape-left: ProgState -> ProgState ; exec-tape-left: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the < instruction executed ; Given a ProgState, returns a new ProgState with the < instruction executed
(define (exec-tape-left w) (define (exec-tape-left w)
(if (zero? (prog-state-dp w))
(error "Can't access negative tape positions")
(prog-state (prog-state-tape w) (prog-state (prog-state-tape w)
(sub1 (prog-state-dp w)) (sub1 (prog-state-dp w))
(prog-state-tape-len w) (prog-state-tape-len w)
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (prog-state-ip w))))) (add1 (prog-state-ip w))
(if (zero? (prog-state-dp w))
(error-code 'error1)
(prog-state-error w))))
; Tests for exec-tape-left ; Tests for exec-tape-left
;(check-exn exn:fail? (exec-tape-left (prog-state (list 1 2 3) 0 3 "" "<" 0))) ;(check-exn exn:fail? (exec-tape-left (prog-state (list 1 2 3) 0 3 "" "<" 0)))
(check-equal? (exec-tape-left (prog-state (list 1 2 3) 2 3 "" "<" 0)) (check-equal? (exec-tape-left (prog-state (list 1 2 3) 2 3 "" "<" 0 #f))
(prog-state (list 1 2 3) 1 3 "" "<" 1)) (prog-state (list 1 2 3) 1 3 "" "<" 1 #f))
(check-equal? (exec-tape-left (prog-state (list 1 2 3) 0 3 "" "<" 0 #f))
(prog-state
(list 1 2 3) -1 3 "" "<" 1
"ERROR: '<' not working. Cannot access negative tape positions.")
)
; exec-tape-right: ProgState -> ProgState ; exec-tape-right: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the > instruction executed ; Given a ProgState, returns a new ProgState with the > instruction executed
@ -194,13 +221,14 @@
(prog-state-tape-len w)) (prog-state-tape-len w))
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (prog-state-ip w))))) (add1 (prog-state-ip w))
(prog-state-error w))))
; Tests for exec-tape-right ; Tests for exec-tape-right
(check-equal? (exec-tape-right (prog-state (list 1 2 3) 0 3 "" ">" 0)) (check-equal? (exec-tape-right (prog-state (list 1 2 3) 0 3 "" ">" 0 #f))
(prog-state (list 1 2 3) 1 3 "" ">" 1)) (prog-state (list 1 2 3) 1 3 "" ">" 1 #f))
(check-equal? (exec-tape-right (prog-state (list 0 1 2) 2 3 "" ">" 0)) (check-equal? (exec-tape-right (prog-state (list 0 1 2) 2 3 "" ">" 0 #f))
(prog-state (list 0 1 2 0) 3 4 "" ">" 1)) (prog-state (list 0 1 2 0) 3 4 "" ">" 1 #f))
; exec-out: ProgState -> ProgState ; exec-out: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the . instruction executed ; Given a ProgState, returns a new ProgState with the . instruction executed
@ -214,13 +242,14 @@
(list->string (list->string
(list (integer->char (list-ref (prog-state-tape w) (prog-state-dp w)))))) (list (integer->char (list-ref (prog-state-tape w) (prog-state-dp w))))))
(prog-state-program w) (prog-state-program w)
(add1 (prog-state-ip w)))) (add1 (prog-state-ip w))
(prog-state-error w)))
; Tests for exec-out ; Tests for exec-out
(check-equal? (exec-out (prog-state (list 50) 0 1 "" ".[->+<]" 0)) (check-equal? (exec-out (prog-state (list 50) 0 1 "" ".[->+<]" 0 #f))
(prog-state (list 50) 0 1 "2" ".[->+<]" 1)) (prog-state (list 50) 0 1 "2" ".[->+<]" 1 #f))
(check-equal? (exec-out (prog-state (list 65) 0 1 "" ".[->+<]" 0)) (check-equal? (exec-out (prog-state (list 65) 0 1 "" ".[->+<]" 0 #f))
(prog-state (list 65) 0 1 "A" ".[->+<]" 1)) (prog-state (list 65) 0 1 "A" ".[->+<]" 1 #f))
; WalkingDirection can be one of: ; WalkingDirection can be one of:
; - 'forward ; - 'forward
@ -229,7 +258,7 @@
; find-matching: Program Nat WalkingDirection -> Nat ; find-matching: Program Nat WalkingDirection -> Nat
; Given a program, a starting position in the progam and a walking direction ; Given a program, a starting position in the progam and a walking direction
; returns the position of the matching bracket waking in the direction provided. ; returns the position of the matching bracket walking in the given direction.
(define (find-matching prg start wd) (define (find-matching prg start wd)
(define brkt (if (symbol=? wd 'forward) #\] #\[)) (define brkt (if (symbol=? wd 'forward) #\] #\[))
@ -253,7 +282,7 @@
(fm-helper (upd-start start) 0)) (fm-helper (upd-start start) 0))
; Tests for find-first ; Tests for find-matching
(check-equal? (find-matching "[++++++---->><-]++++]+--" 0 'forward) (check-equal? (find-matching "[++++++---->><-]++++]+--" 0 'forward)
15) 15)
(check-equal? (find-matching "[+++++][+---->><-]++++]+--" 7 'forward) (check-equal? (find-matching "[+++++][+---->><-]++++]+--" 7 'forward)
@ -273,15 +302,16 @@
(prog-state-program w) (prog-state-program w)
(add1 (if jump (add1 (if jump
(find-matching (prog-state-program w) (prog-state-ip w) 'forward) (find-matching (prog-state-program w) (prog-state-ip w) 'forward)
(prog-state-ip w))))) (prog-state-ip w)))
(prog-state-error w)))
; Tests for exec-loop-start ; Tests for exec-loop-start
(check-equal? (exec-loop-start (check-equal? (exec-loop-start
(prog-state '(0) 0 1 "" "[++--]++--+-[]" 0)) (prog-state '(0) 0 1 "" "[++--]++--+-[]" 0 #f))
(prog-state '(0) 0 1 "" "[++--]++--+-[]" 6)) (prog-state '(0) 0 1 "" "[++--]++--+-[]" 6 #f))
(check-equal? (exec-loop-start (check-equal? (exec-loop-start
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 0)) (prog-state '(1) 0 1 "" "[++--]++--+-[]" 0 #f))
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 1)) (prog-state '(1) 0 1 "" "[++--]++--+-[]" 1 #f))
; exec-loop-end: ProgState -> ProgState ; exec-loop-end: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the ] instruction executed ; Given a ProgState, returns a new ProgState with the ] instruction executed
@ -295,15 +325,16 @@
(prog-state-program w) (prog-state-program w)
(add1 (if jump (add1 (if jump
(find-matching (prog-state-program w) (prog-state-ip w) 'backward) (find-matching (prog-state-program w) (prog-state-ip w) 'backward)
(prog-state-ip w))))) (prog-state-ip w)))
(prog-state-error w)))
; Tests for exec-loop-end ; Tests for exec-loop-end
(check-equal? (exec-loop-end (check-equal? (exec-loop-end
(prog-state '(0) 0 1 "" "[++--]++--+-[]" 5)) (prog-state '(0) 0 1 "" "[++--]++--+-[]" 5 #f))
(prog-state '(0) 0 1 "" "[++--]++--+-[]" 6)) (prog-state '(0) 0 1 "" "[++--]++--+-[]" 6 #f))
(check-equal? (exec-loop-end (check-equal? (exec-loop-end
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 5)) (prog-state '(1) 0 1 "" "[++--]++--+-[]" 5 #f))
(prog-state '(1) 0 1 "" "[++--]++--+-[]" 1)) (prog-state '(1) 0 1 "" "[++--]++--+-[]" 1 #f))
; insert-in-tape: DataTape Byte Nat -> DataTape ; insert-in-tape: DataTape Byte Nat -> DataTape
; Given a datatape, a byte to insert and an index, returns a datatape with the ; Given a datatape, a byte to insert and an index, returns a datatape with the
@ -325,7 +356,8 @@
(define (exec-in w get-input done) (define (exec-in w get-input done)
; got-input: Byte -> _ ; got-input: Byte -> _
; Given a byte, updates the ProgState and calls `done` with the new ProgState ; Given a byte, updates the ProgState and calls `done`
; with the new ProgState.
(define (got-input byte) (define (got-input byte)
(done (prog-state (done (prog-state
(insert-in-tape (prog-state-tape w) byte (prog-state-dp w)) (insert-in-tape (prog-state-tape w) byte (prog-state-dp w))
@ -333,19 +365,21 @@
(prog-state-tape-len w) (prog-state-tape-len w)
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (prog-state-ip w))))) (add1 (prog-state-ip w))
(prog-state-error w))))
(get-input got-input)) (get-input got-input))
; Tests for get-input ; Tests for get-input
(test-begin (test-begin
(exec-in (exec-in
(prog-state (list 0) 0 1 "," "" 0) (prog-state (list 0) 0 1 "," "" 0 #f)
(lambda (done) (done 50)) (lambda (done) (done 50))
(lambda (w) (check-equal? w (prog-state (list 50) 0 1 "," "" 1))))) (lambda (w) (check-equal? w (prog-state (list 50) 0 1 "," "" 1 #f)))))
; execute: ProgState (ProgState -> _) ((Byte -> _) -> _) -> _ ; execute: ProgState (ProgState -> _) ((Byte -> _) -> _) -> _
; Given an initial ProgState state, calls done when the final ProgState has been ; Given an initial ProgState state,
; calls done when the final ProgState has been
; computed by executing the program ; computed by executing the program
(define (execute prog done get-input) (define (execute prog done get-input)
@ -365,7 +399,8 @@
(define program-len (string-length (prog-state-program prog))) (define program-len (string-length (prog-state-program prog)))
; execute-help: ProgState -> _ ; execute-help: ProgState -> _
; Helper function for `execute`. Given an initial ProgState state, calls done ; Helper function for `execute`.
; Given an initial ProgState state, calls done
; 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)]
@ -373,7 +408,8 @@
; Fetch current instruction ; Fetch current instruction
(define inst (string-ref (prog-state-program w) (prog-state-ip w))) (define inst (string-ref (prog-state-program w) (prog-state-ip w)))
(cond [(char=? inst #\,) (cond [(char=? inst #\,)
(exec-in w get-input (lambda (ps) (execute ps done get-input)))] (exec-in w get-input
(lambda (ps) (execute ps done get-input)))]
[else (execute (execute-sync inst w) done get-input)])])) [else (execute (execute-sync inst w) done get-input)])]))
(execute-help prog)) (execute-help prog))
@ -381,20 +417,20 @@
; Tests for execute ; Tests for execute
(test-begin (test-begin
(execute (execute
(prog-state (list 0) 0 3 "" "" 0) (prog-state (list 0) 0 3 "" "" 0 #f)
(lambda (w) (check-equal? w (prog-state (list 0) 0 3 "" "" 0))) (lambda (w) (check-equal? w (prog-state (list 0) 0 3 "" "" 0 #f)))
(lambda (done) (done 0)))) (lambda (done) (done 0))))
; assert that 5+2 to ASCII = "7" ; assert that 5+2 to ASCII = "7"
(test-begin (test-begin
(execute (execute
(prog-state (list 0) 0 1 "" "++>+++++[<+>-]++++++++[<++++++>-]<." 0) (prog-state (list 0) 0 1 "" "++>+++++[<+>-]++++++++[<++++++>-]<." 0 #f)
(lambda (w) (lambda (w)
(check-equal? w (prog-state (list 55 0) 0 2 "7" (check-equal? w (prog-state (list 55 0) 0 2 "7"
"++>+++++[<+>-]++++++++[<++++++>-]<." 35))) "++>+++++[<+>-]++++++++[<++++++>-]<." 35 #f)))
(lambda (done) (done 0)))) (lambda (done) (done 0))))
(test-begin (test-begin
(execute (execute
(prog-state (list 0) 0 1 "" ",." 0) (prog-state (list 0) 0 1 "" ",." 0 #f)
(lambda (w) (check-equal? w (prog-state (list 49) 0 1 "1" ",." 2))) (lambda (w) (check-equal? w (prog-state (list 49) 0 1 "1" ",." 2 #f)))
(lambda (done) (done 49)))) ; ASCII for "1" (lambda (done) (done 49)))) ; ASCII for "1"