-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (48 loc) · 1.04 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
package goose
import (
"fmt"
"os"
"path/filepath"
"github.com/calico32/goose/interpreter"
"github.com/calico32/goose/parser"
"github.com/calico32/goose/token"
)
func Run(path string) (exitCode int, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Goose panicked: %v", r)
exitCode = -1
}
}()
fset := token.NewFileSet()
abspath, err := filepath.Abs(path)
f, err := parser.ParseFile(fset, abspath, nil, nil)
if err != nil {
return -1, err
}
i, err := interpreter.New(f, fset, false, os.Stdin, os.Stdout, os.Stderr)
if err != nil {
return -1, err
}
i.Run()
return
}
func RunCode(source string) (exitCode int, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Goose panicked: %v", r)
exitCode = -1
}
}()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", source, nil)
if err != nil {
panic(err)
}
i, err := interpreter.New(f, fset, false, os.Stdin, os.Stdout, os.Stderr)
if err != nil {
panic(err)
}
exitCode, err = i.Run()
return
}