-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlisp-x.go
168 lines (150 loc) · 3.4 KB
/
zlisp-x.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package elisp
import (
"github.com/glycerine/zygomys/zygo"
"fmt"
"os"
"reflect"
)
func New() *XZlisp {
return &XZlisp {
lisp: zygo.NewZlisp(),
}
}
func (lpw *XZlisp) LoadFile(path string, vars map[string]interface{}) (err error) {
fp, e := os.Open(path)
if e != nil {
err = e
return
}
defer fp.Close()
if err = lpw.lisp.LoadFile(fp); err != nil {
return
}
err = lpw.setEnv(vars)
return
}
func (lpw *XZlisp) LoadScript(script string, vars map[string]interface{}) (err error) {
if err = lpw.lisp.LoadString(script); err != nil {
return
}
err = lpw.setEnv(vars)
return
}
func (lpw *XZlisp) Dump() {
lpw.lisp.DumpEnvironment()
}
func (lpw *XZlisp) GetGlobal(name string) (res interface{}, err error) {
r, e := lpw.getVar(name)
if e != nil {
err = e
return
}
res = fromValue(r)
return
}
func (lpw *XZlisp) EvalFile(path string, env map[string]interface{}) (res interface{}, err error) {
if err = lpw.LoadFile(path, env); err != nil {
return
}
v, e := lpw.lisp.Run()
if e != nil {
err = e
return
}
res = fromValue(v)
return
}
func (lpw *XZlisp) Eval(script string, env map[string]interface{}) (res interface{}, err error) {
if err = lpw.LoadScript(script, env); err != nil {
return
}
v, e := lpw.lisp.Run()
if e != nil {
err = e
return
}
res = fromValue(v)
return
}
func (lpw *XZlisp) CallFunc(funcName string, args ...interface{}) (res interface{}, err error) {
v, e := lpw.getVar(funcName)
if e != nil {
err = e
return
}
fn, ok := v.(*zygo.SexpFunction)
if !ok {
err = fmt.Errorf("var %s is not with type function", funcName)
return
}
r, e := lpw.callFunc(fn, args...)
if e != nil {
err = e
return
}
res = fromValue(r)
return
}
// bind a var of golang func with a ZLisp function name, so calling ZLisp function
// is just calling the related golang func.
// @param funcVarPtr in format `var funcVar func(....) ...; funcVarPtr = &funcVar`
func (lpw *XZlisp) BindFunc(funcName string, funcVarPtr interface{}) (err error) {
if funcVarPtr == nil {
err = fmt.Errorf("funcVarPtr must be a non-nil poiter of func")
return
}
t := reflect.TypeOf(funcVarPtr)
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Func {
err = fmt.Errorf("funcVarPtr expected to be a pointer of func")
return
}
v, e := lpw.getVar(funcName)
if e != nil {
err = e
return
}
fn, ok := v.(*zygo.SexpFunction)
if !ok {
err = fmt.Errorf("var %s is not with type function", funcName)
return
}
lpw.bindFunc(fn, funcVarPtr)
return
}
func (lpw *XZlisp) BindFuncs(funcName2FuncVarPtr map[string]interface{}) (err error) {
for funcName, funcVarPtr := range funcName2FuncVarPtr {
if err = lpw.BindFunc(funcName, funcVarPtr); err != nil {
return
}
}
return
}
// make a golang func as a built-in ZLisp function, so the function can be called in ZLisp script.
func (lpw *XZlisp) MakeUserFunc(funcName string, funcVar interface{}) (err error) {
_, goFunc, e := bindGoFunc(funcName, funcVar)
if e != nil {
err = e
return
}
lpw.lisp.AddFunction(funcName, goFunc)
return
}
func (lpw *XZlisp) setEnv(vars map[string]interface{}) (err error) {
if len(vars) == 0 {
return nil
}
for k, v := range vars {
lispV := toValue(v, lpw.lisp)
lpw.lisp.AddGlobal(k, lispV)
}
return nil
}
func (lpw *XZlisp) getVar(name string) (v zygo.Sexp, err error) {
var ok bool
v, ok = lpw.lisp.FindObject(name)
if !ok {
err = fmt.Errorf("no var named %s found", name)
return
}
return
}