/
coroutines.go
46 lines (39 loc) · 1000 Bytes
/
coroutines.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
package zygo
import (
"errors"
)
type SexpGoroutine struct {
env *Zlisp
}
func (goro *SexpGoroutine) SexpString(ps *PrintState) string {
return "[coroutine]"
}
func (goro *SexpGoroutine) Type() *RegisteredType {
return nil // TODO what goes here
}
func StartGoroutineFunction(env *Zlisp, name string,
args []Sexp) (Sexp, error) {
switch t := args[0].(type) {
case *SexpGoroutine:
go t.env.Run()
default:
return SexpNull, errors.New("not a goroutine")
}
return SexpNull, nil
}
func CreateGoroutineMacro(env *Zlisp, name string,
args []Sexp) (Sexp, error) {
goroenv := env.Duplicate()
err := goroenv.LoadExpressions(args)
if err != nil {
return SexpNull, nil
}
goro := &SexpGoroutine{goroenv}
// (apply StartGoroutineFunction [goro])
return MakeList([]Sexp{env.MakeSymbol("apply"),
MakeUserFunction("__start", StartGoroutineFunction),
&SexpArray{Val: []Sexp{goro}, Env: env}}), nil
}
func (env *Zlisp) ImportGoroutines() {
env.AddMacro("go", CreateGoroutineMacro)
}