-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
401 lines (374 loc) · 10.1 KB
/
parser.go
File metadata and controls
401 lines (374 loc) · 10.1 KB
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
package golisp2
import (
"errors"
"fmt"
"io"
"strconv"
)
// ParseTokens reads in the tokens, and converts them to a set of expressions.
// Returns the set, and any parse errors that are encountered in the process.
func ParseTokens(ts *TokenScanner) ([]Expr, error) {
ts.Advance() // initializes the scan
exprs, exprsErr := maybeParseExprs(ts)
if exprsErr != nil {
return nil, exprsErr
}
if ts.Err() != nil && !errors.Is(ts.Err(), io.EOF) {
return nil, fmt.Errorf("problem reading source: %w", ts.Err())
}
if !ts.Done() {
return nil, NewParseEOFError("parse ended before EOF", ts.Pos())
}
return exprs, nil
}
// maybeParseExprs will read as many expressions as it can, until it hits EOF or
// a close boundary character.
func maybeParseExprs(ts *TokenScanner) ([]Expr, error) {
exprs := []Expr{}
for {
maybeExpr, maybeExprErr := maybeParseExpr(ts)
if maybeExprErr != nil {
return nil, maybeExprErr
}
if maybeExpr == nil {
return exprs, nil
}
exprs = append(exprs, maybeExpr)
}
}
// maybeParseExpr will attempt to read a complete expression from the scanner.
// Will return an error if any problems are encountered. Will return (nil, nil)
// if the scanner has no more expressions, or the end of a block is reached.
func maybeParseExpr(ts *TokenScanner) (Expr, error) {
maybeNextToken := ts.Token()
if maybeNextToken == nil {
return nil, nil
}
nextToken := *maybeNextToken
switch nextToken.Typ {
case CloseParenTT:
return nil, nil
case OpenParenTT:
return tryParseCall(ts)
case IdentTT:
ts.Advance()
return parseIdentValue(nextToken)
case OpTT:
ts.Advance()
return parseOpValue(nextToken)
case NumberTT:
ts.Advance()
return parseNumberValue(nextToken)
case StringTT:
ts.Advance()
return parseStringValue(nextToken)
default:
return nil, NewParseError("invalid token", nextToken)
}
}
// tryParseCall will attempt to parse a call statement from the current location
// of the scanner.
func tryParseCall(ts *TokenScanner) (Expr, error) {
maybeStartToken := ts.Token()
if maybeStartToken == nil {
return nil, NewParseEOFError("parse on empty scanner", ts.Pos())
}
startToken := *maybeStartToken
if startToken.Typ != OpenParenTT {
return nil, NewParseError(
"call expression must start with open paren", startToken)
}
ts.Advance()
maybeNextToken := ts.Token()
if maybeNextToken == nil {
return nil, NewParseError("parse ended inside of call", startToken)
}
nextToken := *maybeNextToken
if nextToken.Typ == IdentTT {
// note (bs): strongly consider making this a data structure; will make
// rejecting usages of reserved words as idents much easier
switch nextToken.Value {
case "if":
return tryParseIfTail(ts)
case "fn":
return tryParseFnTail(ts)
case "let":
return tryParseLetTail(ts)
case "defun":
panic("defun not implemented")
case "import":
panic("import not implemented")
}
}
return tryParseCallTail(ts)
}
// tryParseCallTail will try to trace a function call. This assumes the first
// paren has already been parsed.
func tryParseCallTail(ts *TokenScanner) (Expr, error) {
bodyExprs, bodyExprsErr := maybeParseExprs(ts)
if bodyExprsErr != nil {
return nil, bodyExprsErr
}
if err := expectCallClose(ts); err != nil {
return nil, err
}
return &CallExpr{
Exprs: bodyExprs,
}, nil
}
// parseStringValue converts the string token to a string value.
func parseStringValue(token ScannedToken) (*StringLiteral, error) {
v := token.Value
if len(v) == 0 {
return &StringLiteral{
Str: "",
Pos: token.Pos,
}, nil
}
leadI, tailI := 0, len(v)
if v[0] == '"' {
leadI = 1
}
if len(v) > 1 && v[len(v)-1] == '"' {
tailI = len(v) - 1
}
return &StringLiteral{
Str: v[leadI:tailI],
Pos: token.Pos,
}, nil
}
// parseIdentValue converts the ident token to an ident value.
func parseIdentValue(token ScannedToken) (Expr, error) {
// todo (bs): this should search for certain reserved words, and reject them.
// e.g. any of the "structural builtins" like if, defun, let, etc.
switch token.Value {
case "nil":
return &NilLiteral{
Pos: token.Pos,
}, nil
case "true":
return &BoolLiteral{
Bool: true,
Pos: token.Pos,
}, nil
case "false":
return &BoolLiteral{
Bool: false,
Pos: token.Pos,
}, nil
default:
return &IdentLiteral{
Val: token.Value,
Pos: token.Pos,
}, nil
}
}
// parseNumberValue converts the number token to a number value.
func parseNumberValue(token ScannedToken) (*NumberLiteral, error) {
// todo (bs): given that this is, you know, a *parser*, it's awfully clumsy to
// outsource the final number parsing to Go. The manual parse should be able
// to correctly map this to a number.
f, e := strconv.ParseFloat(token.Value, 64)
if e != nil {
return nil, NewParseError(
fmt.Sprintf("could not parse number [err=%s]", e),
token,
)
}
return &NumberLiteral{
Num: f,
Pos: token.Pos,
}, nil
}
// parseOpValue converts the operator token to a function value. If the operator
// isn't supported, an error is returned.
func parseOpValue(token ScannedToken) (*FuncLiteral, error) {
// note (bs): this should probably exist as a discrete value
opMap := map[string]func(*EvalContext, ...Value) (Value, error){
"+": addFn,
"-": subFn,
"*": multFn,
"/": divFn,
"==": eqNumFn,
"<": ltNumFn,
">": gtNumFn,
"<=": lteNumFn,
">=": gteNumFn,
}
if fn, ok := opMap[token.Value]; ok {
return &FuncLiteral{
Name: token.Value,
Fn: fn,
Pos: token.Pos,
}, nil
}
return nil, NewParseError("unrecognized operator", token)
}
// tryParseIfTail will complete the parse of an if statement where the open
// paren has already been scanned.
func tryParseIfTail(ts *TokenScanner) (Expr, error) {
maybeStartToken := ts.Token()
if maybeStartToken == nil {
return nil, NewParseEOFError("parse on empty scanner", ts.Pos())
}
startToken := *maybeStartToken
if startToken.Typ != IdentTT || startToken.Value != "if" {
return nil, NewParseError("tryParseIfTail called on non-if", startToken)
}
ts.Advance()
ifBody, ifBodyErr := maybeParseExprs(ts)
if ifBodyErr != nil {
return nil, ifBodyErr
}
var cond, case1, case2 Expr
if len(ifBody) == 0 {
return nil, NewParseError("if statement must have condition", startToken)
}
cond = ifBody[0]
if len(ifBody) > 1 {
case1 = ifBody[1]
}
if len(ifBody) > 2 {
case2 = ifBody[2]
}
if len(ifBody) > 3 {
return nil, NewParseError(
"if statement can have no more than 3 expressions", startToken)
}
if err := expectCallClose(ts); err != nil {
return nil, err
}
return &IfExpr{
Cond: wrapNilExpr(cond),
Case1: wrapNilExpr(case1),
Case2: wrapNilExpr(case2),
Pos: startToken.Pos,
}, nil
}
// tryParseIfTail will complete the parse of an function declaration where the
// open paren has already been scanned.
func tryParseFnTail(ts *TokenScanner) (Expr, error) {
maybeStartToken := ts.Token()
if maybeStartToken == nil {
return nil, NewParseEOFError("parse on empty scanner", ts.Pos())
}
startToken := *maybeStartToken
if startToken.Typ != IdentTT || startToken.Value != "fn" {
return nil, NewParseError("tryParseFnTail called on non-fn", startToken)
}
ts.Advance()
args, argsErr := tryParseFnArgs(ts)
if argsErr != nil {
return nil, argsErr
}
bodyExprs, bodyExprsErr := maybeParseExprs(ts)
if bodyExprsErr != nil {
return nil, bodyExprsErr
}
if err := expectCallClose(ts); err != nil {
return nil, err
}
return &FnExpr{
Args: args,
Body: bodyExprs,
Pos: startToken.Pos,
}, nil
}
// tryParseFnArgs will attempt to parse a set of function arguments from the
// scanner. If a valid set of arguments are not found, an error is returned.
func tryParseFnArgs(ts *TokenScanner) ([]Arg, error) {
if err := expectCallOpen(ts); err != nil {
return nil, err
}
args := []Arg{}
for {
maybeNextToken := ts.Token()
if maybeNextToken == nil {
// todo (bs): add proper parse error info here
return nil, NewParseEOFError("file ended in function args", ts.Pos())
}
nextToken := *maybeNextToken
ts.Advance()
switch nextToken.Typ {
case IdentTT:
args = append(args, Arg{
Ident: nextToken.Value,
})
case CloseParenTT:
return args, nil
default:
return nil, NewParseError("args can only contain idents", nextToken)
}
}
}
// tryParseLetTail will complete the parse of a let statement where the open
// paren has already been scanned.
func tryParseLetTail(ts *TokenScanner) (Expr, error) {
maybeStartToken := ts.Token()
if maybeStartToken == nil {
return nil, NewParseEOFError("parse ended in let statement", ts.Pos())
}
startToken := *maybeStartToken
if startToken.Typ != IdentTT || startToken.Value != "let" {
return nil, NewParseError("tryParseLetTail called on non-let", startToken)
}
ts.Advance()
letExprs, letExprsErr := maybeParseExprs(ts)
if letExprsErr != nil {
return nil, letExprsErr
}
if len(letExprs) != 2 {
return nil, NewParseError(
fmt.Sprintf("let expects 2 arguments, got %d",
len(letExprs)), startToken)
}
asIdent, isIdent := letExprs[0].(*IdentLiteral)
if !isIdent {
return nil, NewParseError(
"let expects an ident as first argument", startToken)
}
val := letExprs[1]
if err := expectCallClose(ts); err != nil {
return nil, err
}
return &LetExpr{
Ident: asIdent,
Value: val,
Pos: startToken.Pos,
}, nil
}
// expectCallOpen will read a open paren from the scanner and advance, or
// return an error.
func expectCallOpen(ts *TokenScanner) error {
maybeNext := ts.Token()
if maybeNext == nil {
return NewParseEOFError("unexpected end of input", ts.Pos())
}
next := *maybeNext
if next.Typ != OpenParenTT {
return NewParseError("expected open paren", next)
}
ts.Advance()
return nil
}
// expectCallClose will read a close paren from the scanner and advance, or
// return an error.
func expectCallClose(ts *TokenScanner) error {
maybeNext := ts.Token()
if maybeNext == nil {
return NewParseEOFError("unexpected end of input", ts.Pos())
}
next := *maybeNext
if next.Typ != CloseParenTT {
return NewParseError("expected close paren", next)
}
ts.Advance()
return nil
}
// wrapNilExpr will return a nil expr if e is nil.
func wrapNilExpr(e Expr) Expr {
if e == nil {
return NewNilLiteral()
}
return e
}