Added a division by zero error
This commit is contained in:
parent
dabd2df07c
commit
b4bd86ad1d
11
creature.go
11
creature.go
|
@ -29,6 +29,7 @@ const (
|
||||||
// registered with the same ID as another one.
|
// registered with the same ID as another one.
|
||||||
ErrorIDTaken Error = iota
|
ErrorIDTaken Error = iota
|
||||||
ErrorUnknownInstruction
|
ErrorUnknownInstruction
|
||||||
|
ErrorDivideByZero
|
||||||
)
|
)
|
||||||
|
|
||||||
// Error returns a textual description of the error.
|
// Error returns a textual description of the error.
|
||||||
|
@ -38,6 +39,8 @@ func (err Error) Error() (description string) {
|
||||||
description = "this ID is taken by another function"
|
description = "this ID is taken by another function"
|
||||||
case ErrorUnknownInstruction:
|
case ErrorUnknownInstruction:
|
||||||
description = "machine encountered unknown instruction"
|
description = "machine encountered unknown instruction"
|
||||||
|
case ErrorDivideByZero:
|
||||||
|
description = "cannot divide by zero"
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -100,7 +103,13 @@ func (machine *Machine) Execute(offset int) (err error) {
|
||||||
// DIV
|
// DIV
|
||||||
// divides the last word on the stack by the second to
|
// divides the last word on the stack by the second to
|
||||||
// last word on the stack
|
// last word on the stack
|
||||||
machine.Push(machine.Pop() / machine.Pop())
|
left := machine.Pop()
|
||||||
|
right := machine.Pop()
|
||||||
|
if left == 0 && right == 0 {
|
||||||
|
err = ErrorDivideByZero
|
||||||
|
return
|
||||||
|
}
|
||||||
|
machine.Push(left / right)
|
||||||
|
|
||||||
case 0x8:
|
case 0x8:
|
||||||
// EQ
|
// EQ
|
||||||
|
|
|
@ -23,7 +23,12 @@ func TestArithmetic(test *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
machine.Execute(0)
|
err := machine.Execute(0)
|
||||||
|
if err != nil {
|
||||||
|
test.Log("machine exited with error:", err)
|
||||||
|
test.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
addResult := machine.Pop()
|
addResult := machine.Pop()
|
||||||
subResult := machine.Pop()
|
subResult := machine.Pop()
|
||||||
mulResult := machine.Pop()
|
mulResult := machine.Pop()
|
||||||
|
|
Loading…
Reference in New Issue