Skip to content

Commit

Permalink
chore: more readable panic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
saffage committed Jun 23, 2024
1 parent edc0c4d commit c1571c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
16 changes: 4 additions & 12 deletions cgen/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,16 @@ func (gen *generator) builtInPrint(call *ast.Call) string {
`fprintf(stdout, "%%f", %s)`,
gen.exprString(call.Args.Nodes[0]),
)

default:
panic("not implemented")
}

case *types.Enum:
return fmt.Sprintf(
`fprintf(stdout, "%%d", %s)`,
gen.exprString(call.Args.Nodes[0]),
)

default:
panic(fmt.Sprintf("printing type %T is not implemented", value.Type))
}

panic(fmt.Sprintf("'$print' for the type %s is not implemented", value.Type))
}

func (gen *generator) builtInPrintln(call *ast.Call) string {
Expand Down Expand Up @@ -142,20 +138,16 @@ func (gen *generator) builtInPrintln(call *ast.Call) string {
`fprintf(stdout, "%%f\n", %s)`,
gen.exprString(call.Args.Nodes[0]),
)

default:
panic("not implemented")
}

case *types.Enum:
return fmt.Sprintf(
`fprintf(stdout, "%%d\n", %s)`,
gen.exprString(call.Args.Nodes[0]),
)

default:
panic(fmt.Sprintf("printing type %T is not implemented", value.Type))
}

panic(fmt.Sprintf("'$println' for the type %s is not implemented", value.Type))
}

func (gen *generator) builtInAssert(call *ast.Call) string {
Expand Down
44 changes: 18 additions & 26 deletions cgen/exprs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ func (gen *generator) exprString(expr ast.Node) string {
return gen.constant(sym.Value())

case nil:
report.TaggedErrorf("cgen", "expression `%s` have no uses", expr)
panic("")
panic(fmt.Sprintf("expression `%s` have no uses", expr))

default:
panic(fmt.Sprintf("idk (%T) %s", sym, sym.Node().Repr()))
panic(fmt.Sprintf("invalid symbol '%T' for an expression: '%s'", sym, sym.Node().Repr()))
}

case *ast.Literal:
Expand All @@ -54,7 +53,7 @@ func (gen *generator) exprString(expr ast.Node) string {
tv := gen.Types[node.X]
if tv == nil {
// Defined in another module?
panic("idk")
panic("unreachable")
}

if types.IsTypeDesc(tv.Type) {
Expand Down Expand Up @@ -117,28 +116,17 @@ func (gen *generator) exprString(expr ast.Node) string {
tv := gen.Types[node.X]
if tv == nil {
// Defined in another module?
panic("idk")
panic("unreachable")
}

if types.IsTypeDesc(tv.Type) {
ty := types.SkipTypeDesc(tv.Type)
tmp := gen.tempVar(ty)

if _struct := types.AsStruct(ty); _struct != nil {
gen.structInit(gen.name(tmp), node, _struct)
if ty := types.AsStruct(types.SkipTypeDesc(tv.Type)); ty != nil {
tmp := gen.tempVar(ty)
gen.structInit(gen.name(tmp), node, ty)
return gen.name(tmp)
// buf.WriteString(fmt.Sprintf("(%s){\n", gen.TypeString(_struct)))
// gen.indent++
// buf.WriteString(gen.structInit(_struct, node.Args.List))
// gen.indent--
// gen.indent(&buf)
// buf.WriteString("}")
// return buf.String()
} else if _enum := types.AsEnum(ty); _enum != nil {
panic("todo")
// return gen.TypeString(_enum) + "__" + node.Args.Repr()
} else {
return "ERROR_CGEN__INVALID_MEMBER_ACCESS"
// Error in the checker
return "ERROR_CGEN__INVALID_CALL"
}
} else if fn := types.AsFunc(tv.Type); fn != nil {
buf := strings.Builder{}
Expand Down Expand Up @@ -166,7 +154,8 @@ func (gen *generator) exprString(expr ast.Node) string {
buf.WriteByte('[')

if len(node.Args.Nodes) != 1 {
panic("idk how to handle it")
// Error in the checker
panic("invalid arguments count for the index expression")
}

buf.WriteString(gen.exprString(node.Args.Nodes[0]))
Expand All @@ -178,8 +167,7 @@ func (gen *generator) exprString(expr ast.Node) string {
// must be prefixes with the type.
tv := gen.Types[expr]
if tv == nil || !types.IsArray(tv.Type) {
// Defined in another module?
panic("idk")
panic("unreachable")
}
ty := types.AsArray(types.SkipUntyped(tv.Type))
tmp := gen.tempVar(ty)
Expand Down Expand Up @@ -209,10 +197,14 @@ func (gen *generator) exprString(expr ast.Node) string {
return ""

default:
fmt.Printf("not implemented '%T'\n", node)
report.TaggedErrorf(
"internal: cgen",
"expression '%T' is not implemented",
node,
)
}

report.Warningf("empty expr at node '%T'", expr)
report.TaggedWarningf("internal: cgen", "empty expression was generated: '%T'", expr)
return "ERROR_CGEN__EXPR"
}

Expand Down
4 changes: 4 additions & 0 deletions cgen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const (
var arrayTypes = map[types.Type]string{}

func (gen *generator) TypeString(ty types.Type) string {
if ty == nil {
panic("can't generate a type string for the nil type")
}

assert.Ok(!types.IsTypeDesc(ty))

switch ty := ty.Underlying().(type) {
Expand Down
3 changes: 1 addition & 2 deletions checker/type_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (check *Checker) typeOfInternal(expr ast.Node) types.Type {
*ast.StmtList,
*ast.List,
*ast.AttributeList:
// *ast.Signature:
panic("ill-formed AST")

case *ast.Empty:
Expand Down Expand Up @@ -185,7 +184,7 @@ func (check *Checker) typeOfCall(node *ast.Call) types.Type {
return tyStruct
}

check.errorf(node.X, "expression is not a function or struct type (%s)", tyOperand)
check.errorf(node.X, "expression is not a function or a struct type (%s)", tyOperand)
return nil
}

Expand Down

0 comments on commit c1571c2

Please sign in to comment.