Let's say we have a piece of code, such as this, stored in "ammo1.txt":
. "ammo1.txt" inc "ammo" by 1
And this one in ammo5.txt:
. "ammo5.txt" inc "ammo" by 5
Let's say for a weird example that we want to give the player 6 ammo in a super-fancy way. What we have to do in order to string these two together (since we want both of them to execute in this example) goes like so:
- Open the temporary destination file
- For each file to be compiled
- Copy all bytes from source file to end of destination file
- Write a seperator to "pad" between the files
- Close the source file
- Copy all bytes from source file to end of destination file
- After all files are copied, close the destination file
- Load the destination file into the robot.
We get some code that looks like this:
set "temp.txt" to "FWRITE_OPEN" . "First file" set "ammo1.txt" to "FREAD_OPEN" goto "#link" . "Second file" set "ammo5.txt" to "FREAD_OPEN" goto "#link" set "" to "FWRITE_OPEN" set "temp.txt" to "LOAD_ROBOT" & "Error loading robot!" end . "link: Transfer the open reading file to the open writing file, followed by linebreak" : "#link" goto "#getLength" . "Transfer each byte by reading the file to a string, then writing the string to the file" set "$temp" "fread&length&" set "$temp" "fwrite&length&" . "Append \r\n, just in case." set "FWRITE" to 13 set "FWRITE" to 10 . "Close the file" set "" to "FREAD_OPEN" goto "#return" . "#getLength: Get the length of a file." : "#getLength" . "This will seek to the end of the file." set "FREAD_POS" to -1 set "length" to "FREAD_POS" . "Move back to beginning of file" set "FREAD_POS" to 0 goto "#return"
Why is \r\n appended after every file? Well, if the .txt file does not end with a line break, and the next file doesn't begin with a line break, the two lines will run together and MegaZeux will fail to load the .txt as a valid Robot, of course. Using \r\n, and not just one of those chars, will be fairly platform independent, as I'm quite sure that MZX will just convert or ignore if it needs to. I'm not entirely sure though. You may also want to append many linebreaks to make it very clear where the files start and end, if you want to open up the compiled txt (for debug purposes, etc).
Why not store the list of files as an array of strings (such as $file0, $file1, $file2, etc.)? Well of course you can do that! Just be careful with loopcount conflicts when you implement your own version. Use soft (label-goto) loops. In fact, you could even load that list of files from a text file, sort of like a makefile, that instructs the compiler what to compile and where.
Why would you ever need to use this? Well, good question. That depends heavily on your coding style. I like to chunk out my robotic files from thing to thing and often I find myself having a slew of robots doing menial tasks. By having this compiler do something like load all of the files as subroutines into a single robot, and then create a loop full of hooks to each of these files automatically, I can basically cram a bunch of stuff into one robot. Again, I don't expect many people to utilize this, but hey, that's OK because this is sort of geared towards projects that are super robot heavy; projects for people that basically want to code in a scopeless hell of a thing like Robotic instead of something else.
Why .txt? MZX is not used frequently enough to warrant a new de facto text format, and these files ARE just text. Besides, MZX natively imports non-bytecode Robots as .txt in its built-in save/load function in the Robot editor.
Why post this? Self-reference for later, maybe starting conversation on the forums, having a free hour in the evening.
Yer wrong, here's why! OK, let me know! I want to flesh out and/or abandon this concept to its logical conclusion.
This post has been edited by CJA: 25 September 2014 - 02:39 PM