refactor: move event system to reducer, remove engine package #32
@@ -1,30 +0,0 @@
|
|||||||
package lambda
|
|
||||||
|
|
||||||
func IsViable(e *Expression) (*Abstraction, Expression, bool) {
|
|
||||||
if e == nil {
|
|
||||||
return nil, nil, false
|
|
||||||
} else if app, appOk := (*e).(*Application); !appOk {
|
|
||||||
return nil, nil, false
|
|
||||||
} else if fn, fnOk := app.abstraction.(*Abstraction); !fnOk {
|
|
||||||
return nil, nil, false
|
|
||||||
} else {
|
|
||||||
return fn, app.argument, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReduceAll(e *Expression, step func()) {
|
|
||||||
it := NewIterator(e)
|
|
||||||
|
|
||||||
for !it.Done() {
|
|
||||||
if fn, arg, ok := IsViable(it.Current()); !ok {
|
|
||||||
it.Next()
|
|
||||||
} else {
|
|
||||||
it.Swap(Substitute(fn.body, fn.parameter, arg))
|
|
||||||
step()
|
|
||||||
|
|
||||||
if _, _, ok := IsViable(it.Parent()); ok {
|
|
||||||
it.Back()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -26,6 +26,18 @@ func (r *NormalOrderReducer) Expression() expr.Expression {
|
|||||||
return r.expression
|
return r.expression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isViable(e *Expression) (*Abstraction, Expression, bool) {
|
||||||
|
if e == nil {
|
||||||
|
return nil, nil, false
|
||||||
|
} else if app, appOk := (*e).(*Application); !appOk {
|
||||||
|
return nil, nil, false
|
||||||
|
} else if fn, fnOk := app.abstraction.(*Abstraction); !fnOk {
|
||||||
|
return nil, nil, false
|
||||||
|
} else {
|
||||||
|
return fn, app.argument, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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() {
|
func (r *NormalOrderReducer) Reduce() {
|
||||||
@@ -33,13 +45,13 @@ func (r *NormalOrderReducer) Reduce() {
|
|||||||
it := NewIterator(&r.expression)
|
it := NewIterator(&r.expression)
|
||||||
|
|
||||||
for !it.Done() {
|
for !it.Done() {
|
||||||
if fn, arg, ok := IsViable(it.Current()); !ok {
|
if fn, arg, ok := isViable(it.Current()); !ok {
|
||||||
it.Next()
|
it.Next()
|
||||||
} else {
|
} else {
|
||||||
it.Swap(Substitute(fn.body, fn.parameter, arg))
|
it.Swap(Substitute(fn.body, fn.parameter, arg))
|
||||||
r.Emit(reducer.StepEvent)
|
r.Emit(reducer.StepEvent)
|
||||||
|
|
||||||
if _, _, ok := IsViable(it.Parent()); ok {
|
if _, _, ok := isViable(it.Parent()); ok {
|
||||||
it.Back()
|
it.Back()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user