refactor: move event system to reducer, remove engine package #32
@@ -33,8 +33,8 @@ func main() {
|
|||||||
compiled := convert.SaccharineToLambda(ast)
|
compiled := convert.SaccharineToLambda(ast)
|
||||||
logger.Info("compiled λ expression", "tree", compiled.String())
|
logger.Info("compiled λ expression", "tree", compiled.String())
|
||||||
|
|
||||||
// Create reducer.
|
// Create reducer with the compiled expression.
|
||||||
reducer := lambda.NewNormalOrderReducer()
|
reducer := lambda.NewNormalOrderReducer(compiled)
|
||||||
|
|
||||||
// If the user selected to track CPU performance, attach a profiler.
|
// If the user selected to track CPU performance, attach a profiler.
|
||||||
if options.Profile != "" {
|
if options.Profile != "" {
|
||||||
@@ -58,7 +58,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run reduction.
|
// Run reduction.
|
||||||
reducer.Reduce(compiled)
|
reducer.Reduce()
|
||||||
|
|
||||||
// Return the final reduced result.
|
// Return the final reduced result.
|
||||||
result := reducer.Expression().String()
|
result := reducer.Expression().String()
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ func runSample(samplePath string) (string, error) {
|
|||||||
compiled := convert.SaccharineToLambda(ast)
|
compiled := convert.SaccharineToLambda(ast)
|
||||||
|
|
||||||
// Create and run the reducer.
|
// Create and run the reducer.
|
||||||
reducer := lambda.NewNormalOrderReducer()
|
reducer := lambda.NewNormalOrderReducer(compiled)
|
||||||
reducer.Reduce(compiled)
|
reducer.Reduce()
|
||||||
|
|
||||||
return reducer.Expression().String() + "\n", nil
|
return reducer.Expression().String() + "\n", nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,20 +6,18 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/pkg/reducer"
|
"git.maximhutz.com/max/lambda/pkg/reducer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Ensure NormalOrderReducer implements reducer.Reducer.
|
|
||||||
var _ reducer.Reducer = (*NormalOrderReducer)(nil)
|
|
||||||
|
|
||||||
// NormalOrderReducer implements normal order (leftmost-outermost) reduction
|
// NormalOrderReducer implements normal order (leftmost-outermost) reduction
|
||||||
// for lambda calculus expressions.
|
// for lambda calculus expressions.
|
||||||
type NormalOrderReducer struct {
|
type NormalOrderReducer struct {
|
||||||
emitter.BaseEmitter[reducer.Event]
|
emitter.BaseEmitter[reducer.Event]
|
||||||
expression expr.Expression
|
expression Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNormalOrderReducer creates a new normal order reducer.
|
// NewNormalOrderReducer creates a new normal order reducer.
|
||||||
func NewNormalOrderReducer() *NormalOrderReducer {
|
func NewNormalOrderReducer(expression Expression) *NormalOrderReducer {
|
||||||
return &NormalOrderReducer{
|
return &NormalOrderReducer{
|
||||||
BaseEmitter: *emitter.New[reducer.Event](),
|
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.
|
// Reduce performs normal order reduction on a lambda expression.
|
||||||
// The expression must be a lambda.Expression; other types are returned unchanged.
|
// The expression must be a lambda.Expression; other types are returned unchanged.
|
||||||
func (r *NormalOrderReducer) Reduce(e expr.Expression) expr.Expression {
|
func (r *NormalOrderReducer) Reduce() {
|
||||||
r.expression = e
|
|
||||||
|
|
||||||
lambdaExpr, ok := e.(Expression)
|
|
||||||
if !ok {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
r.Emit(reducer.StartEvent)
|
r.Emit(reducer.StartEvent)
|
||||||
|
it := NewIterator(&r.expression)
|
||||||
|
|
||||||
ReduceAll(&lambdaExpr, func() {
|
for !it.Done() {
|
||||||
r.expression = lambdaExpr
|
if fn, arg, ok := IsViable(it.Current()); !ok {
|
||||||
|
it.Next()
|
||||||
|
} else {
|
||||||
|
it.Swap(Substitute(fn.body, fn.parameter, arg))
|
||||||
r.Emit(reducer.StepEvent)
|
r.Emit(reducer.StepEvent)
|
||||||
})
|
|
||||||
|
|
||||||
r.expression = lambdaExpr
|
if _, _, ok := IsViable(it.Parent()); ok {
|
||||||
r.Emit(reducer.StopEvent)
|
it.Back()
|
||||||
|
}
|
||||||
return lambdaExpr
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Emit(reducer.StopEvent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type Reducer interface {
|
|||||||
// Emits StartEvent before reduction, StepEvent after each step, and
|
// Emits StartEvent before reduction, StepEvent after each step, and
|
||||||
// StopEvent after completion.
|
// StopEvent after completion.
|
||||||
// Returns the final reduced expression.
|
// Returns the final reduced expression.
|
||||||
Reduce(e expr.Expression) expr.Expression
|
Reduce()
|
||||||
|
|
||||||
// Expression returns the current expression state.
|
// Expression returns the current expression state.
|
||||||
Expression() expr.Expression
|
Expression() expr.Expression
|
||||||
|
|||||||
Reference in New Issue
Block a user