Code Conversion Tools (March 28, 2013)
Programming has enough little annoying problems as it is, let alone trying to move code around between languages. Bizarrely the problem hasn't really improved that much since when I started programming back in 1982, at least then there was a good reason, like.. you know... assembly

- Today, there's lots of conversion tools between main stream languages, but there's just so many languages out there, lots and lots of legacy code just ends up sitting around doing nothing.
I dunno about anybody else, but I try to hang onto my legacy source codes in one big retro collection. The oldest stuff dates back to the mid 90's (don't really have any my 8bit stuff ;( ).. Anyway, it's mostly AMOS and 68k Assembly from the Amiga days. The asm isn't much use, but a lot of legacy BASIC code can be. The trouble with languages like Amos, is that the source code files are tokenized, so you either need AMOS to view them, or a conversion tool.
Converting AMOS to PlayBASIC
Those with a long memories might recall I've had a few tinkers with writing translation programs over the years, even getting a 'mock up' translator to produce actually working conversion of some simple AMOS programs (and a few other dialects) programs. While the Amos convertor worked (to a point) it's basically a brute force solution, since the translator is working in string level making it potentially very hit and miss.
Normally when parsing code, you'd translate the text stream into a tokenized state, then decipher what's going on from the tokens. But for AMOS programs, it makes sense to load the tokenized source code and decode that directly. I'd looked at doing this before and got a fair way into it, only to find a tool that already did most of it. Today though, i've dragged out my old source and cut'n'pasted a new project from it's ashes. Only took a few hours to get it a reasonable translation of a real world binary Amos program.
What you have to remember though, is that AMOS is huge language and I have
absolutely NO intention of building a one stop shop convertor. Rather, the objective is make the tool able to spit out a reasonable translation that a programmer can then use as a basis for the PlayBASIC version. Meaning the program LOGIC should be in tact, but the of course there's no easy way of mapping the AMOS command set to PlayBASIC without writing an abstraction layer.. Which is possible for simple programs (since that's how the previous test worked) but pretty unrealistic for medium to large AMOS programs.
What's Working?
The loader supports about 90% of low level primary token types that AMOS uses, what's missing is the command set declarations. Basically each command name has a numeric token, in some situations that have two or more variations. When you combine that with programs can use external command sets that plug into it, then the dark clouds of doom are soon circling over head. To combat this, i've make declaration tables external.. So yes, [red]you[/red] can improve the quality of the translation by working out what token is what...
At this point the program is basically just spiting out a text file that represents the original AMOS source code (it only knows about 50 commands). While I'll probably leave that in, What I want to do is post process this into something that's more PlayBASIC Friendly.
Catch Up With Work In Progress Blog
There's number of posts in the WIP subject on UnderwareDesign.com that i've skipped here. You can read them bellow.
AMOS BASIC Code Conversionp
Amos To PlayBASIC - Operator Remapping (April 6th,2013)
Each time i fire it up, the solution keeps improving, and only really big stumbling block has been the expression order remapping stuff, but that's now up and running. So far, it's only used when dealing with the ROL/ROR operators. Which present another little challenge, since PlayBASIC has these are functions, so the parameters need remapping completely into expressions to get something meaningful in the output. But it all seems to eb fitting together nicely for once. The remapping pass leaves us with the tokens run that represent each parameter, so just needed to insert the requires param back into the expression in front of the current pointer..
Which just means that when it sees AMOS code like this
Ror.l 1, Variable
we get this fragment back..
Variable=Ror32(Variable,1)
Provided the parameters make sense, the output should make sense.. should, but it's probably possible they won't either
Sample Conversion
Bellow we have today's translation of the standard AMOS sample code. What's missing is mapping of the String function to native versions. But you can wrap (make functions in PB that do what the Amos version does), which works fine..
` --------------------------------------------------------
` This test program for AMOS TO PlayBASIC conversion
` --------------------------------------------------------
stuff =5
DIM stuff(100 ) , cool(50 ) , testarray(10 , 10 +f ) , cool2(1 , 2 , 3 )
PRINT "Hello World"
Add index , 5
Add stuff(10 ) , 10 : Add cool2(1 , 2 , 3 ) , 12345
Add index , 1 : Inc index
Dec index
Dec cool2(1 , 2 , 3 ) : Inc cool2(1 , 2 , 3 ) : Add helloworld , Cos(45 )
Inc index
Inc stuff(10 )
Inc stuff(10 )
PRINT stuff(10 )
a# =10
b# =2.3
PRINT "casting integer"
PRINT a *b#
test2[100 , 100 ] : test2[20 , 20 ]
test3[10 , 100 , "1000" ]
testroutine
testroutine : testroutine
PRINT Param
a =10
b =5
WHILE a >b
PRINT a
Dec a
WEND
IF a =b THEN PRINT "A=B"
IF a =b
PRINT "A ='s B"
End If
IF a <>b
PRINT "option 1"
Else If a =b
PRINT "option 2"
End If
Wait Key
Procedure TESTROUTINE
` Shared STUFF()
` Print STUFF(10)
End Proc
Procedure TEST2[a , b ]
PRINT "2 input params"
End Proc
Procedure TEST3[a , b# , c$ ]
result =a +b# +Val(c$ )
End Proc[result ]
s$ ="Hello World"
number$ ="666"
PRINT s$
PRINT Upper$(s$ )
PRINT Lower$(s$ )
PRINT Left$(s$ , 5 )
PRINT Right$(s$ , 5 )
PRINT Str$(12345 )+s$
PRINT Val(number$ )
PRINT String$(s$ , 5 )
PRINT Space$(10 )+s$
PRINT Flip$(s$ )
PRINT Repeat$(s$ , 5 )
PRINT Instr(s$ , "World" )
PRINT Instr(s$ , "World" , 2 )
PRINT String$("" , 10 )+"nothing"
a =255
DIM memory(10 )
Ror.b(Cos(100 )+Sin(45 )) , ggggg
Ror.l cool +45 +Cos(450 ) , a
Ror.l 5 , a
b =123456
Ror.w a , c : b =22222
Ror.b $0000FFFF , c
Rol.b 3 , c
PRINT Bin$(a )
Wait Key
Becomes this in PlayBASIC,
` --------------------------------------------------------
` This test program for AMOS TO PlayBASIC conversion
` --------------------------------------------------------
stuff =5
DIM stuff(100 ) : DIM cool(50 ) : DIM testarray(10 , 10 +f ) : DIM cool2(1 , 2 , 3 )
PRINT "Hello World"
index +=5
stuff(10 )+=10 : cool2(1 , 2 , 3 )+=12345
index +=1 : index ++
index --
cool2(1 , 2 , 3 )-- : cool2(1 , 2 , 3 )++ : helloworld +=Cos(45 )
index ++
stuff(10 )++
stuff(10 )++
PRINT stuff(10 )
a# =10
b# =2.3
PRINT "casting integer"
PRINT a *b#
test2(100 , 100 ) : test2(20 , 20 )
test3(10 , 100 , "1000" )
testroutine()
testroutine() : testroutine()
PRINT Param
a =10
b =5
WHILE a >b
PRINT a
a --
EndWhile
IF a =b THEN PRINT "A=B"
IF a =b
PRINT "A ='s B"
EndIf
IF a <>b
PRINT "option 1"
ElseIf a =b
PRINT "option 2"
EndIf
WaitKey
Function TESTROUTINE()
` Shared STUFF()
` Print STUFF(10)
EndFunction
Function TEST2(a , b )
PRINT "2 input params"
EndFunction
Function TEST3(a , b# , c$ )
result =a +b# +Val(c$ )
_EXITFUNCTIONHERE: Param =(result )
EndFunction
s$ ="Hello World"
number$ ="666"
PRINT s$
PRINT Upper$(s$ )
PRINT Lower$(s$ )
PRINT Left$(s$ , 5 )
PRINT Right$(s$ , 5 )
PRINT Str$(12345 )+s$
PRINT Val(number$ )
PRINT String$(s$ , 5 )
PRINT Space$(10 )+s$
PRINT Flip$(s$ )
PRINT Repeat$(s$ , 5 )
PRINT Instr(s$ , "World" )
PRINT Instr(s$ , "World" , 2 )
PRINT String$("" , 10 )+"nothing"
a =255
DIM memory(10 )
ggggg = ROR8(ggggg , (Cos(100 )+Sin(45 )))
a = ROR32(a , cool +45 +Cos(450 ))
a = ROR32(a , 5 )
b =123456
c = ROR16(c , a ) : b =22222
c = ROR8(c , $0000FFFF )
c = ROL8(c , 3 )
PRINT Bin$(a )
WaitKey