fix: convert style
This commit is contained in:
@@ -4,8 +4,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
)
|
||||
|
||||
func Lambda() *cobra.Command {
|
||||
@@ -77,7 +78,7 @@ func Lambda() *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output")
|
||||
cmd.Flags().BoolP("verbose", "v", false, "Enable verbose output")
|
||||
|
||||
cmd.AddCommand(LambdaConvert())
|
||||
cmd.AddCommand(LambdaEngine())
|
||||
|
||||
@@ -6,17 +6,15 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/cli"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// inferReprFromPath returns the repr type based on file extension.
|
||||
func inferReprFromPath(path string) (string, error) {
|
||||
switch ext := strings.ToLower(filepath.Ext(path)); ext {
|
||||
case ".lam", ".lambda":
|
||||
case ".lambda", ".lam", ".lc":
|
||||
return "lambda", nil
|
||||
case ".sac", ".saccharine":
|
||||
case ".saccharine", ".sch":
|
||||
return "saccharine", nil
|
||||
default:
|
||||
return "", fmt.Errorf("unknown file extension '%s'", ext)
|
||||
@@ -24,23 +22,29 @@ func inferReprFromPath(path string) (string, error) {
|
||||
}
|
||||
|
||||
func LambdaConvert() *cobra.Command {
|
||||
var inputReprFlag, outputReprFlag string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "convert <input> <output>",
|
||||
Short: "Convert between lambda calculus representations",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
inputPath := args[0]
|
||||
outputPath := args[1]
|
||||
RunE: func(_ *cobra.Command, args []string) error {
|
||||
var err error
|
||||
inputPath, outputPath := args[0], args[1]
|
||||
|
||||
// Infer repr types from extensions.
|
||||
inputRepr, err := inferReprFromPath(inputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("input file: %w", err)
|
||||
// Use flag if provided, otherwise infer from extension.
|
||||
inputRepr := inputReprFlag
|
||||
if inputRepr == "" {
|
||||
if inputRepr, err = inferReprFromPath(inputPath); err != nil {
|
||||
return fmt.Errorf("input file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
outputRepr, err := inferReprFromPath(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("output file: %w", err)
|
||||
outputRepr := outputReprFlag
|
||||
if outputRepr == "" {
|
||||
if outputRepr, err = inferReprFromPath(outputPath); err != nil {
|
||||
return fmt.Errorf("output file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Read input file.
|
||||
@@ -57,23 +61,10 @@ func LambdaConvert() *cobra.Command {
|
||||
return fmt.Errorf("parsing input: %w", err)
|
||||
}
|
||||
|
||||
if viper.GetBool("verbose") {
|
||||
fmt.Fprintf(os.Stderr, "Parsed %s from %s\n", inputRepr, inputPath)
|
||||
}
|
||||
|
||||
// Convert to output repr if different.
|
||||
var result cli.Repr
|
||||
if inputRepr != outputRepr {
|
||||
result, err = r.ConvertTo(repr, outputRepr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting %s to %s: %w", inputRepr, outputRepr, err)
|
||||
}
|
||||
|
||||
if viper.GetBool("verbose") {
|
||||
fmt.Fprintf(os.Stderr, "Converted to %s\n", outputRepr)
|
||||
}
|
||||
} else {
|
||||
result = repr
|
||||
result, err := r.ConvertTo(repr, outputRepr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting %s to %s: %w", inputRepr, outputRepr, err)
|
||||
}
|
||||
|
||||
// Marshal output.
|
||||
@@ -88,13 +79,12 @@ func LambdaConvert() *cobra.Command {
|
||||
return fmt.Errorf("writing output file: %w", err)
|
||||
}
|
||||
|
||||
if viper.GetBool("verbose") {
|
||||
fmt.Fprintf(os.Stderr, "Wrote %s to %s\n", outputRepr, outputPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&inputReprFlag, "input", "i", "", "Input representation (inferred from extension if unset)")
|
||||
cmd.Flags().StringVarP(&outputReprFlag, "output", "o", "", "Output representation (inferred from extension if unset)")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user