-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathport_builtins.go
329 lines (288 loc) · 6.4 KB
/
port_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"errors"
"os"
"unicode/utf8"
"io"
)
func FnIsPort(nargs int) error {
if nargs != 1 {
return errors.New("port? takes 1 argument")
}
v := stack.Pop()
_, ok1 := v.(InputPort)
_, ok2 := v.(OutputPort)
stack.Push(Boolean(ok1 || ok2))
return nil
}
func FnCallWithInputFile(nargs int) error {
if nargs != 2 {
return errors.New("call-with-input-file takes 2 arguments")
}
FnOpenInputFile(1)
port, ok := stack.Pop().(InputPort)
if !ok {
return errors.New(
"call-with-input-file takes an input port as the 1st argument",
)
}
InputPortStack = append(InputPortStack, port)
p, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New(
"call-with-input-file takes a procedure as the 2nd argument",
)
}
p.Eval()
port.Close()
InputPortStack = InputPortStack[:len(InputPortStack)-1]
return nil
}
func FnCallWithOutputFile(nargs int) error {
if nargs != 2 {
return errors.New("call-with-output-file takes 2 arguments")
}
FnOpenInputFile(1)
port, ok:= stack.Pop().(OutputPort)
if !ok {
return errors.New(
"call-with-output-file takes an output port as the 1st argument",
)
}
OutputPortStack = append(OutputPortStack, port)
p, ok := stack.Pop().(*Procedure)
if !ok {
return errors.New(
"call-with-output-file takes a procedure as the 2nd argument",
)
}
p.Eval()
port.Close()
OutputPortStack = OutputPortStack[:len(OutputPortStack)-1]
return nil
}
func FnOpenInputFile(nargs int) error {
if nargs != 1 {
return errors.New("open-input-file takes 1 argument")
}
fname, ok := stack.Pop().(String)
if !ok {
return errors.New("open-input-file takes a string")
}
f, err := os.Open(*fname.s)
if err != nil {
return err
}
stack.Push(InputPort{f})
return nil
}
func FnOpenOutputFile(nargs int) error {
if nargs != 1 {
return errors.New("open-output-file takes 1 argument")
}
fname, ok := stack.Pop().(String)
if !ok {
return errors.New("open-output-file takes a string")
}
f, err := os.Create(*fname.s)
if err != nil {
return err
}
stack.Push(OutputPort{f})
return nil
}
func FnCloseInputPort(nargs int) error {
if nargs != 1 {
return errors.New("close-input-port takes 1 argument")
}
port, ok := stack.Pop().(InputPort)
if !ok {
return errors.New(
"close-input-port takes an input port as the argument",
)
}
port.Close()
stack.Push(Boolean(true))
return nil
}
func FnCloseOutputPort(nargs int) error {
if nargs != 1 {
return errors.New("close-output-port takes 1 argument")
}
port, ok := stack.Pop().(OutputPort)
if !ok {
return errors.New(
"close-output-port takes an output port as the argument",
)
}
port.Close()
stack.Push(Boolean(true))
return nil
}
func FnRead(nargs int) error {
var port InputPort
if nargs == 0 {
port = InputPortStack[len(InputPortStack) - 1]
} else if nargs == 1 {
var ok bool
port, ok = stack.Pop().(InputPort)
if !ok {
return errors.New("read takes an input port as the argument")
}
} else {
return errors.New("Too many args to read")
}
s := ""
for !validate(s) {
b := make([]byte, 1)
_, err := port.Read(b)
if err == io.EOF {
stack.Push(Eof{})
return nil
} else if err != nil {
return err
}
s += string(b[0])
}
p := NewParser(s)
p.skipWs()
v, err := p.GetValue()
if err != nil {
return err
}
stack.Push(v)
return nil
}
func FnReadChar(nargs int) error {
var port InputPort
if nargs == 0 {
port = InputPortStack[len(InputPortStack) - 1]
} else if nargs == 1 {
var ok bool
port, ok = stack.Pop().(InputPort)
if !ok {
return errors.New("read-char takes an input port as the argument")
}
} else {
return errors.New("Too many args to read-char")
}
bs := []byte{}
r := utf8.RuneError
for r == utf8.RuneError {
b := make([]byte, 1)
_, err := port.Read(b)
if err == io.EOF {
stack.Push(Eof{})
return nil
} else if err != nil {
return err
}
bs = append(bs, b[0])
r, _ = utf8.DecodeRune(b)
}
stack.Push(Char(r))
return nil
}
func FnPeekChar(nargs int) error {
var port InputPort
if nargs == 0 {
port = InputPortStack[len(InputPortStack) - 1]
} else if nargs == 1 {
var ok bool
port, ok = stack.Pop().(InputPort)
if !ok {
return errors.New("peek-char takes an input port as the argument")
}
} else {
return errors.New("Too many args to peek-char")
}
bs := []byte{}
r := utf8.RuneError
for r == utf8.RuneError {
b := make([]byte, 1)
_, err := port.Read(b)
if err == io.EOF {
stack.Push(Eof{})
return nil
} else if err != nil {
return err
}
bs = append(bs, b[0])
r, _ = utf8.DecodeRune(b)
}
port.Seek(-int64(len(bs)), io.SeekCurrent)
stack.Push(Char(r))
return nil
}
func FnIsEofObject(nargs int) error {
if nargs != 1 {
return errors.New("eof-object? takes 1 argument")
}
_, ok := stack.Pop().(Eof)
stack.Push(Boolean(ok))
return nil
}
func FnIsCharReady(nargs int) error {
var port InputPort
if nargs == 0 {
port = InputPortStack[len(InputPortStack) - 1]
} else if nargs == 1 {
var ok bool
port, ok = stack.Pop().(InputPort)
if !ok {
return errors.New("char-ready? takes an input port as the argument")
}
} else {
return errors.New("Too many args to char-ready?")
}
if _, err := port.Read(make([]byte, 1)); err == io.EOF {
stat, _ := port.Stat()
stack.Push(Boolean((stat.Mode() & os.ModeCharDevice) != 0))
return nil
} else if err != nil {
return err
}
port.Seek(-1, io.SeekCurrent)
stack.Push(Boolean(true))
return nil
}
func FnWrite(nargs int) error {
var port OutputPort
v := stack.Pop()
if nargs == 1 {
port = OutputPortStack[len(OutputPortStack) - 1]
} else if nargs == 2 {
var ok bool
port, ok = stack.Pop().(OutputPort)
if !ok {
return errors.New("write takes an input port as the argument")
}
} else {
return errors.New("Too many args to write")
}
OutputPortStack = append(OutputPortStack, port)
WriteValue(v, false)
OutputPortStack = OutputPortStack[:len(OutputPortStack) - 1]
stack.Push(v)
return nil
}
func FnDisplay(nargs int) error {
var port OutputPort
v := stack.Pop()
if nargs == 1 {
port = OutputPortStack[len(OutputPortStack) - 1]
} else if nargs == 2 {
var ok bool
port, ok = stack.Pop().(OutputPort)
if !ok {
return errors.New("display takes an input port as the argument")
}
} else {
return errors.New("Too many args to display")
}
OutputPortStack = append(OutputPortStack, port)
WriteValue(v, true)
OutputPortStack = OutputPortStack[:len(OutputPortStack) - 1]
stack.Push(v)
return nil
}