Skip to content

Add Unit Lexer - #436

Open
Chadnaut wants to merge 1 commit into
masterfrom
Add-Unit-Lexer
Open

Add Unit Lexer#436
Chadnaut wants to merge 1 commit into
masterfrom
Add-Unit-Lexer

Conversation

@Chadnaut

Copy link
Copy Markdown
Collaborator
/** Unit method `npx` becomes `px(n)` */
px <- function(n) { return n * 10 }

class UserConfig { </ help=10px /> text = "string" }
local c = compilestring("return 20"+"px;")

fe.log(UserConfig.getattributes("text").help) // 100 (attr)
fe.log(c()) // 200 (compiled)
fe.log(30px) // 300 (int)
fe.log(0x28px) // 400 (hex)
fe.log(0.5px * 100) // 500 (float)
fe.log(0.6e+2px) // 600 (scientific)
fe.log(0106px) // 700 (octal)
fe.log("800px") // 800px (String unaffected)
fe.log(@"
""
\""
900px
") // Multiline string unaffected

@Chadnaut
Chadnaut requested a review from oomek August 24, 2026 11:57
@oomek

oomek commented Aug 24, 2026

Copy link
Copy Markdown
Owner

So how does it work? You'll have to define all the units in the script?

@Chadnaut

Copy link
Copy Markdown
Collaborator Author

Works with any function. For built-in globals just add them to fevm.

@oomek

oomek commented Aug 24, 2026

Copy link
Copy Markdown
Owner

That's a very clean patch. Cleaner than mine. So what's your plan regarding overridability of these functions?

@Chadnaut

Copy link
Copy Markdown
Collaborator Author

No plans, it's the same as everything else. If you name it the same it'll override the existing function.

@oomek

oomek commented Aug 24, 2026

Copy link
Copy Markdown
Owner

Hmm, this may break layouts without throwing any errors. People may not know that internally units are transcribed to function calls.

@oomek

oomek commented Aug 24, 2026

Copy link
Copy Markdown
Owner

So basically any function will be called when you for example 10on_tick ?

@Chadnaut

Copy link
Copy Markdown
Collaborator Author

Won't break any existing layouts since they won't be using the new units.
But it's the same issue for any redefined builtin.

Yes, any valid identifier suffixing any valid number will be called.

@oomek

oomek commented Aug 24, 2026

Copy link
Copy Markdown
Owner

Won't break any existing layouts since they won't be using the new units.
But it's the same issue for any redefined builtin.

Yeah I know, and I'm not sure if I like it.

Yes, any valid identifier suffixing any valid number will be called.

My implementation just throws an error, which is a good thing in my opinion.

@Chadnaut

Copy link
Copy Markdown
Collaborator Author

This implementation is generic and flexible. Ultimately the user has complete control.

Those who override native functions should know what they're doing, if not they're destined for trouble eventually. Overrides are essential to extend functionality, add effects, logging, etc.

The ability to create your own units is useful too. Columns, rows, padding, steps, etc. Units are just another shortcut.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants