## Summary - Added doc comments across the codebase: `pkg/lambda`, `pkg/saccharine`, `pkg/codec`, `pkg/engine`, `pkg/iterator`, `pkg/set`, `pkg/convert`, `internal/registry`, and `cmd/lambda`. - Made lambda and saccharine expression structs use public fields instead of getters, matching `go/ast` conventions. - Removed superfluous constructors for saccharine and lambda expression/statement types in favor of struct literals. - Consolidated saccharine token constructors into a single `NewToken` function. - Removed the unused `trace` package. ## Test plan - [x] `go build ./...` passes. - [x] `go test ./...` passes. - [ ] Verify `go doc` output renders correctly for documented packages. Reviewed-on: #45 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package registry
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
)
|
|
|
|
// A Conversion is a type-erased transformation from one representation to
|
|
// another. It operates on Expr values, hiding the underlying representation
|
|
// types.
|
|
type Conversion interface {
|
|
// InType returns the name of the source representation.
|
|
InType() string
|
|
// OutType returns the name of the target representation.
|
|
OutType() string
|
|
|
|
// Run applies the conversion to the given expression. Returns an error if
|
|
// the expression's data does not match the expected source type.
|
|
Run(Expr) (Expr, error)
|
|
}
|
|
|
|
// A registeredConversion adapts a typed codec.Conversion[T, U] into the
|
|
// type-erased Conversion interface. It extracts the underlying T from an Expr,
|
|
// applies the conversion, and wraps the result as a new Expr.
|
|
type registeredConversion[T, U any] struct {
|
|
conversion codec.Conversion[T, U]
|
|
inType, outType string
|
|
}
|
|
|
|
func (c registeredConversion[T, U]) Run(expr Expr) (Expr, error) {
|
|
t, ok := expr.Data().(T)
|
|
if !ok {
|
|
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
|
|
}
|
|
|
|
u, err := c.conversion(t)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewExpr(c.outType, u), nil
|
|
}
|
|
|
|
func (c registeredConversion[T, U]) InType() string { return c.inType }
|
|
|
|
func (c registeredConversion[T, U]) OutType() string { return c.outType }
|
|
|
|
// RegisterConversion registers a typed conversion function between two
|
|
// representations.
|
|
func RegisterConversion[T, U any](
|
|
registry *Registry,
|
|
conversion codec.Conversion[T, U],
|
|
inType, outType string,
|
|
) error {
|
|
registry.converter.Add(registeredConversion[T, U]{conversion, inType, outType})
|
|
|
|
return nil
|
|
}
|