Make divide-by-zero error, handle error gracefully in grow

This commit is contained in:
Ian Gulliver
2021-11-22 20:46:20 -08:00
parent a5e44a7e07
commit c547abb296
3 changed files with 10 additions and 12 deletions

View File

@@ -52,11 +52,8 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
score, err := def.score(prog) score, err := def.score(prog)
if err != nil { if err != nil {
if statusChan != nil { // Can never get best score
close(statusChan) continue
}
return nil, err
} }
if score > status.BestScore { if score > status.BestScore {
@@ -89,7 +86,10 @@ func (def *Definition) score(prog *vm.Program) (uint64, error) {
sample.SetInputs(state) sample.SetInputs(state)
state.Execute() // ignore error err = state.Execute()
if err != nil {
return 0, err
}
score += sample.matchingOuts(state) score += sample.matchingOuts(state)
} }

View File

@@ -1,5 +1,6 @@
package vm package vm
import "fmt"
import "math" import "math"
type opHandler func(*State, *Instruction) type opHandler func(*State, *Instruction)
@@ -79,8 +80,7 @@ func (state *State) handleDivideUnsigned(instr *Instruction) {
in2 := state.readUnsigned(instr.Operands[1]) in2 := state.readUnsigned(instr.Operands[1])
if in2 == 0 { if in2 == 0 {
// Divide by zero just returns zero state.setError(fmt.Errorf("Divide by zero"))
state.writeUnsigned(instr.Operands[0], 0)
} else { } else {
state.writeUnsigned(instr.Operands[0], in1/in2) state.writeUnsigned(instr.Operands[0], in1/in2)
} }
@@ -91,8 +91,7 @@ func (state *State) handleDivideSigned(instr *Instruction) {
in2 := state.readSigned(instr.Operands[1]) in2 := state.readSigned(instr.Operands[1])
if in2 == 0 { if in2 == 0 {
// Divide by zero just returns zero state.setError(fmt.Errorf("Divide by zero"))
state.writeSigned(instr.Operands[0], 0)
} else { } else {
state.writeSigned(instr.Operands[0], in1/in2) state.writeSigned(instr.Operands[0], in1/in2)
} }

View File

@@ -75,8 +75,7 @@ func (state *State) processInstruction() {
state.instructionCount += 1 state.instructionCount += 1
if state.program.InstructionLimit > 0 && state.instructionCount > state.program.InstructionLimit { if state.program.InstructionLimit > 0 && state.instructionCount > state.program.InstructionLimit {
state.err = fmt.Errorf("Instruction limit (%d) exceeded", state.program.InstructionLimit) state.setError(fmt.Errorf("Instruction limit (%d) exceeded", state.program.InstructionLimit))
state.running = false
} }
for state.functionIndex < 0 || for state.functionIndex < 0 ||