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
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
|
|
"git.maximhutz.com/max/lambda/internal/cli"
|
|
"git.maximhutz.com/max/lambda/internal/config"
|
|
"git.maximhutz.com/max/lambda/internal/engine"
|
|
"git.maximhutz.com/max/lambda/internal/plugins"
|
|
"git.maximhutz.com/max/lambda/pkg/convert"
|
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
|
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
|
)
|
|
|
|
func main() {
|
|
// Parse CLI arguments.
|
|
options, err := config.FromArgs()
|
|
cli.HandleError(err)
|
|
|
|
logger := options.GetLogger()
|
|
logger.Info("using program arguments", "args", os.Args)
|
|
logger.Info("parsed CLI options", "options", options)
|
|
|
|
// Get input.
|
|
input, err := options.Source.Extract()
|
|
cli.HandleError(err)
|
|
|
|
// Parse code into syntax tree.
|
|
ast, err := saccharine.Parse(input)
|
|
cli.HandleError(err)
|
|
logger.Info("parsed syntax tree", "tree", ast)
|
|
|
|
// Compile expression to lambda calculus.
|
|
compiled := convert.SaccharineToLambda(ast)
|
|
logger.Info("compiled λ expression", "tree", lambda.Stringify(compiled))
|
|
|
|
// Create reduction engine.
|
|
process := engine.New(options, &compiled)
|
|
|
|
// If the user selected to track CPU performance, attach a profiler to the
|
|
// process.
|
|
if options.Profile != "" {
|
|
plugins.NewPerformance(options.Profile, process)
|
|
}
|
|
|
|
// If the user selected to produce a step-by-step explanation, attach an
|
|
// observer here.
|
|
if options.Explanation {
|
|
plugins.NewExplanation(process)
|
|
}
|
|
|
|
// If the user opted to track statistics, attach a tracker here, too.
|
|
if options.Statistics {
|
|
plugins.NewStatistics(process)
|
|
}
|
|
|
|
// If the user selected for verbose debug logs, attach a reduction tracker.
|
|
if options.Verbose {
|
|
plugins.NewLogs(logger, process)
|
|
}
|
|
|
|
process.Run()
|
|
|
|
// Return the final reduced result.
|
|
result := lambda.Stringify(compiled)
|
|
err = options.Destination.Write(result)
|
|
cli.HandleError(err)
|
|
}
|