feat: explanation as observer too

This commit is contained in:
2025-12-29 01:31:09 -05:00
parent 5ceb845137
commit 17cf8f86f8
6 changed files with 47 additions and 26 deletions

View File

@@ -4,17 +4,13 @@ it:
@ go build -o ${BINARY_NAME} ./cmd/lambda @ go build -o ${BINARY_NAME} ./cmd/lambda
@ chmod +x ${BINARY_NAME} @ chmod +x ${BINARY_NAME}
simple: it TEST=simple
@ ./lambda.exe -p profile/cpu.prof - < ./samples/simple.txt > program.out
thunk: it run: it
@ ./lambda.exe -p profile/cpu.prof - < ./samples/thunk.txt > program.out @ ./lambda.exe - < ./samples/$(TEST).txt > program.out
saccharine: it profile: it
@ ./lambda.exe -p profile/cpu.prof - < ./samples/saccharine.txt > program.out @ ./lambda.exe -p profile/cpu.prof - < ./samples/$(TEST).txt > program.out
church: it
@ ./lambda.exe -p profile/cpu.prof - < ./samples/church.txt > program.out
prof: prof:
@ go tool pprof -top profile/cpu.prof @ go tool pprof -top profile/cpu.prof

View File

@@ -7,6 +7,7 @@ import (
"git.maximhutz.com/max/lambda/internal/cli" "git.maximhutz.com/max/lambda/internal/cli"
"git.maximhutz.com/max/lambda/internal/config" "git.maximhutz.com/max/lambda/internal/config"
"git.maximhutz.com/max/lambda/internal/executer" "git.maximhutz.com/max/lambda/internal/executer"
"git.maximhutz.com/max/lambda/internal/explanation"
"git.maximhutz.com/max/lambda/internal/performance" "git.maximhutz.com/max/lambda/internal/performance"
"git.maximhutz.com/max/lambda/internal/statistics" "git.maximhutz.com/max/lambda/internal/statistics"
"git.maximhutz.com/max/lambda/pkg/convert" "git.maximhutz.com/max/lambda/pkg/convert"
@@ -43,20 +44,23 @@ func main() {
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled)) logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
} }
process := executer.New(options) process := executer.New(options, &compiled)
if options.Profile != "" { if options.Profile != "" {
profiler := performance.Track(options.Profile) profiler := performance.Track(options.Profile)
process.On("start", profiler.Start) process.On("start", profiler.Start)
process.On("end", profiler.End) process.On("end", profiler.End)
} }
if options.Explanation {
explanation.Track(process)
}
statistics := statistics.Track() statistics := statistics.Track()
process.On("start", statistics.Start) process.On("start", statistics.Start)
process.On("step", statistics.Step) process.On("step", statistics.Step)
process.On("end", statistics.End) process.On("end", statistics.End)
process.Run(&compiled) process.Run()
cli.HandleError(err)
fmt.Println(lambda.Stringify(compiled)) fmt.Println(lambda.Stringify(compiled))
fmt.Fprint(os.Stderr, statistics.Results.String()) fmt.Fprint(os.Stderr, statistics.Results.String())

View File

@@ -1,7 +1,6 @@
package executer package executer
import ( import (
"fmt"
"log/slog" "log/slog"
"git.maximhutz.com/max/lambda/internal/config" "git.maximhutz.com/max/lambda/internal/config"
@@ -10,28 +9,22 @@ import (
) )
type Executor struct { type Executor struct {
Config *config.Config Config *config.Config
Expression *lambda.Expression
emitter.Emitter emitter.Emitter
} }
func New(config *config.Config) *Executor { func New(config *config.Config, expression *lambda.Expression) *Executor {
return &Executor{Config: config} return &Executor{Config: config, Expression: expression}
} }
func (e Executor) Run(expr *lambda.Expression) { func (e Executor) Run() {
e.Emit("start") e.Emit("start")
if e.Config.Explanation { for lambda.ReduceOnce(e.Expression) {
fmt.Println(lambda.Stringify(*expr))
}
for lambda.ReduceOnce(expr) {
e.Emit("step") e.Emit("step")
if e.Config.Verbose { if e.Config.Verbose {
slog.Info("reduction", "tree", lambda.Stringify(*expr)) slog.Info("reduction", "tree", lambda.Stringify(*e.Expression))
}
if e.Config.Explanation {
fmt.Println(" =", lambda.Stringify(*expr))
} }
} }

View 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))
}