From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Mon, 15 Oct 2018 07:54:30 +0000 (+0800) Subject: fix the operation for binary operator with the same variable (#16) X-Git-Tag: v0.1.1~16 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=d769c89103ea2d03047c879039f7623623605897;p=bytom%2Fequity.git fix the operation for binary operator with the same variable (#16) --- diff --git a/compiler/compile.go b/compiler/compile.go index c3d277f..7145274 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -498,7 +498,9 @@ func compileStatement(b *builder, stk stack, contract *Contract, env *environ, c } // restore the defined variable counts - counts[stmt.variable.Name] = varCount + if tmpCounts[stmt.variable.Name] > 0 { + counts[stmt.variable.Name] = varCount + } // modify stack name stk.str = stmt.variable.Name diff --git a/compiler/stack.go b/compiler/stack.go index 1459f02..c86e2b6 100644 --- a/compiler/stack.go +++ b/compiler/stack.go @@ -52,20 +52,58 @@ func (stk stack) dropN(n int) stack { return stk } -func (stk stack) find(str string) int { +func (stk stack) recurFind(str string) int { if stk.isEmpty() { return -1 } if stk.str == str { return 0 } - res := stk.drop().find(str) + res := stk.drop().recurFind(str) if res < 0 { return res } return res + 1 } +func (stk stack) count() map[string]int { + if stk.isEmpty() { + return nil + } + stackCounts := make(map[string]int) + for { + stackCounts[stk.str]++ + stk = stack{stk.prev} + if stk.stackEntry == nil { + break + } + } + return stackCounts +} + +func (stk stack) find(str string) int { + stackCounts := stk.count() + if stk.isEmpty() || stackCounts[str] == 0 { + return -1 + } + + var pos int + for { + if stk.str == str { + if stackCounts[str] == 1 { + break + } + stackCounts[str]-- + } + stk = stack{stk.prev} + if stk.stackEntry == nil { + break + } + pos++ + } + return pos +} + func (stk stack) roll(n int) stack { var x func(stack, int) (stack, string) x = func(stk stack, n int) (stack, string) {