## Description The lambda CLI previously only wrote output to stdout using shell redirection. This PR adds support for writing results to files using the `-o` flag. This is implemented using a new `Destination` interface that mirrors the existing `Source` pattern. Changes: - Added `Destination` interface with `StdoutDestination` and `FileDestination` implementations. - Added `-o` flag to CLI argument parser for output file specification. - Updated `Config` to use `Destination` instead of direct output handling. - Refactored main to use `Destination.Write()` for result output. - Updated Makefile targets (`run`, `profile`, `explain`) to use `-o` flag instead of shell redirection. ### Decisions The `-o` flag defaults to stdout when not specified or when set to `-`. This maintains backward compatibility while providing explicit file output capability. ## Benefits - Cleaner command-line interface without shell redirection. - Symmetric design with `Source` interface for input. - More portable across different shells and environments. - Explicit output handling improves code clarity. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). - [ ] Tests pass (if applicable). - [ ] Documentation updated (if applicable). Reviewed-on: #13 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
78 lines
2.1 KiB
Go
78 lines
2.1 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/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.
|
|
result := lambda.Stringify(compiled)
|
|
err = options.Destination.Write(result)
|
|
cli.HandleError(err)
|
|
}
|