feat: statistics flag, commented some more

This commit is contained in:
2025-12-29 20:00:29 -05:00
parent 3f9f3a603f
commit 351faa7e08
8 changed files with 64 additions and 49 deletions

View File

@@ -1,9 +1,10 @@
package config
// Arguments given to program.
// 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.
}

View File

@@ -5,13 +5,14 @@ import (
"os"
)
// Define the correct logger for the program to use.
// Returns a structured logger with the appropriate configurations.
func (c Config) GetLogger() *slog.Logger {
var level slog.Level
// By default, only print out errors.
level := slog.LevelError
// If the user set the output to be "VERBOSE", return the debug logs.
if c.Verbose {
level = slog.LevelInfo
} else {
level = slog.LevelError
}
return slog.New(

View File

@@ -7,13 +7,14 @@ import (
// Extract the program configuration from the command-line arguments.
func FromArgs() (*Config, error) {
// Parse flags.
// Relevant flags.
verbose := flag.Bool("v", false, "Verbosity. If set, the program will print logs.")
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
statistics := flag.Bool("s", false, "Statistics. If set, the process will print various statistics about the run.")
profile := flag.String("p", "", "CPU profiling. If set, the program will run a performance profile during execution.")
flag.Parse()
// Parse non-flag arguments.
// There must only be one input argument.
if flag.NArg() == 0 {
return nil, fmt.Errorf("no input given")
} else if flag.NArg() > 1 {
@@ -25,7 +26,7 @@ func FromArgs() (*Config, error) {
if flag.Arg(0) == "-" {
source = StdinSource{}
} else {
source = StringSource{data: flag.Arg(0)}
source = StringSource{Data: flag.Arg(0)}
}
return &Config{
@@ -33,5 +34,6 @@ func FromArgs() (*Config, error) {
Verbose: *verbose,
Explanation: *explanation,
Profile: *profile,
Statistics: *statistics,
}, nil
}

View File

@@ -5,21 +5,21 @@ import (
"os"
)
// Defines the consumption of different types of input sources.
// A method of extracting input from the user.
type Source interface {
// Get the data.
Pull() (string, error)
// Fetch data from this source.
Extract() (string, error)
}
// A source coming from a string.
type StringSource struct{ data string }
// A source defined by a string.
type StringSource struct{ Data string }
func (s StringSource) Pull() (string, error) { return s.data, nil }
func (s StringSource) Extract() (string, error) { return s.Data, nil }
// A source coming from standard input.
// A source pulling from standard input.
type StdinSource struct{}
func (s StdinSource) Pull() (string, error) {
func (s StdinSource) Extract() (string, error) {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return "", err