-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
108 lines (88 loc) · 1.97 KB
/
main.go
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
102
103
104
105
106
107
108
package main
import (
"flag"
"fmt"
"os"
"github.com/Konstantin8105/py4go/py4go"
)
var (
transpileCommand = flag.NewFlagSet(
"transpile", flag.ContinueOnError)
transpileHelpFlag = transpileCommand.Bool(
"h", false, "print help information")
astCommand = flag.NewFlagSet(
"ast", flag.ContinueOnError)
astHelpFlag = astCommand.Bool(
"h", false, "print help information")
stderr = os.Stderr
)
func main() {
flag.Usage = func() {
usage := "Usage: %s [<command>] [<flags>] file1.py ...\n\n"
usage += "Commands:\n"
usage += " transpile\ttranspile an input Python source file or files to Go\n"
usage += " ast\t\tprint AST before translated Go code\n"
usage += "\n"
fmt.Fprintf(stderr, usage, os.Args[0])
flag.PrintDefaults()
}
transpileCommand.SetOutput(stderr)
astCommand.SetOutput(stderr)
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return
}
var isAst, isTranspile bool
switch os.Args[1] {
case "ast":
err := astCommand.Parse(os.Args[2:])
if err != nil {
fmt.Fprintf(os.Stdout, "ast command cannot parse: %v", err)
return
}
if *astHelpFlag || astCommand.NArg() == 0 {
fmt.Fprintf(stderr, "Usage: %s ast file.py ...\n", os.Args[0])
astCommand.PrintDefaults()
return
}
isAst = true
case "transpile":
err := transpileCommand.Parse(os.Args[2:])
if err != nil {
fmt.Fprintf(os.Stdout, "transpile command cannot parse: %v", err)
return
}
if *transpileHelpFlag || transpileCommand.NArg() == 0 {
fmt.Fprintf(stderr,
"Usage: %s transpile file1.py ...\n",
os.Args[0])
transpileCommand.PrintDefaults()
return
}
isTranspile = true
default:
flag.Usage()
return
}
for _, file := range os.Args[2:] {
asttree, err := py4go.Parse(file)
if err != nil {
fmt.Println(err)
continue
}
if isAst {
fmt.Println(asttree)
continue
}
if !isTranspile {
continue
}
code, err := py4go.Transpile(asttree)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(code)
}
}