Skip to main content
Notice removed Draw attention by C5H8NNaO4
Bounty Ended with Emma Marcier's answer chosen by C5H8NNaO4
Update space between lines, capitalize acronyms
Source Link

Feedback to increase code quality would be highly appreciated.
I'd

I'd also like to know if I'm missing crucial features a lexer/parser would have to include. If I'm doing anything inherently wrong or do use inappropriate techniques, that would be helpful to know as well.

If I'm doing anything inherently wrong or do use inappropriate techniques, that would be helpful to know as well.

You can flatten the recursive structure of the AST by changing the grammar of addition and multiplication tokens to repeatedly parse its rhsRHS while its condition matches by using .repeat, or by using .unfold which recurses first and flattens the structure after parsing the node. This can reduce the size of the AST a lot.

Feedback to increase code quality would be highly appreciated.
I'd also like to know if I'm missing crucial features a lexer/parser would have to include. If I'm doing anything inherently wrong or do use inappropriate techniques, that would be helpful to know as well.

You can flatten the recursive structure of the AST by changing the grammar of addition and multiplication tokens to repeatedly parse its rhs while its condition matches by using .repeat, or by using .unfold which recurses first and flattens the structure after parsing the node. This can reduce the size of the AST a lot.

Feedback to increase code quality would be highly appreciated.

I'd also like to know if I'm missing crucial features a lexer/parser would have to include.

If I'm doing anything inherently wrong or do use inappropriate techniques, that would be helpful to know as well.

You can flatten the recursive structure of the AST by changing the grammar of addition and multiplication tokens to repeatedly parse its RHS while its condition matches by using .repeat, or by using .unfold which recurses first and flattens the structure after parsing the node. This can reduce the size of the AST a lot.

added 1184 characters in body
Source Link
C5H8NNaO4
  • 201
  • 1
  • 9

Edit:

A few thoughts from my side. I don't really like prefixing methods or properties with a _. I think I can move the regular expression into an own object as they aren't tied to the instance. I think I can get rid of the _transform method by overriding transform in the constructor. I just thought that storing a function in a property that gets called by a class method is convenient since you could use it to validate input. If there's a cleaner way of doing this, that would be nice. I could use a Map store the function, then I wouldn't have to expose a _transform property.

I think that the binding powers should be changed to compare the current token against the next token. Currently, they work as follows. Given the source 1 + 2 * 3 and binding powers 50/50, 60/60 for the tokens + and *, the + token will compete with the * token over the 2 token. I thought that's easier to grasp, but it turns out that you can't use it to break out of the current parsing step, without using until. Which is a likely need. f.e. using ) to designate the end of an expression. This only works if I compare the binding powers of two adjacent tokens.


Edit:

A few thoughts from my side. I don't really like prefixing methods or properties with a _. I think I can move the regular expression into an own object as they aren't tied to the instance. I think I can get rid of the _transform method by overriding transform in the constructor. I just thought that storing a function in a property that gets called by a class method is convenient since you could use it to validate input. If there's a cleaner way of doing this, that would be nice. I could use a Map store the function, then I wouldn't have to expose a _transform property.

I think that the binding powers should be changed to compare the current token against the next token. Currently, they work as follows. Given the source 1 + 2 * 3 and binding powers 50/50, 60/60 for the tokens + and *, the + token will compete with the * token over the 2 token. I thought that's easier to grasp, but it turns out that you can't use it to break out of the current parsing step, without using until. Which is a likely need. f.e. using ) to designate the end of an expression. This only works if I compare the binding powers of two adjacent tokens.

Tweeted twitter.com/StackCodeReview/status/1321829172227002369
edited tags
Link
Dan Oberlam
  • 8k
  • 2
  • 33
  • 74
Notice added Draw attention by C5H8NNaO4
Bounty Started worth 50 reputation by C5H8NNaO4
Source Link
C5H8NNaO4
  • 201
  • 1
  • 9
Loading