Skip to content

modifing binding power of the unary "plus" and "dash"#3

Open
table-harmony wants to merge 1 commit intotlaceby:mainfrom
table-harmony:main
Open

modifing binding power of the unary "plus" and "dash"#3
table-harmony wants to merge 1 commit intotlaceby:mainfrom
table-harmony:main

Conversation

@table-harmony
Copy link
Copy Markdown

Sense the binding power of the prefix "dash" was unary, it was above the binding power of multiplicative.
Therefore it would have higher precedence: 10 * 10 - 10, for this case the dash would be parsed as a unary and not the correct additive.
image
As you can see the binary expression is parsed as a left for 10 and a right for the binary expression 10 - 10 which is wrong.
That is way in this commit I have changed the binding power of the token "dash" and added the token "plus" as prefix operations such as +10, -10 and modified their binding power to additive sense a "dash" prefix is identical to 0 - 10 and therefore should be parsed with the binding power of additive


// Unary/Prefix
nud(lexer.TYPEOF, unary, parse_prefix_expr)
nud(lexer.DASH, unary, parse_prefix_expr)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't think this has an actual effect.

The nud function looks like this:

func nud (kind lexer.TokenKind, bp binding_power, nud_fn nud_handler) {
	bp_lu[kind] = primary
	nud_lu[kind] = nud_fn
}

While it takes in a binding power, it doesn't use it.

I came here to see what the resulting code was, but while the video removed that param, the code here still has it, but it doesn't do anything from what I can see.

That said, this errors out:

stmt := `45.2 + 5 * 4 - -5;`

I'm not sure of the fix yet.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hello actually I believe it is the solution sense I have made my own interpreter in go while following the base of this tutorial and it does work for me
image
while this is the parser
image

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