Skip to content

Support multi-digit macro arguments in angle brackets#844

Merged
ISSOtm merged 5 commits into
gbdev:masterfrom
Rangi42:paren-args
Apr 20, 2021
Merged

Support multi-digit macro arguments in angle brackets#844
ISSOtm merged 5 commits into
gbdev:masterfrom
Rangi42:paren-args

Conversation

@Rangi42

@Rangi42 Rangi42 commented Apr 19, 2021

Copy link
Copy Markdown
Contributor

This allows access to arguments past \9 without using shift.

@Rangi42 Rangi42 added enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM labels Apr 19, 2021
@Rangi42 Rangi42 requested a review from ISSOtm April 19, 2021 03:48
@Rangi42 Rangi42 mentioned this pull request Apr 19, 2021
@Rangi42 Rangi42 added this to the v0.5.1 milestone Apr 19, 2021
@Rangi42

Rangi42 commented Apr 19, 2021

Copy link
Copy Markdown
Contributor Author

There's no open issue for this feature but I remember it being suggested before. (Full expressions inside parentheses wouldn't be practical because that's parsing, not lexing.)

Edit: it was issue #460. At the time it was closed because of "the difficulty in implementing the original request, on top of a suitable workaround existing". However, readParentheticMacroArgNum is not a complex implementation, and the suggested workaround was to "expand \#, split on commas, and pick the Nth element", which is more cumbersome than the original approach using shift.

@aaaaaa123456789 's original reversedb example would be simplified to:

reversedb: MACRO
  for ___current, _NARG, 0, -1
    db \<___current>
  endr
ENDM

And pokecrystal's object_event would be:

object_event: MACRO
	db \3, \2 + 4, \1 + 4, \4
	dn \6, \5
	db \7, \8
	dn \9, \<10>
	db \<11>
	dw \<12>, \<13>
_NUM_OBJECT_EVENTS = _NUM_OBJECT_EVENTS + 1
ENDM

@Rangi42

Rangi42 commented Apr 19, 2021

Copy link
Copy Markdown
Contributor Author

This also removes the out-of-date documentation lines about macros having max 256 arguments.

Rangi42 added 3 commits April 19, 2021 17:01
This allows access to arguments past \9 without using 'shift'
For example, \(_NARG) will get the last argument
@ISSOtm

ISSOtm commented Apr 20, 2021

Copy link
Copy Markdown
Member

While I certainly don't have any problems with the code change (except maybe how many forward declarations are needed? :S), I'm less sure about the syntax change.

This syntax couldn't be \{42}, for example, because that's already an escape used to remove {'s special meaning. But then, giving \( special meaning means that we won't be able to give ( any special meaning. There's nothing wrong with the change per se, but I want us to be sure that we won't regret this decision later.

@Rangi42

Rangi42 commented Apr 20, 2021

Copy link
Copy Markdown
Contributor Author

Agreed about the forward declarations; I'll see if there's a rearrangement that reduces their overall count.

You're right about parentheses: #704 proposes keeping track of parentheses nesting in macro arguments and allowing commas inside them, so e.g. mac fn(1, 2) would pass fn(1, 2) as \1. To disable that the parentheses would have to be escapable, like mac fn\(1, 2).

Alternate syntaxes: brackets \[10], angle brackets \<10>, or various paired punctuation (\$10$, \^10^, \:10:, etc). (Not single quotes \'10', that would conflict with potentially escaping \' within '' for character literals or some kind of special string.) Brackets might be getting used for inline fragments and/or structs, and while neither of those uses currently demands a \[ escape, it's plausible that we'll end up with one. I don't think \< would ever be necessary though; it's just for the < and << operators. How about it?

@ISSOtm

ISSOtm commented Apr 20, 2021

Copy link
Copy Markdown
Member

\$ sounds like hex, so not that one; \^ looks weird imo. That seems to leave \: and \<, so I guess \<. It should be possible for us to be bitten regardless, but it seems unlikely, so the risk is imo acceptable.

`\(` is more likely to be a valid escape sequence in the
future (as is `\[`) and `\{` is already taken.
@Rangi42 Rangi42 changed the title Support multi-digit macro arguments in parentheses Support multi-digit macro arguments in angle brackets Apr 20, 2021
Comment thread src/asm/lexer.c
Comment thread src/asm/lexer.c Outdated
Identifiers, {interpolations} and \<macroArgs> are distinct
@Rangi42

Rangi42 commented Apr 20, 2021

Copy link
Copy Markdown
Contributor Author

(Angle brackets are kind of appropriate for being related to macro args. Some older assemblers used them to surround argument values that needed actual commas, e.g. mac <1,2>,3 would have \1 be 1,2 and \2 be 3.)

(Also, this assembler allowed 36 arguments, \0-\9 and then \A-\Z. Being case-sensitive, \N wouldn't even conflict with \n. :P Of course that's irrelevant now with \<36> and above.)

@Rangi42 Rangi42 requested a review from ISSOtm April 20, 2021 14:44
@ISSOtm ISSOtm merged commit c3e2721 into gbdev:master Apr 20, 2021
@Rangi42 Rangi42 deleted the paren-args branch April 20, 2021 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants