feat: explanation as observer too
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package executer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
@@ -10,28 +9,22 @@ import (
|
||||
)
|
||||
|
||||
type Executor struct {
|
||||
Config *config.Config
|
||||
Config *config.Config
|
||||
Expression *lambda.Expression
|
||||
emitter.Emitter
|
||||
}
|
||||
|
||||
func New(config *config.Config) *Executor {
|
||||
return &Executor{Config: config}
|
||||
func New(config *config.Config, expression *lambda.Expression) *Executor {
|
||||
return &Executor{Config: config, Expression: expression}
|
||||
}
|
||||
|
||||
func (e Executor) Run(expr *lambda.Expression) {
|
||||
func (e Executor) Run() {
|
||||
e.Emit("start")
|
||||
|
||||
if e.Config.Explanation {
|
||||
fmt.Println(lambda.Stringify(*expr))
|
||||
}
|
||||
|
||||
for lambda.ReduceOnce(expr) {
|
||||
for lambda.ReduceOnce(e.Expression) {
|
||||
e.Emit("step")
|
||||
if e.Config.Verbose {
|
||||
slog.Info("reduction", "tree", lambda.Stringify(*expr))
|
||||
}
|
||||
if e.Config.Explanation {
|
||||
fmt.Println(" =", lambda.Stringify(*expr))
|
||||
slog.Info("reduction", "tree", lambda.Stringify(*e.Expression))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
internal/explanation/tracker.go
Normal file
28
internal/explanation/tracker.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package explanation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/executer"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Tracker struct {
|
||||
process *executer.Executor
|
||||
}
|
||||
|
||||
func Track(process *executer.Executor) *Tracker {
|
||||
tracker := &Tracker{process: process}
|
||||
process.On("start", tracker.Start)
|
||||
process.On("step", tracker.Step)
|
||||
|
||||
return tracker
|
||||
}
|
||||
|
||||
func (t *Tracker) Start() {
|
||||
fmt.Println(lambda.Stringify(*t.process.Expression))
|
||||
}
|
||||
|
||||
func (t *Tracker) Step() {
|
||||
fmt.Println(" =", lambda.Stringify(*t.process.Expression))
|
||||
}
|
||||
Reference in New Issue
Block a user