docs: document remaining packages and simplify AST types (#45)
## 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>
This commit was merged in pull request #45.
This commit is contained in:
@@ -6,19 +6,29 @@ import (
|
||||
"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)
|
||||
}
|
||||
|
||||
type convertedConversion[T, U any] struct {
|
||||
// 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 convertedConversion[T, U]) Run(expr Expr) (Expr, error) {
|
||||
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)
|
||||
@@ -32,12 +42,18 @@ func (c convertedConversion[T, U]) Run(expr Expr) (Expr, error) {
|
||||
return NewExpr(c.outType, u), nil
|
||||
}
|
||||
|
||||
func (c convertedConversion[T, U]) InType() string { return c.inType }
|
||||
func (c registeredConversion[T, U]) InType() string { return c.inType }
|
||||
|
||||
func (c convertedConversion[T, U]) OutType() string { return c.outType }
|
||||
func (c registeredConversion[T, U]) OutType() string { return c.outType }
|
||||
|
||||
func RegisterConversion[T, U any](registry *Registry, conversion func(T) (U, error), inType, outType string) error {
|
||||
registry.converter.Add(convertedConversion[T, U]{conversion, inType, 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user