Closes #26 - Added -i flag to select interpreter (lambda or debruijn) - Created debruijn package with Expression interface - Variable contains index and optional label - Abstraction contains only body (no parameter) - Application structure remains similar - Implemented De Bruijn reduction without variable renaming - Shift operation handles index adjustments - Substitute replaces by index instead of name - Abstracted Engine into interface with two implementations - LambdaEngine: original named variable engine - DeBruijnEngine: new index-based engine - Added conversion functions between representations - LambdaToDeBruijn: converts named to indexed - DeBruijnToLambda: converts indexed back to named - SaccharineToDeBruijn: direct saccharine to De Bruijn - Updated main to switch engines based on -i flag - All test samples pass with both engines Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
37 lines
865 B
Go
37 lines
865 B
Go
package engine
|
|
|
|
import (
|
|
"git.maximhutz.com/max/lambda/internal/config"
|
|
"git.maximhutz.com/max/lambda/pkg/emitter"
|
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
|
)
|
|
|
|
// A process for reducing one λ-expression using named variables.
|
|
type LambdaEngine struct {
|
|
Config *config.Config
|
|
Expression *lambda.Expression
|
|
emitter.Emitter
|
|
}
|
|
|
|
// NewLambdaEngine creates a new lambda engine.
|
|
func NewLambdaEngine(config *config.Config, expression interface{}) *LambdaEngine {
|
|
expr := expression.(*lambda.Expression)
|
|
return &LambdaEngine{Config: config, Expression: expr}
|
|
}
|
|
|
|
// Run begins the reduction process.
|
|
func (e *LambdaEngine) Run() {
|
|
e.Emit("start")
|
|
|
|
lambda.ReduceAll(e.Expression, func() {
|
|
e.Emit("step")
|
|
})
|
|
|
|
e.Emit("end")
|
|
}
|
|
|
|
// GetResult returns the stringified result.
|
|
func (e *LambdaEngine) GetResult() string {
|
|
return lambda.Stringify(*e.Expression)
|
|
}
|