This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
193 lines (166 loc) · 4.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package goal
// expr are built by the first left to right pass, resulting in a tree
// of blocks producing a whole expression, with simplified token
// information, and stack-like order. It is a non-resolved AST intermediary
// representation.
type expr interface {
node()
}
// exprs represents a sequence of expressions applied in sequence monadically.
type exprs []expr
// astToken represents a simplified token after processing into expr.
type astToken struct {
Type astTokenType
Pos int
Text string
}
// astTokenType represents tokens in a ppExpr.
type astTokenType int
// These constants represent the possible tokens in a ppExpr.
const (
astNUMBER astTokenType = iota
astSTRING
astIDENT
astMONAD
astDYAD
astADVERB // only within astDerivedVerb
astREGEXP
astEMPTYLIST
)
// astReturn represents an early return :x or 'x.
type astReturn struct {
Expr expr
OnError bool
}
// astLog represents a debugging \expr statement.
type astLog struct {
Expr expr
Pos int
}
// astAssign represents an assignment x:y.
type astAssign struct {
Name string // x
Global bool // whether :: or not
Right expr // y
Pos int
}
// astListAssign represents an assignment (x0;...):y.
type astListAssign struct {
Names []string // (x0;...)
Global bool // whether :: or not
Right expr // y
Pos int
}
// astAssignOp represents a variable assignment with a built-in operator, of
// the form x op: y, semantically equivalent to x: x op y.
type astAssignOp struct {
Name string // x
Global bool // wether :: or not
Dyad string // op
Right expr // y
Pos int
}
// astAssinAmendOp represents an assign-amend call with a built-in operator, of
// the form x[y]op: z, semantically equivalent to x: @[x;y;op;z].
type astAssignAmendOp struct {
Name string // x
Global bool // whether :: or not
Dyad string // op
Indices expr // y
Right expr // z
Pos int
}
// astAssinDeepAmendOp represents an assign-amend call with a built-in operator, of
// the form x[y;...]op: z, semantically equivalent to x: .[x;y;op;z].
type astAssignDeepAmendOp struct {
Name string // x
Global bool // whether :: or not
Dyad string // op
Indices *astList // y
Right expr // z
Pos int
}
// astStrand represents a stranding of literals, like 1 23 456
type astStrand struct {
Items []expr
Pos int
Interp bool
}
// astQq represents an interpolation construct qq/STRING/.
type astQq struct {
Tokens []astToken
Pos int
}
// astDerivedVerb represents a derived verb.
type astDerivedVerb struct {
Adverb *astToken
Verb expr
}
type astParen struct {
Expr expr // parenthesized sub-expressions
EndPos int
}
type astApply2 struct {
Verb expr // dyad
Left expr
Right expr
}
type astApply2Adverb struct {
Verb expr // derived verb
Left expr
Right expr
}
type astApplyN struct {
Verb expr
Args []expr
EndPos int
}
type astList struct {
Args []expr
EndPos int
}
type astSeq struct {
Body []expr
EndPos int
}
type astLambda struct {
Body []expr
Args []string
StartPos int
EndPos int
}
type astNop struct{}
func (es exprs) node() {}
func (t *astToken) node() {}
func (a *astReturn) node() {}
func (a *astLog) node() {}
func (a *astAssign) node() {}
func (a *astListAssign) node() {}
func (a *astAssignOp) node() {}
func (a *astAssignAmendOp) node() {}
func (a *astAssignDeepAmendOp) node() {}
func (st *astStrand) node() {}
func (qq *astQq) node() {}
func (dv *astDerivedVerb) node() {}
func (p *astParen) node() {}
func (a *astApply2) node() {}
func (a *astApply2Adverb) node() {}
func (a *astApplyN) node() {}
func (l *astList) node() {}
func (b *astSeq) node() {}
func (b *astLambda) node() {}
func (n *astNop) node() {}
func nonEmpty(e expr) bool {
switch e := e.(type) {
case *astNop:
return false
case exprs:
return len(e) > 0
default:
return true
}
}
type parseCLOSE struct {
Pos int
}
func (p parseCLOSE) Error() string { return "CLOSE" }