-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.nim
850 lines (816 loc) · 32.8 KB
/
parser.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
from token import nil
from errors import nil
from position import nil
from nodes import nil
import strutils
import parseResult
import sequtils
# Vars
var tokens: seq[token.Token]
var tok_idx: int
var next_tok_idx: int
var current_tok: token.Token
var next_tok : token.Token
proc update_current_tok() =
if tok_idx >= 0 and tok_idx < len(tokens):
current_tok = tokens[tok_idx]
if next_tok_idx >= 0 and next_tok_idx < len(tokens):
next_tok = tokens[next_tok_idx]
proc advance() =
tok_idx += 1
next_tok_idx += 1
update_current_tok()
proc reverse(amount : int = 1) : token.Token =
tok_idx -= amount
update_current_tok()
return current_tok
echo tokens.map(proc (x: token.Token) : string = token.toString(x)).join(", ")
# Procedure declarations
proc parse(): ParseResult
proc statements(): ParseResult
proc statement() : ParseResult
proc expression(): ParseResult
proc comp_expression(): ParseResult
proc arith_expression(): ParseResult
proc term(): ParseResult
proc factor(): ParseResult
proc power(): ParseResult
proc call(): ParseResult
proc atom(): ParseResult
proc if_expr_b_or_c(): ParseResult
proc if_expr_b(): ParseResult
proc if_expr_cases(case_keyword : string): ParseResult
proc if_expr() : ParseResult
proc while_expr() : ParseResult
proc for_expr() : ParseResult
proc list_expr() : ParseResult
proc try_expr() : ParseResult
proc func_def() : ParseResult
proc lhs() : ParseResult
proc identifier() : ParseResult
proc accessor(): ParseResult
proc class_expr(): ParseResult
proc bin_op(func_a: proc(): ParseResult, ops: seq[string], func_b: proc(): ParseResult, keyword_ops: seq[(string, string)]) : ParseResult
# implementation
proc parse() : ParseResult =
let res = statements()
if ((res.error.name == "NoError") and (current_tok.tokType != token.TT_EOF)):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"expected '+', '-', '*', or '/'"
))
return res
proc bin_op(func_a: proc(): ParseResult, ops: seq[string], func_b: proc(): ParseResult, keyword_ops: seq[(string, string)]) : ParseResult =
let res = newParseResult()
var left = res.register(func_a())
if res.error.name != "NoError": return res
let pos_start = position.copy(current_tok.pos_start)
while (ops.contains(current_tok.tokType)) or (keyword_ops.contains((current_tok.tokType, current_tok.value))):
let op_tok = current_tok
res.register_advancement()
advance()
let right = res.register(func_b())
if res.error.name != "NoError": return res
left = nodes.BinOpNode(left: left, op: op_tok, right: right, pos_start: pos_start, pos_end: current_tok.pos_end)
return res.success(left)
proc statements() : ParseResult =
let res = newParseResult()
var statements : seq[nodes.Node]
statements = @[]
let pos_start = position.copy(current_tok.pos_start)
var more_statements = true
while (current_tok.tokType == token.TT_NEWLINE):
res.register_advancement()
advance()
var statement : nodes.Node = res.register(statement())
if res.error.name != "NoError": return res
statements.add(statement)
while true:
var newline_count = 0
while current_tok.tokType == token.TT_NEWLINE:
res.register_advancement()
advance()
newline_count += 1
if newline_count == 0:
more_statements = false
if not more_statements:
break
statement = res.try_register(statement())
if statement of nodes.EmptyNode:
discard reverse(res.to_reverse_count)
more_statements = false
continue
statements.add(statement)
let endRes = nodes.ProgramNode(statements: statements, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end))
return res.success(endRes)
proc statement() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
if token.matches(current_tok, token.TT_KEYWORD, "SHAMIL"):
res.register_advancement()
advance()
if current_tok.tokType != token.TT_STRING:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect ki thi string"
))
let str = current_tok
res.register_advancement()
advance()
return res.success(nodes.IncludeNode(includes: str.value, pos_start: pos_start, pos_end: position.copy(str.pos_end)))
if token.matches(current_tok, token.TT_KEYWORD, "WAPIS"):
res.register_advancement()
advance()
let expression = res.try_register(expression())
if expression == nodes.emptyNode:
discard reverse(res.to_reverse_count)
return res.success(nodes.ReturnNode(returnValue: expression, pos_start: pos_start, pos_end: position.copy(current_tok.pos_start)))
if token.matches(current_tok, token.TT_KEYWORD, "SHURU"):
res.register_advancement()
advance()
return res.success(nodes.ContinueNode(pos_start: pos_start, pos_end: position.copy(current_tok.pos_start)))
if token.matches(current_tok, token.TT_KEYWORD, "TODHO"):
res.register_advancement()
advance()
return res.success(nodes.BreakNode(pos_start: pos_start, pos_end: position.copy(current_tok.pos_start)))
if token.matches(current_tok, token.TT_KEYWORD, "KAHO"):
res.register_advancement()
advance()
let assertion = res.register(expression())
if res.error.name != "NoError": return res
return res.success(nodes.AssertNode(assertion: assertion, pos_start: pos_start, pos_end: position.copy(current_tok.pos_start)))
let expression = res.register(expression())
if res.error.name != "NoError":
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expected 'RAKHO', 'AGAR', 'FOR', 'JABKE', 'KAM', Number, naam, '+', '-', '(', '[', 'TODHO', 'SHURU', 'WAPIS', 'KAHO' or 'NAHI'"
))
return res.success(expression)
proc identifier() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
if current_tok.tokType != token.TT_IDENTIFIER:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha NAAM"
))
let tok = current_tok
res.register_advancement()
advance()
return res.success(nodes.VarAccessNode(identifier: tok, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)))
proc lhs() : ParseResult =
return bin_op(identifier, @[token.TT_DIV, token.TT_DOT], atom, @[("", ""), ("", "")])
proc expression() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_end)
var node : nodes.Node
if (token.matches(current_tok, token.TT_KEYWORD, "RAKHO")) or (token.matches(current_tok, token.TT_KEYWORD, "ABSE")):
let pos_start = position.copy(current_tok.pos_start)
let assign_type = current_tok
res.register_advancement()
advance()
let identifier = res.register(lhs())
if current_tok.tokType != token.TT_EQUALS:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha '='"
))
res.register_advancement()
advance()
let value = res.register(expression())
if res.error.name != "NoError": return res
node = nodes.VarAssignNode(identifier: identifier, assign_type: assign_type, value: value, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end))
return res.success(node)
node = res.register(bin_op(comp_expression, @[], comp_expression, @[(token.TT_KEYWORD, "OR"), (token.TT_KEYWORD, "YA")]))
if res.error.name != "NoError": return res
if current_tok.tokType == token.TT_RPAREN:
res.register_advancement()
advance()
var arg_nodes : seq[nodes.Node] = @[]
if current_tok.tokType == token.TT_LPAREN:
res.register_advancement()
advance()
else:
arg_nodes.add(res.register(expression()))
if res.error.name != "NoError":
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha ')', 'RAKHO', 'AGAR', 'FOR', 'JABKE', 'KAM', int, float, naam, '+', '-', '(', '[' ya 'NAHI'"
))
while current_tok.tokType == token.TT_COMMA:
res.register_advancement()
advance()
arg_nodes.add(res.register(expression()))
if res.error.name != "NoError": return res
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha ',' ya ')'"
))
res.register_advancement()
advance()
return res.success(nodes.CallNode(callee: node, args: arg_nodes, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)))
return res.success(node)
proc comp_expression() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
if token.matches(current_tok, token.TT_KEYWORD, "NAHI"):
let unary_op = current_tok
res.register_advancement()
advance()
let expression = res.register(comp_expression())
if res.error.name != "NoError": return res
return res.success(nodes.UnaryOpNode(unary_op: unary_op, factor: expression, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)))
return bin_op(arith_expression, @[token.TT_NE, token.TT_EE, token.TT_GTE, token.TT_GT, token.TT_LTE, token.TT_LT], arith_expression, @[("", ""), ("", "")])
proc arith_expression() : ParseResult =
return bin_op(term, @[token.TT_MINUS, token.TT_PLUS], term, @[("", ""), ("", "")])
proc term() : ParseResult =
return bin_op(factor, @[token.TT_MUL, token.TT_DIV], factor, @[("", ""), ("", "")])
proc factor(): ParseResult =
let res = newParseResult()
if current_tok.tokType == token.TT_MINUS:
let pos_start = position.copy(current_tok.pos_start)
let unary_op = current_tok
res.register_advancement()
advance()
let factor = res.register(factor())
if res.error.name != "NoError": return res
return res.success(nodes.UnaryOpNode(unary_op: unary_op, factor: factor, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)))
return power()
proc power(): ParseResult =
return bin_op(call, @[token.TT_POW, token.TT_MOD], factor, @[("", ""), ("", "")])
proc call(): ParseResult =
let res = newParseResult()
var accessor = res.register(accessor())
let pos_start = position.copy(current_tok.pos_start)
if res.error.name != "NoError": return res
if current_tok.tokType == token.TT_RPAREN:
res.register_advancement()
advance()
var arg_nodes : seq[nodes.Node] = @[]
if current_tok.tokType == token.TT_LPAREN:
res.register_advancement()
advance()
else:
arg_nodes.add(res.register(expression()))
if res.error.name != "NoError":
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha ')', 'RAKHO', 'AGAR', 'FOR', 'JABKE', 'KAM', int, float, naam, '+', '-', '(', '[' ya 'NAHI'"
))
while current_tok.tokType == token.TT_COMMA:
res.register_advancement()
advance()
arg_nodes.add(res.register(expression()))
if res.error.name != "NoError": return res
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha ',' ya ')'"
))
res.register_advancement()
advance()
return res.success(nodes.CallNode(callee: accessor, args: arg_nodes, pos_start: pos_start, pos_end: position.copy(current_tok.pos_start)))
return res.success(accessor)
proc accessor(): ParseResult =
return bin_op(atom, @[token.TT_DOT], atom, @[("", ""), ("", "")])
proc atom(): ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
if current_tok.tokType == token.TT_NUMBER:
let num = current_tok
res.register_advancement()
advance()
return res.success(nodes.NumberNode(value: num, pos_start: pos_start, pos_end: position.copy(num.pos_end)))
elif current_tok.tokType == token.TT_STRING:
let str = current_tok
res.register_advancement()
advance()
return res.success(nodes.StringNode(value: str.value, pos_start: pos_start, pos_end: position.copy(str.pos_end)))
elif current_tok.tokType == token.TT_RSQUARE:
let list_expression = res.register(list_expr())
if res.error.name != "NoError": return res
return res.success(list_expression)
elif token.matches(current_tok, token.TT_KEYWORD, "AGAR"):
let if_expression = res.register(if_expr())
if res.error.name != "NoError": return res
return res.success(if_expression)
elif token.matches(current_tok, token.TT_KEYWORD, "JABKE"):
let while_expression = res.register(while_expr())
if res.error.name != "NoError": return res
return res.success(while_expression)
elif token.matches(current_tok, token.TT_KEYWORD, "FOR"):
let while_expression = res.register(for_expr())
if res.error.name != "NoError": return res
return res.success(while_expression)
elif token.matches(current_tok, token.TT_KEYWORD, "BANAO"):
let class_expression = res.register(class_expr())
if res.error.name != "NoError": return res
return res.success(class_expression)
elif (current_tok.tokType == token.TT_IDENTIFIER and next_tok.tokType == token.TT_RCURLY) or (current_tok.tokType == token.TT_RCURLY):
let pos_start = position.copy(current_tok.pos_start)
var name: string
if current_tok.tokType == token.TT_RCURLY:
name = ""
else:
name = current_tok.value
res.register_advancement()
advance()
res.register_advancement()
advance()
if current_tok.tokType == token.TT_LCURLY:
res.register_advancement()
advance()
return res.success(nodes.ObjectNode(decls: nodes.ListNode(elements: @[], pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)),
objName: name, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)
)
)
let decls = res.register(statements())
if res.error.name != "NoError": return res
if current_tok.tokType != token.TT_LCURLY:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha '}'"
))
res.register_advancement()
advance()
return res.success(nodes.ObjectNode(decls: decls, objName: name, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)))
elif current_tok.tokType == token.TT_IDENTIFIER:
let id = current_tok
res.register_advancement()
advance()
return res.success(nodes.VarAccessNode(identifier: id, pos_start: pos_start, pos_end: position.copy(id.pos_end)))
elif token.matches(current_tok, token.TT_KEYWORD, "KOSHISH"):
let try_expression = res.register(try_expr())
if res.error.name != "NoError": return res
return res.success(try_expression)
elif token.matches(current_tok, token.TT_KEYWORD, "KAM"):
let function_expression = res.register(func_def())
if res.error.name != "NoError": return res
return res.success(function_expression)
elif current_tok.tokType == token.TT_RPAREN:
res.register_advancement()
advance()
let exp = res.register(expression())
if res.error.name != "NoError": return res
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
pos_start, position.copy(current_tok.pos_end),
"Expected ')'"
))
res.register_advancement()
advance()
return res.success(exp)
return res.failure(errors.InvalidSyntaxError(
pos_start, position.copy(current_tok.pos_end),
"Expected NUMBER"
))
proc while_expr() : ParseResult =
let res = newParseResult()
var body: nodes.Node
if not token.matches(current_tok, token.TT_KEYWORD, "JABKE"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'JABKE'"
))
res.register_advancement()
advance()
let condition = res.register(expression())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "PHIR"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'PHIR'"
))
res.register_advancement()
advance()
if current_tok.tokType == token.TT_NEWLINE:
res.register_advancement()
advance()
body = res.register(statements())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'KHATAM'"
))
res.register_advancement()
advance()
return res.success(nodes.WhileNode(condition: condition, body: body, should_return_null: true))
body = res.register(expression())
if res.error.name != "NoError": return res
return res.success(nodes.WhileNode(condition: condition, body: body, should_return_null: false))
proc class_expr(): ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
if not token.matches(current_tok, token.TT_KEYWORD, "BANAO"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'BANAO'"
))
res.register_advancement()
advance()
var var_name : nodes.VarAccessNode
if current_tok.tokType == token.TT_IDENTIFIER:
var_name = cast[nodes.VarAccessNode](res.register(identifier()))
if res.error.name != "NoError": return res
else:
var_name = nil
var inher_name : nodes.Node
if current_tok.tokType != token.TT_RPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam ya '('"
))
res.register_advancement()
advance()
var arg_name_toks : seq[token.Token] = @[]
if current_tok.tokType == token.TT_IDENTIFIER:
arg_name_toks.add(current_tok)
res.register_advancement()
advance()
while current_tok.tokType == token.TT_COMMA:
res.register_advancement()
advance()
if current_tok.tokType != token.TT_IDENTIFIER:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam"
))
arg_name_toks.add(current_tok)
res.register_advancement()
advance()
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam ya ',' ya ')'"
))
else:
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam ya ',' ya ')'"
))
res.register_advancement()
advance()
if current_tok.tokType == token.TT_COLON:
res.register_advancement()
advance()
inher_name = res.register(expression())
if res.error.name != "NoError": return res
else:
inher_name = nil
var body = cast[nodes.ListNode](res.register(statements()))
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'KHATAM'"
))
res.register_advancement()
advance()
let accessNode = nodes.Node(nodes.VarAccessNode(identifier: token.Token(tokType: token.TT_IDENTIFIER, value: "$", pos_start: body.pos_start, pos_end: body.pos_start)))
let var_type = token.Token(tokType: token.TT_KEYWORD, value: "ABSE", pos_start: body.pos_start, pos_end: body.pos_start)
let obj = nodes.ObjectNode(decls: nodes.ListNode(elements: @[], pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)),
objName: var_name.identifier.value, pos_start: body.pos_start, pos_end: body.pos_start
)
let ret_node = nodes.ReturnNode(returnValue: accessNode)
let assignment = if inher_name == nil:
@[cast[nodes.Node](nodes.VarAssignNode(identifier: accessNode, assign_type: var_type, value: obj))]
else:
@[cast[nodes.Node](nodes.VarAssignNode(identifier: accessNode, assign_type: var_type, value: inher_name))]
body.elements.insert(assignment, 0)
body.elements.add(ret_node)
return res.success(nodes.FuncDefNode(var_name: var_name, arg_names: arg_name_toks, body: body,
should_auto_return: false, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end))
)
proc func_def() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
var var_name : nodes.Node
if not token.matches(current_tok, token.TT_KEYWORD, "KAM"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'KAM'"
))
res.register_advancement()
advance()
if current_tok.tokType == token.TT_IDENTIFIER:
var_name = res.register(lhs())
if res.error.name != "NoError": return res
else:
var_name = nil
if current_tok.tokType != token.TT_RPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam ya '('"
))
res.register_advancement()
advance()
var arg_name_toks : seq[token.Token] = @[]
if current_tok.tokType == token.TT_IDENTIFIER:
arg_name_toks.add(current_tok)
res.register_advancement()
advance()
while current_tok.tokType == token.TT_COMMA:
res.register_advancement()
advance()
if current_tok.tokType != token.TT_IDENTIFIER:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam"
))
arg_name_toks.add(current_tok)
res.register_advancement()
advance()
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam ya ',' ya ')'"
))
else:
if current_tok.tokType != token.TT_LPAREN:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam ya ',' ya ')'"
))
res.register_advancement()
advance()
var body : nodes.Node
if current_tok.tokType == token.TT_ARROW:
res.register_advancement()
advance()
body = res.register(expression())
if res.error.name != "NoError": return res
return res.success(nodes.FuncDefNode(var_name: var_name, arg_names: arg_name_toks, body: body,
should_auto_return: true, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end))
)
if current_tok.tokType != token.TT_NEWLINE:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha '->' YA Nai line"
))
res.register_advancement()
advance()
body = res.register(statements())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'KHATAM'"
))
res.register_advancement()
advance()
return res.success(nodes.FuncDefNode(var_name: var_name, arg_names: arg_name_toks, body: body,
should_auto_return: false, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end))
)
proc for_expr() : ParseResult =
let res = newParseResult()
if not token.matches(current_tok, token.TT_KEYWORD, "FOR"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'FOR'"
))
res.register_advancement()
advance()
if current_tok.tokType != token.TT_IDENTIFIER:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha naam"
))
let var_name = current_tok
res.register_advancement()
advance()
if current_tok.tokType != token.TT_EQUALS:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha '='"
))
res.register_advancement()
advance()
let start_value = res.register(expression())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "SE"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'SE'"
))
res.register_advancement()
advance()
let end_value = res.register(expression())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "TAK"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'TAK'"
))
res.register_advancement()
advance()
var step_value : nodes.Node = nodes.emptyNode
if token.matches(current_tok, token.TT_KEYWORD, "BADHAO"):
res.register_advancement()
advance()
step_value = res.register(expression())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "PHIR"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'PHIR'"
))
res.register_advancement()
advance()
var body : nodes.Node = nodes.emptyNode
if current_tok.tokType == token.TT_NEWLINE:
res.register_advancement()
advance()
body = res.register(statements())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'KHATAM'"
))
res.register_advancement()
advance()
return res.success(nodes.ForNode(var_name: var_name, start_value: start_value, end_value: end_value, step_value: step_value, body: body, should_return_null: true))
body = res.register(expression())
if res.error.name != "NoError": return res
return res.success(nodes.ForNode(var_name: var_name, start_value: start_value, end_value: end_value, step_value: step_value, body: body, should_return_null: false))
proc if_expr_cases(case_keyword : string) : ParseResult =
let res = newParseResult()
var cases : seq[(nodes.Node, nodes.Node, bool)] = @[]
var else_case : (nodes.Node, bool) = (nodes.emptyNode, false)
if not token.matches(current_tok, token.TT_KEYWORD, case_keyword):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha '" & case_keyword & "'"
))
res.register_advancement()
advance()
let condition = res.register(expression())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "PHIR"):
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'PHIR'"
))
res.register_advancement()
advance()
if current_tok.tokType == token.TT_NEWLINE:
res.register_advancement()
advance()
let statements = res.register(statements())
if res.error.name != "NoError": return res
cases.add((condition, statements, true))
if token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
res.register_advancement()
advance()
else:
let all_cases = res.register_if_cases(if_expr_b_or_c())
if res.error.name != "NoError": return res
let (new_cases, new_else_case) = all_cases
cases = cases.concat(new_cases)
else_case = new_else_case
else:
let expression = res.register(statement())
if res.error.name != "NoError": return res
cases.add((condition, expression, false))
let all_cases = res.register_if_cases(if_expr_b_or_c())
if res.error.name != "NoError": return res
let (new_cases, new_else_case) = all_cases
cases = cases.concat(new_cases)
else_case = new_else_case
return res.success_if_cases((cases, else_case))
proc if_expr_b() : ParseResult =
return if_expr_cases("WARNAAGAR")
proc if_expr_c() : ParseResult =
let res = newParseResult()
var else_case : (nodes.Node, bool) = (nodes.emptyNode, false)
if token.matches(current_tok, token.TT_KEYWORD, "WARNA"):
res.register_advancement()
advance()
if current_tok.tokType == token.TT_NEWLINE:
res.register_advancement()
advance()
let statements = res.register(statements())
if res.error.name != "NoError": return res
else_case = (statements, true)
if token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
res.register_advancement()
advance()
else:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha 'KHATAM'"
))
else:
let expression = res.register(statement())
if res.error.name != "NoError": return res
else_case = (expression, false)
return res.success_else_case(else_case)
proc if_expr_b_or_c(): ParseResult =
let res = newParseResult()
var cases: seq[(nodes.Node, nodes.Node, bool)] = @[]
var else_case : (nodes.Node, bool) = (nodes.emptyNode, false)
if token.matches(current_tok, token.TT_KEYWORD, "WARNAAGAR"):
let all_cases = res.register_if_cases(if_expr_b())
if res.error.name != "NoError": return res
(cases, else_case) = all_cases
else:
else_case = res.register_else_case(if_expr_c())
if res.error.name != "NoError": return res
return res.success_if_cases((cases, else_case))
proc if_expr() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
let all_cases = res.register_if_cases(if_expr_cases("AGAR"))
if res.error.name != "NoError": return res
let (cases, else_case) = all_cases
return res.success(nodes.IfNode(cases: cases, else_case: else_case, pos_start: pos_start, pos_end: position.copy(current_tok.pos_end)))
proc list_expr() : ParseResult =
let res = newParseResult()
var element_nodes : seq[nodes.Node] = @[]
let pos_start = position.copy(current_tok.pos_start)
if current_tok.tokType != token.TT_RSQUARE:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha '['"
))
res.register_advancement()
advance()
if current_tok.tokType == token.TT_LSQUARE:
res.register_advancement()
advance()
else:
element_nodes.add(res.register(expression()))
if res.error.name != "NoError":
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha ']', 'RAKHO', 'AGAR', 'FOR', 'JABKE', 'KAM', int, float, naam, '+', '-', '(' ya 'NAHI'"
))
while current_tok.tokType == token.TT_COMMA:
res.register_advancement()
advance()
element_nodes.add(res.register(expression()))
if res.error.name != "NoError": return res
if current_tok.tokType != token.TT_LSQUARE:
return res.failure(errors.InvalidSyntaxError(
current_tok.pos_start, current_tok.pos_end,
"Expect kiya tha ',' ya ']'"
))
res.register_advancement()
advance()
return res.success(nodes.ListNode(
elements: element_nodes,
pos_start: pos_start,
pos_end: position.copy(current_tok.pos_end)
))
proc try_expr() : ParseResult =
let res = newParseResult()
let pos_start = position.copy(current_tok.pos_start)
if not token.matches(current_tok, token.TT_KEYWORD, "KOSHISH"):
return res.failure(errors.InvalidSyntaxError(
pos_start, current_tok.pos_end,
"Expected NUMBER, 'KOSHISH'"
))
res.register_advancement()
advance()
if current_tok.tokType == token.TT_NEWLINE:
let try_block = res.register(statements())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "MUSHKIL"):
return res.failure(errors.InvalidSyntaxError(
pos_start, current_tok.pos_end,
"Expected 'MUSHKIL'"
))
res.register_advancement()
advance()
let except_block = res.register(statements())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "KHATAM"):
return res.failure(errors.InvalidSyntaxError(
pos_start, current_tok.pos_end,
"Expected 'KHATAM'"
))
res.register_advancement()
advance()
return res.success(nodes.TryNode(try_block: try_block, except_block: except_block, may_return: false, pos_start: pos_start, pos_end: current_tok.pos_end))
let try_block = res.register(statement())
if res.error.name != "NoError": return res
if not token.matches(current_tok, token.TT_KEYWORD, "MUSHKIL"):
return res.failure(errors.InvalidSyntaxError(
pos_start, current_tok.pos_end,
"Expected 'MUSHKIL'"
))
res.register_advancement()
advance()
let except_block = res.register(statement())
if res.error.name != "NoError": return res
return res.success(nodes.TryNode(try_block: try_block, except_block: except_block, may_return: true, pos_start: pos_start, pos_end: current_tok.pos_end))
proc input*(toks: seq[token.Token]): (nodes.Node, errors.Error) =
tokens = toks
tok_idx = -1
next_tok_idx = 0
advance()
let parsed = parse()
return (parsed.node, parsed.error)