Tutorial - Turn a QB64 interpreter into a compiler - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Prolific Programmers (https://staging.qb64phoenix.com/forumdisplay.php?fid=26) +---- Forum: Dav (https://staging.qb64phoenix.com/forumdisplay.php?fid=34) +---- Thread: Tutorial - Turn a QB64 interpreter into a compiler (/showthread.php?tid=134) |
Tutorial - Turn a QB64 interpreter into a compiler - Dav - 04-23-2022 Tutorial: How to turn a QB64 interpreter into a compiler.
(WINDOWS ONLY!)
Several of our members 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 an compiler that turn BAS code in standalone EXE's. Here's a tutorial on how I did it. With this method you can make your own EXE producing compiler in QB64. It's easier to explain the method by just going through the steps of making one, so in this tutorial we will turn a small interpreter into a EXE producing compiler. 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 and other basic compilers out there do. The EXE's will read itself and run the attached code. I've attached all the needed source files to this post at the bottom for easier saving. So...Download all the attached BAS files before we begin. STEP #1) Compile the MarkExeSize.bas tool to an EXE first. The interpreter and compiler EXE's we make here will need to be marked by that tool. You can read what MarkExeSize does in its source code. (MarkExeBas.bas)
Code: (Select All) '=============== STEP #2) Compile the sample interpreter.bas to EXE. This is just an example interpreter. The main thing is that this interpreter is made to open itself up when run, and load source code attached to itself, instead of loading an external BAS file. Think of it as the runtime file. But don't attach any BAS code to it yet, just compile it for now. (When using your own interpreter you will need to adapt it to load code this way too). (interpreter.bas)
Code: (Select All) 'Mini Interpreter runtime. 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)
Code: (Select All) 'Mini Compiler example 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.exe tool on both the interpreter.exe and compiler.exe programs. 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.exe If 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 below. 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)
Code: (Select All) REM Sample program 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 markexesize.bas (Size: 2.64 KB / Downloads: 59) interpreter.bas (Size: 1.3 KB / Downloads: 58) compiler.bas (Size: 829 bytes / Downloads: 58) demo.bas (Size: 113 bytes / Downloads: 55) RE: Tutorial - Turn a QB64 interpreter into a compiler - bplus - 04-23-2022 Wow this will come in handy when I get my interpreter updated. Oh the projects on my ToDo List are many! Thanks Dav, love those games of yours as well! RE: Tutorial - Turn a QB64 interpreter into a compiler - Coolman - 04-29-2022 I am testing the basic interpreter yabasic. it allows like the excellent old RapidQ to combine the interpreter with the source code program converted into pseudocode to generate a standalone executable. the version of yabasic that I have compiled has a size of about 302 kb. which allows to generate light executables easily distributable... RE: Tutorial - Turn a QB64 interpreter into a compiler - Dav - 04-29-2022 It's been a long while since I've checked out yabasic. Looks like it has grown well. Gonna play with it some over the weekend. Thanks... - Dav |