millfork icon indicating copy to clipboard operation
millfork copied to clipboard

Macro Variable Declaration

Open agg23 opened this issue 3 years ago • 2 comments

Millfork does not support declaring new variables inside of macros. What is the reasoning behind that? Are there non-trivial issues with collisions that don't occur in "normal" scopes (I'm assuming the primary concern is the variable scope extending beyond the macro)?

Regardless of the answer here, can we get errors on variable declaration within macros? I have hit several scenarios where I forgot about variable declarations when converting a function to a macro, and using the newly declared variable results in the opaque error Exception in thread "main" millfork.env.UndefinedIdentifierException: Variable `variable` is not defined, which doesn't give a clear indication of where the usage exists.

agg23 avatar Oct 01 '20 17:10 agg23

Thanks for noticing, the nonhelpful error message is just an oversight.

There are two reasons why variable declarations don't work in macros:

  • variable declarations have to be at the top level of the function, macro invocations can appear almost anywhere

  • variable declarations are processed before macro expansion

I'm assuming the primary concern is the variable scope extending beyond the macro

For what is worth, labels in assembly code in macros already have their own unique scope, and I guess variables could be the same.

KarolS avatar Oct 05 '20 19:10 KarolS

Since 0.3.30, macros can declare constants, visible only within that macro's scope.

KarolS avatar Dec 15 '21 23:12 KarolS