Add jump instructions

master
Jonas Franz 5 years ago
parent e51c5be227
commit 55e5cc35e2
Signed by: JonasFranzDEV
GPG Key ID: 7293A220B7C38080
  1. 45
      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) {

Loading…
Cancel
Save