-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
1099 lines (929 loc) · 46.1 KB
/
interpreter.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
from parse.parser import Parser
from parse.node import AstNode, NodeType, NodeFunctionReturn
from parse.source_location import SourceLocation
from parse.node import *
from interpreter.scope import *
from interpreter.stack import Stack
from interpreter.function import BuiltinFunction, BuiltinFunctionArguments
from interpreter.typing.basic_type import BasicType
from interpreter.basic_object import BasicObject
from interpreter.basic_value import BasicValue
from interpreter.env.globals import Globals
from interpreter.variable import VariableType
from interpreter.env.builtins import builtin_object_new, obj_to_string
from lexer import TokenType, LexerToken
from error import InterpreterError, ErrorList, ErrorType, Error
class ReturnJump(Exception):
pass
class Interpreter():
def __init__(self, source_location):
self.source_location = source_location
self.error_list = ErrorList()
# declare scopes + global scope
self.stack = Stack()
self.in_try = False
self.global_scope = Scope(None)
self._top_level_scope = None
# _globals is only used for pyimport
self._globals = Globals()
self._globals.apply_to_scope(self.global_scope)
@property
def current_scope(self):
if self._top_level_scope is not None:
return self._top_level_scope
return self.global_scope
def open_scope(self):
self._top_level_scope = Scope(self.current_scope)
def close_scope(self):
if self.current_scope == self.global_scope:
raise Exception('cannot close global scope!')
self._top_level_scope = self.current_scope.parent
return self.current_scope
def error(self, node, type, message, cont=False, name=None, classnames=None, object=None):
if name is None and classnames is None:
if type == ErrorType.Syntax:
name = "Syntax Error"
classnames = ["Exception", "SyntaxError"]
elif type == ErrorType.DoesNotExist:
name = "DoesNotExist Error"
classnames = ["Exception", "DoesNotExistError"]
elif type == ErrorType.TypeError:
name = "Type Error"
classnames = ["Exception", "TypeError"]
elif type == ErrorType.MultipleDefinition:
name = "Multiple Definition Error"
classnames = ["Exception", "MultipleDefinitionError"]
elif type == ErrorType.ArgumentError:
name = "Argument Error"
classnames = ["Exception", "Argument Error"]
elif type == ErrorType.InterruptedError:
name = "Interrupted Error"
classnames = ["Exception", "InterruptedError"]
elif type == ErrorType.MacroExpansionError:
name = "Macro Expansion Error"
classnames = ["Exception", "MacroExpansionError"]
else:
name = "Exception"
classnames = ["Exception"]
location = None
if node is not None:
location = node.location
if not cont and self.in_try:
raise InterpreterError('Interpreter error', node, type, message, cont, name, classnames, object)
err = Error(type, location, message, self.source_location.filename, name)
self.error_list.push_error(err)
self.error_list.print_errors()
if cont or not self.in_try:
raise InterpreterError('Interpreter error', node, type, message, cont, name, classnames, object)
def visit(self, node, allow_casting=False):
if isinstance(node, BasicValue):
return node
try:
caller_name = "visit_{}".format(str(node.type.name))
caller = getattr(self, caller_name)
except:
raise Exception('No visitor function defined for node {}'.format(node))
if caller_name == "visit_Assign":
return caller(node, allow_casting)
return caller(node)
def visit_UnaryOp(self, node):
funstr = '__noop__'
if node.token.type == TokenType.Plus:
val = self.visit(node.expression)
return BasicValue(+val.value)
elif node.token.type == TokenType.Minus:
val = self.visit(node.expression)
return BasicValue(-val.value)
elif node.token.type == TokenType.Not:
funstr = '__not__'
# if val is None or val.value == 0:
# return BasicValue(1)
# else:
# return BasicValue(0)
elif node.token.type == TokenType.BitwiseNot:
funstr = '__bitnot__'
member_access_call_node = NodeCall(
NodeMemberExpression(
node.expression,
LexerToken(funstr, TokenType.Identifier),
node.token
),
NodeArgumentList(
[],
node.token
)
)
return self.visit(member_access_call_node)
def visit_BinOp(self, node):
funstr = '__noop__'
if node.token.type == TokenType.Plus:
funstr = '__add__'
elif node.token.type == TokenType.Minus:
funstr = '__sub__'
elif node.token.type == TokenType.Multiply:
funstr = '__mul__'
elif node.token.type == TokenType.Exponentiation:
funstr = '__expon__'
elif node.token.type == TokenType.Divide:
funstr = '__div__'
elif node.token.type == TokenType.Modulus:
funstr = '__mod__'
elif node.token.type == TokenType.LessThan:
funstr = '__lt__'
elif node.token.type == TokenType.LessThanEqual:
funstr = '__lte__'
elif node.token.type == TokenType.GreaterThan:
funstr = '__gt__'
elif node.token.type == TokenType.GreaterThanEqual:
funstr = '__gte__'
elif node.token.type == TokenType.Compare:
funstr = '__eql__'
elif node.token.type == TokenType.NotCompare:
funstr = '__noteql__'
elif node.token.type == TokenType.And:
funstr = '__and__'
elif node.token.type == TokenType.Or:
funstr = '__or__'
elif node.token.type == TokenType.BitwiseOr:
funstr = '__bitor__'
elif node.token.type == TokenType.BitwiseAnd:
funstr = '__bitand__'
elif node.token.type == TokenType.BitwiseXor:
funstr = '__bitxor__'
elif node.token.type == TokenType.BitwiseLShift:
funstr = '__bitshiftleft__'
elif node.token.type == TokenType.BitwiseRShift:
funstr = '__bitshiftright__'
elif node.token.type == TokenType.Spaceship:
funstr = '__compare__'
member_access_call_node = NodeCall(
NodeMemberExpression(
node.left,
LexerToken(funstr, TokenType.Identifier),
node.token
),
NodeArgumentList(
[node.right],
node.token
)
)
return self.visit(member_access_call_node)
def visit_Type(self, node):
pass
def visit_Declare(self, node):
type_node_value = None
if isinstance(node, NodeSplatArgument):
node = NodeDeclare(None, node.token, NodeNone(node.token))
if node.type_node is not None:
type_node_value = self.visit(node.type_node)
if self.current_scope.find_variable_info(node.name.value, limit=True) != None:
self.error(node, ErrorType.MultipleDefinition, "multiple definition of '{}'".format(node.name.value))
self.current_scope.declare_variable(node.name.value, type_node_value, node.allow_casting)
val = self.visit(node.value, node.allow_casting)
return val
def visit_Import(self, node):
# the imported file is already lexed and parsed from the parser, so this
# acts like a block and visits the statements inside. This means that if we
# import inside a function, any variables should only be available to that
# scope.
old_source_location = self.source_location
self.source_location = node.source_location
for child in node.children:
self.visit(child)
self.source_location = old_source_location
def visit_FunctionReturn(self, node):
value = self.visit(node.value_node)
self.stack.push(value)
raise ReturnJump()
def visit_Number(self, node):
return BasicValue(node.value)
def visit_String(self, node):
return BasicValue(node.value)
def visit_Block(self, node, create_scope=True):
if create_scope:
self.open_scope()
# visit each statement in block
for child in node.children:
self.visit(child)
if create_scope:
self.close_scope()
def assignment_typecheck(self, node, type_object, assignment_value, detailed_return=False, allow_casting=False):
if type_object is None:
self.error(node, ErrorType.TypeError, 'Set with decltype but decltype resolved to None')
if detailed_return:
return False, assignment_value
return False
elif not isinstance(type_object, BasicType):
self.error(node, ErrorType.TypeError, '{} is not a valid type object and cannot be used as a declaration type'.format(type_object))
if detailed_return:
return False, assignment_value
return False
else:
assignment_type = BasicValue(assignment_value).lookup_type(self.global_scope)
if isinstance(assignment_type, BasicObject):
assignment_type = assignment_value.parent
if assignment_type is None:
self.error(node, ErrorType.TypeError, 'Assignment requires type {} but could not resolve a runtime type of assignment value'.format(type_object))
if detailed_return:
return None, assignment_value
return None
if isinstance(assignment_type, BasicValue):
assignment_type = assignment_type.extract_value()
if not isinstance(assignment_type, BasicType):
self.error(node, ErrorType.TypeError, '{} is not a valid runtime type object'.format(assignment_type))
if detailed_return:
return False, assignment_value
return False
if not type_object.compare_type(assignment_type):
if allow_casting:
try:
assignment_value = builtin_object_new(BuiltinFunctionArguments(interpreter=self, this_object=type_object, arguments=[assignment_value], node=node))
assignment_type = type_object
except:
self.error(node, ErrorType.TypeError, 'Attempted to assign <{}> to a value of type <{}>'.format(type_object.friendly_typename, assignment_type.friendly_typename))
if detailed_return:
return False, assignment_value
return False
else:
self.error(node, ErrorType.TypeError, 'Attempted to assign <{}> to a value of type <{}>'.format(type_object.friendly_typename, assignment_type.friendly_typename))
if detailed_return:
return False, assignment_value
return False
if detailed_return:
return True, assignment_value
return True
def visit_Assign(self, node, allow_casting=False):
if isinstance(node.lhs, NodeVariable):
node.allow_casting = allow_casting
target_info = self.walk_variable(node.lhs)
target_value = target_info.value_wrapper
value = self.visit(node.value)
# TYPE CHECK
if target_info.decltype is not None:
typecheck_value, value = self.assignment_typecheck(node.lhs, target_info.decltype, value, True, allow_casting)
if typecheck_value is not True:
return None
target_value.assign_value(value)
return value
elif isinstance(node.lhs, NodeMemberExpression):
(target, member) = self.walk_member_expression(node.lhs)
if not isinstance(target, BasicObject):
self.error(node, ErrorType.TypeError, 'member expression not assignable')
return None
target_type = target.parent
value = self.visit(node.value)
target.assign_member(member.name, value)
return value
elif isinstance(node.lhs, NodeArrayAccessExpression):
member_access_call_node = NodeCall(
NodeMemberExpression(
node.lhs.lhs,
LexerToken('__set__', TokenType.Identifier),
node.lhs.token
),
NodeArgumentList(
[node.lhs.access_expr, node.value],
node.lhs.token
)
)
return self.visit(member_access_call_node)
else:
self.error(node, ErrorType.TypeError, 'cannot assign {}'.format(node.lhs))
return None
def collect_args(self, node):
collected_args = []
for arg in node.argument_list.arguments:
arg_visited = self.visit(arg)
if type(arg_visited) == list:
for arg in arg_visited:
collected_args.append(arg)
else:
collected_args.append(arg_visited)
return collected_args
def wrap_arg_in_node(self, name, arg):
if type(arg) == str:
return NodeString(LexerToken('"{}"'.format(arg)))
elif type(arg) in (float, int):
return NodeNumber(LexerToken(str(arg)))
elif type(arg) == list:
return NodeArrayExpression(
map(lambda x: self.wrap_arg_in_node(name, x), arg),
LexerToken(name, TokenType.Identifier)
)
else:
raise Exception("Invalid argument {} type({}) not supported".format(arg, type(arg)))
def call_function(self, name, args):
from ParaCode import ParaCode
retval = 0
node_args = []
for arg in args:
node_args.append(self.wrap_arg_in_node(name, arg))
paraCode = ParaCode()
paraCode.eval(data="{};".format(name), interpret=False, default_imports=[])
lhs = paraCode.ast[0]
name_token = LexerToken(name, TokenType.Identifier)
if isinstance(lhs, NodeMemberExpression):
parent, member = self.walk_member_expression(lhs)
target = member.value
node_args = [parent]+node_args
else:
target = lhs
call_node = NodeCall(
target,
NodeArgumentList(
node_args,
name_token
)
)
return self.visit_Call(call_node).extract_value()
def visit_Call(self, node):
this_arg = None
is_member_call = False
target = None
# for `a.b()`, pass in `a` as the this value.
if isinstance(node.lhs, NodeMemberExpression):
t, m = self.walk_member_expression(node.lhs)
target = m.value
this_arg = t
is_member_call = True
else:
target = self.visit(node.lhs)
return_value = 0
if target is not None:
if isinstance(target, BuiltinFunction):
collected_args = self.collect_args(node)
this_value = None
if this_arg is not None:
this_value = this_arg
return self.call_builtin_function(target, this_value, collected_args, node)
# user-defined function
elif isinstance(target, NodeFunctionExpression):
collected_args = None
if isinstance(node.lhs, NodeMemberExpression):
if (node.lhs.identifier.value == "__and__" and not self.check_object_truthy(node.lhs.lhs)) or (node.lhs.identifier.value == "__or__" and self.check_object_truthy(node.lhs.lhs)):
collected_args = [BasicObject(None, {"_value": 0})]
if collected_args is None:
collected_args = self.collect_args(node)
if is_member_call: # a.b('test') -> pass 'a' in as first argument
if this_arg is not None:
this_value = this_arg
if this_value is not None:
collected_args = [this_value, *collected_args]
expected_arg_count = len(target.argument_list.arguments)
given_arg_count = len(collected_args)
original_given_arg_count = given_arg_count
if len(target.argument_list.arguments) > 0 and isinstance(target.argument_list.arguments[-1], NodeSplatArgument):
if given_arg_count > 0:
new_collected_args = []
for i in range(0, given_arg_count):
if i < expected_arg_count - 1:
new_collected_args.append(collected_args[i])
elif i == expected_arg_count - 1:
new_collected_args.append([])
try:
value = BasicValue(eval(collected_args[i]))
except:
value = BasicValue(eval(str(BasicType(collected_args[i]))))
new_collected_args[expected_arg_count - 1].append(value)
elif i > expected_arg_count - 1:
try:
value = BasicValue(eval(collected_args[i]))
except:
value = BasicValue(eval(str(BasicType(collected_args[i]))))
new_collected_args[expected_arg_count - 1].append(value)
amount = len(new_collected_args)
for i in range(0, expected_arg_count):
if i > amount - 1:
new_collected_args.append([])
collected_args = new_collected_args
if not isinstance(collected_args[-1], BasicObject) and not isinstance(collected_args[-1], BasicValue) and not isinstance(collected_args[-1], BasicType):
try:
value = BasicValue(eval(collected_args[-1]))
except:
value = BasicValue(eval(str(BasicType(collected_args[-1]))))
else:
value = collected_args[-1]
collected_args[-1] = value
given_arg_count = len(collected_args)
else:
collected_args.append(BasicValue([]))
given_arg_count = len(collected_args)
elif expected_arg_count < given_arg_count:
self.error(node, ErrorType.ArgumentError, 'method expected {} arguments, {} given'.format(expected_arg_count, original_given_arg_count))
return None
for i in range(0, expected_arg_count):
if i >= given_arg_count:
if isinstance(target.argument_list.arguments[i], NodeSplatArgument):
arg = NodeDeclare(None, target.argument_list.arguments[i].token, NodeNone(target.argument_list.arguments[i].token))
else:
arg = target.argument_list.arguments[i]
if not isinstance(arg.value, NodeNone):
try:
value = BasicValue(eval(target.argument_list.arguments[i].value.value.token.value))
except:
value = BasicValue(eval(str(BasicType(target.argument_list.arguments[i].value.value.token.value))))
collected_args.append(value)
given_arg_count += 1
if expected_arg_count > given_arg_count:
self.error(node, ErrorType.ArgumentError, 'method expected {} arguments, {} given'.format(expected_arg_count, given_arg_count))
return None
# typecheck args
for i in range(0, expected_arg_count):
if isinstance(target.argument_list.arguments[i], NodeSplatArgument):
target_arg = NodeDeclare(None, target.argument_list.arguments[i].token, NodeNone(target.argument_list.arguments[i].token))
else:
target_arg = target.argument_list.arguments[i]
call_arg = collected_args[i]
type_node = target_arg.type_node
if type_node is not None:
decltype = self.visit(type_node)
self.assignment_typecheck(target_arg, decltype, call_arg)
# push arguments to stack
for arg in collected_args:
self.stack.push(arg)
self.call_function_expression(target)
# the return value is pushed onto the stack at end of block or return
# statement. Pop it off and return as a value
result = self.stack.pop()
if not isinstance(result, BasicValue):
self.error(node, ErrorType.TypeError, 'expected method to return an instance of BasicValue, got {}'.format(result))
return None
return result
else: # objects......
if this_arg is None:
this_arg = NodeNone(node.token)
member_access_call_node = NodeCall(
NodeMemberExpression(
target,
LexerToken('__call__', TokenType.Identifier),
node.token
),
NodeArgumentList(
[
NodeArrayExpression(
[this_arg]+node.argument_list.arguments,
node.token
)
],
node.token
)
)
return self.visit(member_access_call_node)
self.error(node, ErrorType.TypeError, 'invalid call: {} is not callable'.format(target))
def walk_variable(self, node):
var = self.current_scope.find_variable_info(node.value)
if var is None:
self.error(node, ErrorType.DoesNotExist, "Referencing undefined variable '{}'".format(node.value))
return None
return var
def visit_Variable(self, node):
var = self.walk_variable(node)
if var is not None:
return var.value_wrapper.value
return None
def check_object_truthy(self, node):
member_access_call_node = NodeCall(
NodeMemberExpression(
node,
LexerToken('__bool__', TokenType.Identifier),
node.token
),
NodeArgumentList(
[],
node.token
)
)
result = self.visit(member_access_call_node)
if result is None:
self.error(node, ErrorType.TypeError, 'cannot check if object {} is truthy'.format(node))
return None
int_result = result.extract_value()
if type(int_result) != int:
self.error(node, ErrorType.TypeError, 'expected __bool__ call to return an int'.format(node))
return None
return int_result != 0
def visit_IfStatement(self, node):
truthy_result = self.check_object_truthy(node.expr)
if truthy_result:
return self.visit_Block(node.block)
elif node.else_block is not None:
# use visit rather than direct to visit_Block
# since else_block can also be a NodeIfStatement in the
# case of `elif`
return self.visit(node.else_block)
def visit_Try(self, node):
if node.catch_block is not None and node.catch_block != [] and node.else_block is None and node.finally_block is None:
#print("AA")
try:
self.in_try = True
return self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
found = False
for i in range(len(node.catch_block)):
if node.expr[i] is None:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
else:
if type(node.expr[i]) == list:
for value in node.expr[i]:
if value.token.value in e.classnames:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif type(node.expr[i].token.value) == str:
#print("AAA")
# print(node.expr[i].token.value)
# print(e.object.members["message"])
# print(node.expr[i + 1].token.value)
if node.expr[i].token.value in e.classnames:
if node.variable[i] is not None:
node.variable[i].type_node.token.value = node.expr[i].token.value
# print(node.variable[i].value)
# node.variable[i].value.token.token.value = e.object
# print(node.variable[i].value)
self.visit_Declare(node.variable[i])
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif i == len(node.catch_block) - 1 and not found:
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
elif i == len(node.catch_block) - 1 and not found:
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
elif node.catch_block is not None and node.catch_block != [] and node.else_block is None and node.finally_block is not None:
#print("B")
try:
self.in_try = True
self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
found = False
for i in range(len(node.catch_block)):
if node.expr[i] is None:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
else:
if type(node.expr[i]) == list:
for value in node.expr[i]:
if value.token.value in e.classnames:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif type(node.expr[i].token.value) == str:
if node.expr[i].token.value in e.classnames:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif i == len(node.catch_block) - 1 and not found:
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
elif i == len(node.catch_block) - 1 and not found:
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
finally:
self.in_try = True
return self.visit_Block(node.finally_block)
elif (node.catch_block is not None and node.catch_block != []) and node.else_block is not None and node.finally_block is None:
#print("C")
try:
self.in_try = True
self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
found = False
for i in range(len(node.catch_block)):
if node.expr[i] is None:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
else:
if type(node.expr[i]) == list:
for value in node.expr[i]:
if value.token.value in e.classnames:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif type(node.expr[i].token.value) == str:
if node.expr[i].token.value in e.classnames:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif i == len(node.catch_block) - 1 and not found:
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
elif i == len(node.catch_block) - 1 and not found:
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
else:
self.in_try = True
return self.visit_Block(node.else_block)
elif (node.catch_block is not None and node.catch_block != []) and node.else_block is not None and node.finally_block is not None:
# BasicObject(parent=None, members={'raise': AstNode[FunctionExpression, AstNode[Block, LexerToken[Type:TokenType.Keyword, Value:'return']]], 'message': 'A'})
try:
self.in_try = True
self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
found = False
for i in range(len(node.catch_block)):
if node.expr[i] is None:
#print("AB")
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
else:
if type(node.expr[i]) == list:
#print("B")
for value in node.expr[i]:
if value.token.value in e.classnames:
if i == len(node.catch_block) - 1:
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
found = True
self.visit_Block(node.catch_block[i])
elif type(node.expr[i].token.value) == str:
#print("C")
if node.expr[i].token.value in e.classnames:
#print("CA")
if i == len(node.catch_block) - 1:
#print("CAA")
self.in_try = True
return self.visit_Block(node.catch_block[i])
else:
#print("CAB")
# print(node)
found = True
self.visit_Block(node.catch_block[i])
elif i == len(node.catch_block) - 1 and not found:
#print("CB")
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
elif i == len(node.catch_block) - 1 and not found:
#print("D")
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
else:
self.visit_Block(node.else_block)
finally:
self.in_try = True
return self.visit_Block(node.finally_block)
elif (node.catch_block is None or node.catch_block == []) and node.else_block is not None and node.finally_block is None:
#print("E")
try:
self.in_try = True
return self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
else:
self.in_try = True
return self.visit_Block(node.else_block)
elif (node.catch_block is None or node.catch_block == []) and node.else_block is None and node.finally_block is not None:
#print("F")
try:
self.in_try = True
return self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
finally:
self.in_try = True
return self.visit_Block(node.else_block)
elif (node.catch_block is None or node.catch_block == []) and node.else_block is not None and node.finally_block is not None:
try:
self.in_try = True
return self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
else:
self.in_try = True
return self.visit_Block(node.else_block)
finally:
self.in_try = True
return self.visit_Block(node.finally_block)
else:
try:
self.in_try = True
return self.visit_Block(node.block)
except InterpreterError as e:
if e.type == ErrorType.Syntax:
self.in_try = False
return self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
self.in_try = True
self.error(e.node, e.type, e.message, True, e.name, e.classnames, e.object)
def visit_While(self, node):
truthy_result = self.check_object_truthy(node.expr)
while truthy_result:
self.visit_Block(node.block)
truthy_result = self.check_object_truthy(node.expr)
def visit_For(self, node):
# call __iterate__ passing in a function expression
# as a callback for each item in the iterable.
# create an argument list with a single argument, the target.
# it will be named whatever the var is in the for loop statement
argument_list = NodeArgumentList(
[NodeDeclare(None, node.var_token, NodeNone(node.token))],
node.token
)
fnexpr_node = NodeFunctionExpression(argument_list, node.block)
member_access_call_node = NodeCall(
NodeMemberExpression(
node.expr,
LexerToken('__iterate__', TokenType.Identifier),
node.token
),
NodeArgumentList(
[fnexpr_node],
node.token
)
)
self.visit(member_access_call_node)
def visit_SplatArgument(self, node):
# get variable
value = self.visit(node.expr)
if value is None:
self.error(node, ErrorType.TypeError, 'cannot perform splat operation: value is null')
return None
if not isinstance(value, BasicValue):
self.error(node, ErrorType.TypeError, 'cannot perform splat operation: value is not a BasicValue')
return None
extracted_value = value.extract_value()
if not isinstance(extracted_value, list):
self.error(node, ErrorType.TypeError, 'cannot perform splat operation: value must be an array')
return None
args = []
# loop over each item in array and push to stack
for item in extracted_value:
args.append(BasicValue(item).extract_basicvalue())
return args
def visit_ArgumentList(self, node):
# read arguments backwards as values are popped from stack
for argument in reversed(node.arguments):
# retrieve value
value = self.stack.pop()
# declare variable
self.visit_Declare(argument)
# set variable to passed in value
if isinstance(value, AstNode):
value = self.visit(value)
if isinstance(argument, NodeSplatArgument):
argument = NodeDeclare(None, argument.token, NodeNone(argument.token))
self.current_scope.set_variable(argument.name.value, value)
def visit_FunctionExpression(self, node):
return node
def visit_Macro(self, node):
return self.visit(node.expr)
def visit_Mixin(self, node):
from parse.parser import Parser
parser = Parser(node.tokens, self.source_location)
ast = parser.parse()
if len(parser.error_list.errors) > 0:
self.error(node, ErrorType.MacroExpansionError, 'Macro expansion failed:\n{}'.format('\t'.join(map(lambda x: str(x), parser.error_list.errors))))
return None
last_value = None
for node in ast:
last_value = self.visit(node)
return BasicValue(last_value).extract_basicvalue()
def call_builtin_function(self, fun, this_object, arguments, node):
basic_value_result = fun.call(BuiltinFunctionArguments(interpreter=self, this_object=this_object, arguments=arguments, node=node))
if not isinstance(basic_value_result, BasicValue):
self.error(node, ErrorType.TypeError, 'expected method {} to return an instance of BasicValue, got {}'.format(fun, basic_value_result))
return None
return basic_value_result
def call_function_expression(self, node):
# create our scope before block so argument variables are contained
self.open_scope()
function_scope = self.current_scope
# visit our arguments
self.visit(node.argument_list)
# self.visit would normally be used here, but we need create_scope
try:
self.visit_Block(node.block, create_scope=False)
# check if block contains a return statement
last_child = None
for child in node.block.children:
last_child = child
if isinstance(child, NodeFunctionReturn):
break
# no return statement, push return code 0 to the stack
if type(last_child) != NodeFunctionReturn:
self.stack.push(BasicValue(0)) # should just be null or something