Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro Variable Declaration #77

Open
agg23 opened this issue Oct 1, 2020 · 2 comments
Open

Macro Variable Declaration #77

agg23 opened this issue Oct 1, 2020 · 2 comments

Comments

@agg23
Copy link
Contributor

agg23 commented Oct 1, 2020

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.

@KarolS
Copy link
Owner

KarolS commented Oct 5, 2020

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
Copy link
Owner

KarolS commented Dec 15, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants