Skip to content

feat(expression): implement binary operation parser with support for …#103

Open
LiYulin-s wants to merge 1 commit intoHejsil:masterfrom
LiYulin-s:feat/binary-op
Open

feat(expression): implement binary operation parser with support for …#103
LiYulin-s wants to merge 1 commit intoHejsil:masterfrom
LiYulin-s:feat/binary-op

Conversation

@LiYulin-s
Copy link
Copy Markdown
Contributor

I wrote a table driven parser for expressions, introducing binaryOp, a utility for automatically generating expression parsers based on a table of operators, precedence, and associativity. It is heavily inspired by Parsec's buildExpressionParser.

Copy link
Copy Markdown
Owner

@Hejsil Hejsil left a comment

Choose a reason for hiding this comment

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

I have not thoroughly read through all the code, so my understanding is a bit limited, but what are the benefits here compared to the examples/math.zig?

@LiYulin-s
Copy link
Copy Markdown
Contributor Author

Compared to the math.zig example, this is designed as a reusable, strongly-typed parsing framework rather than a one-off grammar. It lets you define operator precedence and associativity explicitly, supports prefix/postfix/infix forms, and rigorously enforces operator signatures at compile time. While the math example is great for learning, a manual approach becomes harder to maintain and avoid left-recursion once a parser grows beyond basic arithmetic. This PR scales much better when adding new operators or semantics. :-)

@Hejsil
Copy link
Copy Markdown
Owner

Hejsil commented Mar 28, 2026

avoid left-recursion

Well, the math example doesn't avoid left recursion because the recursiveRef is able to handle that case. Anyways I'll see I'll need to have a proper look at this at some pointer later and think about it

@LiYulin-s
Copy link
Copy Markdown
Contributor Author

fine <3

@Hejsil
Copy link
Copy Markdown
Owner

Hejsil commented Mar 29, 2026

Alright, had a look around and I'm not really convinced this is the way to go for mecha. Sorry to let your hard work go to waste. I've creating and issue where the API can be discussed: #104

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