-
Notifications
You must be signed in to change notification settings - Fork 2
/
nj.go
55 lines (47 loc) · 1.07 KB
/
nj.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
package nj
import (
"io"
"io/ioutil"
"os"
"unsafe"
"github.com/coyove/nj/bas"
"github.com/coyove/nj/internal"
"github.com/coyove/nj/parser"
)
type LoadOptions struct {
Globals bas.Map
MaxStackSize int64
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
}
func LoadFile(path string, opt *LoadOptions) (*bas.Program, error) {
code, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return loadCode(*(*string)(unsafe.Pointer(&code)), path, opt)
}
func LoadString(code string, opt *LoadOptions) (*bas.Program, error) {
return loadCode(code, internal.UnnamedLoadString(), opt)
}
func loadCode(code, name string, opt *LoadOptions) (*bas.Program, error) {
n, err := parser.Parse(code, name)
if err != nil {
return nil, err
}
if internal.IsDebug() {
n.Dump(os.Stderr)
}
return compileNodeTopLevel(name, code, n, opt)
}
func Run(p *bas.Program, err error) bas.Value {
if err != nil {
return bas.Error(nil, err)
}
return p.Run()
}
func MustRun(p *bas.Program, err error) bas.Value {
internal.PanicErr(err)
return p.Run()
}