Spin to Basic
Dr_Acula
Posts: 5,484
Next crazy project - a Spin to Basic converter!
The aim here is to take an existing object and split up the Spin code and convert it to Basic or C. Given that the BCX Basic to C converter is now working so well, and given that Spin is possibly closer to Basic than to C, I am thinking of writing a program to convert Spin to Basic. Then you can run it through BCX and get Spin to C as well.
The PASM part is already working by extracting it and running it through Homespun.
So the aim is to convert Spin to Basic eg
1) find and replace. Convert "Repeat" to "For" or Do Loops.
2) Convert the indents into blocks of code by inserting things like "End If" and "End For" at the end of the block.
3) Maths. Convert == to = Convert := to = Some things in BCX can even stay the same, eg += *=
4) Convert some things through to Catalina C directly, eg Bytemove gets converted to existing Peek and Poke functions, ditto reading the counter, reading pins.
5) Variable declarations - PUB mypub | a,b,c becomes
Sub mypub
Dim a As Integer
Dim b As Integer
Dim c As Integer
That is the theory anyway!
Next question is what program to write this in. Ideally it would be something that can run on both windows and linux. Heater found Qt recently which looks great. However, for quick programming I'd prefer Basic, so I thought I'd check out Basic programs that can run on both platforms. The 'universal' language out there seems to be Java, and on my Apad there is a great development platform called Basic4Android that takes Basic code and converts it to Java. So I figured maybe there is something similar we could use here that takes Basic and uses the power and universality of java behind the scenes.
I have managed to find this program http://www.jabaco.org/download.html
I must say that this program is the fastest program I've ever used from download to a Hello World. It will take longer to describe this than to actually do it, and I was wondering if some kind soul with a linux box can check if it works on linux too:
1) Website http://www.jabaco.org/download.html
2) Put in email and click register
3) Click the download button. About a 6 meg file
4) Install it
5) Email will have arrived by now with the registration. Copy and paste the number
6) Start with "SDI application"
7) Put a button on the form
8) Put a label on the form (the "A")
9) Double click the button which goes to the code view
10) type in Label1.Caption="Hello World"
11) Click the green triangle "run" button at the top
12) Click the button and it will change the label to "Hello World"
I already have java on my computer, and I guess there is a chance one might need to get java?
If this works on linux, I am very tempted to take the Catalina IDE and move it over to jabaco.
Thoughts and feedback would be most appreciated.
The aim here is to take an existing object and split up the Spin code and convert it to Basic or C. Given that the BCX Basic to C converter is now working so well, and given that Spin is possibly closer to Basic than to C, I am thinking of writing a program to convert Spin to Basic. Then you can run it through BCX and get Spin to C as well.
The PASM part is already working by extracting it and running it through Homespun.
So the aim is to convert Spin to Basic eg
1) find and replace. Convert "Repeat" to "For" or Do Loops.
2) Convert the indents into blocks of code by inserting things like "End If" and "End For" at the end of the block.
3) Maths. Convert == to = Convert := to = Some things in BCX can even stay the same, eg += *=
4) Convert some things through to Catalina C directly, eg Bytemove gets converted to existing Peek and Poke functions, ditto reading the counter, reading pins.
5) Variable declarations - PUB mypub | a,b,c becomes
Sub mypub
Dim a As Integer
Dim b As Integer
Dim c As Integer
That is the theory anyway!
Next question is what program to write this in. Ideally it would be something that can run on both windows and linux. Heater found Qt recently which looks great. However, for quick programming I'd prefer Basic, so I thought I'd check out Basic programs that can run on both platforms. The 'universal' language out there seems to be Java, and on my Apad there is a great development platform called Basic4Android that takes Basic code and converts it to Java. So I figured maybe there is something similar we could use here that takes Basic and uses the power and universality of java behind the scenes.
I have managed to find this program http://www.jabaco.org/download.html
I must say that this program is the fastest program I've ever used from download to a Hello World. It will take longer to describe this than to actually do it, and I was wondering if some kind soul with a linux box can check if it works on linux too:
1) Website http://www.jabaco.org/download.html
2) Put in email and click register
3) Click the download button. About a 6 meg file
4) Install it
5) Email will have arrived by now with the registration. Copy and paste the number
6) Start with "SDI application"
7) Put a button on the form
8) Put a label on the form (the "A")
9) Double click the button which goes to the code view
10) type in Label1.Caption="Hello World"
11) Click the green triangle "run" button at the top
12) Click the button and it will change the label to "Hello World"
I already have java on my computer, and I guess there is a chance one might need to get java?
If this works on linux, I am very tempted to take the Catalina IDE and move it over to jabaco.
Thoughts and feedback would be most appreciated.
Comments
I've been playing around with a Spin to bytecode compiler for a few days. My approach is to generate an intermediate SPASM (SPin bytecode assembly) program, and then run a Spin assembler on it to produce the bytecodes. A similar approach could be taken to convert Spin to any language. That is, the front end would parse Spin code and generate an intermediate output. The final step would be done by converters that would translate the intermediate code into C, Basic, PASM or Spin bytecode. A special converter would be needed for each target language.
I'm writing my compiler in C because I am more proficient in it than other languages. I hope to post some code in a few days, but I may wait until it's a bit further along.
Dave
Can you give an example of what your intermediate code looks like? I can imagine that intermediate code might be a lot easier to parse than raw Spin.
The language you write this in may not matter. In the BCX converter, I do multiple passes (9 at present) and each pass takes an input file off the disk and produces an output file. So you can mix and match any language you like for doing the passes. So an IDE ends up running multiple console line programs - eg it does a 9 pass conversion, then runs BCX, then runs Homespun, then runs Catalina, then runs Payload.
Your approach makes a lot of sense. Do you have a bytecode listing by any chance?
The intermediate code is Spin assembly, which is not the best choice for a generic intermediate language. PropBasic would actually make a better intermediate language converting to other languages. Once I get the Spin compiler working it should be fairly easy to change the generated output to something else.
The Spin Hello World program is attached below along with the compiler/assembler listing.
Dave
I need to be able to read and write to arbitrary locations in hub mem.
As some know I use Prop Asm a lot more than SPIN, so in SPIN I have some difficulties.
Another thing I looked at was Gimple. This is the intermediate language generated by the GNU gcc compiler. In theory, we could convert any standard C program into Spin, Basic, PASM, etc. by writing a converter from Gimple to that language.
Dave
a = (b * (c+d))
and expand it out to a series of stack instructions.
Like you say, they ought to be rather similar for lots of different languages.
I would have thought intuitively that "Case" ought to be fairly easy in comparison. Just a series of "if" statements?
Is the spin compiler obfuscating this?
Gimple is a nice language for the intermediate stage. It does use temporary values instead of stack to hold results as they are computed. Normally Gimple is converted to a register transfer language (RTL) at the next step. I think it would be fairly easy to convert Gimple to PropBasic. It's not that hard to convert to a stack-based model either. Of course, the tricky part is to produce optimized code.