-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.nim
676 lines (575 loc) · 21.3 KB
/
parse.nim
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import lex
import tables
import strformat
import strutils
import sequtils
import sugar
type TypeExpressionType* = enum
Number,
String,
Function,
Void
type TypeExpression* = ref object
case typeExpressionType: TypeExpressionType
of Number: discard
of String: discard
of Function:
params: seq[TypeExpression]
returnType: TypeExpression
of Void: discard
let voidType = TypeExpression(typeExpressionType: TypeExpressionType.Void)
type AstType* = enum
Program,
Assignment,
Reassignment,
FunctionCall,
Id,
NumberLiteral,
StringLiteral,
Function,
AnonFunction,
Type,
Any,
Extern,
type Ast* = ref object
typeExpression*: TypeExpression
case astType*: AstType
of AstType.Program:
lines*: seq[Ast]
of AstType.Assignment:
lhs*: Ast
lhsType*: Ast
rhs*: Ast
of AstType.Reassignment:
reassignmentLhs*: Ast
reassignmentRhs*: Ast
of AstType.FunctionCall:
function*: Ast
params*: seq[Ast]
of AstType.Id:
name*: string
of AstType.NumberLiteral:
number*: int
of AstType.StringLiteral:
stringValue*: string
of AstType.Function:
functionName*: Ast
functionParams*: seq[tuple[name: Ast, `type`: Ast]]
returnType*: Ast
body*: seq[Ast]
of AstType.AnonFunction:
anonFunctionParams*: seq[Ast]
anonBody*: seq[Ast]
of AstType.Type:
typeType*: TypeExpression
of AstType.Any: discard
of AstType.Extern:
externCode*: Ast
externType*: Ast
let anyAst = Ast(astType: AstType.Any)
func `$`*(typeExpression: TypeExpression): string
func `$`*(ast: Ast): string
# structural type equivalence
func `~=`(te1: TypeExpression, te2: TypeExpression): bool =
case te1.typeExpressionType:
of TypeExpressionType.Number:
result = te2.typeExpressionType == TypeExpressionType.Number
of TypeExpressionType.String:
result = te2.typeExpressionType == TypeExpressionType.String
of TypeExpressionType.Void:
result = te2.typeExpressionType == TypeExpressionType.Void
of TypeExpressionType.Function:
result = te2.typeExpressionType == TypeExpressionType.Function and
te1.returnType ~= te2.returnType and
te1.params.len == te2.params.len
for (p1, p2) in zip(te1.params, te2.params):
result = result and p1 ~= p2
template `!~=`(te1: TypeExpression, te2: TypeExpression): bool =
not (te1 ~= te2)
proc parse*(tokens: seq[Token], source: string): Ast =
var index = 0
var typeTable = initTable[string, TypeExpression]()
proc currentToken(useIndex = index): Token
proc error(message: string, lineHint = true) =
var error: string
error &= fmt("\nerr: {message}\n")
if lineHint:
error &= getLineFromIndex(currentToken(index - 1).index, source)
error &= "\n\n"
error &= getStackTrace()
raise newException(OSError, error)
proc errorIfStreamEnd(useIndex = index) =
if useIndex >= tokens.len:
error("unexpected end of token stream. try a semicolon?", lineHint = false)
proc currentToken(useIndex = index): Token =
errorIfStreamEnd()
tokens[useIndex]
template next = index += 1
template moreTokens: bool = index < tokens.len
proc hasToken(token: Token, useIndex = index): bool =
errorIfStreamEnd()
currentToken(useIndex).tokenType == token.tokenType and
currentToken(useIndex).value == token.value
proc expectToken(token: Token) =
errorIfStreamEnd()
if not hasToken(token):
error(fmt"invalid token: {currentToken()} at {index}. expected {token}.")
proc consumeToken(token: Token) =
expectToken(token)
next()
template op(s: string): Token = Token(tokenType: TokenType.Operator, value: s)
template punc(s: string): Token = Token(tokenType: TokenType.Punctuation, value: s)
template alpha(s: string): Token = Token(tokenType: TokenType.Alpha, value: s)
proc parseJoined(parser: proc (): Ast, sepToken: Token, endToken: Token): seq[Ast] =
while not hasToken(endToken):
result.add(parser())
if not hasToken(endToken):
consumeToken(sepToken)
proc parseJoinedPairs(parser: proc (): tuple[name: Ast, `type`: Ast], sepToken: Token, endToken: Token): seq[tuple[name: Ast, `type`: Ast]] =
var first = true
while not hasToken(endToken):
if first:
first = false
else:
consumeToken(sepToken)
result.add(parser())
proc parseProgram(): Ast
proc parseLine(): Ast
proc parseAssignment(): Ast
proc parseVariableAssignment(lhs: Ast): Ast
proc parseFunctionAssignment(lhs: Ast): Ast
proc parseOperatorAssignment(): Ast
proc parseExpression(): Ast
proc parseExpressionOptAnonFunc(funcType: TypeExpression): Ast
proc parseExtern(): Ast
proc parseBoundId(): Ast
proc parseId(): Ast
proc parseIdTypePair(): tuple[name: Ast, `type`: Ast]
proc parseNum(): Ast
proc parseString(): Ast
proc parseType(): Ast
proc parseAnonFunction(funcType: TypeExpression): Ast
proc parseProgram(): Ast =
var lines: seq[Ast]
while moreTokens():
lines.add(parseLine())
result = Ast(
astType: AstType.Program,
lines: lines,
typeExpression: voidType,
)
proc parseLine(): Ast =
if hasToken(alpha"let"):
result = parseAssignment()
else:
result = parseExpression()
consumeToken(punc";")
proc parseAssignment(): Ast =
consumeToken(alpha"let")
if currentToken().tokenType == TokenType.Alpha:
let lhs = parseBoundId()
if typeTable.hasKey(lhs.name):
error(fmt"{lhs.name} is already defined.")
if hasToken(punc"("):
result = parseFunctionAssignment(lhs)
elif hasToken(op":") or hasToken(op"="):
result = parseVariableAssignment(lhs)
elif hasToken(punc"("):
result = parseOperatorAssignment()
else:
error("invalid assignment.")
proc parseVariableAssignment(lhs: Ast): Ast =
var lhsType: Ast
if hasToken(op":"):
next()
lhsType = parseType()
var rhs: Ast
if hasToken(op"="):
next()
rhs = parseExpression()
if not lhsType.isNil and lhsType.typeType !~= rhs.typeExpression:
error(fmt"a {rhs.typeExpression} is being assigned to {lhs.name}: {lhsType}")
consumeToken(punc";")
elif hasToken(punc";"):
next()
rhs = anyAst
else:
error("invalid assignment.")
typeTable[lhs.name] = lhsType.typeType
result = Ast(
astType: AstType.Assignment,
lhs: lhs,
lhsType: if lhsType.isNil: Ast(astType: AstType.Type, typeType: rhs.typeExpression) else: lhsType,
rhs: rhs,
typeExpression: voidType,
)
proc parseFunctionAssignment(lhs: Ast): Ast =
if typeTable.hasKey(lhs.name):
error(fmt"{lhs.name} is already defined")
next()
let params = parseJoinedPairs(parseIdTypePair, punc",", punc")")
next()
consumeToken(op":")
let returnType = parseType()
for (name, `type`) in params:
if typeTable.hasKey(name.name):
error(fmt"{name} is already defined.")
else:
typeTable[name.name] = `type`.typeType
typeTable["result"] = returnType.typeType
var paramTypes: seq[TypeExpression]
for (_, `type`) in params:
paramTypes.add(`type`.typeType)
typeTable[lhs.name] = TypeExpression(
typeExpressionType: TypeExpressionType.Function,
params: paramTypes,
returnType: returnType.typeType
)
var body: seq[Ast]
if hasToken(op"="):
next()
consumeToken(punc"{")
while not hasToken(punc"}"):
body.add(parseLine())
next()
consumeToken(punc";")
elif hasToken(punc";"):
body = @[anyAst]
next()
else:
error("invalid assignment.")
for (name, `type`) in params:
typeTable.del(name.name)
typeTable.del("result")
result = Ast(
astType: AstType.Function,
functionName: lhs,
functionParams: params,
returnType: returnType,
body: body,
typeExpression: voidType,
)
proc parseOperatorAssignment(): Ast =
consumeToken(punc"(")
let first = parseIdTypePair()
let (firstId, firstType) = first
consumeToken(punc")")
let operator = currentToken()
if operator.tokenType != TokenType.Operator:
error("that should be an operator.")
if typeTable.hasKey(operator.value):
error(fmt"{operator.value} is already defined")
next()
consumeToken(punc"(")
let second = parseIdTypePair()
let (secondId, secondType) = second
consumeToken(punc")")
if typeTable.hasKey(firstId.name):
error(fmt"{firstId.name} is already defined")
if typeTable.hasKey(secondId.name):
error(fmt"{secondId.name} is already defined")
typeTable[firstId.name] = firstType.typeType
typeTable[secondId.name] = secondType.typeType
consumeToken(op":")
let returnType = parseType()
typeTable["result"] = returnType.typeType
typeTable[operator.value] = TypeExpression(
typeExpressionType: TypeExpressionType.Function,
params: @[firstType.typeType, secondType.typeType],
returnType: returnType.typeType
)
var body: seq[Ast]
if hasToken(op"="):
next()
consumeToken(punc"{")
while not hasToken(punc"}"):
body.add(parseLine())
next()
consumeToken(punc";")
elif hasToken(punc";"):
body = @[anyAst]
next()
else:
error("invalid assignment.")
typeTable.del(firstId.name)
typeTable.del(secondId.name)
typeTable.del("result")
result = Ast(
astType: AstType.Function,
functionName: Ast(astType: AstType.Id, name: operator.value),
functionParams: @[first, second],
returnType: returnType,
body: body,
typeExpression: voidType,
)
proc functionApplicationType(f: TypeExpression, params: seq[TypeExpression]): TypeExpression =
if f.typeExpressionType != TypeExpressionType.Function:
error("not a function.")
if params.len != f.params.len:
error(fmt"{params} should be {f.params}")
for index, param in pairs(params):
if param !~= f.params[index]:
error(fmt"{param} should be a {f.params[index]}")
f.returnType
# todo: for repeated calls
# e.g., f(10)("a")(6) doesn't work right now
# proc parseCalls(function: Ast): Ast
proc parseExpression(): Ast =
var firstExpression: Ast
if hasToken(punc"("):
next()
firstExpression = parseExpression()
consumeToken(punc")")
elif currentToken().tokenType == TokenType.Number:
firstExpression = parseNum()
elif currentToken().tokenType == TokenType.String:
firstExpression = parseString()
elif currentToken().tokenType == TokenType.Alpha:
firstExpression = parseId()
elif hasToken(op"@"):
firstExpression = parseExtern()
elif hasToken(punc"{"):
error("anonymous functions are not allowed here.")
else:
error(fmt"invalid expression at {index}");
if hasToken(op"="):
next()
let secondExpression = parseExpression()
if firstExpression.typeExpression !~= secondExpression.typeExpression:
error(fmt"{firstExpression.typeExpression} != {secondExpression.typeExpression}")
result = Ast(
astType: AstType.Reassignment,
reassignmentLhs: firstExpression,
reassignmentRhs: secondExpression,
typeExpression: voidType,
)
elif currentToken().tokenType == TokenType.Operator:
let operator = currentToken()
if not typeTable.hasKey(operator.value):
error(fmt"{operator.value} is not defined.")
next()
let secondExpression = parseExpression()
let operatorAst = Ast(
astType: Id,
name: operator.value,
typeExpression: typeTable[operator.value],
)
let params = @[firstExpression, secondExpression]
let appType = functionApplicationType(operatorAst.typeExpression, params.map(param => param.typeExpression))
result = Ast(
astType: FunctionCall,
function: operatorAst,
params: params,
typeExpression: appType,
)
elif hasToken(punc"("):
next()
var params: seq[Ast]
var firstExpressionParamIndex = 0
while not hasToken(punc")"):
params.add(
parseExpressionOptAnonFunc(firstExpression.typeExpression.params[firstExpressionParamIndex])
)
inc firstExpressionParamIndex
if not hasToken(punc")"):
consumeToken(punc",")
consumeToken(punc")")
let appType = functionApplicationType(firstExpression.typeExpression, params.map(param => param.typeExpression))
result = Ast(
astType: FunctionCall,
function: firstExpression,
params: params,
typeExpression: appType,
)
else:
result = firstExpression
proc parseExpressionOptAnonFunc(funcType: TypeExpression): Ast =
if hasToken(punc"{"):
parseAnonFunction(funcType)
else:
parseExpression()
proc parseExtern(): Ast =
next()
consumeToken(punc"{")
let externCode = parseString()
consumeToken(op":")
let externType = parseType()
consumeToken(punc"}")
result = Ast(
astType: AstType.Extern,
externCode: externCode,
externType: externType,
typeExpression: externType.typeType,
)
proc parseBoundId(): Ast =
let idToken = currentToken()
assert idToken.tokenType == TokenType.Alpha
result = Ast(
astType: Id,
name: idToken.value,
)
next()
proc parseId(): Ast =
var id = parseBoundId()
if not typeTable.hasKey(id.name):
error(fmt"undefined variable ""{id}""")
id.typeExpression = typeTable[id.name]
result = id
proc parseIdTypePair(): tuple[name: Ast, `type`: Ast] =
(parseBoundId(), (consumeToken(op":"); parseType()))
proc parseNum(): Ast =
let numToken = currentToken()
let number: int = numToken.value.parseInt()
result = Ast(
astType: NumberLiteral,
number: number,
typeExpression: TypeExpression(
typeExpressionType: TypeExpressionType.Number,
),
)
next()
proc parseString(): Ast =
let stringToken = currentToken()
result = Ast(
astType: StringLiteral,
stringValue: stringToken.value,
typeExpression: TypeExpression(
typeExpressionType: TypeExpressionType.String,
),
)
next()
proc parseType(): Ast =
if hasToken(alpha"number"):
next()
result = Ast(astType: AstType.Type, typeType: TypeExpression(typeExpressionType: TypeExpressionType.Number))
elif hasToken(alpha"string"):
next()
result = Ast(astType: AstType.Type, typeType: TypeExpression(typeExpressionType: TypeExpressionType.String))
elif hasToken(alpha"void"):
next()
result = Ast(astType: AstType.Type, typeType: TypeExpression(typeExpressionType: TypeExpressionType.Void))
elif hasToken(punc"("):
next()
result = parseType()
if hasToken(punc","):
next()
let params = parseJoined(parseType, punc",", punc")").map(ast => ast.typeType)
next()
consumeToken(op"->")
let returnType = parseType()
result = Ast(
astType: AstType.Type,
typeType: TypeExpression(
typeExpressionType: TypeExpressionType.Function,
params: @[result.typeType] & params,
returnType: returnType.typeType))
else:
consumeToken(punc")")
else:
error(fmt"invalid type: {currentToken()}")
nil
if hasToken(op"->"):
next()
let returnType = parseType()
result = Ast(
astType: AstType.Type,
typeType: TypeExpression(
typeExpressionType: TypeExpressionType.Function,
params: @[result.typeType],
returnType: returnType.typeType))
# proc anonFunctionHasParams(): bool =
# var tempIndex = index
# result = true
# while result and not hasToken(op":"):
# if not (
# currentToken(tempIndex).tokenType == TokenType.Alpha and (hasToken(punc",", tempIndex + 1) or hasToken(op"=>", tempIndex + 1))
# ): result = false
# inc tempIndex, 2
proc parseAnonFunction(funcType: TypeExpression): Ast =
consumeToken(punc"{")
let funcParams = funcType.params
var params: seq[Ast]
while not hasToken(op"=>"):
params.add(parseBoundId())
if not hasToken(op"=>"):
consumeToken(punc",")
next()
if params.len != funcParams.len:
error("anonymous function type mismatch.")
for i, param in pairs(params):
typeTable[param.name] = funcParams[i]
typeTable["result"] = funcType.returnType
var lines: seq[Ast]
while not hasToken(punc"}"):
lines.add(parseLine())
next()
for param in params:
typeTable.del(param.name)
typeTable.del("result")
result = Ast(
astType: AstType.AnonFunction,
anonFunctionParams: params,
anonBody: lines,
typeExpression: funcType,
)
result = parseProgram()
func `$`*(typeExpression: TypeExpression): string =
case typeExpression.typeExpressionType:
of TypeExpressionType.Number: "number"
of TypeExpressionType.String: "string"
of TypeExpressionType.Function: fmt"""({typeExpression.params.join(", ")}) -> ({typeExpression.returnType})""" # TODO
of TypeExpressionType.Void: "void"
proc encodeChar(c: char): string =
let numString = ord(c).intToStr
for c in numString:
result &= cast[char]((cast[byte](c) - cast[byte]('0')) + cast[byte]('a'))
func `$`*(ast: Ast): string =
case ast.astType:
of AstType.Program:
for line in ast.lines:
result &= $line
result &= "\n"
of AstType.Assignment:
result = fmt"let {ast.lhs} = {ast.rhs};"
of AstType.Reassignment:
result = fmt"{ast.reassignmentLhs} = {ast.reassignmentRhs}"
of AstType.FunctionCall:
result = fmt"""{ast.function}({ast.params.join(", ")})"""
of AstType.AnonFunction:
var paramStrings: seq[string]
for name in ast.anonFunctionParams:
paramStrings.add(fmt"{name}")
result = fmt"""function ({paramStrings.map(param => param[0]).join(", ")})"""
result &= "{"
result &= "let result;"
for line in ast.anonBody:
result &= $line & ";"
result &= "return result;"
result &= "}"
of AstType.Id:
for c in ast.name:
if not (('a' <= c and c <= 'z') or ('A' <= c and c <= 'Z')):
result &= "__op__" & encodeChar(c)
else:
result &= c
of AstType.NumberLiteral:
result = $ast.number
of AstType.StringLiteral:
result = "\"" & ast.stringValue & "\""
of AstType.Function:
var paramStrings: seq[string]
for (name, _) in ast.functionParams:
paramStrings.add(fmt"{name}")
result = fmt"""function {ast.functionName}({paramStrings.join(", ")})"""
result &= "{"
result &= "let result;"
for line in ast.body:
result &= $line & ";"
result &= "return result;"
result &= "}"
of AstType.Type:
result = $ast.typeType;
of AstType.Any:
result = ""
of AstType.Extern:
result = ast.externCode.stringValue