57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"git.maximhutz.com/max/lambda/internal/cli"
|
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
|
"git.maximhutz.com/max/lambda/pkg/parser"
|
|
"git.maximhutz.com/max/lambda/pkg/tokenizer"
|
|
)
|
|
|
|
func main() {
|
|
|
|
options, err := cli.ParseOptions(os.Args[1:])
|
|
cli.HandleError(err)
|
|
|
|
logger := cli.GetLogger(*options)
|
|
logger.Info("Using program arguments.", "args", os.Args)
|
|
logger.Info("Parsed CLI options.", "options", options)
|
|
|
|
if options.Input == "-" {
|
|
options.Input, err = cli.ReadInput()
|
|
cli.HandleError(err)
|
|
}
|
|
|
|
tokens, fails := tokenizer.GetTokens([]rune(options.Input))
|
|
if len(fails) > 0 {
|
|
cli.HandleError(errors.Join(fails...))
|
|
}
|
|
logger.Info("Parsed tokens.", "tokens", tokens)
|
|
|
|
expression, err := parser.GetTree(tokens)
|
|
cli.HandleError(err)
|
|
logger.Info("Parsed syntax tree.", "tree", lambda.Stringify(expression))
|
|
|
|
start := time.Now()
|
|
|
|
if options.Explanation {
|
|
fmt.Println(lambda.Stringify(expression))
|
|
}
|
|
|
|
for lambda.ReduceOnce(&expression) {
|
|
logger.Info("Reduction.", "tree", lambda.Stringify(expression))
|
|
if options.Explanation {
|
|
fmt.Println(" =", lambda.Stringify(expression))
|
|
}
|
|
}
|
|
|
|
elapsed := time.Since(start).Milliseconds()
|
|
|
|
fmt.Println(lambda.Stringify(expression))
|
|
fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms")
|
|
}
|