-
Notifications
You must be signed in to change notification settings - Fork 4
/
yip.py
1115 lines (839 loc) · 35.4 KB
/
yip.py
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
TT_PLUS = 'PLUS'
TT_MINUS = 'MINUS'
TT_DIV = 'DIV'
TT_MUL = 'MUL'
TT_LPAREN = 'LPAREN'
TT_RPAREN = 'RPAREN'
TT_INT = 'INT'
TT_FLOAT = 'FLOAT'
TT_EOF = 'EOF'
TT_POWER = 'EXPONENTIAL'
#########################################
### CONSTANTS
#########################################
DEBUG = False
DIGITS = '0123456789.'
class Tokens:
def __init__(self, type, value=None, pos_start=None, pos_end=None):
self.type = type
self.value = value
self.pos_start = pos_start
self.pos_end = pos_end
def __repr__(self) -> str:
if self.value:
return f'{self.type}:{self.value}'
return f'{self.type}'
#################################################################################################
##### LEXER
##### The lexer takes the source code and converts it into tokens.
##### The lexer is also called a tokenizer or scanner.
#################################################################################################
class Lexer:
"""
Lexer class for tokenizing input text.
"""
def __init__(self, fn, text):
"""
Initialize the Lexer object.
Parameters:
- fn (str): The filename or filepath associated with the input text.
- text (str): The input text to be tokenized.
"""
self.fn = fn
self.text = text
self.pos = Position(-1, 0, -1, fn, text)
self.current_char = None
self.advance()
def advance(self):
"""
Advance the current character pointer to the next character in the input text.
"""
self.pos.advance(self.current_char)
self.current_char = self.text[self.pos.idx] if self.pos.idx < len(self.text) else None
def make_tokens(self):
"""
Tokenize the input text and return a list of tokens.
Returns:
- tokens (list): A list of tokens.
- error (Exception or None): An error message if encountered during tokenization, or None if no error occurred.
"""
tokens = []
while self.current_char is not None:
if self.current_char in ' \t':
self.advance()
elif self.current_char in '0123456789':
tokens.append(self.make_number())
elif self.current_char == '+':
tokens.append(Tokens(TT_PLUS, pos_start= self.pos))
self.advance()
elif self.current_char == '-':
tokens.append(Tokens(TT_MINUS, pos_start= self.pos))
self.advance()
elif self.current_char == '*':
tokens.append(Tokens(TT_MUL, pos_start= self.pos))
self.advance()
elif self.current_char == '/':
tokens.append(Tokens(TT_DIV, pos_start= self.pos))
self.advance()
elif self.current_char == '(':
tokens.append(Tokens(TT_LPAREN, pos_start= self.pos))
self.advance()
elif self.current_char == ')':
tokens.append(Tokens(TT_RPAREN, pos_start= self.pos))
self.advance()
else:
pos_start = self.pos.copy()
char = self.current_char
self.advance()
return [], Exception(pos_start, self.pos, f"Illegal character '{char}'")
tokens.append(Tokens(TT_EOF, pos_start = self.pos))
return tokens, None
def make_number(self):
"""
Tokenize a number and return the corresponding token.
Returns:
- token (Tokens): The token representing the number.
"""
num_str = ''
dot_count = 0
pos_start = self.pos.copy()
while self.current_char is not None and self.current_char in DIGITS:
if self.current_char == '.':
if dot_count == 1:
break
dot_count += 1
num_str += '.'
else:
num_str += self.current_char
self.advance()
if dot_count == 0:
return Tokens(TT_INT, int(num_str), pos_start, self.pos)
else:
return Tokens(TT_FLOAT, float(num_str), pos_start, self.pos)
#################################################################################################
##### NODES
##### Nodes are the building blocks of the AST.
##### Nodes are the data structures that represent the code.
##### Nodes are the data structures that represent the code.
#################################################################################################
class NumberNode:
def __init__(self, token):
self.token = token
self.pos_start = self.token.pos_start
self.pos_end = self.token.pos_end
def __repr__(self) -> str:
return f'{self.token}'
class BinOpNode:
def __init__(self, left_node, op_token, right_node):
self.left_node = left_node
self.op_token = op_token
self.right_node = right_node
self.pos_start = self.left_node.pos_start
self.pos_end = self.right_node.pos_end
def __repr__(self) -> str:
return f'({self.left_node}, {self.op_token}, {self.right_node})'
class UnaryOpNode:
def __init__(self, op_token, node):
self.op_token = op_token
self.node = node
self.pos_start = self.op_token.pos_start
self.pos_end = node.pos_end
def __repr__(self) -> str:
return f'({self.op_token}, {self.node})'
class VarAccessNode:
def __init__(self, token):
self.token = token
def __repr__(self) -> str:
return f'{self.token}'
def create_ast(tokens):
"""
Creates an abstract syntax tree (AST) from a list of tokens.
Args:
tokens (list): A list of tokens.
Returns:
BinOpNode: The root node of the AST.
"""
if len(tokens) == 0:
return None
root = tokens[0]
for i in range(len(tokens)):
token = tokens[i]
if token.type == TT_PLUS:
root = BinOpNode(root, token, tokens[i + 1])
elif token.type == TT_MINUS:
root = BinOpNode(root, token, tokens[i + 1])
elif token.type == TT_MUL:
if i + 2 < len(tokens) and tokens[i + 2].type in (TT_PLUS, TT_MINUS):
# Wrap higher precedence operation in parentheses
root = BinOpNode(root, token, (tokens[i + 1], tokens[i + 2], tokens[i + 3]))
else:
root = BinOpNode(root, token, tokens[i + 1])
elif token.type == TT_DIV:
root = BinOpNode(root, token, tokens[i + 1])
elif token.type == TT_POWER:
root = BinOpNode(root, token, tokens[i + 1])
elif token.type == TT_INT:
root = NumberNode(token)
elif token.type == TT_FLOAT:
root = NumberNode(token)
elif token.type == TT_LPAREN:
root = BinOpNode(root, token, tokens[i + 1])
elif token.type == TT_RPAREN:
root = BinOpNode(root, token, tokens[i + 1])
else:
raise Exception(f'Unknown token: {token}')
return root
#################################################################################################
##### PARSER
##### The parser takes the tokens and converts them into an AST.
##### The parser is also called a syntactic analyzer.
#################################################################################################
# Updated Parser class with operator precedence
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.token_index = -1
self.advance()
def advance(self):
"""
Advances the token index and sets the current_token attribute to the next token in the list of tokens.
"""
self.token_index += 1
self.current_token = self.tokens[self.token_index] if self.token_index < len(self.tokens) else None
def parse(self):
"""
Parses the tokens and returns the resulting parse tree.
"""
return self.expr()
def factor(self):
"""
Parses a factor expression and returns the corresponding parse tree node.
"""
res = ParseResult()
token = self.current_token
if token.type in (TT_PLUS, TT_MINUS):
res.register_advancement()
self.advance()
factor = res.register(self.factor())
if res.error:
return res
return res.success(UnaryOpNode(token, factor))
elif token.type in (TT_INT, TT_FLOAT):
res.register_advancement()
self.advance()
return res.success(NumberNode(token))
elif token.type == TT_LPAREN:
res.register_advancement()
self.advance()
expr = res.register(self.expr())
if res.error:
return res
if self.current_token.type == TT_RPAREN:
res.register_advancement()
self.advance()
return res.success(expr)
else:
return res.failure(InvalidSyntaxError(
self.current_token.pos_start, self.current_token.pos_end,
"Expected ')'"
))
return res.failure(InvalidSyntaxError(
token.pos_start, token.pos_end,
"Expected int or float"
))
def power(self):
"""
Parses a power expression and returns the corresponding parse tree node.
"""
return self.bin_op(self.factor, (TT_POWER, ), self.factor)
def term(self):
"""
Parses a term expression and returns the corresponding parse tree node.
"""
return self.bin_op(self.power, (TT_MUL, TT_DIV), self.factor)
def expr(self):
"""
Parses an expression and returns the corresponding parse tree node.
"""
return self.bin_op(self.term, (TT_PLUS, TT_MINUS), self.term)
def bin_op(self, func_a, ops, func_b):
"""
Parses a binary operation expression and returns the corresponding parse tree node.
"""
res = ParseResult()
left = res.register(func_a())
if res.error:
return res
while self.current_token.type in ops:
op_token = self.current_token
res.register_advancement()
self.advance()
right = res.register(func_b())
if res.error:
return res
left = BinOpNode(left, op_token, right)
return res.success(left)
#################################################################################################
##### ERROR
##### The error class is used to handle errors.
##### The error class is used to handle errors.
#################################################################################################
class Error:
"""
Represents an error that occurred during the execution of a program.
Attributes:
pos_start (Position): The starting position of the error.
pos_end (Position): The ending position of the error.
error_name (str): The name of the error.
details (str): Additional details about the error.
"""
def __init__(self, pos_start, pos_end, error_name, details):
self.pos_start = pos_start
self.pos_end = pos_end
self.error_name = error_name
self.details = details
def as_string(self):
"""
Returns a string representation of the error.
Returns:
str: The string representation of the error.
"""
result = f'{self.error_name}: {self.details}\n'
result += f'File {self.pos_start.fn}, line {self.pos_start.ln + 1}'
return result
def __repr__(self) -> str:
return f'{self.as_string()}'
class IllegalCharError(Error):
"""
Error raised when an illegal character is encountered.
Attributes:
pos_start (Position): The start position of the illegal character.
pos_end (Position): The end position of the illegal character.
details (str): Additional details about the error.
"""
def __init__(self, pos_start, pos_end, details):
super().__init__(pos_start, pos_end, 'Illegal Character', details)
class InvalidSyntaxError(Error):
"""
Represents an error that occurs when there is invalid syntax in the code.
Attributes:
pos_start (Position): The starting position of the error.
pos_end (Position): The ending position of the error.
details (str): Additional details about the error.
"""
def __init__(self, pos_start, pos_end, details=''):
super().__init__(pos_start, pos_end, 'Invalid Syntax', details)
class ExpectedTokenError(Error):
"""
Represents an error that occurs when an expected token is missing or incorrect.
Attributes:
pos_start (Position): The starting position of the error.
pos_end (Position): The ending position of the error.
details (str): Additional details about the error.
"""
def __init__(self, pos_start, pos_end, details=''):
super().__init__(pos_start, pos_end, 'Expected Token', details)
class RTError(Error):
"""
Represents an error that occurs during runtime.
Attributes:
pos_start (Position): The starting position of the error.
pos_end (Position): The ending position of the error.
details (str): Additional details about the error.
context (Context): The context of the error.
"""
def __init__(self, pos_start, pos_end, details=''):
super().__init__(pos_start, pos_end, 'Runtime Error', details)
#################################################################################################
##### POSITION
##### The position class is used to keep track of the position of a character in a file.
##### The position class is used to keep track of the position of a character in a file.
#################################################################################################
class Position:
"""
Represents a position in a file.
Attributes:
idx (int): The index of the position.
ln (int): The line number of the position.
col (int): The column number of the position.
fn (str): The file name.
ftxt (str): The file text.
"""
def __init__(self, idx, ln, col, fn, ftxt):
self.idx = idx
self.ln = ln
self.col = col
self.fn = fn
self.ftxt = ftxt
def advance(self, current_char = None):
"""
Advances the position by one character.
Args:
current_char (str, optional): The current character. Defaults to None.
Returns:
Position: The updated position.
"""
self.idx += 1
self.col += 1
if current_char is not None and current_char == '\n':
self.ln += 1
self.col = 0
return self
def copy(self):
"""
Creates a copy of the position.
Returns:
Position: The copied position.
"""
return Position(self.idx, self.ln, self.col, self.fn, self.ftxt)
def __repr__(self) -> str:
"""
Returns a string representation of the position.
Returns:
str: The string representation of the position.
"""
return f'{self.idx}:{self.ln}:{self.col}:{self.fn}:{self.ftxt}'
#################################################################################################
##### PARSE RESULT
##### The parse result is a data structure that contains the result of the parse.
##### The parse result is a data structure that contains the result of the parse.
#################################################################################################
class ParseResult:
"""
Represents the result of a parsing operation.
Attributes:
error (str): An error message if the parsing operation encountered an error, otherwise None.
node (Any): The parsed node if the parsing operation was successful, otherwise None.
advance_count (int): The number of tokens advanced during the parsing operation.
"""
def __init__(self):
self.error = None
self.node = None
self.advance_count = 0
def register_advancement(self):
self.advance_count += 1
def register(self, res):
self.advance_count += res.advance_count
if res.error: self.error = res.error
return res.node
def success(self, node):
self.node = node
return self
def failure(self, error):
if not self.error or self.advance_count == 0:
self.error = error
return self
#################################################################################################
##### RUNTIME RESULT
##### The runtime result is a data structure that contains the result of the runtime.
##### The runtime result is a data structure that contains the result of the runtime.
#################################################################################################
class RuntimeResult:
"""
Represents the result of a runtime operation.
Attributes:
value (Any): The value of the runtime operation.
error (str): An error message if the runtime operation encountered an error, otherwise None.
"""
def __init__(self):
self.value = None
self.error = None
def register(self, res):
if res.error: self.error = res.error
return res.value
def success(self, value):
self.value = value
return self
def failure(self, error):
self.error = error
return self
def should_return(self):
return self.error or self.value
def __repr__(self) -> str:
return f'{self.value}, {self.error}'
#################################################################################################
##### VALUES
##### The values are the data types that the interpreter can handle.
##### The values are the data types that the interpreter can handle.
#################################################################################################
class Number:
def __init__(self, value):
self.value = value
self.set_pos()
def set_pos(self, pos_start=None, pos_end=None):
self.pos_start = pos_start
self.pos_end = pos_end
return self
def added_to(self, other):
if isinstance(other, Number):
return Number(self.value + other.value).set_pos(self.pos_start, other.pos_end), None
def subtracted_by(self, other):
if isinstance(other, Number):
return Number(self.value - other.value).set_pos(self.pos_start, other.pos_end), None
def multiplied_by(self, other):
if isinstance(other, Number):
return Number(self.value * other.value).set_pos(self.pos_start, other.pos_end), None
def divided_by(self, other):
if isinstance(other, Number):
if other.value == 0:
return None, RTError(
other.pos_start, other.pos_end,
'Division by zero'
)
return Number(self.value / other.value).set_pos(self.pos_start, other.pos_end), None
def __repr__(self):
return str(self.value)
class String:
def __init__(self, value):
self.value = value
def added_to(self, other):
if isinstance(other, String):
return String(self.value + other.value).set_pos(self.pos_start, other.pos_end), None
def __repr__(self):
return f'"{self.value}"'
class List:
def __init__(self, elements):
self.elements = elements
def added_to(self, other):
new_list = self.copy()
new_list.elements.append(other)
return new_list, None
def subtracted_by(self, other):
if isinstance(other, Number):
new_list = self.copy()
try:
new_list.elements.pop(other.value)
return new_list, None
except:
return None, RTError(
other.pos_start, other.pos_end,
'Element at this index could not be removed from list because index is out of bounds',
)
def multiplied_by(self, other):
if isinstance(other, List):
new_list = self.copy()
new_list.elements.extend(other.elements)
return new_list, None
def divided_by(self, other):
if isinstance(other, Number):
try:
return self.elements[other.value], None
except:
return None, RTError(
other.pos_start, other.pos_end,
'Element at this index could not be retrieved from list because index is out of bounds',
)
def copy(self):
return List(self.elements[:])
def __repr__(self):
return f'[{", ".join([str(x) for x in self.elements])}]'
class BaseFunction:
def __init__(self, name):
self.name = name or '<anonymous>'
def generate_new_context(self):
new_context = Context(self.name, self.context, self.context.parent_entry_pos)
new_context.symbol_table = SymbolTable(new_context.parent.symbol_table)
return new_context
def check_args(self, arg_names, args):
res = RuntimeResult()
if len(args) > len(arg_names):
return res.failure(RTError(
self.pos_start, self.pos_end,
f'{len(args) - len(arg_names)} too many args passed into {self}',
))
if len(args) < len(arg_names):
return res.failure(RTError(
self.pos_start, self.pos_end,
f'{len(arg_names) - len(args)} too few args passed into {self}',
))
return res.success(None)
def populate_args(self, arg_names, args, exec_ctx):
for i in range(len(args)):
arg_name = arg_names[i]
arg_value = args[i]
arg_value.set_context(exec_ctx)
exec_ctx.symbol_table.set(arg_name, arg_value)
def check_and_populate_args(self, arg_names, args, exec_ctx):
res = RuntimeResult()
res.register(self.check_args(arg_names, args))
if res.error: return res
self.populate_args(arg_names, args, exec_ctx)
return res.success(None)
class Function(BaseFunction):
def __init__(self, name, body_node, arg_names):
super().__init__(name)
self.body_node = body_node
self.arg_names = arg_names
def execute(self, args):
res = RuntimeResult()
interpreter = Interpreter()
exec_ctx = self.generate_new_context()
res.register(self.check_and_populate_args(self.arg_names, args, exec_ctx))
if res.error: return res
value = res.register(interpreter.visit(self.body_node, exec_ctx))
if res.error: return res
return res.success(value)
def copy(self):
copy = Function(self.name, self.body_node, self.arg_names)
copy.set_context(self.context)
copy.set_pos(self.pos_start, self.pos_end)
return copy
def __repr__(self):
return f'<function {self.name}>'
class BuiltInFunction(BaseFunction):
def __init__(self, name):
super().__init__(name)
def execute(self, args):
res = RuntimeResult()
exec_ctx = self.generate_new_context()
method_name = f'execute_{self.name}'
method = getattr(self, method_name, self.no_visit_method)
res.register(self.check_and_populate_args(method.arg_names, args, exec_ctx))
if res.error: return res
return_value = res.register(method(exec_ctx))
if res.error: return res
return res.success(return_value)
def no_visit_method(self, node):
raise Exception(f'No execute_{self.name} method defined')
def copy(self):
copy = BuiltInFunction(self.name)
copy.set_context(self.context)
copy.set_pos(self.pos_start, self.pos_end)
return copy
def __repr__(self):
return f'<built-in function {self.name}>'
def execute_PRINT(self, exec_ctx):
print(str(exec_ctx.symbol_table.get('value')))
return RuntimeResult().success(Number.null)
execute_PRINT.arg_names = ['value']
def execute_PRINT_RET(self, exec_ctx):
return RuntimeResult().success(String(str(exec_ctx.symbol_table.get('value'))))
execute_PRINT_RET.arg_names = ['value']
def execute_INPUT(self, exec_ctx):
text = input()
return RuntimeResult().success(String(text))
execute_INPUT.arg_names = []
def execute_INPUT_INT(self, exec_ctx):
while True:
text = input()
try:
number = int(text)
break
except ValueError:
print(f"'{text}' must be an integer. Try again!")
return RuntimeResult().success(Number(number))
execute_INPUT_INT.arg_names = []
def execute_CLEAR(self, exec_ctx):
os.system('cls' if os.name == 'nt' else 'clear')
return RuntimeResult().success(Number.null)
execute_CLEAR.arg_names = []
def execute_IS_NUMBER(self, exec_ctx):
is_number = isinstance(exec_ctx.symbol_table.get('value'), Number)
return RuntimeResult().success(Number.true if is_number else Number.false)
execute_IS_NUMBER.arg_names = ['value']
def execute_IS_STRING(self, exec_ctx):
is_number = isinstance(exec_ctx.symbol_table.get('value'), String)
return RuntimeResult().success(Number(true if is_number else Number.false))
def execute_IS_LIST(self, exec_ctx):
is_number = isinstance(exec_ctx.symbol_table.get('value'), List)
return RuntimeResult().success(Number(true if is_number else Number.false))
def execute_IS_FUNCTION(self, exec_ctx):
is_number = isinstance(exec_ctx.symbol_table.get('value'), BaseFunction)
return RuntimeResult().success(Number(True if is_number else Number.false))
def execute_APPEND(self, exec_ctx):
list_ = exec_ctx.symbol_table.get('list')
value = exec_ctx.symbol_table.get('value')
if not isinstance(list_, List):
return RuntimeResult().failure(RTError(
self.pos_start, self.pos_end,
'First argument must be list',
exec_ctx
))
list_.elements.append(value)
return RuntimeResult().success(Number.null)
#################################################################################################
##### CONTEXT
##### The context is the environment in which the interpreter executes the code.
##### The context is the environment in which the interpreter executes the code.
#################################################################################################
class Context:
"""
The context class is used to store variables.
"""
def __init__(self, display_name, parent=None, parent_entry_pos=None):
"""
Initialize the context object.
Args:
display_name (str): The name of the context.
parent (Context, optional): The parent context. Defaults to None.
parent_entry_pos (Position, optional): The position of the parent context. Defaults to None.
"""
self.display_name = display_name
self.parent = parent
self.parent_entry_pos = parent_entry_pos
self.symbol_table = {}
def get(self, var_name):
"""
Gets the value of a variable.
Args:
var_name (str): The name of the variable.
Returns:
Any: The value of the variable.
"""
value = self.symbol_table.get(var_name, None)
if value is None and self.parent:
return self.parent.get(var_name)
return value
def set(self, var_name, value):
"""
Sets the value of a variable.
Args:
var_name (str): The name of the variable.
value (Any): The value of the variable.
"""
self.symbol_table[var_name] = value
def __repr__(self) -> str:
"""
Returns a string representation of the context.
Returns:
str: The string representation of the context.
"""
return f'{self.symbol_table}'
#################################################################################################
##### INTERPRETER
##### The interpreter takes the AST and executes the code.
##### The interpreter takes the AST and executes the code.
#################################################################################################
class Interpreter:
"""
The interpreter class is used to interpret the AST.
Methods:
visit(node, context): Visits a node in the AST and interprets it.
no_visit_method(node): Raises an exception if no visit method is found for a node.
visit_ParseResult(node): Visits a ParseResult node.
visit_NumberNode(node): Interprets a number node.
visit_BinOpNode(node): Interprets a binary operation node.
visit_UnaryOpNode(node): Interprets a unary operation node.
interpret(node): Interprets the AST.
"""
def visit(self, node, context):
"""
Visits a node in the AST and interprets it.
Args:
node (Any): The node to visit.
context (Any): The context in which the node is being visited.
Returns:
Any: The result of interpreting the node.
"""
method_name = f'visit_{type(node).__name__}'
method = getattr(self, method_name, self.no_visit_method)
return method(node, context)
def no_visit_method(self, node):
"""
Raises an exception if no visit method is found for a node.
Args:
node (Any): The node that does not have a visit method.
Raises:
Exception: Raised when no visit method is found for a node.
"""
raise Exception(f'No visit_{type(node).__name__} method defined')
def visit_ParseResult(self, node):
"""
Visits a ParseResult node.
Args:
node (ParseResult): The ParseResult node to visit.
Returns:
Any: The result of visiting the underlying node.
"""
return self.visit(node.node)
def visit_NumberNode(self, node):
"""
Interprets a number node.
Args:
node (NumberNode): The number node to interpret.