style: restructure cli and registry packages (#43)
## 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>
This commit was merged in pull request #43.
This commit is contained in:
@@ -160,6 +160,8 @@ linters:
|
|||||||
arguments:
|
arguments:
|
||||||
# make error messages clearer
|
# make error messages clearer
|
||||||
- "sayRepetitiveInsteadOfStutters"
|
- "sayRepetitiveInsteadOfStutters"
|
||||||
|
# require comments on public interface methods
|
||||||
|
- "checkPublicInterface"
|
||||||
|
|
||||||
# incrementing an integer variable by 1 is recommended to be done using the `++` operator
|
# incrementing an integer variable by 1 is recommended to be done using the `++` operator
|
||||||
- name: increment-decrement
|
- name: increment-decrement
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/internal/cli"
|
"git.maximhutz.com/max/lambda/internal/cli"
|
||||||
"git.maximhutz.com/max/lambda/internal/config"
|
"git.maximhutz.com/max/lambda/internal/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LambdaReduce() *cobra.Command {
|
func LambdaReduce() *cobra.Command {
|
||||||
@@ -27,14 +27,14 @@ func LambdaReduce() *cobra.Command {
|
|||||||
inputPath := args[0]
|
inputPath := args[0]
|
||||||
|
|
||||||
// Get input source.
|
// Get input source.
|
||||||
var source config.Source
|
var source cli.Source
|
||||||
if inputPath == "-" {
|
if inputPath == "-" {
|
||||||
source = config.StdinSource{}
|
source = cli.StdinSource{}
|
||||||
} else {
|
} else {
|
||||||
source = config.FileSource{Path: inputPath}
|
source = cli.FileSource{Path: inputPath}
|
||||||
}
|
}
|
||||||
|
|
||||||
destination := config.StdoutDestination{}
|
destination := cli.StdoutDestination{}
|
||||||
|
|
||||||
r := GetRegistry()
|
r := GetRegistry()
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ func LambdaReduce() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find engine.
|
// Find engine.
|
||||||
var engine cli.Engine
|
var engine registry.Engine
|
||||||
if engineFlag == "" {
|
if engineFlag == "" {
|
||||||
if engine, err = r.GetDefaultEngine(inputRepr); err != nil {
|
if engine, err = r.GetDefaultEngine(inputRepr); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -77,8 +77,7 @@ func LambdaReduce() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create process.
|
// Create process.
|
||||||
process := engine.Load()
|
process, err := engine.Load(compiled)
|
||||||
err = process.Set(compiled)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.maximhutz.com/max/lambda/internal/cli"
|
|
||||||
"git.maximhutz.com/max/lambda/internal/registry"
|
"git.maximhutz.com/max/lambda/internal/registry"
|
||||||
"git.maximhutz.com/max/lambda/pkg/convert"
|
"git.maximhutz.com/max/lambda/pkg/convert"
|
||||||
"git.maximhutz.com/max/lambda/pkg/engine/normalorder"
|
"git.maximhutz.com/max/lambda/pkg/engine/normalorder"
|
||||||
@@ -13,14 +12,15 @@ func GetRegistry() *registry.Registry {
|
|||||||
r := registry.New()
|
r := registry.New()
|
||||||
|
|
||||||
// Codecs
|
// Codecs
|
||||||
r.MustAddConversions(cli.ConvertCodec(convert.Saccharine2Lambda{}, "saccharine", "lambda")...)
|
(registry.RegisterConversion(r, convert.Saccharine2Lambda, "saccharine", "lambda"))
|
||||||
|
(registry.RegisterConversion(r, convert.Lambda2Saccharine, "lambda", "saccharine"))
|
||||||
|
|
||||||
// Engines
|
// Engines
|
||||||
r.MustAddEngine(cli.ConvertEngine(normalorder.Engine{}, "normalorder", "lambda"))
|
(registry.RegisterEngine(r, normalorder.NewProcess, "normalorder", "lambda"))
|
||||||
|
|
||||||
// Marshalers
|
// Marshalers
|
||||||
r.MustAddMarshaler(cli.ConvertMarshaler(lambda.Marshaler{}, "lambda"))
|
(registry.RegisterCodec(r, lambda.Marshaler{}, "lambda"))
|
||||||
r.MustAddMarshaler(cli.ConvertMarshaler(saccharine.Marshaler{}, "saccharine"))
|
(registry.RegisterCodec(r, saccharine.Marshaler{}, "saccharine"))
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
2
internal/cli/cli.go
Normal file
2
internal/cli/cli.go
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// Package cli package provides various utilities to the 'lambda' program.
|
||||||
|
package cli
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Conversion interface {
|
|
||||||
InType() string
|
|
||||||
OutType() string
|
|
||||||
|
|
||||||
Run(Repr) (Repr, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type forwardCodec[T, U any] struct {
|
|
||||||
codec codec.Codec[T, U]
|
|
||||||
inType, outType string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c forwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
|
||||||
t, ok := r.Data().(T)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := c.codec.Encode(t)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewRepr(c.outType, u), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c forwardCodec[T, U]) InType() string { return c.inType }
|
|
||||||
|
|
||||||
func (c forwardCodec[T, U]) OutType() string { return c.outType }
|
|
||||||
|
|
||||||
type backwardCodec[T, U any] struct {
|
|
||||||
codec codec.Codec[T, U]
|
|
||||||
inType, outType string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c backwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
|
||||||
u, ok := r.Data().(U)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("could not parse '%v' as '%s'", r, c.outType)
|
|
||||||
}
|
|
||||||
|
|
||||||
t, err := c.codec.Decode(u)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewRepr(c.inType, t), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c backwardCodec[T, U]) InType() string { return c.outType }
|
|
||||||
|
|
||||||
func (c backwardCodec[T, U]) OutType() string { return c.inType }
|
|
||||||
|
|
||||||
func ConvertCodec[T, U any](e codec.Codec[T, U], inType, outType string) []Conversion {
|
|
||||||
return []Conversion{
|
|
||||||
forwardCodec[T, U]{e, inType, outType},
|
|
||||||
backwardCodec[T, U]{e, inType, outType},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +1,29 @@
|
|||||||
package config
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A method of writing output to the user.
|
// A Destination is method of writing output to the user.
|
||||||
type Destination interface {
|
type Destination interface {
|
||||||
// Write data to this destination.
|
// Write data to this destination.
|
||||||
Write(data string) error
|
Write(data string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// A destination writing to stdout.
|
// An StdoutDestination writes to stdout.
|
||||||
type StdoutDestination struct{}
|
type StdoutDestination struct{}
|
||||||
|
|
||||||
|
// Write outputs to standard output.
|
||||||
func (d StdoutDestination) Write(data string) error {
|
func (d StdoutDestination) Write(data string) error {
|
||||||
fmt.Println(data)
|
fmt.Println(data)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// A destination writing to a file.
|
// A FileDestination writes to a file.
|
||||||
type FileDestination struct{ Path string }
|
type FileDestination struct{ Path string }
|
||||||
|
|
||||||
|
// Write outputs to a file.
|
||||||
func (d FileDestination) Write(data string) error {
|
func (d FileDestination) Write(data string) error {
|
||||||
return os.WriteFile(d.Path, []byte(data+"\n"), 0644)
|
return os.WriteFile(d.Path, []byte(data+"\n"), 0644)
|
||||||
}
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import "git.maximhutz.com/max/lambda/pkg/engine"
|
|
||||||
|
|
||||||
type Engine interface {
|
|
||||||
Load() Process
|
|
||||||
Name() string
|
|
||||||
InType() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type convertedEngine[T any] struct {
|
|
||||||
engine engine.Engine[T]
|
|
||||||
name string
|
|
||||||
inType string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e convertedEngine[T]) InType() string { return e.inType }
|
|
||||||
|
|
||||||
func (e convertedEngine[T]) Name() string { return e.name }
|
|
||||||
|
|
||||||
func (e convertedEngine[T]) Load() Process {
|
|
||||||
return convertedProcess[T]{e.engine.Load(), e.inType}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ConvertEngine[T any](e engine.Engine[T], name, inType string) Engine {
|
|
||||||
return &convertedEngine[T]{e, name, inType}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Marshaler interface {
|
|
||||||
codec.Marshaler[Repr]
|
|
||||||
|
|
||||||
InType() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type convertedMarshaler[T any] struct {
|
|
||||||
codec codec.Marshaler[T]
|
|
||||||
inType string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c convertedMarshaler[T]) Decode(s string) (Repr, error) {
|
|
||||||
t, err := c.codec.Decode(s)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewRepr(c.inType, t), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c convertedMarshaler[T]) Encode(r Repr) (string, error) {
|
|
||||||
t, ok := r.Data().(T)
|
|
||||||
if !ok {
|
|
||||||
dataType := reflect.TypeOf(r.Data())
|
|
||||||
allowedType := reflect.TypeFor[T]()
|
|
||||||
return "", fmt.Errorf("marshaler for '%s' cannot parse '%s'", allowedType, dataType)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.codec.Encode(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c convertedMarshaler[T]) InType() string { return c.inType }
|
|
||||||
|
|
||||||
func ConvertMarshaler[T any](e codec.Marshaler[T], inType string) Marshaler {
|
|
||||||
return convertedMarshaler[T]{e, inType}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
type Repr interface {
|
|
||||||
// Id returns to name of the objects underlying representation. If is
|
|
||||||
// assumed that if two Repr objects have the same Id(), they share the same
|
|
||||||
// representation.
|
|
||||||
Id() string
|
|
||||||
|
|
||||||
Data() any
|
|
||||||
}
|
|
||||||
|
|
||||||
type baseRepr struct {
|
|
||||||
id string
|
|
||||||
data any
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r baseRepr) Id() string { return r.id }
|
|
||||||
|
|
||||||
func (r baseRepr) Data() any { return r.data }
|
|
||||||
|
|
||||||
func NewRepr(id string, data any) Repr { return baseRepr{id, data} }
|
|
||||||
@@ -1,24 +1,26 @@
|
|||||||
package config
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A method of extracting input from the user.
|
// A Source is a method of extracting input from the user.
|
||||||
type Source interface {
|
type Source interface {
|
||||||
// Fetch data from this source.
|
// Extract fetches data from this source.
|
||||||
Extract() (string, error)
|
Extract() (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A source defined by a string.
|
// A StringSource is defined by a string.
|
||||||
type StringSource struct{ Data string }
|
type StringSource struct{ Data string }
|
||||||
|
|
||||||
|
// Extract pulls input data from the internal string.
|
||||||
func (s StringSource) Extract() (string, error) { return s.Data, nil }
|
func (s StringSource) Extract() (string, error) { return s.Data, nil }
|
||||||
|
|
||||||
// A source pulling from standard input.
|
// A StdinSource pulls from standard input.
|
||||||
type StdinSource struct{}
|
type StdinSource struct{}
|
||||||
|
|
||||||
|
// Extract pulls input data from standard input.
|
||||||
func (s StdinSource) Extract() (string, error) {
|
func (s StdinSource) Extract() (string, error) {
|
||||||
data, err := io.ReadAll(os.Stdin)
|
data, err := io.ReadAll(os.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -28,9 +30,10 @@ func (s StdinSource) Extract() (string, error) {
|
|||||||
return string(data), nil
|
return string(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// A source reading from a file.
|
// A FileSource reads from a file.
|
||||||
type FileSource struct{ Path string }
|
type FileSource struct{ Path string }
|
||||||
|
|
||||||
|
// Extract pulls input data from the file source.
|
||||||
func (s FileSource) Extract() (string, error) {
|
func (s FileSource) Extract() (string, error) {
|
||||||
data, err := os.ReadFile(s.Path)
|
data, err := os.ReadFile(s.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
// Package "config" parses ad handles the user settings given to the program.
|
|
||||||
package config
|
|
||||||
|
|
||||||
// Configuration settings for the program.
|
|
||||||
type Config struct {
|
|
||||||
Source Source // The source code given to the program.
|
|
||||||
Destination Destination // The destination for the final result.
|
|
||||||
Verbose bool // Whether or not to print debug logs.
|
|
||||||
Explanation bool // Whether or not to print an explanation of the reduction.
|
|
||||||
Profile string // If not nil, print a CPU profile during execution.
|
|
||||||
Statistics bool // Whether or not to print statistics.
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Returns a structured logger with the appropriate configurations.
|
|
||||||
func (c Config) GetLogger() *slog.Logger {
|
|
||||||
// By default, only print out errors.
|
|
||||||
level := slog.LevelError
|
|
||||||
|
|
||||||
// If the user set the output to be "VERBOSE", return the debug logs.
|
|
||||||
if c.Verbose {
|
|
||||||
level = slog.LevelInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
return slog.New(
|
|
||||||
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
||||||
Level: level,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Extract the program configuration from the command-line arguments.
|
|
||||||
func FromArgs() (*Config, error) {
|
|
||||||
// Relevant flags.
|
|
||||||
verbose := flag.Bool("v", false, "Verbosity. If set, the program will print logs.")
|
|
||||||
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
|
|
||||||
statistics := flag.Bool("s", false, "Statistics. If set, the process will print various statistics about the run.")
|
|
||||||
profile := flag.String("p", "", "CPU profiling. If an output file is defined, the program will profile its execution and dump its results into it.")
|
|
||||||
file := flag.String("f", "", "File. If set, read source from the specified file.")
|
|
||||||
output := flag.String("o", "", "Output. If set, write result to the specified file. Use '-' for stdout (default).")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// Parse source type.
|
|
||||||
var source Source
|
|
||||||
if *file != "" {
|
|
||||||
// File flag takes precedence.
|
|
||||||
if flag.NArg() > 0 {
|
|
||||||
return nil, fmt.Errorf("cannot specify both -f flag and positional argument")
|
|
||||||
}
|
|
||||||
source = FileSource{Path: *file}
|
|
||||||
} else if flag.NArg() == 0 {
|
|
||||||
return nil, fmt.Errorf("no input given")
|
|
||||||
} else if flag.NArg() > 1 {
|
|
||||||
return nil, fmt.Errorf("more than 1 command-line argument")
|
|
||||||
} else {
|
|
||||||
// Positional argument.
|
|
||||||
if flag.Arg(0) == "-" {
|
|
||||||
source = StdinSource{}
|
|
||||||
} else {
|
|
||||||
source = StringSource{Data: flag.Arg(0)}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse destination type.
|
|
||||||
var destination Destination
|
|
||||||
if *output == "" || *output == "-" {
|
|
||||||
destination = StdoutDestination{}
|
|
||||||
} else {
|
|
||||||
destination = FileDestination{Path: *output}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Config{
|
|
||||||
Source: source,
|
|
||||||
Destination: destination,
|
|
||||||
Verbose: *verbose,
|
|
||||||
Explanation: *explanation,
|
|
||||||
Profile: *profile,
|
|
||||||
Statistics: *statistics,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
50
internal/registry/codec.go
Normal file
50
internal/registry/codec.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Codec interface {
|
||||||
|
codec.Codec[Repr]
|
||||||
|
|
||||||
|
InType() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type convertedCodec[T any] struct {
|
||||||
|
codec codec.Codec[T]
|
||||||
|
inType string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c convertedCodec[T]) Decode(s string) (Repr, error) {
|
||||||
|
t, err := c.codec.Decode(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewRepr(c.inType, t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c convertedCodec[T]) Encode(r Repr) (string, error) {
|
||||||
|
t, ok := r.Data().(T)
|
||||||
|
if !ok {
|
||||||
|
dataType := reflect.TypeOf(r.Data())
|
||||||
|
allowedType := reflect.TypeFor[T]()
|
||||||
|
return "", fmt.Errorf("Codec for '%s' cannot parse '%s'", allowedType, dataType)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.codec.Encode(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c convertedCodec[T]) InType() string { return c.inType }
|
||||||
|
|
||||||
|
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}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
43
internal/registry/conversion.go
Normal file
43
internal/registry/conversion.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Conversion interface {
|
||||||
|
InType() string
|
||||||
|
OutType() string
|
||||||
|
|
||||||
|
Run(Repr) (Repr, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type convertedConversion[T, U any] struct {
|
||||||
|
conversion codec.Conversion[T, U]
|
||||||
|
inType, outType string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c convertedConversion[T, U]) Run(r Repr) (Repr, error) {
|
||||||
|
t, ok := r.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 NewRepr(c.outType, u), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c convertedConversion[T, U]) InType() string { return c.inType }
|
||||||
|
|
||||||
|
func (c convertedConversion[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})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,27 +1,23 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
|
||||||
"git.maximhutz.com/max/lambda/internal/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Converter struct {
|
type Converter struct {
|
||||||
data map[string][]cli.Conversion
|
data map[string][]Conversion
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConverter() *Converter {
|
func NewConverter() *Converter {
|
||||||
return &Converter{data: map[string][]cli.Conversion{}}
|
return &Converter{data: map[string][]Conversion{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Converter) Add(c cli.Conversion) {
|
func (g *Converter) Add(c Conversion) {
|
||||||
conversionsFromIn, ok := g.data[c.InType()]
|
conversionsFromIn, ok := g.data[c.InType()]
|
||||||
if !ok {
|
if !ok {
|
||||||
conversionsFromIn = []cli.Conversion{}
|
conversionsFromIn = []Conversion{}
|
||||||
}
|
}
|
||||||
|
|
||||||
conversionsFromIn = append(conversionsFromIn, c)
|
conversionsFromIn = append(conversionsFromIn, c)
|
||||||
g.data[c.InType()] = conversionsFromIn
|
g.data[c.InType()] = conversionsFromIn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Converter) ConversionsFrom(t string) []cli.Conversion {
|
func (g *Converter) ConversionsFrom(t string) []Conversion {
|
||||||
return g.data[t]
|
return g.data[t]
|
||||||
}
|
}
|
||||||
|
|||||||
46
internal/registry/engine.go
Normal file
46
internal/registry/engine.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/engine"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Engine interface {
|
||||||
|
Load(Repr) (Process, error)
|
||||||
|
Name() string
|
||||||
|
InType() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type convertedEngine[T any] struct {
|
||||||
|
engine engine.Engine[T]
|
||||||
|
name string
|
||||||
|
inType string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e convertedEngine[T]) InType() string { return e.inType }
|
||||||
|
|
||||||
|
func (e convertedEngine[T]) Name() string { return e.name }
|
||||||
|
|
||||||
|
func (e convertedEngine[T]) Load(r Repr) (Process, error) {
|
||||||
|
t, ok := r.Data().(T)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("'ncorrent format '%s' for engine '%s'", r.ID(), e.inType)
|
||||||
|
}
|
||||||
|
|
||||||
|
process, err := e.engine(t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return convertedProcess[T]{process, e.inType}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterEngine[T any](registry *Registry, e engine.Engine[T], name, inType string) error {
|
||||||
|
if _, ok := registry.engines[name]; ok {
|
||||||
|
return fmt.Errorf("engine '%s' already registered", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
registry.engines[name] = &convertedEngine[T]{e, name, inType}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package cli
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/engine"
|
"git.maximhutz.com/max/lambda/pkg/engine"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,14 +26,6 @@ func (b convertedProcess[T]) Get() (Repr, error) {
|
|||||||
return NewRepr(b.inType, s), nil
|
return NewRepr(b.inType, s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b convertedProcess[T]) Set(r Repr) error {
|
|
||||||
if t, ok := r.Data().(T); ok {
|
|
||||||
return b.process.Set(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("Incorrent format '%s' for engine '%s'.", r.Id(), b.inType)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b convertedProcess[T]) Step(i int) bool {
|
func (b convertedProcess[T]) Step(i int) bool {
|
||||||
return b.process.Step(i)
|
return b.process.Step(i)
|
||||||
}
|
}
|
||||||
@@ -4,68 +4,23 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"iter"
|
"iter"
|
||||||
"maps"
|
"maps"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/internal/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
marshalers map[string]cli.Marshaler
|
codecs map[string]Codec
|
||||||
converter *Converter
|
converter *Converter
|
||||||
engines map[string]cli.Engine
|
engines map[string]Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Registry {
|
func New() *Registry {
|
||||||
return &Registry{
|
return &Registry{
|
||||||
marshalers: map[string]cli.Marshaler{},
|
codecs: map[string]Codec{},
|
||||||
converter: NewConverter(),
|
converter: NewConverter(),
|
||||||
engines: map[string]cli.Engine{},
|
engines: map[string]Engine{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) AddConversions(conversions ...cli.Conversion) error {
|
func (r Registry) GetEngine(name string) (Engine, error) {
|
||||||
for _, conversion := range conversions {
|
|
||||||
r.converter.Add(conversion)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (r *Registry) MustAddConversions(conversions ...cli.Conversion) {
|
|
||||||
if err := r.AddConversions(conversions...); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Registry) AddMarshaler(c cli.Marshaler) error {
|
|
||||||
if _, ok := r.marshalers[c.InType()]; ok {
|
|
||||||
return fmt.Errorf("marshaler for '%s' already registered", c.InType())
|
|
||||||
}
|
|
||||||
|
|
||||||
r.marshalers[c.InType()] = c
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Registry) MustAddMarshaler(c cli.Marshaler) {
|
|
||||||
if err := r.AddMarshaler(c); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Registry) AddEngine(e cli.Engine) error {
|
|
||||||
if _, ok := r.engines[e.Name()]; ok {
|
|
||||||
return fmt.Errorf("engine '%s' already registered", e.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
r.engines[e.Name()] = e
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Registry) MustAddEngine(e cli.Engine) {
|
|
||||||
if err := r.AddEngine(e); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Registry) GetEngine(name string) (cli.Engine, error) {
|
|
||||||
e, ok := r.engines[name]
|
e, ok := r.engines[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("engine '%s' not found", name)
|
return nil, fmt.Errorf("engine '%s' not found", name)
|
||||||
@@ -74,11 +29,11 @@ func (r Registry) GetEngine(name string) (cli.Engine, error) {
|
|||||||
return e, nil
|
return e, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Registry) ListEngines() iter.Seq[cli.Engine] {
|
func (r Registry) ListEngines() iter.Seq[Engine] {
|
||||||
return maps.Values(r.engines)
|
return maps.Values(r.engines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) GetDefaultEngine(id string) (cli.Engine, error) {
|
func (r *Registry) GetDefaultEngine(id string) (Engine, error) {
|
||||||
for _, engine := range r.engines {
|
for _, engine := range r.engines {
|
||||||
if engine.InType() == id {
|
if engine.InType() == id {
|
||||||
return engine, nil
|
return engine, nil
|
||||||
@@ -88,8 +43,8 @@ func (r *Registry) GetDefaultEngine(id string) (cli.Engine, error) {
|
|||||||
return nil, fmt.Errorf("no engine for '%s'", id)
|
return nil, fmt.Errorf("no engine for '%s'", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) ConvertTo(repr cli.Repr, outType string) (cli.Repr, error) {
|
func (r *Registry) ConvertTo(repr Repr, outType string) (Repr, error) {
|
||||||
path, err := r.ConversionPath(repr.Id(), outType)
|
path, err := r.ConversionPath(repr.ID(), outType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -105,17 +60,17 @@ func (r *Registry) ConvertTo(repr cli.Repr, outType string) (cli.Repr, error) {
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) Marshal(repr cli.Repr) (string, error) {
|
func (r *Registry) Marshal(repr Repr) (string, error) {
|
||||||
m, ok := r.marshalers[repr.Id()]
|
m, ok := r.codecs[repr.ID()]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("no marshaler for '%s'", repr.Id())
|
return "", fmt.Errorf("no marshaler for '%s'", repr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.Encode(repr)
|
return m.Encode(repr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) Unmarshal(s string, outType string) (cli.Repr, error) {
|
func (r *Registry) Unmarshal(s string, outType string) (Repr, error) {
|
||||||
m, ok := r.marshalers[outType]
|
m, ok := r.codecs[outType]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("no marshaler for '%s'", outType)
|
return nil, fmt.Errorf("no marshaler for '%s'", outType)
|
||||||
}
|
}
|
||||||
@@ -137,8 +92,8 @@ func reverse[T any](list []T) []T {
|
|||||||
return reversed
|
return reversed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) ConversionPath(from, to string) ([]cli.Conversion, error) {
|
func (r *Registry) ConversionPath(from, to string) ([]Conversion, error) {
|
||||||
backtrack := map[string]cli.Conversion{}
|
backtrack := map[string]Conversion{}
|
||||||
iteration := []string{from}
|
iteration := []string{from}
|
||||||
for len(iteration) > 0 {
|
for len(iteration) > 0 {
|
||||||
nextIteration := []string{}
|
nextIteration := []string{}
|
||||||
@@ -157,7 +112,7 @@ func (r *Registry) ConversionPath(from, to string) ([]cli.Conversion, error) {
|
|||||||
iteration = nextIteration
|
iteration = nextIteration
|
||||||
}
|
}
|
||||||
|
|
||||||
reversedPath := []cli.Conversion{}
|
reversedPath := []Conversion{}
|
||||||
current := to
|
current := to
|
||||||
for current != from {
|
for current != from {
|
||||||
conversion, ok := backtrack[current]
|
conversion, ok := backtrack[current]
|
||||||
|
|||||||
24
internal/registry/repr.go
Normal file
24
internal/registry/repr.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
// A Repr is a lambda calculus expression. It can have any type of
|
||||||
|
// representation, so long as that class is known to the registry it is handled
|
||||||
|
// by.
|
||||||
|
type Repr interface {
|
||||||
|
// ID returns the name of the underlying representation. It is assumed that
|
||||||
|
// if two expressions have the same Id(), they have the same representation.
|
||||||
|
ID() string
|
||||||
|
|
||||||
|
// The base expression data.
|
||||||
|
Data() any
|
||||||
|
}
|
||||||
|
|
||||||
|
type baseRepr struct {
|
||||||
|
id string
|
||||||
|
data any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r baseRepr) ID() string { return r.id }
|
||||||
|
|
||||||
|
func (r baseRepr) Data() any { return r.data }
|
||||||
|
|
||||||
|
func NewRepr(id string, data any) Repr { return baseRepr{id, data} }
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package codec
|
package codec
|
||||||
|
|
||||||
type Codec[T, U any] interface {
|
type Conversion[T, U any] = func(T) (U, error)
|
||||||
Encode(T) (U, error)
|
|
||||||
Decode(U) (T, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Marshaler[T any] = Codec[T, string]
|
type Codec[T any] interface {
|
||||||
|
Encode(T) (string, error)
|
||||||
|
Decode(string) (T, error)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package convert
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||||
)
|
)
|
||||||
@@ -125,14 +124,10 @@ func decodeExression(l lambda.Expression) saccharine.Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Saccharine2Lambda struct{}
|
func Lambda2Saccharine(l lambda.Expression) (saccharine.Expression, error) {
|
||||||
|
|
||||||
func (c Saccharine2Lambda) Decode(l lambda.Expression) (saccharine.Expression, error) {
|
|
||||||
return decodeExression(l), nil
|
return decodeExression(l), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Saccharine2Lambda) Encode(s saccharine.Expression) (lambda.Expression, error) {
|
func Saccharine2Lambda(s saccharine.Expression) (lambda.Expression, error) {
|
||||||
return encodeExpression(s), nil
|
return encodeExpression(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ codec.Codec[saccharine.Expression, lambda.Expression] = (*Saccharine2Lambda)(nil)
|
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
package emitter
|
|
||||||
|
|
||||||
import "git.maximhutz.com/max/lambda/pkg/set"
|
|
||||||
|
|
||||||
type Emitter[E comparable] interface {
|
|
||||||
On(E, func()) Listener[E]
|
|
||||||
Off(Listener[E])
|
|
||||||
Emit(E)
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseEmitter[E comparable] struct {
|
|
||||||
listeners map[E]set.Set[Listener[E]]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEmitter[E]) On(kind E, fn func()) Listener[E] {
|
|
||||||
if e.listeners[kind] == nil {
|
|
||||||
e.listeners[kind] = set.New[Listener[E]]()
|
|
||||||
}
|
|
||||||
|
|
||||||
listener := &BaseListener[E]{kind, fn}
|
|
||||||
e.listeners[kind].Add(listener)
|
|
||||||
return listener
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEmitter[E]) Off(listener Listener[E]) {
|
|
||||||
kind := listener.Kind()
|
|
||||||
if e.listeners[kind] != nil {
|
|
||||||
e.listeners[kind].Remove(listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEmitter[E]) Emit(event E) {
|
|
||||||
if e.listeners[event] == nil {
|
|
||||||
e.listeners[event] = set.New[Listener[E]]()
|
|
||||||
}
|
|
||||||
|
|
||||||
for listener := range e.listeners[event].Items() {
|
|
||||||
listener.Run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func New[E comparable]() *BaseEmitter[E] {
|
|
||||||
return &BaseEmitter[E]{
|
|
||||||
listeners: map[E]set.Set[Listener[E]]{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package emitter
|
|
||||||
|
|
||||||
type Listener[E comparable] interface {
|
|
||||||
Kind() E
|
|
||||||
Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseListener[E comparable] struct {
|
|
||||||
kind E
|
|
||||||
fn func()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l BaseListener[E]) Kind() E {
|
|
||||||
return l.kind
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l BaseListener[E]) Run() {
|
|
||||||
l.fn()
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
|
// Package engine defines a general process of reducing a lambda calculus
|
||||||
|
// expression.
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
type Engine[T any] interface {
|
// A Process handles the reduction of a
|
||||||
Load() Process[T]
|
|
||||||
}
|
|
||||||
|
|
||||||
type Process[T any] interface {
|
type Process[T any] interface {
|
||||||
Get() (T, error)
|
Get() (T, error)
|
||||||
Set(T) error
|
|
||||||
Step(int) bool
|
Step(int) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// An Engine is an object that handles
|
||||||
|
type Engine[T any] = func(T) (Process[T], error)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// Package normalorder contains an engine that reduces a 'lambda.Expression'
|
||||||
|
// in the normal order.
|
||||||
package normalorder
|
package normalorder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -5,20 +7,20 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Process struct {
|
type process struct {
|
||||||
expr lambda.Expression
|
expr lambda.Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Process) Get() (lambda.Expression, error) {
|
func (e process) Get() (lambda.Expression, error) {
|
||||||
return e.expr, nil
|
return e.expr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Process) Set(l lambda.Expression) error {
|
func (e *process) Set(l lambda.Expression) error {
|
||||||
e.expr = l
|
e.expr = l
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Process) Step(i int) bool {
|
func (e *process) Step(i int) bool {
|
||||||
for range i {
|
for range i {
|
||||||
next, reduced := ReduceOnce(e.expr)
|
next, reduced := ReduceOnce(e.expr)
|
||||||
if !reduced {
|
if !reduced {
|
||||||
@@ -31,12 +33,10 @@ func (e *Process) Step(i int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
type Engine struct {
|
// NewProcess creates a new redution process.
|
||||||
|
func NewProcess(expression lambda.Expression) (engine.Process[lambda.Expression], error) {
|
||||||
|
return &process{expr: expression}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Engine) Load() engine.Process[lambda.Expression] {
|
var _ engine.Process[lambda.Expression] = (*process)(nil)
|
||||||
return &Process{}
|
var _ engine.Engine[lambda.Expression] = NewProcess
|
||||||
}
|
|
||||||
|
|
||||||
var _ engine.Process[lambda.Expression] = (*Process)(nil)
|
|
||||||
var _ engine.Engine[lambda.Expression] = (*Engine)(nil)
|
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ package normalorder
|
|||||||
|
|
||||||
import "git.maximhutz.com/max/lambda/pkg/lambda"
|
import "git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
|
|
||||||
|
// ReduceOnce attempts to apply a single reduction to a lambda expression.
|
||||||
|
// It returns (1) the final expression (reduced, or not), and (2) whether or not
|
||||||
|
// a reduction was applied.
|
||||||
|
//
|
||||||
|
// If a reduction is not applied, it returns the original expression.
|
||||||
func ReduceOnce(e lambda.Expression) (lambda.Expression, bool) {
|
func ReduceOnce(e lambda.Expression) (lambda.Expression, bool) {
|
||||||
switch e := e.(type) {
|
switch e := e.(type) {
|
||||||
case lambda.Abstraction:
|
case lambda.Abstraction:
|
||||||
@@ -16,4 +16,4 @@ func (m Marshaler) Encode(e Expression) (string, error) {
|
|||||||
return e.String(), nil
|
return e.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ codec.Marshaler[Expression] = (*Marshaler)(nil)
|
var _ codec.Codec[Expression] = (*Marshaler)(nil)
|
||||||
@@ -21,4 +21,4 @@ func (m Marshaler) Encode(e Expression) (string, error) {
|
|||||||
return stringifyExpression(e), nil
|
return stringifyExpression(e), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ codec.Marshaler[Expression] = (*Marshaler)(nil)
|
var _ codec.Codec[Expression] = (*Marshaler)(nil)
|
||||||
Reference in New Issue
Block a user