fix: update main and tests for new Reducer API
Update call sites to match the new Reducer interface where the expression is passed to the constructor and Reduce() takes no arguments.
This commit is contained in:
@@ -6,20 +6,18 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/reducer"
|
||||
)
|
||||
|
||||
// Ensure NormalOrderReducer implements reducer.Reducer.
|
||||
var _ reducer.Reducer = (*NormalOrderReducer)(nil)
|
||||
|
||||
// NormalOrderReducer implements normal order (leftmost-outermost) reduction
|
||||
// for lambda calculus expressions.
|
||||
type NormalOrderReducer struct {
|
||||
emitter.BaseEmitter[reducer.Event]
|
||||
expression expr.Expression
|
||||
expression Expression
|
||||
}
|
||||
|
||||
// NewNormalOrderReducer creates a new normal order reducer.
|
||||
func NewNormalOrderReducer() *NormalOrderReducer {
|
||||
func NewNormalOrderReducer(expression Expression) *NormalOrderReducer {
|
||||
return &NormalOrderReducer{
|
||||
BaseEmitter: *emitter.New[reducer.Event](),
|
||||
expression: expression,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,23 +28,22 @@ func (r *NormalOrderReducer) Expression() expr.Expression {
|
||||
|
||||
// Reduce performs normal order reduction on a lambda expression.
|
||||
// The expression must be a lambda.Expression; other types are returned unchanged.
|
||||
func (r *NormalOrderReducer) Reduce(e expr.Expression) expr.Expression {
|
||||
r.expression = e
|
||||
func (r *NormalOrderReducer) Reduce() {
|
||||
r.Emit(reducer.StartEvent)
|
||||
it := NewIterator(&r.expression)
|
||||
|
||||
lambdaExpr, ok := e.(Expression)
|
||||
if !ok {
|
||||
return e
|
||||
for !it.Done() {
|
||||
if fn, arg, ok := IsViable(it.Current()); !ok {
|
||||
it.Next()
|
||||
} else {
|
||||
it.Swap(Substitute(fn.body, fn.parameter, arg))
|
||||
r.Emit(reducer.StepEvent)
|
||||
|
||||
if _, _, ok := IsViable(it.Parent()); ok {
|
||||
it.Back()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r.Emit(reducer.StartEvent)
|
||||
|
||||
ReduceAll(&lambdaExpr, func() {
|
||||
r.expression = lambdaExpr
|
||||
r.Emit(reducer.StepEvent)
|
||||
})
|
||||
|
||||
r.expression = lambdaExpr
|
||||
r.Emit(reducer.StopEvent)
|
||||
|
||||
return lambdaExpr
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ type Reducer interface {
|
||||
// Emits StartEvent before reduction, StepEvent after each step, and
|
||||
// StopEvent after completion.
|
||||
// Returns the final reduced expression.
|
||||
Reduce(e expr.Expression) expr.Expression
|
||||
Reduce()
|
||||
|
||||
// Expression returns the current expression state.
|
||||
Expression() expr.Expression
|
||||
|
||||
Reference in New Issue
Block a user