feat: new engine format

This commit is contained in:
2026-02-07 00:12:50 -05:00
parent 8b80cea32b
commit 1d94fa70ff
11 changed files with 45 additions and 100 deletions

View File

@@ -1,3 +1,5 @@
// Package normalorder contains an engine that reduces a 'lambda.Expression'
// in the normal order.
package normalorder
import (
@@ -5,20 +7,20 @@ import (
"git.maximhutz.com/max/lambda/pkg/lambda"
)
type Process struct {
type process struct {
expr lambda.Expression
}
func (e Process) Get() (lambda.Expression, error) {
func (e process) Get() (lambda.Expression, error) {
return e.expr, nil
}
func (e *Process) Set(l lambda.Expression) error {
func (e *process) Set(l lambda.Expression) error {
e.expr = l
return nil
}
func (e *Process) Step(i int) bool {
func (e *process) Step(i int) bool {
for range i {
next, reduced := ReduceOnce(e.expr)
if !reduced {
@@ -31,12 +33,10 @@ func (e *Process) Step(i int) bool {
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] {
return &Process{}
}
var _ engine.Process[lambda.Expression] = (*Process)(nil)
var _ engine.Engine[lambda.Expression] = (*Engine)(nil)
var _ engine.Process[lambda.Expression] = (*process)(nil)
var _ engine.Engine[lambda.Expression] = NewProcess

View File

@@ -2,6 +2,11 @@ package normalorder
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) {
switch e := e.(type) {
case lambda.Abstraction: