Add jump instructions

master
Jonas Franz 5 years ago
parent e51c5be227
commit 55e5cc35e2
Signed by: JonasFranzDEV
GPG Key ID: 7293A220B7C38080
  1. 27
      hal/instructions.go

@ -45,6 +45,9 @@ var instructions = []*Instruction{
InstructionLoadNum, InstructionLoadNum,
InstructionStore, InstructionStore,
InstructionJumpNeg, InstructionJumpNeg,
InstructionJumpPos,
InstructionJumpNull,
InstructionJump,
InstructionAddnum, InstructionAddnum,
} }
@ -121,19 +124,37 @@ var InstructionStore = &Instruction{
}, },
} }
var InstructionJumpNeg = &Instruction{ func newJumpInstruction(name string, doJump func(accumulator float64) bool) *Instruction {
Name: "JUMPNEG", return &Instruction{
Name: "JUMPPOS",
ExecuteWithOperand: func(module *Module, operand float64) error { ExecuteWithOperand: func(module *Module, operand float64) error {
index := int64(operand) index := int64(operand)
if _, ok := module.programStorage[index]; !ok { if _, ok := module.programStorage[index]; !ok {
return fmt.Errorf("index %d does not exist in program storage", index) return fmt.Errorf("index %d does not exist in program storage", index)
} }
if module.accumulator < 0 { if doJump(module.accumulator) {
return module.setProgramCounter(index) return module.setProgramCounter(index)
} }
return nil 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{ var InstructionAddnum = &Instruction{
Name: "ADDNUM", Name: "ADDNUM",

Loading…
Cancel
Save