refactor: replace string-based emitter with type-safe generic event system
Refactors the event emitter system from string-based messages to a type-safe generic implementation using typed events. Consolidates separate tracker packages into a unified plugins architecture. Changes: - Replace Emitter with BaseEmitter[E comparable] using generics - Add Event type with StartEvent, StepEvent, and StopEvent constants - Create Listener[E] interface with BaseListener implementation - Consolidate explanation, performance, and statistics trackers into internal/plugins package - Simplify main CLI by using plugin constructors instead of manual event subscription - Add Items() iterator method to Set for idiomatic range loops
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
type Engine struct {
|
||||
Config *config.Config
|
||||
Expression *lambda.Expression
|
||||
emitter.Emitter
|
||||
emitter.BaseEmitter[Event]
|
||||
}
|
||||
|
||||
// Create a new engine, given an unreduced λ-expression.
|
||||
@@ -22,11 +22,11 @@ func New(config *config.Config, expression *lambda.Expression) *Engine {
|
||||
|
||||
// Begin the reduction process.
|
||||
func (e Engine) Run() {
|
||||
e.Emit("start")
|
||||
e.Emit(StartEvent)
|
||||
|
||||
lambda.ReduceAll(e.Expression, func() {
|
||||
e.Emit("step")
|
||||
e.Emit(StepEvent)
|
||||
})
|
||||
|
||||
e.Emit("end")
|
||||
e.Emit(StopEvent)
|
||||
}
|
||||
|
||||
9
internal/engine/events.go
Normal file
9
internal/engine/events.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package engine
|
||||
|
||||
type Event int
|
||||
|
||||
const (
|
||||
StartEvent Event = iota
|
||||
StepEvent
|
||||
StopEvent
|
||||
)
|
||||
25
internal/plugins/debug.go
Normal file
25
internal/plugins/debug.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Logs struct {
|
||||
logger *slog.Logger
|
||||
process *engine.Engine
|
||||
}
|
||||
|
||||
func NewLogs(logger *slog.Logger, process *engine.Engine) *Logs {
|
||||
plugin := &Logs{logger, process}
|
||||
process.On(engine.StopEvent, plugin.Step)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
func (t *Logs) Step() {
|
||||
stringified := lambda.Stringify(*t.process.Expression)
|
||||
t.logger.Info("reduction", "tree", stringified)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// Package "explanation" provides a observer to gather the reasoning during the
|
||||
// reduction, and present a thorough explanation to the user for each step.
|
||||
package explanation
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -10,23 +10,23 @@ import (
|
||||
)
|
||||
|
||||
// Track the reductions made by a reduction proess.
|
||||
type Tracker struct {
|
||||
type Explanation struct {
|
||||
process *engine.Engine
|
||||
}
|
||||
|
||||
// Attaches a new explanation tracker to a process.
|
||||
func Track(process *engine.Engine) *Tracker {
|
||||
tracker := &Tracker{process: process}
|
||||
process.On("start", tracker.Start)
|
||||
process.On("step", tracker.Step)
|
||||
func NewExplanation(process *engine.Engine) *Explanation {
|
||||
plugin := &Explanation{process: process}
|
||||
process.On(engine.StartEvent, plugin.Start)
|
||||
process.On(engine.StepEvent, plugin.Step)
|
||||
|
||||
return tracker
|
||||
return plugin
|
||||
}
|
||||
|
||||
func (t *Tracker) Start() {
|
||||
func (t *Explanation) Start() {
|
||||
fmt.Println(lambda.Stringify(*t.process.Expression))
|
||||
}
|
||||
|
||||
func (t *Tracker) Step() {
|
||||
func (t *Explanation) Step() {
|
||||
fmt.Println(" =", lambda.Stringify(*t.process.Expression))
|
||||
}
|
||||
@@ -1,28 +1,34 @@
|
||||
// Package "performance" provides a tracker to observer CPU performance during
|
||||
// execution.
|
||||
package performance
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime/pprof"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
)
|
||||
|
||||
// Observes a reduction process, and publishes a CPU performance profile on
|
||||
// completion.
|
||||
type Tracker struct {
|
||||
type Performance struct {
|
||||
File string
|
||||
filePointer *os.File
|
||||
Error error
|
||||
}
|
||||
|
||||
// Create a performance tracker that outputs a profile to "file".
|
||||
func Track(file string) *Tracker {
|
||||
return &Tracker{File: file}
|
||||
func NewPerformance(file string, process *engine.Engine) *Performance {
|
||||
plugin := &Performance{File: file}
|
||||
process.On(engine.StartEvent, plugin.Start)
|
||||
process.On(engine.StopEvent, plugin.Stop)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
// Begin profiling.
|
||||
func (t *Tracker) Start() {
|
||||
func (t *Performance) Start() {
|
||||
var absPath string
|
||||
|
||||
absPath, t.Error = filepath.Abs(t.File)
|
||||
@@ -47,7 +53,7 @@ func (t *Tracker) Start() {
|
||||
}
|
||||
|
||||
// Stop profiling.
|
||||
func (t *Tracker) End() {
|
||||
func (t *Performance) Stop() {
|
||||
pprof.StopCPUProfile()
|
||||
t.filePointer.Close()
|
||||
}
|
||||
44
internal/plugins/statistics.go
Normal file
44
internal/plugins/statistics.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/internal/statistics"
|
||||
)
|
||||
|
||||
// An observer, to track reduction performance.
|
||||
type Statistics struct {
|
||||
start time.Time
|
||||
steps uint64
|
||||
}
|
||||
|
||||
// Create a new reduction performance Statistics.
|
||||
func NewStatistics(process *engine.Engine) *Statistics {
|
||||
plugin := &Statistics{}
|
||||
process.On(engine.StartEvent, plugin.Start)
|
||||
process.On(engine.StepEvent, plugin.Step)
|
||||
process.On(engine.StopEvent, plugin.Step)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
func (t *Statistics) Start() {
|
||||
t.start = time.Now()
|
||||
t.steps = 0
|
||||
}
|
||||
|
||||
func (t *Statistics) Step() {
|
||||
t.steps++
|
||||
}
|
||||
|
||||
func (t *Statistics) Stop() {
|
||||
results := statistics.Results{
|
||||
StepsTaken: t.steps,
|
||||
TimeElapsed: uint64(time.Since(t.start).Milliseconds()),
|
||||
}
|
||||
|
||||
fmt.Fprint(os.Stderr, results.String())
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// An observer, to track reduction performance.
|
||||
type Tracker struct {
|
||||
start time.Time
|
||||
steps uint64
|
||||
}
|
||||
|
||||
// Create a new reduction performance tracker.
|
||||
func Track() *Tracker {
|
||||
return &Tracker{}
|
||||
}
|
||||
|
||||
func (t *Tracker) Start() {
|
||||
t.start = time.Now()
|
||||
t.steps = 0
|
||||
}
|
||||
|
||||
func (t *Tracker) Step() {
|
||||
t.steps++
|
||||
}
|
||||
|
||||
func (t *Tracker) End() {
|
||||
results := Results{
|
||||
StepsTaken: t.steps,
|
||||
TimeElapsed: uint64(time.Since(t.start).Milliseconds()),
|
||||
}
|
||||
|
||||
fmt.Fprint(os.Stderr, results.String())
|
||||
}
|
||||
Reference in New Issue
Block a user