refactor: move event system to reducer, remove engine package #32
@@ -33,8 +33,8 @@ func main() {
|
||||
compiled := convert.SaccharineToLambda(ast)
|
||||
logger.Info("compiled λ expression", "tree", compiled.String())
|
||||
|
||||
// Create reducer.
|
||||
reducer := lambda.NewNormalOrderReducer()
|
||||
// Create reducer with the compiled expression.
|
||||
reducer := lambda.NewNormalOrderReducer(compiled)
|
||||
|
||||
// If the user selected to track CPU performance, attach a profiler.
|
||||
if options.Profile != "" {
|
||||
@@ -58,7 +58,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Run reduction.
|
||||
reducer.Reduce(compiled)
|
||||
reducer.Reduce()
|
||||
|
||||
// Return the final reduced result.
|
||||
result := reducer.Expression().String()
|
||||
|
||||
@@ -30,8 +30,8 @@ func runSample(samplePath string) (string, error) {
|
||||
compiled := convert.SaccharineToLambda(ast)
|
||||
|
||||
// Create and run the reducer.
|
||||
reducer := lambda.NewNormalOrderReducer()
|
||||
reducer.Reduce(compiled)
|
||||
reducer := lambda.NewNormalOrderReducer(compiled)
|
||||
reducer.Reduce()
|
||||
|
||||
return reducer.Expression().String() + "\n", nil
|
||||
}
|
||||
|
||||
@@ -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