Banks Problems

Blitz3D Forums/Blitz3D Beginners Area/Banks Problems

neos300(Posted 2009) [#1]
Alright, so i'm making a brainfuck interperter, and so i thought i would use banks for it. Now, for some odd reason my code has no effect every once in a while and crashes after few operations. Does anyone have any idead why this is?
My code:

;BF interperter
a$ = CommandLine$()
Global bytes = CreateBank(5000)
Global cmp = 1
Global cp = 1
Global dgo = 0
Global lp = 0
If a <> ""
f = ReadFile(a$)
While Not Eof(f)
j$ = ReadLine$(f)
bf(j$)
Wend
Else
.c
Print "Current memory pointer cell:" + cmp
Print "Current cell value:" + PeekByte(bytes, cmp)
Print "Next cell value:" + PeekByte(bytes, cmp + 1)
If cmp = 0
Else
Print "Previous cell value:" + PeekByte(bytes, cmp - 1)
EndIf
b$ = Input$("")
bf(b$)
Goto c
EndIf

Function bf(m$)
tl = Len(m$)
While Not cp = tl
h$ = Mid(m, cp, 1)
Select h$
Case ">"
If Not dgo = 1 Then cmp = cmp + 1
Case "<"
If Not dgo = 1 Then cmp = cmp - 1
If cmp <= 0
cmp = 0
EndIf
Case "+"
PokeByte bytes,cmp,PeekByte(bytes, cmp) + 1
Case "-"
PokeByte bytes,cmp,PeekByte(bytes, cmp) - 1
Case "."
Write Chr$(PeekByte(bytes,cmp))
Case ","
s = Input()
PokeByte bytes,cmp,s
Case "["
If Not PeekByte(bytes, cmp) = 0
lp = cp
Else
dgo = 1
EndIf
Case "]"
If Not PeekByte(bytes,cmp) = 0
cp = lp
Else
dgo = 0
EndIf

End Select
cp = cp + 1
Wend
End Function



_PJ_(Posted 2009) [#2]
Well for start it's a little confusing. As well as I'm noyt entirely sure whatthis program is for.

For example:
lines such as
If Not PeekByte(bytes,cmp) = 0

When I think you mean:
If (PeekByte(bytes,cmp))

which returns TRUE when PeekBytebyte(Byte,cmp) is anything else than 0

Also with

Not dgo = 1

This will ONLY return TRUE when dgo = 0
if dgo is ANYTHING EXCEPT 0, the result will be 0

I think it should be

If (dgo<>1)


Again,
Not cp = tl

would only return TRUE if
cp is ANYTHING EXCEPT 0
AND
tl = 0

OR

cp = 0
AND tl = 1

Usign parenthesis can help:

If (Not(cp = tl))


This way, the entire operand (cp=tl) is considered.




When using a boolean operator on an integer, you are reducing the integer to either 0 or 1 in the argument.
So for an integer, a% of ANYTHING EXCEPT 0:
Not a%
Would ALWAYS equal 0
Because a%<>0 is a TRUE result therefore a%=1 for the argument.


neos300(Posted 2009) [#3]
Alright, i'll fix up my code. (Also look at http://en.wikipedia.org/wiki/Brainfuck to see what this is for)

But that still doesnt really solve my problems.


_PJ_(Posted 2009) [#4]
Thanks for that link :) I'll have a better look over what you have and see if I can help some more now I know the idea behind it!


neos300(Posted 2009) [#5]
K, thanks