style: restructure cli and registry packages #43
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func GetRegistry() *registry.Registry {
|
|||||||
(registry.RegisterCodec(r, convert.Saccharine2Lambda{}, "saccharine", "lambda"))
|
(registry.RegisterCodec(r, convert.Saccharine2Lambda{}, "saccharine", "lambda"))
|
||||||
|
|
||||||
// Engines
|
// Engines
|
||||||
(registry.RegisterEngine(r, normalorder.Engine{}, "normalorder", "lambda"))
|
(registry.RegisterEngine(r, normalorder.NewProcess, "normalorder", "lambda"))
|
||||||
|
|
||||||
// Marshalers
|
// Marshalers
|
||||||
(registry.RegisterMarshaler(r, lambda.Marshaler{}, "lambda"))
|
(registry.RegisterMarshaler(r, lambda.Marshaler{}, "lambda"))
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Engine interface {
|
type Engine interface {
|
||||||
Load() Process
|
Load(Repr) (Process, error)
|
||||||
Name() string
|
Name() string
|
||||||
InType() string
|
InType() string
|
||||||
}
|
}
|
||||||
@@ -22,8 +22,18 @@ func (e convertedEngine[T]) InType() string { return e.inType }
|
|||||||
|
|
||||||
func (e convertedEngine[T]) Name() string { return e.name }
|
func (e convertedEngine[T]) Name() string { return e.name }
|
||||||
|
|
||||||
func (e convertedEngine[T]) Load() Process {
|
func (e convertedEngine[T]) Load(r Repr) (Process, error) {
|
||||||
return convertedProcess[T]{e.engine.Load(), e.inType}
|
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 {
|
func RegisterEngine[T any](registry *Registry, e engine.Engine[T], name, inType string) error {
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package registry
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package registry
|
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 {
|
type Repr interface {
|
||||||
// Id returns to name of the objects underlying representation. If is
|
// ID returns the name of the underlying representation. It is assumed that
|
||||||
// assumed that if two Repr objects have the same Id(), they share the same
|
// if two expressions have the same Id(), they have the same representation.
|
||||||
// representation.
|
|
||||||
ID() string
|
ID() string
|
||||||
|
|
||||||
|
// The base expression data.
|
||||||
Data() any
|
Data() any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
||||||
Reference in New Issue
Block a user