Files
lambda/cmd/lambda/lambda.go
2025-12-30 15:58:14 -05:00

77 lines
2.1 KiB
Go

package main
import (
"fmt"
"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/explanation"
"git.maximhutz.com/max/lambda/internal/performance"
"git.maximhutz.com/max/lambda/internal/statistics"
"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 != "" {
profiler := performance.Track(options.Profile)
process.On("start", profiler.Start)
process.On("end", profiler.End)
}
// If the user selected to produce a step-by-step explanation, attach an
// observer here.
if options.Explanation {
explanation.Track(process)
}
// If the user opted to track statistics, attach a tracker here, too.
if options.Statistics {
statistics := statistics.Track()
process.On("start", statistics.Start)
process.On("step", statistics.Step)
process.On("end", statistics.End)
}
// If the user selected for verbose debug logs, attach a reduction tracker.
if options.Verbose {
process.On("step", func() {
logger.Info("reduction", "tree", lambda.Stringify(compiled))
})
}
process.Run()
// Return the final reduced result.
fmt.Println(lambda.Stringify(compiled))
}