diff --git a/cmd/lambda/lambda.go b/cmd/lambda/lambda.go index 3f02e5c..2089cb5 100644 --- a/cmd/lambda/lambda.go +++ b/cmd/lambda/lambda.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "git.maximhutz.com/max/lambda/internal/cli" @@ -72,5 +71,7 @@ func main() { process.Run() // Return the final reduced result. - fmt.Println(lambda.Stringify(compiled)) + result := lambda.Stringify(compiled) + err = options.Destination.Write(result) + cli.HandleError(err) } diff --git a/internal/config/config.go b/internal/config/config.go index bba51df..063ee2e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,9 +3,10 @@ package config // Configuration settings for the program. type Config struct { - Source Source // The source code given to the program. - Verbose bool // Whether or not to print debug logs. - Explanation bool // Whether or not to print an explanation of the reduction. - Profile string // If not nil, print a CPU profile during execution. - Statistics bool // Whether or not to print statistics. + Source Source // The source code given to the program. + Destination Destination // The destination for the final result. + Verbose bool // Whether or not to print debug logs. + Explanation bool // Whether or not to print an explanation of the reduction. + Profile string // If not nil, print a CPU profile during execution. + Statistics bool // Whether or not to print statistics. } diff --git a/internal/config/destination.go b/internal/config/destination.go new file mode 100644 index 0000000..e9fd236 --- /dev/null +++ b/internal/config/destination.go @@ -0,0 +1,27 @@ +package config + +import ( + "fmt" + "os" +) + +// A method of writing output to the user. +type Destination interface { + // Write data to this destination. + Write(data string) error +} + +// A destination writing to stdout. +type StdoutDestination struct{} + +func (d StdoutDestination) Write(data string) error { + fmt.Println(data) + return nil +} + +// A destination writing to a file. +type FileDestination struct{ Path string } + +func (d FileDestination) Write(data string) error { + return os.WriteFile(d.Path, []byte(data+"\n"), 0644) +} diff --git a/internal/config/parse_from_args.go b/internal/config/parse_from_args.go index 41f738e..908e7e1 100644 --- a/internal/config/parse_from_args.go +++ b/internal/config/parse_from_args.go @@ -13,6 +13,7 @@ func FromArgs() (*Config, error) { statistics := flag.Bool("s", false, "Statistics. If set, the process will print various statistics about the run.") profile := flag.String("p", "", "CPU profiling. If an output file is defined, the program will profile its execution and dump its results into it.") file := flag.String("f", "", "File. If set, read source from the specified file.") + output := flag.String("o", "", "Output. If set, write result to the specified file. Use '-' for stdout (default).") flag.Parse() // Parse source type. @@ -36,8 +37,17 @@ func FromArgs() (*Config, error) { } } + // Parse destination type. + var destination Destination + if *output == "" || *output == "-" { + destination = StdoutDestination{} + } else { + destination = FileDestination{Path: *output} + } + return &Config{ Source: source, + Destination: destination, Verbose: *verbose, Explanation: *explanation, Profile: *profile,