-
Notifications
You must be signed in to change notification settings - Fork 16
/
functions.go
599 lines (511 loc) · 17.6 KB
/
functions.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package interpreter
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"strings"
"time"
"github.com/remogatto/gospeccy/src/formats"
"github.com/remogatto/gospeccy/src/spectrum"
"github.com/sbinet/go-eval"
)
// Contains the names of all defined functions
var definedFunctions = make(map[string]byte)
func defineFunction(name string, t *eval.FuncType, f eval.FuncValue) {
w.DefineVar(name, t, f)
definedFunctions[name] = 0
}
type Function struct {
Name string // Name of the variable used to access the function
Type *eval.FuncType // Function type
Value eval.FuncValue // The function itself
Help_key string // Help
Help_value string
}
var functionsToAdd []Function
func DefineFunction(f Function) {
mutex.Lock()
defer mutex.Unlock()
if w == nil {
// Postpone the function definition until interpreter initialization
functionsToAdd = append(functionsToAdd, f)
} else {
defineFunction(f.Name, f.Type, f.Value)
if (f.Help_key != "") && (f.Help_value != "") {
help_keys = append(help_keys, f.Help_key)
help_vals = append(help_vals, f.Help_value)
}
}
}
// ================
// Various commands
// ================
var help_keys []string
var help_vals []string
// Signature: func help()
func wrapper_help(t *eval.Thread, in []eval.Value, out []eval.Value) {
fmt.Fprintf(stdout, "\nAvailable commands:\n")
maxKeyLen := 1
for i := 0; i < len(help_keys); i++ {
if len(help_keys[i]) > maxKeyLen {
maxKeyLen = len(help_keys[i])
}
}
for i := 0; i < len(help_keys); i++ {
fmt.Fprintf(stdout, " %s", help_keys[i])
for j := len(help_keys[i]); j < maxKeyLen; j++ {
fmt.Fprintf(stdout, " ")
}
fmt.Fprintf(stdout, " %s\n", help_vals[i])
}
}
// Signature: func exit()
func wrapper_exit(t *eval.Thread, in []eval.Value, out []eval.Value) {
// Implementation note:
// The following test has to be there only in cases in which something can go wrong.
// For example if the user tried to execute "exit(); audio(false)" then GoSpeccy would panic.
// An alternative way would be to actually terminate the whole program at the 1st statement - so that
// ["audio(false)" or whatever else] is not executed - alas this is somewhat problematic,
// since once the script "exit(); audio(false)" runs, it cannot be stopped halfway
// through its execution. Using "runtime.Goexit()" would solve this issue, but only partially,
// since it is potentially possible for the statement "audio(false)" to be hidden in a defer statement.
// So, the best option (until somebody implements a better one) is to convert the problematic commands
// into statements that are doing nothing while the application is in the process of being exited.
if app.TerminationInProgress() || app.Terminated() {
return
}
app.RequestExit()
}
// Signature: func vars() []string
func wrapper_vars(t *eval.Thread, in []eval.Value, out []eval.Value) {
vars := make([]eval.Value, 0, len(intp.vars))
for varName, _ := range intp.vars {
s := string_value_t(varName)
vars = append(vars, &s)
}
var slice eval.Slice
base := array_value_t(vars)
slice.Base = &base
slice.Len = int64(len(vars))
slice.Cap = int64(len(vars))
out[0].(eval.SliceValue).Set(t, slice)
}
// Signature: func definedFunction(name string) bool
func wrapper_definedFunction(t *eval.Thread, in []eval.Value, out []eval.Value) {
name := in[0].(eval.StringValue).Get(t)
_, defined := definedFunctions[name]
out[0].(eval.BoolValue).Set(t, defined)
}
// Signature: func reset()
func wrapper_reset(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
romLoaded := make(chan (<-chan bool))
speccy.CommandChannel <- spectrum.Cmd_Reset{romLoaded}
<-(<-romLoaded)
}
// Signature: func addSearchPath(path string)
func wrapper_addSearchPath(t *eval.Thread, in []eval.Value, out []eval.Value) {
path := in[0].(eval.StringValue).Get(t)
spectrum.AddCustomSearchPath(path)
}
// Signature: func setDownloadPath(path string)
func wrapper_setDownloadPath(t *eval.Thread, in []eval.Value, out []eval.Value) {
path := in[0].(eval.StringValue).Get(t)
spectrum.SetDownloadPath(path)
}
func load(path string) {
var program interface{}
program, err := formats.ReadProgram(path)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
if _, isTAP := program.(*formats.TAP); isTAP {
romLoaded := make(chan (<-chan bool))
speccy.CommandChannel <- spectrum.Cmd_Reset{romLoaded}
<-(<-romLoaded)
}
errChan := make(chan error)
speccy.CommandChannel <- spectrum.Cmd_Load{path, program, errChan}
err = <-errChan
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
}
// Signature: func load(path string)
func wrapper_load(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
path := in[0].(eval.StringValue).Get(t)
var err error
path, err = spectrum.ProgramPath(path)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
load(path)
}
// Signature: func cmdLineArg() string
func wrapper_cmdLineArg(t *eval.Thread, in []eval.Value, out []eval.Value) {
out[0].(eval.StringValue).Set(t, cmdLineArg)
}
// Signature: func save(path string)
func wrapper_save(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
path := in[0].(eval.StringValue).Get(t)
ch := make(chan *formats.FullSnapshot)
speccy.CommandChannel <- spectrum.Cmd_MakeSnapshot{ch}
fullSnapshot := <-ch
data, err := fullSnapshot.EncodeSNA()
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
err = ioutil.WriteFile(path, data, 0600)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
}
if app.Verbose {
fmt.Fprintf(stdout, "wrote SNA snapshot \"%s\"", path)
}
}
// Signature: func fps(n float32)
func wrapper_fps(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
fps := in[0].(eval.FloatValue).Get(t)
speccy.CommandChannel <- spectrum.Cmd_SetFPS{float32(fps), nil}
}
// Signature: func ula_accuracy(accurateEmulation bool)
func wrapper_ulaAccuracy(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
accurateEmulation := in[0].(eval.BoolValue).Get(t)
speccy.CommandChannel <- spectrum.Cmd_SetUlaEmulationAccuracy{accurateEmulation}
}
// Signature: func wait(milliseconds uint)
func wrapper_wait(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
milliseconds := in[0].(eval.UintValue).Get(t)
time.Sleep(time.Millisecond * time.Duration(milliseconds))
}
// Signature: func script(scriptName string)
func wrapper_script(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
path := in[0].(eval.StringValue).Get(t)
var err error
path, err = spectrum.ScriptPath(path)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
err = runScript(w, path, false /*optional*/)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
}
// Signature: func optionalScript(scriptName string)
func wrapper_optionalScript(t *eval.Thread, in []eval.Value, out []eval.Value) {
scriptName := in[0].(eval.StringValue).Get(t)
err := runScript(w, scriptName, true /*optional*/)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
}
// Signature: func screenshot(screenshotName string)
func wrapper_screenshot(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
path := in[0].(eval.StringValue).Get(t)
ch := make(chan []byte)
speccy.CommandChannel <- spectrum.Cmd_MakeVideoMemoryDump{ch}
data := <-ch
err := ioutil.WriteFile(path, data, 0600)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
}
if app.Verbose {
fmt.Fprintf(stdout, "wrote screenshot \"%s\"", path)
}
}
// Signature: func puts(str string)
func wrapper_puts(t *eval.Thread, in []eval.Value, out []eval.Value) {
str := in[0].(eval.StringValue).Get(t)
fmt.Fprintf(stdout, "%s", str)
}
// Signature: func acceleratedLoad(on bool)
func wrapper_acceleratedLoad(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
enable := in[0].(eval.BoolValue).Get(t)
speccy.CommandChannel <- spectrum.Cmd_SetAcceleratedLoad{enable}
}
type WOS struct {
URL string
MachineType string
Publication string
Score string
}
func url_printer(URL eval.Value) string {
s := URL.(eval.StringValue).Get(nil)
if len(s) > 60 {
var buf bytes.Buffer
i := 0
for _, rune := range s {
if i < 10 {
buf.WriteRune(rune)
} else if i == 10 {
buf.WriteString("...")
} else if (i > 10) && (i < len(s)-(60-3)) {
// Nothing
} else {
buf.WriteRune(rune)
}
i++
}
s = buf.String()
}
return s
}
// Signature: func wosFind(pattern string) []WOS
func wrapper_wosFind(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
pattern := in[0].(eval.StringValue).Get(t)
pattern = strings.Replace(pattern, " ", "*", -1)
var records []spectrum.WosRecord
records, err := spectrum.WosQuery(app, "regexp="+url.QueryEscape(pattern))
if err != nil {
fmt.Fprintf(stdout, "%s", err)
var emptySlice eval.Slice
out[0].(eval.SliceValue).Set(t, emptySlice)
return
}
var woss []eval.Value
for _, r := range records {
for _, url := range r.FtpFiles {
mac_value := string_value_t(r.MachineType)
pub_value := string_value_t(r.Publication)
sco_value := string_value_t(r.Score)
url_value := string_value_t(url)
var wos struct_value_t
wos.fields = []eval.Value{&url_value, &mac_value, &pub_value, &sco_value}
wos.names = []string{"URL", "Machine type", "Publication", "Score"}
wos.printers_orNil = []func(eval.Value) string{url_printer, nil, nil, nil}
wos.hide_orNil = nil
wos.printStyle = MULTI_LINE
woss = append(woss, &wos)
}
}
var slice eval.Slice
base := array_value_t(woss)
slice.Base = &base
slice.Len = int64(len(woss))
slice.Cap = int64(len(woss))
out[0].(eval.SliceValue).Set(t, slice)
}
// Signature: func wosDownload(wos WOS) string
func wrapper_wosDownload(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
var url string = in[0].(eval.StructValue).Field(t, 0).(eval.StringValue).Get(t)
filePath, err := spectrum.WosGet(app, stdout, url)
if err != nil {
fmt.Fprintf(stdout, "%s", err)
out[0].(eval.StringValue).Set(t, "")
return
}
out[0].(eval.StringValue).Set(t, filePath)
}
// Signature: func wosLoad(wos WOS)
func wrapper_wosLoad(t *eval.Thread, in []eval.Value, out []eval.Value) {
if app.TerminationInProgress() || app.Terminated() {
return
}
var url string = in[0].(eval.StructValue).Field(t, 0).(eval.StringValue).Get(t)
filePath, err := spectrum.WosGet(app, stdout, url)
if err != nil {
fmt.Fprintf(stdout, "%s", err)
return
}
load(filePath)
}
// ==============
// Initialization
// ==============
func defineFunctions(w *eval.World) {
{
var functionSignature func()
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_help, functionSignature)
defineFunction("help", funcType, funcValue)
help_keys = append(help_keys, "help()")
help_vals = append(help_vals, "This help")
}
{
var functionSignature func()
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_exit, functionSignature)
defineFunction("exit", funcType, funcValue)
help_keys = append(help_keys, "exit()")
help_vals = append(help_vals, "Terminate this program")
}
{
var functionSignature func() []string
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_vars, functionSignature)
defineFunction("vars", funcType, funcValue)
help_keys = append(help_keys, "vars()")
help_vals = append(help_vals, "Get the names of all variables")
}
{
var functionSignature func()
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_reset, functionSignature)
defineFunction("reset", funcType, funcValue)
help_keys = append(help_keys, "reset()")
help_vals = append(help_vals, "Reset the emulated machine")
}
{
var functionSignature func(string) bool
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_definedFunction, functionSignature)
defineFunction("definedFunction", funcType, funcValue)
help_keys = append(help_keys, "definedFunction(name string) bool")
help_vals = append(help_vals, "Returns whether a Go function exists")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_addSearchPath, functionSignature)
defineFunction("addSearchPath", funcType, funcValue)
help_keys = append(help_keys, "addSearchPath(path string)")
help_vals = append(help_vals, "Append to the paths searched when loading snapshots, scripts, etc")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_setDownloadPath, functionSignature)
defineFunction("setDownloadPath", funcType, funcValue)
help_keys = append(help_keys, "setDownloadPath(path string)")
help_vals = append(help_vals, `Set path where to download files (""=default path)`)
}
{
var functionSignature func() string
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_cmdLineArg, functionSignature)
defineFunction("cmdLineArg", funcType, funcValue)
help_keys = append(help_keys, "cmdLineArg() string)")
help_vals = append(help_vals, "The 1st non-flag command-line argument, or an empty string")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_load, functionSignature)
defineFunction("load", funcType, funcValue)
help_keys = append(help_keys, "load(path string)")
help_vals = append(help_vals, "Load state from file (.SNA, .Z80, .Z80.ZIP, etc)")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_save, functionSignature)
defineFunction("save", funcType, funcValue)
help_keys = append(help_keys, "save(path string)")
help_vals = append(help_vals, "Save state to file (SNA format)")
}
{
var functionSignature func(float32)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_fps, functionSignature)
defineFunction("fps", funcType, funcValue)
help_keys = append(help_keys, "fps(n float32)")
help_vals = append(help_vals, "Change the display refresh frequency (0=default FPS)")
}
{
var functionSignature func(bool)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_ulaAccuracy, functionSignature)
defineFunction("ula", funcType, funcValue)
help_keys = append(help_keys, "ula(accurateEmulation bool)")
help_vals = append(help_vals, "Enable/disable accurate ULA emulation")
}
{
var functionSignature func(uint)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_wait, functionSignature)
defineFunction("wait", funcType, funcValue)
help_keys = append(help_keys, "wait(milliseconds uint)")
help_vals = append(help_vals, "Wait before executing the next command")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_script, functionSignature)
defineFunction("script", funcType, funcValue)
help_keys = append(help_keys, "script(scriptName string)")
help_vals = append(help_vals, "Load and evaluate the specified Go script")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_optionalScript, functionSignature)
defineFunction("optionalScript", funcType, funcValue)
help_keys = append(help_keys, "optionalScript(scriptName string)")
help_vals = append(help_vals, "Load (if found) and evaluate the specified Go script")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_screenshot, functionSignature)
defineFunction("screenshot", funcType, funcValue)
help_keys = append(help_keys, "screenshot(screenshotName string)")
help_vals = append(help_vals, "Take a screenshot of the current display")
}
{
var functionSignature func(string)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_puts, functionSignature)
defineFunction("puts", funcType, funcValue)
help_keys = append(help_keys, "puts(str string)")
help_vals = append(help_vals, "Print the given string")
}
{
var functionSignature func(bool)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_acceleratedLoad, functionSignature)
defineFunction("acceleratedLoad", funcType, funcValue)
help_keys = append(help_keys, "acceleratedLoad(on bool)")
help_vals = append(help_vals, "Set accelerated tape load on/off")
}
{
var functionSignature func(string) []WOS
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_wosFind, functionSignature)
defineFunction("wosFind", funcType, funcValue)
help_keys = append(help_keys, "wosFind(pattern string) []WOS")
help_vals = append(help_vals, "Find tapes and snapshots on worldofspectrum.org")
}
{
var functionSignature func(WOS) string
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_wosDownload, functionSignature)
defineFunction("wosDownload", funcType, funcValue)
help_keys = append(help_keys, "wosDownload(wos WOS) string")
help_vals = append(help_vals, "Download from worldofspectrum.org")
}
{
var functionSignature func(WOS)
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_wosLoad, functionSignature)
defineFunction("wosLoad", funcType, funcValue)
help_keys = append(help_keys, "wosLoad(wos WOS)")
help_vals = append(help_vals, "Same as load(wosDownload(wos))")
}
for _, f := range functionsToAdd {
defineFunction(f.Name, f.Type, f.Value)
if (f.Help_key != "") && (f.Help_value != "") {
help_keys = append(help_keys, f.Help_key)
help_vals = append(help_vals, f.Help_value)
}
}
functionsToAdd = nil
}