error message clarity

This commit is contained in:
Neemek 2025-09-16 20:00:43 +02:00
parent 4a0c315ccb
commit 16e756bd4e

View file

@ -108,7 +108,7 @@ func (e CompilerError) Format() string {
// print import stack trace // print import stack trace
for i := len(e.Trace) - 1; i >= 0; i-- { for i := len(e.Trace) - 1; i >= 0; i-- {
p := e.Trace[i] p := e.Trace[i]
b.WriteString(fmt.Sprintf("\n[%d] %s", i+1, p)) b.WriteString(fmt.Sprintf("\n[%d] %s", i, p))
} }
return b.String() return b.String()
@ -631,13 +631,13 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
} }
if !l.Matches(r) { if !l.Matches(r) {
return nil, c.error(fmt.Sprintf("cannot perform binary %s on different types: %s and %s", n.BinaryOperation, l, r), n) return nil, c.error(fmt.Sprintf("cannot %s %s and %s; different types", n.BinaryOperation, l, r), n)
} }
switch n.BinaryOperation { switch n.BinaryOperation {
case BinarySubtraction, BinaryMultiplication, BinaryDivision: case BinarySubtraction, BinaryMultiplication, BinaryDivision:
if l.Type() != TypeNumber { if l.Type() != TypeNumber {
return nil, c.error(fmt.Sprintf("cannot perform binary %s non-number type %s", n.BinaryOperation, l), n) return nil, c.error(fmt.Sprintf("cannot %s values of non-number type %s", n.BinaryOperation, l), n)
} }
return &NumberSignature{}, nil return &NumberSignature{}, nil
@ -1115,18 +1115,18 @@ func (c *Compiler) computeBinary(n *BinaryNode) (Value, error) {
} }
if l.Type() != r.Type() { if l.Type() != r.Type() {
return nil, c.error(fmt.Sprintf("cannot perform binary %s on different types %s and %s", n.BinaryOperation, l.Type(), r.Type()), n) return nil, c.error(fmt.Sprintf("cannot %s different types %s and %s", n.BinaryOperation, l.Type(), r.Type()), n)
} }
// perform type check // perform type check
switch n.BinaryOperation { switch n.BinaryOperation {
case BinarySubtraction, BinaryMultiplication, BinaryDivision, BinaryLess, BinaryGreater, BinaryLessEqual, BinaryGreaterEqual: case BinarySubtraction, BinaryMultiplication, BinaryDivision, BinaryLess, BinaryGreater, BinaryLessEqual, BinaryGreaterEqual:
if l.Type() != NumberValueType { if l.Type() != NumberValueType {
return nil, c.error(fmt.Sprintf("cannot do binary %s on non-number type %s", n.BinaryOperation, l.Type()), n) return nil, c.error(fmt.Sprintf("cannot %s values of non-number type %s", n.BinaryOperation, l.Type()), n)
} }
case BinaryAnd, BinaryOr: case BinaryAnd, BinaryOr:
if l.Type() != BoolValueType { if l.Type() != BoolValueType {
return nil, c.error(fmt.Sprintf("cannot do binary %s on non-boolean type %s", n.BinaryOperation, l.Type()), n) return nil, c.error(fmt.Sprintf("cannot %s values of non-boolean type %s", n.BinaryOperation, l.Type()), n)
} }
case BinaryEquality, BinaryInequality: case BinaryEquality, BinaryInequality:
// can compare all types with themselves // can compare all types with themselves
@ -1144,7 +1144,7 @@ func (c *Compiler) computeBinary(n *BinaryNode) (Value, error) {
case ListValueType: case ListValueType:
v = append(l.(*ListValue).Items, r.(*ListValue).Items...) v = append(l.(*ListValue).Items, r.(*ListValue).Items...)
default: default:
return nil, c.error(fmt.Sprintf("cannot perform binary add on type %s", l.Type()), n) return nil, c.error(fmt.Sprintf("cannot add values of type %s", l.Type()), n)
} }
case BinarySubtraction: case BinarySubtraction:
v = l.(*NumberValue).Number - r.(*NumberValue).Number v = l.(*NumberValue).Number - r.(*NumberValue).Number