This repository has been archived by the owner on May 23, 2021. It is now read-only.
/
expr.go
156 lines (131 loc) · 2.97 KB
/
expr.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
package parens
import (
"fmt"
"reflect"
"strings"
)
var (
_ Expr = (*ConstExpr)(nil)
_ Expr = (*DefExpr)(nil)
_ Expr = (*QuoteExpr)(nil)
_ Expr = (*InvokeExpr)(nil)
_ Expr = (*IfExpr)(nil)
_ Expr = (*DoExpr)(nil)
)
// ConstExpr returns the Const value wrapped inside when evaluated. It has
// no side-effect on the VM.
type ConstExpr struct{ Const Any }
// Eval returns the constant value unmodified.
func (ce ConstExpr) Eval() (Any, error) { return ce.Const, nil }
// QuoteExpr expression represents a quoted form and
type QuoteExpr struct{ Form Any }
// Eval returns the quoted form unmodified.
func (qe QuoteExpr) Eval() (Any, error) {
// TODO: re-use this for syntax-quote and unquote?
return qe.Form, nil
}
// DefExpr creates a global binding with the Name when evaluated.
type DefExpr struct {
Env *Env
Name string
Value Expr
}
// Eval creates a symbol binding in the global (root) stack frame.
func (de DefExpr) Eval() (Any, error) {
de.Name = strings.TrimSpace(de.Name)
if de.Name == "" {
return nil, fmt.Errorf("%w: '%s'", ErrInvalidBindName, de.Name)
}
val, err := de.Value.Eval()
if err != nil {
return nil, err
}
de.Env.setGlobal(de.Name, val)
return Symbol(de.Name), nil
}
// IfExpr represents the if-then-else form.
type IfExpr struct{ Test, Then, Else Expr }
// Eval the expression
func (ife IfExpr) Eval() (Any, error) {
target := ife.Else
if ife.Test != nil {
test, err := ife.Test.Eval()
if err != nil {
return nil, err
}
if IsTruthy(test) {
target = ife.Then
}
}
if target == nil {
return Nil{}, nil
}
return target.Eval()
}
// DoExpr represents the (do expr*) form.
type DoExpr struct{ Exprs []Expr }
// Eval the expression
func (de DoExpr) Eval() (Any, error) {
var res Any
var err error
for _, expr := range de.Exprs {
res, err = expr.Eval()
if err != nil {
return nil, err
}
}
if res == nil {
return Nil{}, nil
}
return res, nil
}
// InvokeExpr performs invocation of target when evaluated.
type InvokeExpr struct {
Env *Env
Name string
Target Expr
Args []Expr
}
// Eval the expression
func (ie InvokeExpr) Eval() (Any, error) {
val, err := ie.Target.Eval()
if err != nil {
return nil, err
}
fn, ok := val.(Invokable)
if !ok {
return nil, Error{
Cause: ErrNotInvokable,
Message: fmt.Sprintf("value of type '%s' is not invokable", reflect.TypeOf(val)),
}
}
var args []Any
for _, ae := range ie.Args {
v, err := ae.Eval()
if err != nil {
return nil, err
}
args = append(args, v)
}
ie.Env.push(stackFrame{
Name: ie.Name,
Args: args,
Vars: map[string]Any{},
})
defer ie.Env.pop()
return fn.Invoke(ie.Env, args...)
}
// GoExpr evaluates an expression in a separate goroutine.
type GoExpr struct {
Env *Env
Value Any
}
// Eval forks the given context to get a child context and launches goroutine
// with the child context to evaluate the
func (ge GoExpr) Eval() (Any, error) {
child := ge.Env.Fork()
go func() {
_, _ = child.Eval(ge.Value)
}()
return nil, nil
}