diff --git a/hal/instructions.go b/hal/instructions.go index 6f7e669..0c58dd6 100644 --- a/hal/instructions.go +++ b/hal/instructions.go @@ -45,6 +45,9 @@ var instructions = []*Instruction{ InstructionLoadNum, InstructionStore, InstructionJumpNeg, + InstructionJumpPos, + InstructionJumpNull, + InstructionJump, InstructionAddnum, } @@ -121,20 +124,38 @@ var InstructionStore = &Instruction{ }, } -var InstructionJumpNeg = &Instruction{ - Name: "JUMPNEG", - ExecuteWithOperand: func(module *Module, operand float64) error { - index := int64(operand) - if _, ok := module.programStorage[index]; !ok { - return fmt.Errorf("index %d does not exist in program storage", index) - } - if module.accumulator < 0 { - return module.setProgramCounter(index) - } - return nil - }, +func newJumpInstruction(name string, doJump func(accumulator float64) bool) *Instruction { + return &Instruction{ + Name: "JUMPPOS", + ExecuteWithOperand: func(module *Module, operand float64) error { + index := int64(operand) + if _, ok := module.programStorage[index]; !ok { + return fmt.Errorf("index %d does not exist in program storage", index) + } + if doJump(module.accumulator) { + return module.setProgramCounter(index) + } + return nil + }, + } } +var InstructionJumpNeg = newJumpInstruction("JUMPNEG", func(accumulator float64) bool { + return accumulator > 0 +}) + +var InstructionJumpPos = newJumpInstruction("JUMPPOS", func(accumulator float64) bool { + return accumulator < 0 +}) + +var InstructionJumpNull = newJumpInstruction("JUMPNULL", func(accumulator float64) bool { + return accumulator == 0 +}) + +var InstructionJump = newJumpInstruction("JUMP", func(accumulator float64) bool { + return true +}) + var InstructionAddnum = &Instruction{ Name: "ADDNUM", ExecuteWithOperand: func(module *Module, operand float64) {