Skip to content

Krate Tutorial

Joseph edited this page Feb 4, 2021 · 9 revisions

Krate is a self-contained way of writing arithmetic scripts in cfg without directly using scalu.

This framework provides two kinds of sequences

Assignment:

var1;is;var2;assignment_operator1;var3

Conditional:

if;var1;conditional_operator1;var2

Suppose I want to calculate 7 + 4 and store the result in a register called p, then print that variable to command line. Easy:

p;is;7;plus;4;print

This will print 11 to the console.

Other examples:

a;is;9;minus;3 //answer is 6 a;is;4;copy //copy 4 straight to register a d;is;1;or;0 //this is a bitwise operation f;is;3;and;17 c;is;7;not

These are cool, but they do not affect the "outside" world. This is done with conditional sequences.

Suppose I want to call my_alias if a is 3, otherwise call my_other_alias.

alias true "my_alias" alias false "my_other_alias" if;a;eq;3

This will execute "true" and thus "my_alias" if a is 3, otherwise execute "false" and "my_other_alias". "my_alias" is a conventional alias that can be defined however you like.

Other examples:

if;a;ne;7 //if a is not 7 if;a;gt;3 //if a is greater than 3 if;b;lt;d //if b is less than d if;4;gte;15 //if 4 is greater than or equal to 15 if;f;lte;16 //if f is less than or equal to 16

The great thing about krate is that it naturally composes with the established language. Suppose register b represents my current class, for instance. This can be aliased just how you imagine it would be.

Say we want to store the current class in register b:

alias current_class b

alias set_pyro_class "current_class;is;3;copy"

This can be further improved; aliasing a constant is sort of like declaring an enum, like in regular programming!

alias pyro_class 3

alias set_pyro_class "current_class;is;pyro_class;copy"

And that's all there is to know!

A side note about the print command; for code size reasons, it only prints the contents of register p. To print a, we can just write a command like this:

alias print_a "p;is;a;copy;print"

Limitations

In theory, krate's limitations are arbitrary; they mostly involve changing a couple variables in the Python generator script. If you want, you can compile a version of krate with 1000, 64-bit registers and build a very slow, inefficient computer. In practice, this is massively overkill for the extremely light computational needs of config scriptors. krate currently provides 16 registers (labeled a-p) that operate with 5-bit precision (numbers 0-32 can be represented). This seems to be a good middle ground for script writers, as they won't have to worry too much about running out of registers, but also won't have to worry too much about performance issues if they decide to write a complex script. This also keeps the krate config in a size I would deem reasonable

Build

krate.py is a python script which generates a scalu script which generates the output config; Python is only involved because scalu does not have a macro system or compile-time facilities to generate all the boilerplate needed to make compiling krate independently viable. After generation, you'll need to remove event prefixes added by scalu to avoid namespace collision (replace '$' with ''). You will also need to delete all instances of the string "delete"; this is because scalu does not allow naming an event with a number as the first character, so getting around this requires prefixing events with a string of letter characters you delete later (replace 'delete' with '').

Clone this wiki locally