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 Rust-esque macros #59

Closed MysteryBlokHed closed 3 years ago

MysteryBlokHed commented 3 years ago

The text replacement definitions can be useful, but adding Rust-like macros could also be useful. If you want to make a pack that lets users define their own functions to add functionality but you don't want to make them copy+paste a bunch of boilerplate, then you could use a macro to do it for them. Something like this could work:

!macro on_event
!takes $event
!takes $name
!takes $run
func $name
tag tick
obj $name $event
execute as @e[scores={$name=1..}] run $run
endfunc
!end

The above could be used as something like:

?on_event minecraft.used:minecraft.carrot_on_a_stick item_used say Hello!

The macro call would be converted to the following Databind code:

func item_used
tag tick
obj item_used minecraft.used:minecraft.carrot_on_a_stick
execute as @e[scores={item_used=1..}] run say Hello!
endfunc

The syntax is just an idea, so there might be better ways to implement it. If this issue is closed, !def definitions should be removed.

MysteryBlokHed commented 3 years ago

I don't think the way I've gone about implementing this is this PR is the best way. I think I'll try to do something more typical to other languages, like this:

# a macro with no args
!def some_macro()
    say A macro with no args
!end

# a macro with one arg
!def another_macro($arg)
    say Hello, $arg!
!end

Support for string literal tokenization would also be needed, though I doubt I'll support escaping things like \n immediately. I'll probably support multiline strings by default, without really trying to.

Macro calls could look like this:

?some_macro()
?another_macro("World")
MysteryBlokHed commented 3 years ago

Another idea would be to add a specific kind of file that lets you define project-wide macros. That way people could make libraries for Databind that give you macros to help development. Something like a .macros or .m.databind file extension could work for this.