Skip to content

Use templates to reduce the redundant number-lexing functions#1963

Open
Rangi42 wants to merge 5 commits into
gbdev:masterfrom
Rangi42:parse-number-templates
Open

Use templates to reduce the redundant number-lexing functions#1963
Rangi42 wants to merge 5 commits into
gbdev:masterfrom
Rangi42:parse-number-templates

Conversation

@Rangi42
Copy link
Copy Markdown
Contributor

@Rangi42 Rangi42 commented Apr 29, 2026

I've always been irritated by how we have so many of essentially the same function. Decimal numbers are still an odd one out because they don't have a prefix, but that was still tractable.

Ideally we'd be able to unify the rgbasm and rgblink functions, but their logic for how to read characters and handle errors is just too different. Maybe it could still be templated, but that would be too little gain for too much cost of extra abstraction layers.

This doesn't unify handling of fixed-point literals or graphics constants, because even though those are still "numeric literals" with "underscore digit separator handling", they're still too different to share logic. (Fixed-point literals have their whole fractional part, and graphics constants have two bitplanes and an 8-digit limit.)

Note that linkerscripts still have more lax parsing than assembly -- they don't enforce any rules about how '_' digit separators can be placed, and they don't warn for overflow (because rgblink has no -Wlarge-constant). I'll deal with that separately. (It's going to be tricky, because so far we only output warnings regarding object files, so all the warning-location-stack code assumes traversable FileStackNodes, which don't exist for included linkerscripts. Whee~)

@Rangi42 Rangi42 added this to the 1.0.2 milestone Apr 29, 2026
@Rangi42 Rangi42 requested a review from ISSOtm April 29, 2026 11:58
@Rangi42 Rangi42 added rgbasm This affects RGBASM rgblink This affects RGBLINK refactoring This PR is intended to clean up code more than change functionality labels Apr 29, 2026
@Rangi42
Copy link
Copy Markdown
Contributor Author

Rangi42 commented Apr 30, 2026

Here's an example of how the templates get compiled: https://godbolt.org/z/34M3Gf5Tv

Copy link
Copy Markdown
Member

@ISSOtm ISSOtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad idea!

Comment thread src/asm/lexer.cpp Outdated
Comment thread src/asm/lexer.cpp Outdated
Comment thread src/asm/lexer.cpp Outdated
Comment thread src/asm/lexer.cpp Outdated
Comment thread src/asm/lexer.cpp Outdated
@Rangi42 Rangi42 requested a review from ISSOtm May 1, 2026 11:34
Comment thread src/asm/lexer.cpp Outdated
@Rangi42 Rangi42 requested a review from ISSOtm May 11, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring This PR is intended to clean up code more than change functionality rgbasm This affects RGBASM rgblink This affects RGBLINK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants