Forums - Open Redstone Engineers
Binary to Autoprogram converter - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: Binary to Autoprogram converter (/thread-5843.html)



Binary to Autoprogram converter - Magic :^) - 02-19-2015

So, I've finished making the autoprogram templater. I'll comment it better and clean up the code later, but it works as intended right now so I'm uploading it.

TODO: add more error messages and warnings and stuff for template syntax errors
TODO: general cleanup. (more comments, less stupid code, remove references to nonexistent methods.)
TODO: support parsing hex files to support writing to hex rom.
TODO: add support for basic escape characters like \n
TODO: allow (0){ ... } declarations.
TODO: have a check for endless recursive template calling. e.g. check for stoopid definitions.

usage: binScripter.py (sourcefile) (templatefile) (outputfile)

basic syntax etc:

The syntax is simple, but you can be quite creative Wink

There are 3 main declaration types.

The var declaration defines what should go in the autoprogram file when it reaches a reference to variableName. If the next bit to write is 0, it will write "zero_block_id", and vice versa:
Code:
(~variableName){"zero_block_id" : "one_block_id"}

The template declaration acts similarly to a function definition. When templateName is called, its contents will be processed and then the parser will return back to where the template was called from:
Code:
(templateName){"abcdefg"}

This declaration is what is first executable. The number in brackets tells the program how many times this sequence should be repeated:
Code:
(9){"This text would repeat 9 times"}

Other things to note:
you can't declare a new template or var inside the body of another declaration. That has to be done at the top level of the templater file.
The program will warn you if your source binary is bigger than your templated rom's capacity.
The program terminates when it runs out of binary to parse. This is why it can handle recursive functions without going into an infinite loop ;3
Declarations can be made later in the file, but they are not known to loops that are executed before the declaration.

Here's the code:
(v1.5) https://github.com/MagicalGentleman/binScripter
(This is a python 3 program)


RE: Binary to Autoprogram converter - Phase - 02-24-2015

Thank you for using Python 3.

I was making a language in Python (https://github.com/Phasesaber/Flow), but I found Java to be easier with OOP.

http://pbin.in/SometimesILikeMen


RE: Binary to Autoprogram converter - Magic :^) - 02-26-2015

Code updated!

...

It's a bit easier to use now xD


RE: Binary to Autoprogram converter - Magic :^) - 03-09-2015

Ah it wasn't mentioned earlier, but there is some nicer commenting syntax now. You just use
Code:
(0){ ... }
and put your stuff in there. It also means you can really easily comment out sections of your template by replacing what's in the () brackets with 0.

Also, here's something you can include at the top of your file to make templating a bit easier:
Code:
(0){
    autoprog navigation syntax:
    (This was pulled from autoprog's source code)

        ',':
            x = 0
        ';':
            x = 0
            z = 0
        ' ':
            x += 1
        '$':
            x -= 1
        '<':
            z += 1
        '>':
            z -= 1
        '+':
            y += 1
        '-':
            y -= 1
}

(Right){">"}
(Left){"<"}
(Forwards){" "}
(Back){"$"}
(Up){"+"}
(Down){"-"}

I've also decided to use python's sys.setrecursionlimit() to prevent a template file from being trapped in an infinite recursive loop and outputting garbage. (If this happens, it is the template writer's fault for not iterating through the binary source while using a recursive call.)


RE: Binary to Autoprogram converter - Magic :^) - 03-12-2015

k so I'm going to push an update later that will stop this:
Code:
(destructiveRecursive){[destructiveRecursive]}
(1){[destructiveRecursive]}
from doing bad things.

added a limit so idiots don't try the above code to write infinitely to a text file.
you can change that by using -r or --recursivelimit, followed by a number. The default is 512 atm.
(The above is a command line arg btw)

I'll update once I find a nice way to handle the exception this throws.

EDIT: done.


RE: Binary to Autoprogram converter - Magic :^) - 03-12-2015

New feature added!

You can now reference a template instead of a string in the ~var type declarations.
for example:
Code:
(exampleTemplate){"this is basically called conditionally now.\n"}
(~exampleVariable){"string literal\n":[exampleTemplate]}

no variables referencing variables though. If you really needed to, you could always call a template which then referenced the variable from there.

EDIT: I may add support for vars referencing vars at some point though... The way I would do it would also clean my code up a bit. hmn...


RE: Binary to Autoprogram converter - Magic :^) - 04-05-2015

I have finally got an actual autoprogram conversion example Big Grin

I have taken the opportunity to make a short templating library for standard torch rom, so the actual part that describes your rom's properties looks like this:
Code:
(4){
    (16){
        (8){[bit]}
        [seekF]
        "\n" - this is here so the output looks nice.
    }
    [seekD]
    "\n" - this is here for same reason as above.
}



RE: Binary to Autoprogram converter - Nickster258 - 04-06-2015

Keep in mind: autoprogram was rewritten in java for the 1.8 update so some background stuff may change.


RE: Binary to Autoprogram converter - Magic :^) - 04-08-2015

yeah, the script parser is general purpose so I don't need to worry about that. I can use this for pretty much anything that needs repetitive work. It just happens that I started this project with autoprogram in mind Tongue

also I can abstract the keywords used by autoprogram with a library style of script writing so changes can be made pretty easily to existing scripts.