8000 Compiler Question: why invert token.Less to token.Greater · Issue #390 · d5/tengo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Compiler Question: why invert token.Less to token.Greater #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shiyuge opened this issue Aug 12, 2022 · 1 comment
Closed

Compiler Question: why invert token.Less to token.Greater #390

shiyuge opened this issue Aug 12, 2022 · 1 comment

Comments

@shiyuge
Copy link
Contributor
shiyuge commented Aug 12, 2022

in compiler.go:

tengo/compiler.go

Lines 144 to 153 in 6fc8053

if node.Token == token.Less {
if err := c.Compile(node.RHS); err != nil {
return err
}
if err := c.Compile(node.LHS); err != nil {
return err
}
c.emit(node, parser.OpBinaryOp, int(token.Greater))
return nil
} else if node.Token == token.LessEq {

It seems that < is translated to > when compiling tengo scripts. I wonder why. This makes BinaryOp() in user defined types to never execute LessEq and Less branch.

For example I have a custom defined type Number, and

  1. Number(14)>12 works because it would invoke Number.BinaryOp that I implemented
  2. Number(12)<14 throws a runtime error, because compiler inverts this to 14>Number(12) in compilation process, and it would invoke Int.BinaryOp and fail.

Are there any special design considerations behind this? Can I simply send a PR to remove this piece of logic in compiler?

@shiyuge
Copy link
Contributor Author
shiyuge commented Aug 12, 2022

I have opened a pull request for that:

#391

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

No branches or pull requests

2 participants
0