Skip to content

Commit

Permalink
Fixed a serious bug
Browse files Browse the repository at this point in the history
Thanks @rdebath
  • Loading branch information
JasonHK committed Dec 5, 2016
1 parent a611288 commit eec7615
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions Brainfuck.vb
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,73 @@
Dim CellPointer As UInteger = 0
Dim LoopDepth As Integer = 0
Dim LoopJump(1000) As Integer
Dim InitialComment As Boolean = False
Dim CommentLoop As Boolean = False
Dim CommentDepth As Integer = 0
Dim SkipDelay As Boolean = False

For index = 0 To CodeLength - 1
If Not ForceStop Then
Select Case Code.Chars(index)
Case ">"
If Not InitialComment Then
If Not CommentLoop Then
Try
CellPointer += 1
Catch ex As OverflowException
CellPointer = 29999
End Try
End If
Case "<"
If Not InitialComment Then
If Not CommentLoop Then
Try
CellPointer -= 1
Catch ex As OverflowException
CellPointer = 0
End Try
End If
Case "+"
If Not InitialComment Then
If Not CommentLoop Then
Try
Cell(CellPointer) += 1
Catch ex As OverflowException
Cell(CellPointer) = -128
End Try
End If
Case "-"
If Not InitialComment Then
If Not CommentLoop Then
Try
Cell(CellPointer) -= 1
Catch ex As OverflowException
Cell(CellPointer) = 127
End Try
End If
Case "."
If Not InitialComment Then MainForm.BrainfuckOutput(Cell(CellPointer))
If Not CommentLoop Then MainForm.BrainfuckOutput(Cell(CellPointer))
Case ","
If Not InitialComment Then Cell(CellPointer) = MainForm.BrainfuckInput()
If Not CommentLoop Then Cell(CellPointer) = MainForm.BrainfuckInput()
Case "["
If index = 0 Then InitialComment = True
LoopDepth += 1
LoopJump(LoopDepth) = index
If Cell(CellPointer) = 0 And Not CommentLoop Then
CommentLoop = True
CommentDepth += 1
ElseIf CommentLoop Then
CommentDepth += 1
Else
LoopDepth += 1
LoopJump(LoopDepth) = index
End If
Case "]"
If Cell(CellPointer) = 0 Then
If Cell(CellPointer) = 0 And Not CommentLoop Then
LoopDepth -= 1
Else
ElseIf Not CommentLoop Then
index = LoopJump(LoopDepth)
Else
CommentDepth -= 1
If CommentDepth = 0 Then CommentLoop = False
End If
If LoopDepth = 0 Then InitialComment = False
Case Else
SkipDelay = True
End Select

If Not SkipDelay And Not InitialComment Then
If Not SkipDelay And Not CommentLoop Then
MainForm.BrainfuckPointer(CellPointer, Cell(CellPointer))
Debug.WriteLine("Cell: " & CStr(CellPointer))
Debug.WriteLine("Value: " & CStr(Cell(CellPointer)))
Expand Down

0 comments on commit eec7615

Please sign in to comment.