Skip to content

Commit

Permalink
Add more error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
peachpit-site committed Jan 31, 2025
1 parent 60092f2 commit b14545b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
6 changes: 3 additions & 3 deletions source/compiler/compfcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (cp *Compiler) generateBranch(b *bindle) AlternateType {
for _, loc := range b.valLocs {
cp.Vm.Mem[cp.That()].V.(*err.Error).Args = append(cp.Vm.Mem[cp.That()].V.(*err.Error).Args, loc)
}
cp.cmP("Unthunking error "+text.Emph("vm/types/a")+".", b.tok)
cp.cmP("Unthunking error 'vm/types/a'.", b.tok)
cp.Emit(vm.UntE, cp.That())
cp.Emit(vm.Asgm, b.outLoc, cp.That())
return AltType(values.ERROR)
Expand Down Expand Up @@ -328,7 +328,7 @@ func (cp *Compiler) generateBranch(b *bindle) AlternateType {
for _, loc := range b.valLocs {
cp.Vm.Mem[cp.That()].V.(*err.Error).Args = append(cp.Vm.Mem[cp.That()].V.(*err.Error).Args, loc)
}
cp.cmP("Unthunking error "+text.Emph("vm/types/b")+".", b.tok)
cp.cmP("Unthunking error 'vm/types/b'.", b.tok)
cp.Emit(vm.UntE, cp.That())
cp.Emit(vm.Asgm, b.outLoc, cp.That())
return AltType(values.ERROR)
Expand Down Expand Up @@ -716,7 +716,7 @@ func (cp *Compiler) seekFunctionCall(b *bindle) AlternateType {
for _, loc := range b.valLocs {
cp.Vm.Mem[cp.That()].V.(*err.Error).Args = append(cp.Vm.Mem[cp.That()].V.(*err.Error).Args, loc)
}
cp.cmP("Unthunking error "+text.Emph("vm/types/c")+".", b.tok)
cp.cmP("Unthunking error 'vm/types/c'.", b.tok)
cp.Emit(vm.UntE, cp.That())
cp.Emit(vm.Asgm, b.outLoc, cp.That())
return AltType(values.ERROR)
Expand Down
51 changes: 48 additions & 3 deletions source/err/errorfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,7 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"vm/div/float": {
"vm/div/zero/a": {
Message: func(tok *token.Token, args ...any) string {
return "division by zero"
},
Expand All @@ -2614,7 +2614,34 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"vm/div/int": {
"vm/div/zero/b": {
Message: func(tok *token.Token, args ...any) string {
return "division by zero"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "As it is not possible to divide a number by zero, Pipefish considers this a runtime error."
},
},

"vm/div/zero/c": {
Message: func(tok *token.Token, args ...any) string {
return "division by zero"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "As it is not possible to divide a number by zero, Pipefish considers this a runtime error."
},
},

"vm/div/zero/d": {
Message: func(tok *token.Token, args ...any) string {
return "division by zero"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "As it is not possible to divide a number by zero, Pipefish considers this a runtime error."
},
},

"vm/div/zero/e": {
Message: func(tok *token.Token, args ...any) string {
return "division by zero"
},
Expand Down Expand Up @@ -2688,7 +2715,16 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"vm/go/type": {
"vm/go/type/a": {
Message: func(tok *token.Token, args ...any) string {
return "can't convert Go value of type " + emph(args[0]) + " to Pipefish"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "Not every Go value can or should be converted into Pipefish."
},
},

"vm/go/type/b": {
Message: func(tok *token.Token, args ...any) string {
return "can't convert Go value of type " + emph(args[0]) + " to Pipefish"
},
Expand Down Expand Up @@ -2913,6 +2949,15 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"vm/index/r": {
Message: func(tok *token.Token, args ...any) string {
return fmt.Sprintf("upper bound %v of tuple slice is strictly greater than string length %v", emph(args[0]), args[1])
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return fmt.Sprintf("The greatest value the upper bound of a slice can have is the length of the thing being sliced.")
},
},

"vm/label/exist": {
Message: func(tok *token.Token, args ...any) string {
return fmt.Sprintf("can't convert string %v to a label", emphStr(args[0]))
Expand Down
4 changes: 2 additions & 2 deletions source/vm/gohandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,15 @@ func (vm *Vm) goToPipefish(goValue reflect.Value) values.Value {
pfType = values.MAP
}
default:
return values.Value{values.UNDEFINED_TYPE, []any{"vm/go/type", reflect.TypeOf(someGoDatum).String()}}
return values.Value{values.UNDEFINED_TYPE, []any{"vm/go/type/a", reflect.TypeOf(someGoDatum).String()}}
}
}
sig = append(sig, values.AbstractType{Types: []values.ValueType{pfType}})
}
pfLambda := Lambda{Gocode: &goValue, Sig: sig}
return values.Value{values.FUNC, pfLambda}
}
return values.Value{values.UNDEFINED_TYPE, []any{"vm/go/type", reflect.TypeOf(someGoDatum).String()}}
return values.Value{values.UNDEFINED_TYPE, []any{"vm/go/type/b", reflect.TypeOf(someGoDatum).String()}}
}

// Because the above function will be applied recursively, we apply the goTuple type to the values
Expand Down
12 changes: 6 additions & 6 deletions source/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,35 +336,35 @@ loop:
case Diif:
divisor := vm.Mem[args[2]].V.(int)
if divisor == 0 {
vm.Mem[args[0]] = vm.makeError("vm/div/float", args[3])
vm.Mem[args[0]] = vm.makeError("vm/div/zero/a", args[3])
} else {
vm.Mem[args[0]] = values.Value{values.FLOAT, float64(vm.Mem[args[1]].V.(int)) / float64(divisor)}
}
case Divf:
divisor := vm.Mem[args[2]].V.(float64)
if divisor == 0 {
vm.Mem[args[0]] = vm.makeError("vm/div/float", args[3])
vm.Mem[args[0]] = vm.makeError("vm/div/zero/b", args[3])
} else {
vm.Mem[args[0]] = values.Value{values.FLOAT, vm.Mem[args[1]].V.(float64) / divisor}
}
case Divi:
divisor := vm.Mem[args[2]].V.(int)
if divisor == 0 {
vm.Mem[args[0]] = vm.makeError("vm/div/int", args[3])
vm.Mem[args[0]] = vm.makeError("vm/div/zero/c", args[3])
} else {
vm.Mem[args[0]] = values.Value{values.INT, vm.Mem[args[1]].V.(int) / vm.Mem[args[2]].V.(int)}
}
case Dvfi:
divisor := vm.Mem[args[2]].V.(int)
if divisor == 0 {
vm.Mem[args[0]] = vm.makeError("vm/div/float", args[3])
vm.Mem[args[0]] = vm.makeError("vm/div/zero/d", args[3])
} else {
vm.Mem[args[0]] = values.Value{values.FLOAT, vm.Mem[args[1]].V.(float64) / float64(divisor)}
}
case Dvif:
divisor := vm.Mem[args[2]].V.(float64)
if divisor == 0 {
vm.Mem[args[0]] = vm.makeError("vm/div/float", args[3])
vm.Mem[args[0]] = vm.makeError("vm/div/zero/e", args[3])
} else {
vm.Mem[args[0]] = values.Value{values.FLOAT, float64(vm.Mem[args[1]].V.(int)) / divisor}
}
Expand Down Expand Up @@ -710,7 +710,7 @@ loop:
case values.TUPLE:
tup := container.V.([]values.Value)
if ix[1].V.(int) > len(tup) {
vm.Mem[args[0]] = vm.makeError("vm/index/f", args[3])
vm.Mem[args[0]] = vm.makeError("vm/index/r", args[3])
break Switch
}
vm.Mem[args[0]] = values.Value{values.TUPLE, tup[ix[0].V.(int):ix[1].V.(int)]}
Expand Down

0 comments on commit b14545b

Please sign in to comment.