Skip to content

Commit bf8bb1c

Browse files
authored
refactor: replace panic() calls with error return (#118)
We replace panic() with an error return in places where something is unimplemented. Anything that indicates a bug in the code was left as a panic, but with additional context added. Fixes #117 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 90e6135 commit bf8bb1c

File tree

6 files changed

+14
-16
lines changed

6 files changed

+14
-16
lines changed

cek/builtins.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3512,35 +3512,35 @@ func expModInteger[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35123512
func caseList[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35133513
b.Args.Extract(&m.argHolder, b.ArgCount)
35143514

3515-
panic("implement caseList")
3515+
return nil, errors.New("unimplemented: caseList")
35163516
}
35173517

35183518
func caseData[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35193519
b.Args.Extract(&m.argHolder, b.ArgCount)
35203520

3521-
panic("implement caseData")
3521+
return nil, errors.New("unimplemented: caseData")
35223522
}
35233523

35243524
func dropList[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35253525
b.Args.Extract(&m.argHolder, b.ArgCount)
35263526

3527-
panic("implement dropList")
3527+
return nil, errors.New("unimplemented: dropList")
35283528
}
35293529

35303530
func lengthOfArray[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35313531
b.Args.Extract(&m.argHolder, b.ArgCount)
35323532

3533-
panic("implement lengthOfArray")
3533+
return nil, errors.New("unimplemented: lengthOfArray")
35343534
}
35353535

35363536
func listToArray[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35373537
b.Args.Extract(&m.argHolder, b.ArgCount)
35383538

3539-
panic("implement listToArray")
3539+
return nil, errors.New("unimplemented: listToArray")
35403540
}
35413541

35423542
func indexArray[T syn.Eval](m *Machine[T], b *Builtin[T]) (Value[T], error) {
35433543
b.Args.Extract(&m.argHolder, b.ArgCount)
35443544

3545-
panic("implement indexArray")
3545+
return nil, errors.New("unimplemented: indexArray")
35463546
}

cek/cost_model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cek
22

33
import (
4+
"fmt"
45
"math/big"
56

67
"github.com/blinklabs-io/plutigo/data"
@@ -53,7 +54,7 @@ func iconstantExMem(c syn.IConstant) func() ExMem {
5354
case *syn.ProtoPair:
5455
ex = pairExMem(x.First, x.Second)
5556
default:
56-
panic("Oh no!")
57+
panic(fmt.Sprintf("invalid constant type: %T", c))
5758
}
5859

5960
return ex()

cek/machine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (m *Machine[T]) Run(term syn.Term[T]) (syn.Term[T], error) {
5454
case *Done[T]:
5555
return v.term, nil
5656
default:
57-
panic("unknown machine state")
57+
panic(fmt.Sprintf("unknown machine state: %T", state))
5858
}
5959
if err != nil {
6060
return nil, err
@@ -213,7 +213,7 @@ func (m *Machine[T]) compute(
213213
Term: t.Constr,
214214
}
215215
default:
216-
panic(fmt.Sprintf("unknown term: %v", term))
216+
panic(fmt.Sprintf("unknown term: %T: %v", term, term))
217217
}
218218

219219
return state, nil

syn/conversions.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ func nameToIndex[T any](
188188
Branches: branches,
189189
}
190190
default:
191-
fmt.Printf("%#v", t)
192-
panic("HOW THE FUCK")
191+
panic(fmt.Sprintf("unknown Term type: %T", term))
193192
}
194193

195194
return converted, nil

syn/flat_decode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ func DecodeConstant(d *decoder) (IConstant, error) {
236236
// ProtoList
237237
case len(tags) >= 2 && tags[0] == ProtoListOneTag && tags[1] == ProtoListTwoTag:
238238
// Handle PROTO_LIST_ONE, PROTO_LIST_TWO, rest...
239-
panic("unimplemented: PROTO_LIST")
239+
return nil, errors.New("unimplemented: PROTO_LIST")
240240

241241
// ProtoPair
242242
case len(tags) >= 3 && tags[0] == ProtoPairOneTag && tags[1] == ProtoPairTwoTag && tags[2] == ProtoPairThreeTag:
243243
// Handle PROTO_PAIR_ONE, PROTO_PAIR_TWO, PROTO_PAIR_THREE, rest...
244-
panic("unimplemented: PROTO_PAIR")
244+
return nil, errors.New("unimplemented: PROTO_PAIR")
245245

246246
// Data
247247
case len(tags) == 1 && tags[0] == DataTag:

syn/pretty.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package syn
22

33
import (
44
"fmt"
5-
"reflect"
65
"strings"
76

87
"github.com/blinklabs-io/plutigo/data"
@@ -227,8 +226,7 @@ func printTerm[T Binder](pp *PrettyPrinter, term Term[T], isTopLevel bool) {
227226
case *Constant:
228227
pp.printConstant(t, false)
229228
default:
230-
fmt.Println(reflect.TypeOf(t))
231-
panic(fmt.Sprintf("unknown term: %v", t))
229+
panic(fmt.Sprintf("unknown term: %T: %v", t, t))
232230
}
233231
}
234232

0 commit comments

Comments
 (0)