From bf9b60dce7157e5db3b7e3ed58820b57fc9b80cc Mon Sep 17 00:00:00 2001 From: simomu-github Date: Thu, 22 Sep 2022 22:45:58 +0900 Subject: [PATCH] Fix computation sequence --- instructions.go | 50 ++++++++++++++++++++++---------------------- instructions_test.go | 7 ++++--- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/instructions.go b/instructions.go index e093894..49a29a6 100644 --- a/instructions.go +++ b/instructions.go @@ -63,16 +63,16 @@ func (d Discard) Execute(executor *Executor) error { type Addition struct{} func (a Addition) Execute(executor *Executor) error { - lhs, errLhs := executor.Pop() - if errLhs != nil { - return errLhs - } - rhs, errRhs := executor.Pop() if errRhs != nil { return errRhs } + lhs, errLhs := executor.Pop() + if errLhs != nil { + return errLhs + } + executor.Push(lhs + rhs) return nil @@ -81,16 +81,16 @@ func (a Addition) Execute(executor *Executor) error { type Subtraction struct{} func (s Subtraction) Execute(executor *Executor) error { - lhs, errLhs := executor.Pop() - if errLhs != nil { - return errLhs - } - rhs, errRhs := executor.Pop() if errRhs != nil { return errRhs } + lhs, errLhs := executor.Pop() + if errLhs != nil { + return errLhs + } + executor.Push(lhs - rhs) return nil @@ -99,16 +99,16 @@ func (s Subtraction) Execute(executor *Executor) error { type Multiplication struct{} func (m Multiplication) Execute(executor *Executor) error { - lhs, errLhs := executor.Pop() - if errLhs != nil { - return errLhs - } - rhs, errRhs := executor.Pop() if errRhs != nil { return errRhs } + lhs, errLhs := executor.Pop() + if errLhs != nil { + return errLhs + } + executor.Push(lhs * rhs) return nil @@ -117,16 +117,16 @@ func (m Multiplication) Execute(executor *Executor) error { type Division struct{} func (d Division) Execute(executor *Executor) error { - lhs, errLhs := executor.Pop() - if errLhs != nil { - return errLhs - } - rhs, errRhs := executor.Pop() if errRhs != nil { return errRhs } + lhs, errLhs := executor.Pop() + if errLhs != nil { + return errLhs + } + executor.Push(lhs / rhs) return nil @@ -135,16 +135,16 @@ func (d Division) Execute(executor *Executor) error { type Modulo struct{} func (m Modulo) Execute(executor *Executor) error { - lhs, errLhs := executor.Pop() - if errLhs != nil { - return errLhs - } - rhs, errRhs := executor.Pop() if errRhs != nil { return errRhs } + lhs, errLhs := executor.Pop() + if errLhs != nil { + return errLhs + } + executor.Push(lhs % rhs) return nil diff --git a/instructions_test.go b/instructions_test.go index 850eaf1..1e44450 100644 --- a/instructions_test.go +++ b/instructions_test.go @@ -1,8 +1,9 @@ package whitespace_go import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func newExecutor() *Executor { @@ -63,7 +64,7 @@ func TestAddition(t *testing.T) { func TestSubtraction(t *testing.T) { executor := newExecutor() - executor.stack = []int{1, 2} + executor.stack = []int{2, 1} subtraction := Subtraction{} subtraction.Execute(executor) @@ -93,7 +94,7 @@ func TestDivision(t *testing.T) { func TestModulo(t *testing.T) { executor := newExecutor() - executor.stack = []int{3, 5} + executor.stack = []int{5, 3} modulo := Modulo{} modulo.Execute(executor)