Skip to content

Commit 14875b3

Browse files
committed
tidy main
1 parent cd8846c commit 14875b3

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

usm.go

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,87 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/spf13/cobra"
8-
97
"alon.kr/x/usm/lex"
108
"alon.kr/x/usm/parse"
119
"alon.kr/x/usm/source"
10+
"github.com/spf13/cobra"
1211
)
1312

13+
func setInputSource(cmd *cobra.Command, args []string) error {
14+
if len(args) > 0 {
15+
file, err := os.Open(args[0])
16+
if err != nil {
17+
return fmt.Errorf("error opening file: %v", err)
18+
}
19+
cmd.SetIn(file)
20+
}
21+
return nil
22+
}
23+
24+
func lexCommand(cmd *cobra.Command, args []string) {
25+
view, err := source.ReadSource(cmd.InOrStdin())
26+
if err != nil {
27+
fmt.Printf("Error reading source: %v\n", err)
28+
os.Exit(1)
29+
}
30+
31+
tokens, err := lex.NewTokenizer().Tokenize(view)
32+
if err != nil {
33+
fmt.Printf("Error tokenizing: %v\n", err)
34+
os.Exit(1)
35+
}
36+
37+
_, ctx := view.Detach()
38+
for _, tkn := range tokens {
39+
fmt.Printf("%s ", tkn.String(ctx))
40+
if tkn.Type == lex.SeparatorToken {
41+
fmt.Println()
42+
}
43+
}
44+
}
45+
46+
func fmtCommand(cmd *cobra.Command, args []string) {
47+
view, err := source.ReadSource(cmd.InOrStdin())
48+
if err != nil {
49+
fmt.Printf("Error reading source: %v\n", err)
50+
os.Exit(1)
51+
}
52+
53+
tokens, err := lex.NewTokenizer().Tokenize(view)
54+
if err != nil {
55+
fmt.Printf("Error tokenizing: %v\n", err)
56+
os.Exit(1)
57+
}
58+
59+
tknView := parse.NewTokenView(tokens)
60+
file, perr := parse.NewFileParser().Parse(&tknView)
61+
if perr == nil {
62+
fmt.Print(file.String(view.Ctx()))
63+
} else {
64+
fmt.Println(perr.Error(view.Ctx()))
65+
}
66+
}
67+
1468
func main() {
1569
rootCmd := &cobra.Command{
1670
Use: "usm",
17-
Short: " One Universal assembly language to rule them all.",
71+
Short: "One Universal assembly language to rule them all.",
1872
}
1973

2074
lexCmd := &cobra.Command{
21-
Use: "lex",
22-
Short: "Lex the source code",
23-
Run: func(cmd *cobra.Command, args []string) {
24-
view, err := source.ReadSource(os.Stdin)
25-
if err != nil {
26-
panic(err)
27-
}
28-
29-
tokens, err := lex.NewTokenizer().Tokenize(view)
30-
if err != nil {
31-
panic(err)
32-
}
33-
34-
_, ctx := view.Detach()
35-
for _, tkn := range tokens {
36-
fmt.Printf("%s ", tkn.String(ctx))
37-
if tkn.Type == lex.SeparatorToken {
38-
fmt.Println()
39-
}
40-
}
41-
},
75+
Use: "lex [file]",
76+
Short: "Lex the source code",
77+
Args: cobra.MaximumNArgs(1),
78+
PreRunE: setInputSource,
79+
Run: lexCommand,
4280
}
4381

4482
fmtCmd := &cobra.Command{
45-
Use: "fmt",
46-
Short: "Format the source code",
47-
Run: func(cmd *cobra.Command, args []string) {
48-
view, err := source.ReadSource(os.Stdin)
49-
if err != nil {
50-
panic(err)
51-
}
52-
53-
tokens, err := lex.NewTokenizer().Tokenize(view)
54-
if err != nil {
55-
panic(err)
56-
}
57-
58-
tknView := parse.NewTokenView(tokens)
59-
file, perr := parse.NewFileParser().Parse(&tknView)
60-
if perr == nil {
61-
fmt.Print(file.String(view.Ctx()))
62-
} else {
63-
fmt.Println(perr.Error(view.Ctx()))
64-
}
65-
},
83+
Use: "fmt [file]",
84+
Short: "Format the source code",
85+
Args: cobra.MaximumNArgs(1),
86+
PreRunE: setInputSource,
87+
Run: fmtCommand,
6688
}
6789

6890
rootCmd.AddCommand(lexCmd)

0 commit comments

Comments
 (0)