|
|
|
@ -48,11 +48,14 @@ var instructions = []*Instruction{ |
|
|
|
|
InstructionJumpPos, |
|
|
|
|
InstructionJumpNull, |
|
|
|
|
InstructionJump, |
|
|
|
|
InstructionAdd, |
|
|
|
|
InstructionAddNum, |
|
|
|
|
InstructionSubNum, |
|
|
|
|
InstructionMulNum, |
|
|
|
|
InstructionDivNum, |
|
|
|
|
InstructionAdd, |
|
|
|
|
InstructionSub, |
|
|
|
|
InstructionMul, |
|
|
|
|
InstructionDiv, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var InstructionStart = &Instruction{ |
|
|
|
@ -160,20 +163,6 @@ var InstructionJump = newJumpInstruction("JUMP", func(accumulator float64) bool |
|
|
|
|
return true |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
var InstructionAdd = &Instruction{ |
|
|
|
|
Name: "ADD", |
|
|
|
|
ExecuteWithOperand: func(module *Module, operand float64) error { |
|
|
|
|
index := int(operand) |
|
|
|
|
if len(module.register) <= index { |
|
|
|
|
return fmt.Errorf("index %d does not exist in program storage", index) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.accumulator += module.register[index] |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newMathNumInstruction(name string, operation func(accumulator, value float64) float64) *Instruction { |
|
|
|
|
return &Instruction{ |
|
|
|
|
Name: name, |
|
|
|
@ -199,3 +188,34 @@ var InstructionMulNum = newMathNumInstruction("MULNUM", func(accumulator, value |
|
|
|
|
var InstructionDivNum = newMathNumInstruction("DIVNUM", func(accumulator, value float64) float64 { |
|
|
|
|
return accumulator / value |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func newMathInstruction(name string, operation func(accumulator, register float64) float64) *Instruction { |
|
|
|
|
return &Instruction{ |
|
|
|
|
Name: name, |
|
|
|
|
ExecuteWithOperand: func(module *Module, operand float64) error { |
|
|
|
|
index := int(operand) |
|
|
|
|
if len(module.register) <= index { |
|
|
|
|
return fmt.Errorf("index %d does not exist in register", index) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.accumulator = operation(module.accumulator, module.register[index]) |
|
|
|
|
return nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var InstructionAdd = newMathInstruction("ADD", func(accumulator, value float64) float64 { |
|
|
|
|
return accumulator + value |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
var InstructionSub = newMathInstruction("SUB", func(accumulator, value float64) float64 { |
|
|
|
|
return accumulator - value |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
var InstructionMul = newMathInstruction("MUL", func(accumulator, value float64) float64 { |
|
|
|
|
return accumulator * value |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
var InstructionDiv = newMathInstruction("DIV", func(accumulator, value float64) float64 { |
|
|
|
|
return accumulator / value |
|
|
|
|
}) |
|
|
|
|