-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc_builtins.go
218 lines (185 loc) · 4.02 KB
/
misc_builtins.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"errors"
"os"
"math/big"
"strings"
)
func FnNullEnvironment(nargs int) error {
if nargs != 1 {
return errors.New("null-environment takes 1 argument")
}
version_v, ok := stack.Pop().(Integer)
if !ok {
return errors.New("null-environment takes an integer as the argument")
}
version_bi := big.Int(version_v)
if version_bi.Cmp(big.NewInt(5)) != 0 {
return errors.New("version to null-environment must be 5")
}
stack.Push(&Procedure{
Scope: Scope{map[Symbol]Value{}, nil},
Args: Empty,
Macros: map[Symbol]SyntaxRules{},
})
return nil
}
func FnSchemeReportEnvironment(nargs int) error {
if nargs != 1 {
return errors.New("scheme-report-environment takes 1 argument")
}
version_v, ok := stack.Pop().(Integer)
if !ok {
return errors.New(
"scheme-report-environment takes an integer as the argument",
)
}
version_bi := big.Int(version_v)
if version_bi.Cmp(big.NewInt(5)) != 0 {
return errors.New("version to scheme-report-environment must be 5")
}
scope := map[Symbol]Value{}
for k, v := range BaseScope {
scope[k] = v
}
stack.Push(&Procedure{
Scope: Scope{scope, nil},
Args: Empty,
Macros: map[Symbol]SyntaxRules{},
})
return nil
}
func FnEval(nargs int) error {
if nargs != 2 {
return errors.New("eval takes 2 arguments")
}
expr := stack.Pop()
env, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New("eval takes a procedure for the environment")
}
env.Ins = []Ins{}
if err := env.Gen(expr); err != nil {
return err
}
env.Eval()
return nil
}
func FnIsProcedure(nargs int) error {
_, ok := stack.Pop().(*Procedure)
stack.Push(Boolean(ok))
return nil
}
func FnCallCC(p *Procedure, nargs int) error {
p.IsCont = true
proc := stack.Pop()
vec := []Value{}
for i := 0; i < nargs; i++ {
vec = append(vec)
}
vec = append(vec, p)
p.StackRest = &Stack{}
*p.StackRest = stack
for i := len(vec) - 1; i >= 0; i-- {
stack.Push(vec[i])
}
stack.Push(proc)
call := Procedure{
Scope: p.Scope,
Ins: []Ins{{Call, nil, nargs}},
}
return call.Eval()
}
func FnExit(nargs int) error {
if nargs > 1 {
return errors.New("Wrong arg count to exit")
}
code := 0
if nargs == 1 {
v, ok := stack.Pop().(Integer)
if !ok {
return errors.New("exit takes an integer as the arg")
}
bi := big.Int(v)
code = int(bi.Int64())
}
os.Exit(code)
return nil
}
// SRFI 98
func FnGetEnvironmentVariables(nargs int) error {
env_vals := []Value{}
if nargs != 0 {
return errors.New("get-environment-variables takes no arguments")
}
for _, e := range os.Environ() {
kv := strings.SplitN(e, "=", 2)
var k Value = String{&kv[0]}
var v Value
if len(kv) == 2 {
v = String{&kv[1]}
} else {
s := ""
v = String{&s}
}
env_vals = append(env_vals, &Pair{&k, &v})
}
stack.Push(vec2list(env_vals))
return nil
}
func FnDynamicWind(nargs int) error {
if nargs != 3 {
return errors.New("dynamic-wind takes 3 arguments")
}
before, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New("dynamic-wind takes procedure arguments")
}
thunk, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New("dynamic-wind takes procedure arguments")
}
after, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New("dynamic-wind takes procedure arguments")
}
thunk.Ins = append(
[]Ins{
{Imm, before, 0},
{Call, nil, 0},
},
thunk.Ins...,
)
thunk.Ins = append(
thunk.Ins,
[]Ins{
{Imm, after, 0},
{Call, nil, 0},
}...,
)
return thunk.Eval()
}
func FnValues(nargs int) error {
stack.Push(Integer(*big.NewInt(int64(nargs))))
return nil
}
func FnCallWithValues(nargs int) error {
if nargs != 2 {
return errors.New("call-with-values takes 2 arguments")
}
producer, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New("call-with-values takes procedures as the args")
}
consumer, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New("call-with-values takes procedures as the args")
}
producer.Ins = append(producer.Ins,
[]Ins{
{Imm, consumer, 0},
{Call, nil, -1},
}...,
)
return producer.Eval()
}