fixed matching bracket bug

This commit is contained in:
Tommaso Rodolfo Masera 2018-12-03 16:54:41 +01:00
parent 12920f7ddb
commit 235653359c
2 changed files with 37 additions and 20 deletions

View file

@ -1,4 +1,4 @@
# easybf / DrBrainf\*ck # DrBrainf\*ck
This is the repo for the PF1 final project of _Claudio Maggioni_ and This is the repo for the PF1 final project of _Claudio Maggioni_ and
_Tommaso Rodolfo Masera_. _Tommaso Rodolfo Masera_.
@ -15,7 +15,5 @@ Run the file `gui.rkt` using *DrRacket* or the `racket` CLI tool.
## Bugs ## Bugs
- `[` and `]` are not brainf\*ck compliant since the interpreter does not
search for the matching bracket but it considers the nearest bracket;
- `,` function not implemented (will crash the program if encountered); - `,` function not implemented (will crash the program if encountered);
- No validation or user-friendly error handling. - No validation or user-friendly error handling.

View file

@ -230,23 +230,44 @@
(check-expect (char-at "malusa" 2) "l") (check-expect (char-at "malusa" 2) "l")
(check-error (char-at "another string" 300)) (check-error (char-at "another string" 300))
; find-first: 1String String Nat (Nat -> Nat) -> Nat ; WalkingDirection can be one of:
; Given a 1String `char`, a String `string` and a start index, returns ; - 'forward
; the position of the first occurrence of `char` in `string` starting from the ; - 'backward
; index walking the string changing the index using the `walk` function. ; Interpretation: the direction to walk the Brainf*ck Program with.
(define (find-first char string start walk)
(cond [(string=? (substring string start (add1 start)) ; find-matching: Program Nat WalkingDirection -> Nat
char) start] ; Given a program, a starting position in the progam and a walking direction
[else (find-first char string (walk start) walk)])) ; returns the position of the matching bracket waking in the direction provided.
(define (find-matching prg start wd)
(define brkt (if (symbol=? wd 'forward) #\] #\[))
(define oppos-brkt (if (symbol=? wd 'forward) #\[ #\]))
(define upd-start (if (symbol=? wd 'forward) add1 sub1))
; fm-helper Nat Nat -> Nat
; Given a starting position and a nest accumulator, returns the position of
; the matching bracket waking in the direction provided by updating the
; accumulator recursively with the current level of nesting.
(define (fm-helper s nest)
(cond
[(and (zero? nest) (char=? (string-ref prg s) brkt))
s]
[(char=? (string-ref prg s) oppos-brkt)
(fm-helper (upd-start s) (add1 nest))]
[(char=? (string-ref prg s) brkt)
(fm-helper (upd-start s) (sub1 nest))]
[else
(fm-helper (upd-start s) nest)]))
(fm-helper (upd-start start) 0))
; Tests for find-first ; Tests for find-first
(check-expect (find-first "]" "[++++++---->><-]++++]+--" 0 add1) (check-expect (find-matching "[++++++---->><-]++++]+--" 0 'forward)
15) 15)
(check-expect (find-first "]" "[+++++][+---->><-]++++]+--" 7 add1) (check-expect (find-matching "[+++++][+---->><-]++++]+--" 7 'forward)
17) 17)
(check-expect (find-first "[" "[+++++][+---->><-]++++]+--" 17 sub1) (check-expect (find-matching "[+++++[]+---->><-]++++]+--" 17 'backward)
7) 0)
; exec-loop-start: ProgState -> ProgState ; exec-loop-start: ProgState -> ProgState
; Given a ProgState, returns a new ProgState with the [ instruction executed ; Given a ProgState, returns a new ProgState with the [ instruction executed
@ -259,7 +280,7 @@
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (if jump (add1 (if jump
(find-first "]" (prog-state-program w) (prog-state-ip w) add1) (find-matching (prog-state-program w) (prog-state-ip w) 'forward)
(prog-state-ip w)))))) (prog-state-ip w))))))
; Tests for exec-loop-start ; Tests for exec-loop-start
@ -281,7 +302,7 @@
(prog-state-output w) (prog-state-output w)
(prog-state-program w) (prog-state-program w)
(add1 (if jump (add1 (if jump
(find-first "[" (prog-state-program w) (prog-state-ip w) sub1) (find-matching (prog-state-program w) (prog-state-ip w) 'backward)
(prog-state-ip w)))))) (prog-state-ip w))))))
; Tests for exec-loop-end ; Tests for exec-loop-end
@ -320,5 +341,3 @@
"++>+++++[<+>-]++++++++[<++++++>-]<." 35)) "++>+++++[<+>-]++++++++[<++++++>-]<." 35))
; Run tests ; Run tests
(test) (test)
; TODO: fix matching bracket bug