compiler: convert unary operators properly

Imported from CityOfZion/neo-storm#62 PR.
This commit is contained in:
Evgenii 2019-02-19 16:50:34 +03:00 committed by Roman Khimov
parent 05c07a8567
commit 2daebdfce2

View file

@ -484,7 +484,22 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
case *ast.UnaryExpr: case *ast.UnaryExpr:
ast.Walk(c, n.X) ast.Walk(c, n.X)
c.convertToken(n.Op) // From https://golang.org/ref/spec#Operators
// there can be only following unary operators
// "+" | "-" | "!" | "^" | "*" | "&" | "<-" .
// of which last three are not used in SC
switch n.Op {
case token.ADD:
// +10 == 10, no need to do anything in this case
case token.SUB:
emitOpcode(c.prog, vm.NEGATE)
case token.NOT:
emitOpcode(c.prog, vm.NOT)
case token.XOR:
emitOpcode(c.prog, vm.INVERT)
default:
log.Fatalf("invalid unary operator: %s", n.Op)
}
return nil return nil
case *ast.IncDecStmt: case *ast.IncDecStmt: