-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
177 lines (162 loc) · 4.32 KB
/
ast.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
package main
import (
"cmp"
"fmt"
"reflect"
"strings"
)
var (
exprTrue = ExprKeyword(":true")
exprFalse = ExprKeyword(":false")
exprNil = ExprKeyword(":nil")
)
type Expr interface {
isExpr()
}
func (ExprIdent) isExpr() {}
func (ExprKeyword) isExpr() {}
func (ExprStr) isExpr() {}
func (ExprNum) isExpr() {}
func (ExprList) isExpr() {}
func (ExprVec) isExpr() {}
func (ExprHashMap) isExpr() {}
func (*ExprAtom) isExpr() {}
func (ExprErr) isExpr() {}
func (ExprFunc) isExpr() {}
func (*ExprFn) isExpr() {}
type ExprIdent string
type ExprKeyword string
type ExprStr string
type ExprNum int
type ExprList []Expr
type ExprVec []Expr
type ExprHashMap map[string]Expr
type ExprAtom struct{ Ref Expr }
type ExprErr struct{ It any }
type ExprFunc func([]Expr) (Expr, error)
type ExprFn struct { // if it weren't for TCO, just the above `ExprFunc` would suffice.
params []Expr // all are guaranteed to be `ExprIdent` before constructing an `ExprFn`
body Expr
env *Env
isMacro bool
isVariadic bool
nameMaybe string
}
func (me *ExprFn) envWith(args []Expr) (*Env, error) {
num_args_min, num_args_max := len(me.params), len(me.params)
if me.isVariadic {
num_args_min, num_args_max = len(me.params)-1, -1
}
if err := checkArgsCount(num_args_min, num_args_max, strings.TrimSpace("function "+me.nameMaybe), args); err != nil {
return nil, err
}
if me.isVariadic {
the_var_args := args[len(me.params)-1:]
args = append(append(make([]Expr, 0, len(me.params)-1+len(the_var_args)), args[:len(me.params)-1]...), (ExprList)(the_var_args))
}
return newEnv(me.env, me.params, args), nil
}
// note, `(*ExprFn).Call` is itself an `ExprFunc`
func (me *ExprFn) Call(args []Expr) (Expr, error) {
env, err := me.envWith(args)
if err != nil {
return nil, err
}
return evalAndApply(env, me.body)
}
func (me ExprErr) Error() string {
if err, _ := me.It.(error); err != nil {
return err.Error()
}
if expr, _ := me.It.(Expr); expr != nil {
return str(false, expr)
}
return fmt.Sprintf("%#v", me.It)
}
func exprBool(b bool) ExprKeyword {
if b {
return exprTrue
}
return exprFalse
}
func exprStrOrKeyword(s string) Expr {
if (s != "") && (s[0] == ':') {
return ExprKeyword(s)
}
return ExprStr(s)
}
func compare(args []Expr) (int, error) {
if err := checkArgsCount(2, 2, "comparer", args); err != nil {
return 0, err
}
switch it := args[0].(type) {
case ExprNum:
if other, ok := args[1].(ExprNum); ok {
return cmp.Compare(it, other), nil
}
case ExprStr:
if other, ok := args[1].(ExprStr); ok {
return cmp.Compare(it, other), nil
}
}
return 0, fmt.Errorf("specified operands `%#v` and `%#v` are not comparable", args[0], args[1])
}
func isListOrVec(seq Expr) bool {
ty := reflect.TypeOf(seq)
return (ty == reflect.TypeFor[ExprList]()) || (ty == reflect.TypeFor[ExprVec]())
}
func isListStartingWithIdent(maybeList Expr, ident ExprIdent, mustHaveLen int) (list []Expr, doesListStartWithIdent bool, err error) {
if list, _ = maybeList.(ExprList); len(list) > 0 {
if maybe_ident, _ := list[0].(ExprIdent); maybe_ident == ident {
doesListStartWithIdent, err = true, checkArgsCount(mustHaveLen, mustHaveLen, "form `"+string(ident)+"`", list)
}
}
return
}
func isEq(arg1 Expr, arg2 Expr) bool {
ty1, ty2 := reflect.TypeOf(arg1), reflect.TypeOf(arg2)
if (ty1 != ty2) && ((!isListOrVec(arg1)) || !isListOrVec(arg2)) {
return false
}
switch arg1.(type) {
case ExprErr: // TODO: not fully correct this way:
return ((reflect.TypeOf(arg1.(ExprErr).It) == reflect.TypeOf(arg2.(ExprErr).It)) && (arg1.(ExprErr).Error() == arg2.(ExprErr).Error()))
case *ExprAtom:
return arg1.(*ExprAtom).Ref == arg2.(*ExprAtom).Ref
case ExprVec, ExprList:
sl1, _ := checkIsSeq(arg1)
sl2, _ := checkIsSeq(arg2)
if len(sl1) != len(sl2) {
return false
}
for i := 0; i < len(sl1); i += 1 {
if !isEq(sl1[i], sl2[i]) {
return false
}
}
return true
case ExprHashMap:
hm1, hm2 := arg1.(ExprHashMap), arg2.(ExprHashMap)
if len(hm1) != len(hm2) {
return false
}
for k, v := range hm1 {
if !isEq(v, hm2[k]) {
return false
}
}
return true
default:
return arg1 == arg2
}
}
func str(srcLike bool, args ...Expr) string {
var buf strings.Builder
for i, arg := range args {
if i > 0 && srcLike {
buf.WriteByte(' ')
}
exprWriteTo(&buf, arg, srcLike)
}
return buf.String()
}