-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (91 loc) · 2.95 KB
/
Copy pathmain.go
File metadata and controls
101 lines (91 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Command minipy is the command-line interface: it runs a minipy source file
// or, with no argument, starts an interactive REPL.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/siyul-park/minipy/compiler"
"github.com/siyul-park/minivm/optimize"
)
func main() {
os.Exit(execute(os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
}
func execute(args []string, in io.Reader, out, errOut io.Writer) int {
command := newRootCmd()
command.SetArgs(append([]string(nil), args...))
command.SetIn(in)
command.SetOut(out)
command.SetErr(errOut)
if err := command.Execute(); err != nil {
fmt.Fprintln(errOut, pyError(err))
return 1
}
return 0
}
// newRootCmd builds the cobra command tree. The root runs a file when given one
// and otherwise starts the REPL; `run <file>` is the explicit file form.
func newRootCmd() *cobra.Command {
var optimization int
var modulePaths []string
root := &cobra.Command{
Use: "minipy [file]",
Short: "minipy — a statically-typed Python subset on minivm",
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(command *cobra.Command, args []string) error {
level, err := optLevel(optimization)
if err != nil {
return err
}
paths := append([]string(nil), modulePaths...)
if len(args) == 0 {
return repl(command.Context(), command.InOrStdin(), command.OutOrStdout(), level, paths)
}
return runFile(command.Context(), args[0], command.OutOrStdout(), level, paths)
},
}
root.PersistentFlags().IntVarP(&optimization, "opt", "O", int(optimize.O0),
"optimization level (0..3); 3 enables global value numbering / CSE")
root.PersistentFlags().StringArrayVarP(&modulePaths, "path", "p", nil,
"add a module search path")
root.AddCommand(newRunCommand(&optimization, &modulePaths))
return root
}
func newRunCommand(optimization *int, modulePaths *[]string) *cobra.Command {
return &cobra.Command{
Use: "run <file>",
Short: "compile and run a minipy file",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(command *cobra.Command, args []string) error {
level, err := optLevel(*optimization)
if err != nil {
return err
}
paths := append([]string(nil), (*modulePaths)...)
return runFile(command.Context(), args[0], command.OutOrStdout(), level, paths)
},
}
}
func optLevel(optimization int) (optimize.Level, error) {
if optimization < int(optimize.O0) || optimization > int(optimize.O3) {
return 0, fmt.Errorf("invalid optimization level %d: must be 0..3", optimization)
}
return optimize.Level(optimization), nil
}
func modulePathOptions(paths []string) ([]compiler.Option, error) {
options := make([]compiler.Option, 0, len(paths))
for _, path := range paths {
absolute, err := filepath.Abs(path)
if err != nil {
return nil, err
}
options = append(options, compiler.WithModules(os.DirFS(absolute)))
}
return options, nil
}