-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_generator.py
More file actions
2020 lines (1587 loc) · 64 KB
/
ast_generator.py
File metadata and controls
2020 lines (1587 loc) · 64 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
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Grammar:
Variable ::=
| [a-zA-z etc.]
Boolean ::=
| True
| False
Value ::=
| Boolean
| Int i
BOP ::=
| +
| -
| /
| *
| and
| or
UNOP ::=
| -
| not
Expression ::=
| Value
| Variable
| (e1)
| [e1] bop [e2]
| while [e1] do [e2] endwhile
| if [e1] then [e2] else [e3]
Sentence ::=
| Variable := e
# | Variable = fun [optional] [v1] [v2] ... -> [e] endfun e.g. fun -> return 4 endfun
"""
# ------- IMPORTS ------------
from copy import deepcopy
import lexer
# ------- CONSTANTS ------------
UNOP_PRECEDENCE = 4
START_PRECEDENCE = 1
N_PRECEDENCE_LEVELS = 4
PRECENDENCE_MAP = {
lexer.PLUS: 1,
lexer.MINUS: 1,
lexer.TIMES: 2,
lexer.DIV: 2,
lexer.EXP: 3,
}
# ------- EXTERNS ------------
PRINT = "print"
MEM = "mem"
GET = "get"
GET_STRUCT = "get_struct"
LEN = "len"
SET = "set"
SET_STRUCT = "set_struct"
ADD_STRUCT = "add_struct"
DEL_STRUCT = "del_struct"
EXTERNS_LIST = [PRINT, MEM, GET, LEN, SET,
GET_STRUCT, SET_STRUCT, ADD_STRUCT, DEL_STRUCT, ]
# ------- GRAMMAR PRODUCTION RULES ------------
# VALUE = 'value'
# INTEGER = 'integer'
# BOOLEAN = 'boolean'
# EXPRESSION = 'expression'
# # BOP = "bop"
# VALUE_RULES = [
# [INTEGER],
# [BOOLEAN],
# ]
# BOPS = [
# lexer.PLUS,
# lexer.MINUS,
# lexer.TIMES,
# lexer.DIV,
# ]
# EXPRESSION_RULES = [
# [VALUE],
# [EXPRESSION, BOP, EXPRESSION],
# []
# ]
# ------ EXCEPTIONS ---------
class MissingParens(Exception):
def __init__(self, str):
pass
class ParseError(Exception):
def __init__(self, str):
pass
class BopMissingArg(Exception):
def __init__(self, str):
pass
class UnopAdditionalArg(Exception):
def __init__(self, str):
pass
class UnmatchedParenError(Exception):
def __init__(self, str):
pass
class EndWithOperatorError(Exception):
def __init__(self, str):
pass
class AssignVariableException(Exception):
def __init__(self, str):
pass
# ------ AST CLASSES --------
class AST:
"""
AST is an abstract syntax tree
"""
def __init__(self):
self.sentences = []
def is_empty(self):
return self.sentences == []
class Expr(object):
"""
Expr respresents an expression
ABSTRACT CLASS for all instantiated expression classes
"""
def __init__(self):
pass
def __repr__(self):
return "This is a abstract expression"
class IntValue(Expr):
"""
IntValue represents an Int Value
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(IntValue: " + str(self.value) + ")"
class BoolValue(Expr):
"""
BoolValue represents an Bool Value
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(BoolValue: " + str(self.value) + ")"
class FloatValue(Expr):
"""
FloatValue represents an float Value
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(FloatValue: " + str(self.value) + ")"
class StrValue(Expr):
"""
StrValue represents an String
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(StrValue: " + str(self.value) + ")"
class VarValue(Expr):
"""
IntValue represents an Variable Value
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(VarValue: " + str(self.value) + ")"
class Tuple(Expr):
"""
Tuple represents an Tuple
"""
def __init__(self, exprs_list):
super().__init__()
self.exprs = exprs_list
self.length = len(exprs_list)
def get_exprs(self):
return self.exprs
def get_length(self):
return self.length
def __repr__(self):
return "(Tuple: (" + ", ".join(list(map(lambda e: str(e), self.exprs))) + "))"
class List(Expr):
"""
List represents an list
"""
def __init__(self, exprs_list):
super().__init__()
self.exprs = exprs_list
self.length = len(exprs_list)
def get_exprs(self):
return self.exprs
def get_length(self):
return self.length
def __repr__(self):
return "(List: [" + ", ".join(list(map(lambda e: str(e), self.exprs))) + "])"
class Dict(Expr):
"""
Dict represents an dictionary
"""
def __init__(self, keys_list, values_list):
super().__init__()
assert len(keys_list) == len(values_list)
self.keys = keys_list
self.values = values_list
self.length = len(values_list)
def get_keys(self):
return self.keys
def get_vals(self):
return self.values
def get_length(self):
return self.length
def __repr__(self):
return "(Dict: {" + ", ".join(list(map(lambda k, v: str(k) + " : " + str(v), self.keys, self.values))) + "})"
class Struct(Expr):
"""
Struct represents an struct
"""
def __init__(self, keys_list, values_list):
super().__init__()
assert len(keys_list) == len(values_list)
self.keys = keys_list
self.values = values_list
self.length = len(values_list)
def get_keys(self):
return self.keys
def get_vals(self):
return self.values
def get_length(self):
return self.length
def __repr__(self):
return "(Struct: {|" + ", ".join(list(map(lambda k, v: str(k) + " : " + str(v), self.keys, self.values))) + "|})"
class Bop(Expr):
"""
Bop represents e1 bop e2
"""
def __init__(self, bop, left=None, right=None):
super().__init__()
self.bop = bop
self.left = left
self.right = right
def set_left(self, left):
self.left = left
def set_right(self, right):
self.right = right
def get_bop(self):
return self.bop
def get_left(self):
return self.left
def get_right(self):
return self.right
def __repr__(self):
return "(BOP: " + str(self.left) + str(self.bop) + str(self.right) + ")"
class Unop(Expr):
"""
Unop represents unop e
"""
def __init__(self, unop, expr=None):
super().__init__()
self.unop = unop
self.expr = expr
def set_expr(self, expr):
self.expr = expr
def get_unop(self):
return self.unop
def get_expr(self):
return self.expr
def __repr__(self):
return "(UNOP: " + str(self.unop) + str(self.expr) + ")"
class Assign(Expr):
"""
assign represents var assign expre
"""
def __init__(self, var, expr=None):
super().__init__()
self.var = var
self.expr = expr
def set_expr(self, expr):
self.expr = expr
def set_var(self, var):
self.var = var
def get_expr(self):
return self.expr
def get_var(self):
return self.var
def __repr__(self):
return "(Assign: " + str(self.var) + " := " + str(self.expr) + ")"
class While(Expr):
"""
While represents
while guard_expr dowhile
phrases
endwhile
"""
def __init__(self, guard=None, body_list=None):
super().__init__()
self.guard = guard
self.body = body_list
def set_guard(self, guard):
self.guard = guard
def set_body(self, body_list):
self.body = body_list
def get_guard(self):
return self.guard
def get_body(self):
return self.body
def __repr__(self):
return "(while " + str(self.guard) + " dowhile\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), self.body))) + "\nendwhile)"
class For(Expr):
"""
For represents
For var from int to int by int dofor
phrases
endfor
"""
def __init__(self, index, from_int, end_int, by, body_list):
super().__init__()
self.index = index
self.from_int = from_int
self.end_int = end_int
self.by = by
self.body = body_list
def get_index(self):
return self.index
def get_from(self):
return self.from_int
def get_end(self):
return self.end_int
def get_by(self):
return self.by
def get_body(self):
return self.body
def __repr__(self):
return "(for " + str(self.index) + " from " + str(self.from_int) + " to " + str(self.end_int) + " by " + str(self.by) + " dofor\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), self.body))) + "\nendfor)"
class Function(Expr):
"""
Function represents
fun f a b c ->
body
endfun
"""
def __init__(self, name, args_list, body_list):
super().__init__()
self.name = name
self.args = args_list
self.body = body_list
def get_name(self):
return self.name
def get_args(self):
return self.args
def get_body(self):
return self.body
def __repr__(self):
return "(fun " + str(self.name) + " " + " ".join(list(map(lambda arg: str(arg), self.args))) + " ->\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), self.body))) + "\nendfun)"
class IfThenElse(Expr):
"""
IFThenElse represents an if then else erpression
"""
def __init__(self, if_guard, if_body, elif_guards=[], elif_bodies=[], else_body=None):
super().__init__()
self.if_pair = (if_guard, if_body)
self.elif_list = (elif_guards, elif_bodies)
self.else_body = else_body
def get_if_pair(self):
return self.if_pair
def get_elif_pair_list(self):
return self.elif_list
def get_else(self):
return self.else_body
def __repr__(self):
(if_guard, if_body) = self.if_pair
elif_guards, elif_bodies = self.elif_list
else_body = self.else_body
return ("(if " + str(if_guard) + " then\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), if_body))) + "\nendif\n"
+ ("" if elif_guards == [] else "\n".join(list(map(lambda g, b: "elif " + str(g) + " then\n\t" +
"\n\t".join(list(map(lambda phrase: str(phrase), b))) + "\nendelif\n", elif_guards, elif_bodies))))
+ ("" if else_body == None else "else\n\t" +
"\n\t".join(list(map(lambda phrase: str(phrase), else_body))) + "\nendelse\n")
+ ")"
)
class Extern(Expr):
"""
Extern represents fun extern (arg1 arg2...) with possibly no args as in
extern () , with only open and close brackets.
"""
def __init__(self, fun, args_list=[]):
super().__init__()
self.fun = fun
self.args_list = args_list
def set_args(self, args_list):
self.args_list = args_list
def get_fun(self):
return self.fun
def get_args(self):
return self.args_list
def __repr__(self):
return "(Extern: " + str(self.fun) + "(" + (" ".join(list(map(lambda a: str(a), self.args_list)))) + ")" + ")"
class Apply(Expr):
"""
Apply represents fun (arg1 arg2...) with possibly no args as in
fun () , with only open and close brackets.
"""
def __init__(self, fun, args_list=[]):
super().__init__()
self.fun = fun
self.args_list = args_list
def set_args(self, args_list):
self.args_list = args_list
def get_fun(self):
return self.fun
def get_args(self):
return self.args_list
def __repr__(self):
return "(Apply: " + str(self.fun) + "(" + (" ".join(list(map(lambda a: str(a), self.args_list)))) + ")" + ")"
class Return(Expr):
"""
return represents
return expr;
"""
def __init__(self, body):
super().__init__()
self.body = body
def get_body(self):
return self.body
def __repr__(self):
return "(Return: " + str(self.body) + ";)"
class Ignore(Expr):
"""
ignore represents
expr;
"""
def __init__(self, expr):
super().__init__()
self.expr = expr
def get_expr(self):
return self.expr
def __repr__(self):
return "(Ignore: " + str(self.expr) + ";)"
class Program(Expr):
"""
Program represents a syntacucally valid program
"""
def __init__(self, phrase_list=[]):
super().__init__()
self.phrases = phrase_list
def get_phrases(self):
return self.phrases
def __repr__(self):
return "(Program:\n" + "\n".join(list(map(lambda phrase: str(phrase), self.phrases))) + "\n)"
# ------ MATCH FUNCTIONS --------
# def match_integer(lexbuf, val):
# return match_expr(IntValue(val), lexbuf)
def get_between_brackets(lex_buff, idx):
# assume start after idx
stack = []
stack.append(lexer.LPAREN)
expr_terms = []
i = idx
while (i < len(lex_buff) and stack != []):
typ, val = lex_buff[i]
if val == lexer.LPAREN:
stack.append(val)
elif val == lexer.RPAREN:
if stack != [] and stack[-1] == lexer.LPAREN:
stack.pop()
expr_terms.append((typ, val))
i += 1
if i > len(lex_buff):
raise MissingParens("Missing or Misplaced Parentheses")
if stack != []:
raise MissingParens("Missing or Misplaced Parentheses")
l = len(expr_terms)
expr_terms.pop() # removd last parentheses
return (l, expr_terms)
def get_between_brackets_general(lex_buff, idx, start_sym, end_sym):
# assume start after idx
stack = []
stack.append(start_sym)
expr_terms = []
i = idx
while (i < len(lex_buff) and stack != []):
typ, val = lex_buff[i]
# if val == start_sym:
# stack.append(val)
# elif val == end_sym:
# if stack != [] and stack[-1] == start_sym:
# stack.pop()
if val == lexer.LPAREN:
stack.append(val)
elif val == lexer.RPAREN:
if stack != [] and stack[-1] == lexer.LPAREN:
stack.pop()
if val == lexer.OPEN_TUP:
stack.append(val)
elif val == lexer.CLOSE_TUP:
if stack != [] and stack[-1] == lexer.OPEN_TUP:
stack.pop()
if val == lexer.OPEN_BRACKET:
stack.append(val)
elif val == lexer.CLOSE_BRACKET:
if stack != [] and stack[-1] == lexer.OPEN_BRACKET:
stack.pop()
if val == lexer.OPEN_DICT:
stack.append(val)
elif val == lexer.CLOSE_DICT:
if stack != [] and stack[-1] == lexer.OPEN_DICT:
stack.pop()
if val == lexer.OPEN_STRUCT:
stack.append(val)
elif val == lexer.CLOSE_STRUCT:
if stack != [] and stack[-1] == lexer.OPEN_STRUCT:
stack.pop()
expr_terms.append((typ, val))
i += 1
if i > len(lex_buff):
raise MissingParens("Missing or Misplaced End Symbol")
if stack != []:
raise MissingParens("Missing or Misplaced End Symbol")
l = len(expr_terms)
expr_terms.pop() # removed last parentheses
return (l, expr_terms)
# def match_open_paren(ast, lex_buff):
# lex_typ, val = lex_buff[0]
# if val != lexer.LPAREN:
# return None
# middle_terms, length = get_between_brackets(lex_buff[1:], 0)
# new_lex_buff = lex_buff[1 + length:]
# new_ast = match_expr(None, middle_terms)
# if ast != None:
# ast.set_right(new_ast)
# else:
# ast = new_ast
# return match_expr(ast, new_lex_buff)
# def match_bop(ast, lexbuf, bop):
# bop_node = Bop(bop)
# bop_node.set_left(ast)
# # need to wrap in try.except if theis is undefined
# head = lexbuf[0]
# la_typ, la_val = head
# if (bop == lexer.TIMES or bop == lexer.DIV) and la_typ in lexer.INTEGER:
# right_node = match_expr(None, [head])
# bop_node.set_right(right_node)
# tail = lexbuf[1:]
# return match_expr(bop_node, tail)
# elif (bop == lexer.TIMES or bop == lexer.DIV) and la_val == lexer.LPAREN:
# right_node = match_open_paren(bop_node, lexbuf)
# return right_node
# else:
# right_node = match_expr(None, lexbuf)
# bop_node.set_right(right_node)
# return bop_node
# def match_unop(lexbuf, unop):
# unop_node = Unop(unop)
# head = lexbuf[0]
# tail = lexbuf[1:]
# la_typ, _ = head
# if la_typ in lexer.INTEGER:
# bottom_node = match_expr(None, [head])
# unop_node.set_expr(bottom_node)
# return match_expr(unop_node, tail)
# raise UnopAdditionalArg(
# "Additional arg to a unary operation %s" % (unop))
# def match_expr(ast, lexbuf):
# """
# match_expr(ast, lexbuf) creates an ast from lexbuf, otherwise raises
# appropriate execption
# """
# if lexbuf == []:
# return ast
# typ, val = lexbuf[0]
# tail = lexbuf[1:]
# if typ == lexer.INTEGER:
# return match_integer(tail, val)
# elif ast == None and val in lexer.UNOPS:
# return match_unop(tail, val)
# elif ast == None and val in lexer.BOPS:
# raise BopMissingArg("Missing arg %s" % (val))
# elif ast != None and val in lexer.BOPS:
# return match_bop(ast, tail, val)
# elif ast != None and val in lexer.UNOPS:
# raise UnopAdditionalArg(
# "Additional arg to a unary operation %s" % (val))
# elif val == lexer.LPAREN:
# return match_open_paren(ast, lexbuf)
# # return match_lparent(ast, tail, val)
# elif val == lexer.RPAREN:
# raise UnmatchedParenError("Unmatched right parenthesis %s" % (val))
# raise ParseError("Error in Parsing Tokens")
# def match(lexbuf):
# """
# match(lexbuf) creates an ast from from lexbuf, otherwises
# raises an appropriate exeception
# """
# ast = None
# return match_expr(ast, lexbuf)
# def parse(lex_buff):
# def parse_helper(lex_buff, ast, stack):
# if lex_buff == []:
# return
# return parse_helper(lex_buff, AST(), [])
def get_precedence(symbol, precendence_map=PRECENDENCE_MAP):
return precendence_map[symbol]
def reduce_stack(precedence, stack):
"""
reduce_stack(stack) reduces the stack from the top of the stack
to the end into a unified AST at precedence level precedence
REQUIRES: [precendence] cannot be 0
REQUIRES: STACK is NOT EMPTY
REQUIRES: STACK MUST BE ABLE TO TURNED INTO A VALID AST, E>G> A STACK WITH ONE ELEMENT
MUST BE A VALUE OR VARIABLE!
If the stack has one element, returns that element
"""
if stack == []:
return stack
l = len(stack)
if l == 1:
return stack[0]
if precedence <= 3:
end_stack = stack[:3]
bop = end_stack[1][1]
start = end_stack[0]
end = end_stack[2]
new_stack = deepcopy(stack[3:])
new_stack.insert(0, Bop(bop, start, end))
return reduce_stack(precedence, new_stack)
elif precedence == 4:
unop = stack[0][1]
val = stack[1]
new_stack = deepcopy(stack[2:])
new_stack.insert(0, Unop(unop, val))
return reduce_stack(precedence, new_stack)
def get_function_args(lexbuf, demarcation):
def get_function_args_helper(lexbuf, demarcation, stack, arg, args_list):
if lexbuf == []:
if arg != []:
args_list.append(arg)
return args_list
pair = lexbuf[0]
_, val = pair
rem = lexbuf[1:]
if stack == [] and val == demarcation:
args_list.append(arg)
return get_function_args_helper(rem, demarcation, stack, [], args_list)
if val == lexer.LPAREN:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
elif val == lexer.RPAREN:
if len(stack) >= 1:
if stack[-1] == lexer.LPAREN:
stack.pop()
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
raise MissingParens("lexbuf missing left parens")
if val == lexer.OPEN_TUP:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
elif val == lexer.CLOSE_TUP:
if len(stack) >= 1:
if stack[-1] == lexer.OPEN_TUP:
stack.pop()
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
raise MissingParens("lexbuf missing open tup parens")
if val == lexer.OPEN_BRACKET:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
elif val == lexer.CLOSE_BRACKET:
if len(stack) >= 1:
if stack[-1] == lexer.OPEN_BRACKET:
stack.pop()
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
raise MissingParens("lexbuf missing open bracket")
if val == lexer.OPEN_DICT:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
elif val == lexer.CLOSE_DICT:
if len(stack) >= 1:
if stack[-1] == lexer.OPEN_DICT:
stack.pop()
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
raise MissingParens(
"lexbuf missing open dictionary symbol : {")
if val == lexer.OPEN_STRUCT:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
elif val == lexer.CLOSE_STRUCT:
if len(stack) >= 1:
if stack[-1] == lexer.OPEN_STRUCT:
stack.pop()
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
raise MissingParens(
"lexbuf missing open dictionary symbol : {|")
else:
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
return get_function_args_helper(lexbuf, demarcation, [], [], [])
# def get_data_structure_args(lexbuf, demarcation, start_char, end_char):
# def get_data_structure_args_helper(lexbuf, demarcation, stack, arg, args_list):
# if lexbuf == []:
# if arg != []:
# args_list.append(arg)
# return args_list
# pair = lexbuf[0]
# _, val = pair
# rem = lexbuf[1:]
# if stack == [] and val == demarcation:
# args_list.append(arg)
# return get_data_structure_args_helper(rem, demarcation, stack, [], args_list)
# if val == lexer.LPAREN:
# stack.append(val)
# arg.append(pair)
# return get_data_structure_args_helper(rem, demarcation, stack, arg, args_list)
# elif val == lexer.RPAREN:
# if len(stack) >= 1: