Several members here have made excellent interpreters in QB64 that run BAS code. I ported one of mine to QB64, and wanted to take it further and make it turn BAS code in standalone EXE's instead. Here's a tutorial on how I did mine. It's easier to just go through the motions than just explain, so this post will go through the steps of turning a small interpreter into a EXE producing compiler.
This tutorial will make a mini.exe compiler. It should be able to turn the demo.bas code into a standalone demo.exe program. Please note - this is not a true compiler, but more like a 'bytecode' one. The EXE's produced are merely a special interpreter with source coded binded to it - Like RapidQ does, and some other basic 'compilers out there. I've attached all the needed source files to this post at the bottom for easier downloading.
STEP #1) You are going to need the MarkExeSize tool I posted
here, and it's attached. Compile that to EXE. The interpreter and compiler we make here will need to be marked by it. You can read what that does on the MarkExeSize page.
STEP #2) Compile the sample interpreter.bas to EXE. This is just an example interpreter. The main thing is that it's made to open itself up when run, and load source code attached to itself, instead of loading an external BAS file. Think of this as the runtime file. But don't attach any BAS code to it yet, just compile it for now. You will have to adapt your interpreter to load code this way too.
interpreter.bas'Mini Interpreter runtime.
'A compiled EXE of this runs BAS code attached to it.
DIM Code$
(100) 'space for 100 lines
'==========================================================
'==========================================================
'Make sure something is attached to exe...
Lines = 1
Code$(Lines) = c$
Lines = Lines + 1
ExecuteLine Code$(t)
STEP #3) Compile the compiler.bas to EXE. This little programs whole job is to combine the interpreter+source code together. But - It will have the interpreter runtime attached to it eventually, like the interpreter has code attached to it. We will attach that later. For now just compile it...
compiler.bas'Mini Compiler example
PRINT "A Mini .BAS Compiler" PRINT "Compile .BAS to .EXE"
'First see if this EXE is marked...
'Grab EXE size info
'Make sure data attached...
'Jump to data
'Extract data, make EXE file...
PRINT #2, outdata$;: outdata$
= ""
'Add/attach BAS code to EXE
OPTIONAL STEP: At this point you could run
UPX on those EXE's to reduce their size down to about 500k. You will have to download UPX from off the internet. I use it a lot. Works well on QB64 generated EXE's. Make sure if you do this step, that you do it right here - BEFORE using MarkExeSize on them.
STEP #4) Now use the MarkExeSize tool on both the interpreter.exe and compiler.exe EXE's. It saves their EXE size in the EXE's. IMPORTANT: This is a needed step. Without it, the EXE's won't know how to open a file attached to them.
STEP #5) Now it's time to make the mini.exe compiler program. Drop to a command prompt, into the folder where the new EXE's are, and combine both the compiler.exe+interpreter.exe files like this, making a new file called mini.exe:
copy /b compiler.exe+interpreter.exe mini.exeIf all went well, You just made a new exe file called mini.exe. It's the whole compiler, that contains the interpreter runtime too. Run mini.exe, and you can now compile the demo.bas. It will generate a demo.exe out of it. The interpreter.exe & compiler.exe are no longer needed - mini.exe is the only thing needed to make the EXE files from BAS code.
demo.bas
PRINT "Hit any key to clear..."
Final comments: The example here is just a simple interpreter, just to show you how to do yours. Be aware that unless you encode/decode your source code on the interpreter, people will be able to open up your EXE and see the source code, so I would put in an encoding/decoding method in your interpreter.
Try building this sample first, and you will see how easy it is to turn your interpreter into a byte-code compiler using QB64. Start your own programming language.
Have fun!
- Dav