MysteryBlokHed / databind

Expand the functionality of Minecraft Datapacks.
https://databind.rtfd.io/en/stable
GNU General Public License v3.0
4 stars 0 forks source link

Add macros to Databind #78

Closed MysteryBlokHed closed 3 years ago

MysteryBlokHed commented 3 years ago

Closes #59

Adds macros to Databind. Example definition:

!def macro_name($arg1, $arg2)
    say Hello, $arg1!
    say $arg2 says hi
!end

As seen above, macros can take a list of comma-separated arguments, each beginning with $. When used in the body of the macro, any instances of the argument names are replaced with whatever is passed from a macro call.

An example call would look like this:

?macro_name("World", "Databind")

Which would be replaced with the following:

say Hello, World!
say Databind says hi
MysteryBlokHed commented 3 years ago

Right now you need to have a space between the macro call and its arguments (eg. ?macro_name ("argument")) because of how I'm detecting macro calls, but that probably shouldn't be too hard to fix.

MysteryBlokHed commented 3 years ago

For some reason, it looks like there's a bug right now related to calling two macros in the same file. Even two calls in different functions don't work. This is probably caused if the variables used for calling macros not being fully reset at the end of a call.

MysteryBlokHed commented 3 years ago

Macro calls should now be able to accept any formatting. For example, all of the following calls would work:

!def my_macro($arg1)
    say $arg1
!end

?my_macro("Hello")
?my_macro ("Hello")
?my_macro
    ("Hello")
?my_macro(
    "Hello",
)
MysteryBlokHed commented 3 years ago

The documentation for macros in their current state is done. When the custom filetypes are added, a new section can be made in the macros docs page (docs/macros.rst)

MysteryBlokHed commented 3 years ago

I think that instead of adding a new filetype, I'll make files beginning with @ able to define global macros. Using a new filetype probably wouldn't have worked any way, since you're able to change compiled files in the databind.toml config. For example, a file named @command_macros.databind or @functions.databind would be able to define global macros.

These files will still be able to define functions, but it won't be their main purpose.

MysteryBlokHed commented 3 years ago

The commit's description should say #59, not #58. Luckily it's not super critical.