-
Notifications
You must be signed in to change notification settings - Fork 94
/
main.go
81 lines (65 loc) · 1.5 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
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Gpython binary
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/repl"
"github.com/go-python/gpython/repl/cli"
_ "github.com/go-python/gpython/stdlib"
)
var (
cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to file")
)
// syntaxError prints the syntax
func syntaxError() {
fmt.Fprintf(os.Stderr, `GPython
A python implementation in Go
Full options:
`)
flag.PrintDefaults()
}
func main() {
flag.Usage = syntaxError
flag.Parse()
xmain(flag.Args())
}
func xmain(args []string) {
opts := py.DefaultContextOpts()
opts.SysArgs = args
ctx := py.NewContext(opts)
defer ctx.Close()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
// IF no args, enter REPL mode
if len(args) == 0 {
fmt.Printf("Python 3.4.0 (%s, %s)\n", commit, date)
fmt.Printf("[Gpython %s]\n", version)
fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf("- go version: %s\n", runtime.Version())
replCtx := repl.New(ctx)
cli.RunREPL(replCtx)
} else {
_, err := py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
if err != nil {
py.TracebackDump(err)
os.Exit(1)
}
}
}