d5 / tengo

A fast script language for Go

Home Page:https://tengolang.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

shiyuge opened this issue · comments

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?

I have opened a pull request for that:

#391