'BF translation.bas for QB64 fork (B+=MGA) 2017-07-23
' BF in QB.bas for SmallBASIC 0.12.9 (B+=MGA) 2017-07-22
' I just translated some QB code from Rosetta to SmallBASIC
' and tested a couple of programs Hello World, Goodbye World
' count down also found at Rosetta Code
'======================================================================
' QB64 has excellent Help wiki, thanks to all who put that together!
'======================================================================
'?????????????????????????????????????????????????????????????????????????
' Why does this program hang when I change memory% or memory~% to
' memory& and Only With bf Zeppelin signature.txt file
'????????????????????????????????????????????????????????????????????????
_TITLE "Brainf***, Rosetta Code - Quick Basic translation to SB then to QB64 (fork)" 'for directory stuff
DIM memsize%
, ptr%
, i%
, ln%
, bktCnt%
, ff%
, ascNum%
DIM instChars$
, source$
, fileName$
, fLine$
, code$
, char$
, inLine$
, instruction$
, inChar$
, yes$
memsize% = 20000
instChars$ = "+-<>.,[]" 'valid characters
loadDirList "bf*.txt"
ptr% = 0 'memory pointer
source$ = ""
'==================================== file stuff just to access the BF "program"
PRINT "Sorry, no bf*.txt files found." PRINT:
INPUT "Enter line number of BF Filename you desire "; ln%
fileName$ = dirList$(ln%)
source$ = source$ + fLine$
PRINT "No source code to BF."
'================================================ a check that BF program is correct and brackets balanced
'let's clean the code up, check bracket balance
bktCnt% = 0
code$ = ""
char$
= MID$(source$
, i%
, 1) 'check to see if this is a valid instruction character
code$ = code$ + char$
'count brackets
IF char$
= "[" THEN bktCnt%
= bktCnt%
+ 1 IF char$
= "]" THEN bktCnt%
= bktCnt%
- 1 PRINT "Non BF character found (and ignored): "; char$
IF bktCnt%
THEN 'mismatched brackets 'clear last if any
' ============================================== here is meat and potatoes of translation
DIM memory&
(memsize%
) '<<<<<<<<<<<<< here is the array in question, at type % integer works but numbers are neg ' ====================================================== unsigned itegers ~works too
' ====================================================== so why not long & integers ????????
inLine$ = "" 'input buffer if a comma is used
FOR i%
= 1 TO LEN(code$
) 'loop through the code instruction$
= MID$(code$
, i%
, 1) 'get the instruction we're on memory&(ptr%) = memory&(ptr%) + 1
memory&(ptr%) = memory&(ptr%) - 1
IF memory&
(ptr%
) < 1 OR memory&
(ptr%
) > 255 THEN 'PRINT "?";
CASE "," ' <<<<< rarely used when playing with coded messages inChar$
= LEFT$(inLine$
, 1) 'take the first char off the buffer inLine$
= MID$(inLine$
, 2) 'delete it from the buffer memory&
(ptr%
) = ASC(inChar$
) 'use it ptr% = ptr% + 1
PRINT "Memory pointer out of range" ptr% = ptr% - 1
PRINT "Memory pointer out of range" bktCnt% = 1 'count the bracket we're on
i% = i% + 1 'move the code pointer to the next char
'count nested loops till we find the matching one
IF MID$(code$
, i%
, 1) = "]" THEN bktCnt%
= bktCnt%
- 1 IF MID$(code$
, i%
, 1) = "[" THEN bktCnt%
= bktCnt%
+ 1 i% = i% + 1 'search forward
bktCnt% = -1 'count the bracket we're on
i% = i% - 1 'move the code pointer back a char
'count nested loops till we fine the matching one
IF MID$(code$
, i%
, 1) = "]" THEN bktCnt%
= bktCnt%
- 1 IF MID$(code$
, i%
, 1) = "[" THEN bktCnt%
= bktCnt%
+ 1 i% = i% - 1 'search backwards
'======================================================================== end of translation
' modified function from Help files
' to get a bf program string from a file it is saved in
CONST TmpFile$
= "DIR$INF0.INF" IF NOT Ready%
THEN REDIM dirList$
(ListMAX%
): Ready%
= -1 'DIM array first use IF spec$
> "" THEN 'get file names when a spec is given Index%
= 0: dirList$
(Index%
) = "": ff%
= FREEFILE Index% = Index% + 1
DIRCount% = Index% 'SHARED variable can return the file count
ELSE IF Index%
> 0 THEN Index%
= Index%
- 1 'no spec sends next file name