Files
lambda/pkg/convert/saccharine_to_lambda.go
M.V. Hutz a3ee34732e refactor: rewrite CLI and internal architecture (#41)
## Description

The old architecture used a monolithic `main()` with a custom arg parser, an event-emitter-based runtime, and a plugin system for optional features.
This PR rewrites the CLI and internal architecture to be modular, extensible, and built around a registry of interchangeable components.

- Replace custom CLI arg parsing with Cobra subcommands (`convert`, `reduce`, `engine list`).
- Introduce a registry system (`internal/registry`) for marshalers, codecs, and engines, with BFS-based conversion path resolution.
- Add type-erased adapter layer (`internal/cli`) with `Repr`, `Engine`, `Process`, `Marshaler`, and `Conversion` interfaces wrapping generic `pkg/` types.
- Replace the event-emitter-based `Runtime` with a simpler `Engine`/`Process` model (`pkg/engine`).
- Add generic `Codec[T, U]` and `Marshaler[T]` interfaces (`pkg/codec`).
- Merge `saccharine/token` sub-package into `saccharine` and rename scanner functions from `parse*` to `scan*`.
- Make saccharine-to-lambda conversion bidirectional (encode and decode).
- Add `lambda.Marshaler` and `saccharine.Marshaler` implementing `codec.Marshaler`.
- Remove old infrastructure: `pkg/runtime`, `pkg/expr`, `internal/plugins`, `internal/statistics`.
- Add `make lint` target and update golangci-lint config.

### Decisions

- Cobra was chosen for the CLI framework to support nested subcommands and standard flag handling.
- The registry uses BFS to find conversion paths between representations, allowing multi-hop conversions without hardcoding routes.
- Type erasure via `cli.Repr` (wrapping `any`) enables the registry to work with heterogeneous types while keeping `pkg/` generics type-safe.
- The old plugin/event system was removed entirely rather than adapted, since the new `Process` model can support hooks differently in the future.

## Benefits

- Subcommands make the CLI self-documenting and easier to extend with new functionality.
- The registry pattern decouples representations, conversions, and engines, making it trivial to add new ones.
- BFS conversion routing means adding a single codec automatically enables transitive conversions.
- Simpler `Engine`/`Process` model reduces complexity compared to the event-emitter runtime.
- Consolidating the `token` sub-package reduces import depth and package sprawl.

## Checklist

- [x] Code follows conventional commit format.
- [x] Branch follows naming convention (`<type>/<description>`). Always use underscores.
- [ ] Tests pass (if applicable).
- [ ] Documentation updated (if applicable).

Reviewed-on: #41
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-02-07 03:25:32 +00:00

139 lines
3.5 KiB
Go

package convert
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/codec"
"git.maximhutz.com/max/lambda/pkg/lambda"
"git.maximhutz.com/max/lambda/pkg/saccharine"
)
func encodeAtom(n *saccharine.Atom) lambda.Expression {
return lambda.NewVariable(n.Name)
}
func encodeAbstraction(n *saccharine.Abstraction) lambda.Expression {
result := encodeExpression(n.Body)
parameters := n.Parameters
// If the function has no parameters, it is a thunk. Lambda calculus still
// requires _some_ parameter exists, so generate one.
if len(parameters) == 0 {
freeVars := result.GetFree()
freshName := lambda.GenerateFreshName(freeVars)
parameters = append(parameters, freshName)
}
for i := len(parameters) - 1; i >= 0; i-- {
result = lambda.NewAbstraction(parameters[i], result)
}
return result
}
func encodeApplication(n *saccharine.Application) lambda.Expression {
result := encodeExpression(n.Abstraction)
arguments := []lambda.Expression{}
for _, argument := range n.Arguments {
encodeedArgument := encodeExpression(argument)
arguments = append(arguments, encodeedArgument)
}
for _, argument := range arguments {
result = lambda.NewApplication(result, argument)
}
return result
}
func reduceLet(s *saccharine.LetStatement, e lambda.Expression) lambda.Expression {
var value lambda.Expression
if len(s.Parameters) == 0 {
value = encodeExpression(s.Body)
} else {
value = encodeAbstraction(saccharine.NewAbstraction(s.Parameters, s.Body))
}
return lambda.NewApplication(
lambda.NewAbstraction(s.Name, e),
value,
)
}
func reduceDeclare(s *saccharine.DeclareStatement, e lambda.Expression) lambda.Expression {
freshVar := lambda.GenerateFreshName(e.GetFree())
return lambda.NewApplication(
lambda.NewAbstraction(freshVar, e),
encodeExpression(s.Value),
)
}
func reduceStatement(s saccharine.Statement, e lambda.Expression) lambda.Expression {
switch s := s.(type) {
case *saccharine.DeclareStatement:
return reduceDeclare(s, e)
case *saccharine.LetStatement:
return reduceLet(s, e)
default:
panic(fmt.Errorf("unknown statement type: %v", s))
}
}
func encodeClause(n *saccharine.Clause) lambda.Expression {
result := encodeExpression(n.Returns)
for i := len(n.Statements) - 1; i >= 0; i-- {
result = reduceStatement(n.Statements[i], result)
}
return result
}
func encodeExpression(s saccharine.Expression) lambda.Expression {
switch s := s.(type) {
case *saccharine.Atom:
return encodeAtom(s)
case *saccharine.Abstraction:
return encodeAbstraction(s)
case *saccharine.Application:
return encodeApplication(s)
case *saccharine.Clause:
return encodeClause(s)
default:
panic(fmt.Errorf("unknown expression type: %T", s))
}
}
func decodeExression(l lambda.Expression) saccharine.Expression {
switch l := l.(type) {
case lambda.Variable:
return saccharine.NewAtom(l.Name())
case lambda.Abstraction:
return saccharine.NewAbstraction(
[]string{l.Parameter()},
decodeExression(l.Body()))
case lambda.Application:
return saccharine.NewApplication(
decodeExression(l.Abstraction()),
[]saccharine.Expression{decodeExression(l.Argument())})
default:
panic(fmt.Errorf("unknown expression type: %T", l))
}
}
type Saccharine2Lambda struct{}
func (c Saccharine2Lambda) Decode(l lambda.Expression) (saccharine.Expression, error) {
return decodeExression(l), nil
}
func (c Saccharine2Lambda) Encode(s saccharine.Expression) (lambda.Expression, error) {
return encodeExpression(s), nil
}
var _ codec.Codec[saccharine.Expression, lambda.Expression] = (*Saccharine2Lambda)(nil)