## Description The `internal/cli` package had grown to contain both CLI utilities (source/destination I/O) and registry-level abstractions (repr, conversion, engine, marshaler). This PR separates concerns by moving registry types into `internal/registry` and keeping only CLI I/O types in `internal/cli`. It also simplifies several core abstractions and aligns naming conventions. - Move `Source`, `Destination` from `internal/config` to `internal/cli`. - Move `Repr`, `Conversion`, `Engine`, `Process`, `Codec` from `internal/cli` to `internal/registry`. - Rename "marshalers" to "codecs" throughout the codebase. - Simplify `codec.Codec[T, U]` to `codec.Codec[T]` (string-based marshaling only). - Add `codec.Conversion[T, U]` as a function type alias. - Change `engine.Engine[T]` from an interface to a function type. - Merge `Engine.Load()` + `Process.Set()` into a single `Engine.Load(Repr)` call. - Convert `Saccharine2Lambda` from a struct to standalone conversion functions. - Replace registry methods (`MustAddMarshaler`, `MustAddEngine`, `MustAddConversions`) with generic free functions (`RegisterCodec`, `RegisterEngine`, `RegisterConversion`). - Remove unused `internal/config` package (`Config`, `GetLogger`, `ParseFromArgs`). - Remove unused `pkg/emitter` package. - Rename `Id()` to `ID()` per Go conventions. - Add documentation comments and enable `checkPublicInterface` lint rule. - Rename `reduce_one.go` to `reduce_once.go`. ### Decisions - `Engine[T]` is now a function type (`func(T) (Process[T], error)`) rather than an interface, since the only method was `Load`. - `Codec[T, U]` was split into `Codec[T]` (string marshaling) and `Conversion[T, U]` (type-to-type conversion function), which better reflects how they are actually used. - Registration uses free generic functions (`RegisterCodec`, `RegisterEngine`, `RegisterConversion`) instead of methods on `Registry`, enabling type inference at the call site. ## Benefits - Clearer separation of concerns between CLI I/O and the registry's internal type system. - Simpler abstractions: fewer interfaces, fewer wrapper types, fewer indirections. - Removing unused packages (`config`, `emitter`) reduces maintenance burden. - Naming conventions (`ID`, codecs, `reduce_once`) are more idiomatic. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). - [x] Tests pass (if applicable). - [x] Documentation updated (if applicable). Reviewed-on: #43 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
109 lines
2.2 KiB
Go
109 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.maximhutz.com/max/lambda/internal/cli"
|
|
"git.maximhutz.com/max/lambda/internal/registry"
|
|
)
|
|
|
|
func LambdaReduce() *cobra.Command {
|
|
var inputReprFlag string
|
|
var engineFlag string
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "reduce <input-file>",
|
|
Short: "Reduce a lambda calculus expression",
|
|
SilenceUsage: true,
|
|
Aliases: []string{"run"},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var err error
|
|
if len(args) != 1 {
|
|
return cmd.Help()
|
|
}
|
|
|
|
inputPath := args[0]
|
|
|
|
// Get input source.
|
|
var source cli.Source
|
|
if inputPath == "-" {
|
|
source = cli.StdinSource{}
|
|
} else {
|
|
source = cli.FileSource{Path: inputPath}
|
|
}
|
|
|
|
destination := cli.StdoutDestination{}
|
|
|
|
r := GetRegistry()
|
|
|
|
// Get input.
|
|
input, err := source.Extract()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Use flag if provided, otherwise infer from extension.
|
|
inputRepr := inputReprFlag
|
|
if inputRepr == "" {
|
|
if inputRepr, err = inferReprFromPath(inputPath); err != nil {
|
|
return fmt.Errorf("input file: %w", err)
|
|
}
|
|
}
|
|
|
|
// Find engine.
|
|
var engine registry.Engine
|
|
if engineFlag == "" {
|
|
if engine, err = r.GetDefaultEngine(inputRepr); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if engine, err = r.GetEngine(engineFlag); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Parse code into syntax tree.
|
|
repr, err := r.Unmarshal(input, inputRepr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Compile expression to lambda calculus.
|
|
compiled, err := r.ConvertTo(repr, "lambda")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create process.
|
|
process, err := engine.Load(compiled)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Run reduction.
|
|
for process.Step(1) {
|
|
}
|
|
|
|
// Return the final reduced result.
|
|
result, err := process.Get()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
output, err := r.Marshal(result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return destination.Write(output)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&inputReprFlag, "input", "i", "", "Input representation (inferred from extension if unset)")
|
|
cmd.Flags().StringVarP(&engineFlag, "engine", "e", "", "Reduction engine (inferred from '--input' if unset)")
|
|
|
|
return cmd
|
|
}
|