This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
/
run.go
112 lines (95 loc) · 2.77 KB
/
run.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
109
110
111
112
/*
Copyright 2020 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package run implements the ``igo run'' command.
package run
import (
"go/ast"
"go/token"
"os"
"path/filepath"
"github.com/goplus/igo/cmd/internal/base"
"github.com/goplus/igo/internal/cl"
"github.com/goplus/igo/internal/parser"
"github.com/qiniu/x/log"
exec "github.com/goplus/igo/internal/exec/bytecode"
)
// -----------------------------------------------------------------------------
// Cmd - igo run
var Cmd = &base.Command{
UsageLine: "igo run [-asm -quiet -debug -prof] <goSrcDir|goSrcFile>",
Short: "iGo - Run a Go program",
}
var (
flag = &Cmd.Flag
flagAsm = flag.Bool("asm", false, "generates `asm` code of Go bytecode backend")
flagQuiet = flag.Bool("quiet", false, "don't generate any compiling stage log")
flagDebug = flag.Bool("debug", false, "print debug information")
flagProf = flag.Bool("prof", false, "do profile and generate profile report")
)
func init() {
Cmd.Run = runCmd
}
func runCmd(cmd *base.Command, args []string) {
flag.Parse(args)
if flag.NArg() < 1 {
cmd.Usage(os.Stderr)
}
log.SetFlags(log.Ldefault &^ log.LstdFlags)
if *flagQuiet {
log.SetOutputLevel(0x7000)
} else if *flagDebug {
log.SetOutputLevel(log.Ldebug)
}
if *flagProf {
exec.SetProfile(true)
}
fset := token.NewFileSet()
target, _ := filepath.Abs(flag.Arg(0))
isDir, err := IsDir(target)
if err != nil {
log.Fatalln("input arg check failed:", err)
}
var pkgs map[string]*ast.Package
if isDir {
pkgs, err = parser.ParseDir(fset, target, nil, 0)
} else {
pkgs, err = parser.Parse(fset, target, nil, 0)
}
if err != nil {
log.Fatalln("parser.Parse failed:", err)
}
cl.CallBuiltinOp = exec.CallBuiltinOp
b := exec.NewBuilder(nil)
_, err = cl.NewPackage(b.Interface(), pkgs["main"], fset, cl.PkgActClMain)
if err != nil {
log.Fatalln("cl.NewPackage failed:", err)
}
code := b.Resolve()
if *flagAsm {
code.Dump(os.Stdout)
return
}
exec.NewContext(code).Run()
if *flagProf {
exec.ProfileReport()
}
}
// IsDir checks a target path is dir or not.
func IsDir(target string) (bool, error) {
fi, err := os.Stat(target)
if err != nil {
return false, err
}
return fi.IsDir(), nil
}
// -----------------------------------------------------------------------------