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:
@@ -7,18 +7,24 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||
)
|
||||
|
||||
// A Codec is a type-erased codec that serializes and deserializes expressions
|
||||
// as Expr values, regardless of the underlying representation type.
|
||||
type Codec interface {
|
||||
codec.Codec[Expr]
|
||||
|
||||
// InType returns the name of the representation this codec handles.
|
||||
InType() string
|
||||
}
|
||||
|
||||
type convertedCodec[T any] struct {
|
||||
// A registeredCodec adapts a typed codec.Codec[T] into the type-erased Codec
|
||||
// interface. It wraps decoded values into Expr on decode, and extracts the
|
||||
// underlying T from an Expr on encode.
|
||||
type registeredCodec[T any] struct {
|
||||
codec codec.Codec[T]
|
||||
inType string
|
||||
}
|
||||
|
||||
func (c convertedCodec[T]) Decode(s string) (Expr, error) {
|
||||
func (c registeredCodec[T]) Decode(s string) (Expr, error) {
|
||||
t, err := c.codec.Decode(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -27,7 +33,7 @@ func (c convertedCodec[T]) Decode(s string) (Expr, error) {
|
||||
return NewExpr(c.inType, t), nil
|
||||
}
|
||||
|
||||
func (c convertedCodec[T]) Encode(r Expr) (string, error) {
|
||||
func (c registeredCodec[T]) Encode(r Expr) (string, error) {
|
||||
t, ok := r.Data().(T)
|
||||
if !ok {
|
||||
dataType := reflect.TypeOf(r.Data())
|
||||
@@ -38,13 +44,15 @@ func (c convertedCodec[T]) Encode(r Expr) (string, error) {
|
||||
return c.codec.Encode(t)
|
||||
}
|
||||
|
||||
func (c convertedCodec[T]) InType() string { return c.inType }
|
||||
func (c registeredCodec[T]) InType() string { return c.inType }
|
||||
|
||||
// RegisterCodec registers a typed codec under the given representation name.
|
||||
// Returns an error if a codec for that representation is already registered.
|
||||
func RegisterCodec[T any](registry *Registry, m codec.Codec[T], inType string) error {
|
||||
if _, ok := registry.codecs[inType]; ok {
|
||||
return fmt.Errorf("Codec for '%s' already registered", inType)
|
||||
}
|
||||
|
||||
registry.codecs[inType] = convertedCodec[T]{m, inType}
|
||||
registry.codecs[inType] = registeredCodec[T]{m, inType}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user