From d769c89103ea2d03047c879039f7623623605897 Mon Sep 17 00:00:00 2001 From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Mon, 15 Oct 2018 15:54:30 +0800 Subject: [PATCH] fix the operation for binary operator with the same variable (#16) --- compiler/compile.go | 4 +++- compiler/stack.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) 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) { -- 2.11.0