From 2daebdfce2e5166c806b4be34603e0806aaac679 Mon Sep 17 00:00:00 2001 From: Evgenii Date: Tue, 19 Feb 2019 16:50:34 +0300 Subject: [PATCH] compiler: convert unary operators properly Imported from CityOfZion/neo-storm#62 PR. --- pkg/vm/compiler/codegen.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index fbcfa5027..45993eb37 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -484,7 +484,22 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { case *ast.UnaryExpr: 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 case *ast.IncDecStmt: