diff --git a/hal/instructions.go b/hal/instructions.go index 53c6808..b0913aa 100644 --- a/hal/instructions.go +++ b/hal/instructions.go @@ -50,6 +50,9 @@ var instructions = []*Instruction{ InstructionJump, InstructionAdd, InstructionAddNum, + InstructionSubNum, + InstructionMulNum, + InstructionDivNum, } var InstructionStart = &Instruction{ @@ -171,10 +174,28 @@ var InstructionAdd = &Instruction{ }, } -var InstructionAddNum = &Instruction{ - Name: "ADDNUM", - ExecuteWithOperand: func(module *Module, operand float64) error { - module.accumulator = module.accumulator + operand - return nil - }, +func newMathNumInstruction(name string, operation func(accumulator, value float64) float64) *Instruction { + return &Instruction{ + Name: name, + ExecuteWithOperand: func(module *Module, operand float64) error { + module.accumulator = operation(module.accumulator, operand) + return nil + }, + } } + +var InstructionAddNum = newMathNumInstruction("ADDNUM", func(accumulator, value float64) float64 { + return accumulator + value +}) + +var InstructionSubNum = newMathNumInstruction("SUBNUM", func(accumulator, value float64) float64 { + return accumulator - value +}) + +var InstructionMulNum = newMathNumInstruction("MULNUM", func(accumulator, value float64) float64 { + return accumulator * value +}) + +var InstructionDivNum = newMathNumInstruction("DIVNUM", func(accumulator, value float64) float64 { + return accumulator / value +})