-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaseMothballSimulation.py
More file actions
841 lines (679 loc) · 34.7 KB
/
BaseMothballSimulation.py
File metadata and controls
841 lines (679 loc) · 34.7 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
import os
from numpy import float32 as f32
from ExprEval import evaluate
import re
import inspect
from collections import Counter
import functools
from Enums import ExpressionType
import json
import sys
if getattr(sys, "frozen", False):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
with open(os.path.join(base_path, "Docstrings", "HelpStrings.json")) as f:
HELP_DOCSTRINGS = json.load(f)
class OverwriteError(Exception):
"Attempted to overwrite a basic Mothball function"
pass
class MothballSequence(str):
"Subclass of str, flag for a mothball sequence instead of a generic string."
pass
class NameString(str):
"Subclass of str, meant for arguments that are used to name something"
pass
class BasePlayer:
class CustomMothballFunction:
def __init__(self, name: str, sequence: MothballSequence, arguments: list[inspect.Parameter]):
self.name = name
self.__name__ = name
self.sequence = sequence
self.positional_only = []
self.positional_or_keyword = []
self.keyword_only = []
self.var_positional = []
for arg in arguments:
match arg.kind:
case inspect.Parameter.POSITIONAL_ONLY:
self.positional_only.append(arg)
case inspect.Parameter.POSITIONAL_OR_KEYWORD:
self.positional_or_keyword.append(arg)
case inspect.Parameter.KEYWORD_ONLY:
self.keyword_only.append(arg)
case inspect.Parameter.VAR_POSITIONAL:
self.var_positional.append(arg)
def __repr__(self):
return f"CustomFunction({self.name})"
pi = 3.14159265358979323846
MODIFIERS = tuple()
ALIAS_TO_MODIFIER = {}
FUNCTIONS_BY_TYPE = {}
_can_have_modifiers=()
_can_have_input=()
_fortyfive_methods=()
def __init__(self) -> None:
self.precision = 7
self.inertia_threshold = 0.005
self.modifiers = 0
self.reverse = False
self.previously_sprinting = False
self.previously_sneaking = False
self.previously_in_web = False
self.local_vars: dict[str, int | float] = {"px": 0.0625}
self.local_funcs: dict[str, BasePlayer.CustomMothballFunction] = {}
self.output: list[tuple[ExpressionType, tuple]] = []
self.closed_vars: list[dict] = [] # For declaring functions only
self.call_stack = [] # For debugging and error messaging
self.stop_flag = False
self.last_returned_value = 0 # ye this is a sus implementation but whatever
def stop_execution(self):
self.stop_flag = True
@staticmethod
def record_to_call_stack(func):
"Decorator which appends and pops from call stack for functions which accept a `MothballSequence`"
@functools.wraps(func)
def inner(*args, **kwargs):
args[0].call_stack.append(func.__name__)
func(*args, **kwargs)
args[0].call_stack.pop()
return inner
@staticmethod
def isfloat(string: str):
"Returns `True` if string is a float representation, including infinity, else `False`"
try:
float(string)
return True
except ValueError:
return False
def safe_eval(self, expr: str, datatype: type, locals_dict: dict):
"Evaluate and convert `expr` to `datatype`. If `datatype = str`, it returns the `expr` as normal."
if datatype in [float, int, f32, bool]:
if datatype == bool:
if expr.strip().lower() == "true":
return True
elif expr.strip().lower() == "false":
return False
else:
return bool(expr)
result = evaluate(expr, locals_dict)
converted_value = datatype(result) if result is not None else None
return converted_value
elif datatype == str:
result = []
in_string = False
follows_slash = False
for char in expr:
if char == "\\":
follows_slash = True
continue
if char == '"' and not follows_slash:
in_string = not in_string
elif char in '{}' and follows_slash:
result.append("\\")
result.append(char)
elif char == 'n' and follows_slash:
result.append("\n")
elif in_string:
result.append(char)
elif not in_string and not char.isspace():
raise SyntaxError(f"Invalid string: {expr}\nStrings must be delimited by double quotes, e.g. \"hello\".\nTo insert values, place them inside curly brackets {{}}, such as \"a pixel is {{px}} blocks\"")
follows_slash = False
return self.formatted("".join(result))
else:
return expr
def add_to_output(self, expression_type: ExpressionType, label: str = '', string_or_num: str | float = '', num2: float = 0, strip_label: bool = True):
if strip_label:
label = label.strip()
match expression_type:
case ExpressionType.Z_LABEL | ExpressionType.X_LABEL | ExpressionType.GENERAL_LABEL_WITH_NUMBER:
if num2:
expression_type += 1 # changes ExpressionType Flag
nn = string_or_num - num2
self.output.append((expression_type, (label, ": ", self.truncate_number(num2), " - " if nn <= 0 else " + ", self.truncate_number(abs(nn)) )))
return nn
else:
self.output.append((expression_type, (label, ": ", self.truncate_number(string_or_num) )))
return string_or_num
case ExpressionType.TEXT:
if strip_label:
string_or_num = string_or_num.strip()
self.output.append((expression_type, (string_or_num,)))
case ExpressionType.WARNING:
self.output.append((expression_type, ("Warning", ": ", string_or_num.strip() )))
case ExpressionType.Z_INERTIA_HIT | ExpressionType.X_INERTIA_HIT | ExpressionType.Z_INERTIA_MISS | ExpressionType.X_INERTIA_MISS:
a = abs(abs(string_or_num) - abs(num2))
self.output.append((expression_type, (label, ": ", self.truncate_number(string_or_num), " (", self.truncate_number(a), ")" )))
case ExpressionType.GENERAL_LABEL:
self.output.append((expression_type, (label, )))
def truncate_number(self, value: float):
"Round decimals to `self.precision` decimal places"
return f"{value:.{self.precision}f}".rstrip("0").rstrip(".")
def formatted(self, string: str):
"Formats string just like an f-string"
formatted_string = ""
item_to_eval = ""
in_expr = False
depth = 0
follows_slash = False
for char in string:
if char == "\\":
follows_slash = True
continue
if char == "{" and not follows_slash:
in_expr = not in_expr
if not in_expr:
item_to_eval = ""
formatted_string += char
else:
item_to_eval += char
depth += 1
elif char == "}" and not follows_slash:
if depth == 0:
raise SyntaxError("Unmatched Brackets")
depth -= 1
if in_expr:
item_to_eval += char
item_to_eval = item_to_eval[1:len(item_to_eval) - 1]
if item_to_eval:
x = evaluate(item_to_eval, self.local_vars)
x = str(x)
formatted_string += x
item_to_eval = ''
else:
formatted_string += item_to_eval + char
item_to_eval = ''
in_expr = not in_expr
elif in_expr:
item_to_eval += char
else:
formatted_string += char
follows_slash = False
if depth != 0:
raise SyntaxError("Unmatched Brackets")
return formatted_string
def move(self):
"Implement in the subclasses"
...
###########################################################################
### These functions come by default, meaning both the XZ and Y calculators will have these functions exposed to the user.
### They are: custom_function, printdisplay, repeat, var, precision, help
###########################################################################
@record_to_call_stack
def function(self, name: NameString, *args: NameString, code: MothballSequence, docstring:str=""):
"Create a custom function"
curr_type = inspect.Parameter.POSITIONAL_OR_KEYWORD
parameters = []
for arg in args:
if arg.strip() == "/":
parameters.append((arg.strip(), "", ""))
curr_type = inspect.Parameter.POSITIONAL_ONLY
elif arg.strip() == "*":
parameters.append((arg.strip(), "", ""))
else:
a = re.findall(r"^\s*(\w+)(?:\s*:\s*(\w+))?(?:\s*=\s*(\w+))?\s*$", arg)[0]
parameters.append(a)
mapping = {"str": str, "int": int, "float": float, "MothballSequence": MothballSequence}
p = []
for i in parameters:
arg_name, dtype, default = i
if arg_name == "/":
curr_type = inspect.Parameter.POSITIONAL_OR_KEYWORD
elif arg_name == "*":
curr_type = inspect.Parameter.KEYWORD_ONLY
else:
dtype = mapping.get(dtype, inspect.Parameter.empty)
if default:
p.append(inspect.Parameter(arg_name, curr_type, default=dtype(default), annotation=dtype))
else:
p.append(inspect.Parameter(arg_name, curr_type, annotation=dtype))
self.local_funcs[name] = BasePlayer.CustomMothballFunction(name, code, p)
@record_to_call_stack
def repeat(self, sequence: MothballSequence, count: int, /):
"Run `sequence` for `count` times. Raises `ValueError` if `count < 0`"
if count < 0:
raise ValueError(f"repeat() must have a nonnegative argument 'count'")
if count == 0:
return
parsed_tokens = self.parse(sequence)
runnables = []
for token in parsed_tokens:
runnable = self.tokenize(token, locals=self.local_vars)
self.run(runnable)
runnables.append(runnable)
# This is b/c if something like repeat(var(a,1) sj(a), 2) is run, the tokenizer will fail to process
# a = 1 since var(a,1) wasn't actually run yet.
for _ in range(count-1):
if self.stop_flag:
raise InterruptedError("Stopped execution")
for runnable in runnables:
self.run(runnable)
@record_to_call_stack
def print(self, string: str = "", /):
if self.reverse: # who is using this anyway
string = self.formatted(string)
string = "".join([x for x in reversed(string)])
self.add_to_output(ExpressionType.TEXT, string_or_num=string)
@record_to_call_stack # its set to MothballSequence so safe_eval doesn't attempt to parse it
def var(self, variable_name: NameString, value: MothballSequence = None, /):
"""
Assigns `value` to `variable_name`
A valid variable name is any sequence of alphabet letters a-z or A-Z, numerical digits (0-9), an underscore `_`, or any combination of them. \\
A number cannot be the first character in the variable name.
`var()` will attempt to convert the value to the appropiate datatype, which only supports
```py
int | float | str
```
"""
find_var_regex = r"^([a-zA-Z_][a-zA-Z0-9_]*)$"
if not re.findall(find_var_regex, variable_name): # Either has one match or no matches
raise SyntaxError(f"'{variable_name}' is not a valid variable name")
if variable_name.strip() in self.FUNCTIONS:
raise OverwriteError(f"Cannot set variable name '{variable_name.strip()}' as it is a function name")
if value is None:
self.local_vars[variable_name] = self.last_returned_value
return
final_value = value
try:
final_value = evaluate(value, self.local_vars)
except:
final_value = self.safe_eval(value, str, locals_dict=self.local_vars)
if not final_value:
raise ValueError(f"Unable to deduce the value of '{value}'")
self.local_vars[variable_name] = final_value
def setprecision(self, decimal_places: int, /):
"Sets the decimal precision for displaying outputs, must be an integer between `0` and `16` inclusive, raises `ValueError` otherwise."
if decimal_places < 0 or decimal_places > 16:
raise ValueError(f"precision() only takes integers between 0 to 16 inclusive, got {decimal_places} instead.")
self.precision = decimal_places
@record_to_call_stack
def ballhelp(self, func: MothballSequence):
"Gets help about function `func`"
is_custom_func = False
f = self.FUNCTIONS.get(func)
if f is None:
f = self.local_funcs.get(func)
if f is None:
raise NameError(f"Function {func} not found")
else:
is_custom_func = True
f_sig = f.positional_only + f.positional_or_keyword + f.keyword_only + f.var_positional
else:
f_sig = inspect.signature(f).parameters.values()
aliases = self.ALIASES.get(f.__name__, "")
if not aliases:
aliases = f.__name__
else:
aliases = ', '.join(aliases)
self.add_to_output(ExpressionType.TEXT, string_or_num=f"Help with {f.__name__}:")
self.add_to_output(ExpressionType.TEXT, string_or_num=f" Aliases: {aliases}", strip_label=False)
self.add_to_output(ExpressionType.TEXT, string_or_num=f" Arguments:", strip_label=False)
prev_kind = None
for y in f_sig: # PLEASE ADD * and /
if y.name != "self":
if y.kind == inspect.Parameter.VAR_POSITIONAL:
varargsymbol = "*"
elif y.kind == inspect.Parameter.VAR_KEYWORD:
varargsymbol = "**"
else:
varargsymbol = ""
if y.default == inspect.Parameter.empty:
self.add_to_output(ExpressionType.TEXT, string_or_num=f" {varargsymbol}{y.name}: {y.annotation.__name__}", strip_label=False)
else:
self.add_to_output(ExpressionType.TEXT, string_or_num=f" {varargsymbol}{y.name}: {y.annotation.__name__} = {'_' if not y.default else y.default}", strip_label=False)
kind = y.kind
if y.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD and prev_kind == inspect.Parameter.POSITIONAL_ONLY:
self.add_to_output(ExpressionType.TEXT, string_or_num=f" /,", strip_label=False)
elif y.kind == inspect.Parameter.KEYWORD_ONLY and prev_kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
self.add_to_output(ExpressionType.TEXT, string_or_num=f" *,", strip_label=False)
elif y.kind == inspect.Parameter.KEYWORD_ONLY and prev_kind == inspect.Parameter.POSITIONAL_ONLY:
self.add_to_output(ExpressionType.TEXT, string_or_num=f" /,", strip_label=False)
self.add_to_output(ExpressionType.TEXT, string_or_num=f" *,", strip_label=False)
prev_kind = kind
if is_custom_func:
default = "This is because this was a custom defined function without a docstring written for it."
else:
default = "If you see this message, please annoy @anonnoob (see the about page)"
docstring = HELP_DOCSTRINGS.get(f.__name__, f"{f.__name__} does not have a docstring. {default}")
self.add_to_output(ExpressionType.TEXT, string_or_num=docstring)
def get_suggestions(self, string: str):
"Return a list of suggestions from all available mothball commands that best matches `string`. For example, if `sprintsn` was inputted, a possible suggestion is `sneaksprint`."
matches_start = []
matches_part = [] # If string in word
matches_char_count = {}
for command in BasePlayer.FUNCTIONS.keys():
if command.startswith(string): # 1. Matches start
matches_start.append(command)
elif string in command: # 2. Matches part
matches_part.append(command)
else: # 3. Count chars
cmd_count = Counter(command)
str_count = Counter(string)
off_by = 0
for char, count in str_count.items():
try:
if count == cmd_count[char]:
off_by -= 1
else:
off_by += count - cmd_count[char]
except KeyError: # not a character
off_by += 1
off_by += abs(len(command) - len(string))
if off_by < len(command): matches_char_count[command] = off_by
matches_char_count = sorted(matches_char_count, key=lambda e: matches_char_count[e])
return matches_start + matches_part + matches_char_count
def remove_comments_and_check_strings(self, string: str):
"Removes comments delimited by `#`"
result = []
in_comment = False
follows_slash = False
in_string = False
for char in string:
if char == '"' and not follows_slash:
in_string = not in_string
if char == "#" and not follows_slash and not in_string:
in_comment = not in_comment
continue
if not in_comment:
result.append(char)
if char == "\\" and not follows_slash:
follows_slash = True
else:
follows_slash = False
if in_string:
raise SyntaxError('Unmatched quotes (")')
return "".join(result)
def parse(self, string: str, splitters: tuple = ("\n", " ", "\r", "\t"), strict_whitespace: bool = True) -> list:
"""
Splits the string at any of the splitters that are outside of parenthesis.
Returns the first layer list of strings (or tokens), raises `SyntaxError` if there are missing spaces, or parenthesis are unmatched.
Comments are delimited by the `#` symbol. Anything between comments will be ignored.
`repeat(sprintjump(12), 3) sprint(2) outz(16)` parses into `[repeat(sprintjump(12), 3), sprint(2), outz(16)]`
"""
result = []
token = ""
stack = [] # parenthesis
current = 0
high = len(string)
expecting_whitespace = False
matches_next_element = lambda e: ((e == ")" and stack[-1] == "(") or (e == "]" and stack[-1] == "["))
follows_slash = False
in_string = False
string = self.remove_comments_and_check_strings(string)
# Regex to change '|' into 'x(0) z(0)'
replace_bar_regex = r"(\|)"
string = re.sub(replace_bar_regex, " x(0) z(0) ", string)
for char in string + splitters[0]:
if strict_whitespace:
if expecting_whitespace and not char.isspace():
if char in ")]":
raise SyntaxError(f"Unmatched brackets at character {current}: {string[max(0, current-5):min(high, current + 5)]}")
else:
msg = f"Space needed at character {current}"
if self.call_stack:
msg += f" (inside {', '.join(self.call_stack)})"
msg += f": {string[max(0, current-7):min(high, current + 7)]}"
raise SyntaxError(msg)
else:
expecting_whitespace = False
if char == "\\":
follows_slash = True
token += char
continue
elif char == '"' and not follows_slash:
in_string = not in_string
elif (char == "(" or char == "[") and not follows_slash and not in_string:
stack.append(char)
elif (char == ")" or char == "]") and not follows_slash and not in_string:
if not stack:
raise SyntaxError(f"Unmatched brackets at character {current}: {string[max(0, current-5):min(high, current + 5)]}")
if not matches_next_element(char):
raise SyntaxError(f"Unmatched brackets at character {current}: {string[max(0, current-5):min(high, current + 5)]}")
stack.pop()
if not stack:
token += char
follows_slash = False
if char == ")":
expecting_whitespace = True
current += 1
continue
if char in splitters and not stack and not follows_slash and not in_string:
token = token.strip()
result.append(token) if token else None
token = ""
else:
token += char
current += 1
follows_slash = False
expecting_whitespace = False
if stack:
raise SyntaxError("Unmatched open parethesis")
return result
def tokenize(self, string: str, locals: dict = None) -> dict:
"""
Tokenizes the string to a dictionary containing the function, positional arguments, and keyword arguments of appropiate types.
Returns as a dictionary in the form
```
{"function": function, "inputs": str, "modifiers": int, "args": list, "kwargs": dict}
```
`inputs` is a 1-2 char string determining how key presses determine game movement, examples `wa` or `s`. \
`modifiers` is an int which uses bit flags to indicate which modifiers are on or off. See the attribute `MODIFIERS`
Raises `SyntaxError` if a positional argument follows a keyword argument. \\
Raises `NameError` if a given function doesn't exist. \\
Raises `TypeError` if these functions: (`stop`-related, 45 movement, or non-movement functions) recieves an input.
Raises any other error encountered while converting datatypes.
"""
# tokenize_regex = r'(\W)?([^.\(\-)]+)(?:\.([^\(\.]+))?(?:\[(.*)\])?(?:\((.*)\))?(.+)?'
# r'(\W)?([^.\(\-)]+)(?:\.([^\(\.]+))? (?:\((.*)\))?(.+)?'
e1 = r"(\W)?"
func = r"([^.\[\(\-\)\]]+)"
inputs = r"(?:\.([wasdWASD]+))?"
modifiers = r"(?:\[(.*)\])?"
args = r"(?:\((.*)\))?"
e2 = r"(.+)?"
tokenize_regex = e1 + func + inputs + modifiers + args + e2
error1, func_name, inputs, modifiers, args, error2 = re.findall(tokenize_regex, string, flags=re.DOTALL)[0]
if error1 and error1 != "-":
raise SyntaxError(f"Unknown item {error1} in {string}")
elif error2:
raise SyntaxError(f"Unknown item {error2} in {string}")
if string[0] == "-":
self.reverse = True
else:
self.reverse = False
func = self.FUNCTIONS.get(func_name)
if func is None:
func = self.local_funcs.get(func_name) # CHANGES
if func is None:
if self.call_stack:
error_msg = f"In {', '.join(self.call_stack)} -> "
else:
error_msg = ''
error_msg += f"{func_name} is not a valid function. "
suggestions = self.get_suggestions(func_name)
if suggestions:
suggestions = suggestions[0:min(4, len(suggestions))]
error_msg += f"Did you mean {', '.join(suggestions)}?"
raise NameError(error_msg)
positional_args = []
keyword_args = {}
args = self.parse(args, splitters=",", strict_whitespace=False)
keyword_regex = r"^\s*?(\w+)\s*=\s*(.+)\s*$"
after_keyword = False
for arg in args:
result = re.findall(keyword_regex, arg, flags=re.DOTALL)
if result: # keyword
key,value = result[0]
if key in keyword_args:
raise SyntaxError(f"Repeated keyword argument {key}")
keyword_args[key.strip()] = value.strip()
after_keyword = True
else: # positional
if after_keyword:
raise SyntaxError("Positional argument cannot follow keyword arguments")
positional_args.append(arg)
positional_args, keyword_args = self.check_types(func, positional_args, keyword_args, locals=locals)
if modifiers:
modifiers = self.validate_modifiers(modifiers.split(","))
else:
modifiers = 0
if self._can_have_input and func.__name__ not in self._can_have_input:
if inputs:
raise TypeError(f"{func.__name__}() cannot be modified by an input")
if self._fortyfive_methods and func.__name__ in self._fortyfive_methods:
inputs = "w"
elif not inputs:
inputs = "w"
elif inputs not in ["w","wa","wd", "s", "sa", "sd", "a", "d"]:
raise ValueError(f"function {func_name} received bad input '{inputs}', it can only be w, s, a, d, wa, wd, sa, wd.")
if func.__name__ not in self._can_have_modifiers and modifiers:
raise TypeError(f"{func.__name__}() cannot be modified by a modifier")
return {"function": func, "inputs": inputs, "modifiers": modifiers, "args": positional_args, "kwargs": keyword_args}
def validate_modifiers(self, modifiers: list):
m = 0
for i, modify in enumerate(modifiers):
a = self.ALIAS_TO_MODIFIER[modify.strip()]
m = m | a
if a not in self.MODIFIERS:
raise TypeError(f"No such modifier '{a}'")
return m
def check_types(self, func, args: list, kwargs: dict, locals = None):
"""
Type checks each argument in `args` and `kwargs` according to the annotations in `func`. If successful, returns a list of positional args and a dict of keyword args.
Raises any appropiate error encountered while converting strings to the necessary datatypes.
"""
# print(self.local_vars)
if locals is None:
locals = self.local_vars
converted_args = []
converted_kwargs = {}
if isinstance(func, BasePlayer.CustomMothballFunction):
positional_only = {x.name:x.annotation for x in func.positional_only}
positional_or_keyword = {x.name:x.annotation for x in func.positional_or_keyword}
keyword_only = {x.name:x.annotation for x in func.keyword_only}
var_positional = {x.name:x.annotation for x in func.var_positional}
required_positionals = {x.name:x.annotation for x in func.positional_only if x.default == inspect.Parameter.empty}
else:
signature = inspect.signature(func).parameters.values()
positional_only = {x.name:x.annotation for x in signature if x.kind == inspect.Parameter.POSITIONAL_ONLY and x.name != "self"}
positional_or_keyword = {x.name:x.annotation for x in signature if x.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD and x.name != "self"}
keyword_only = {x.name:x.annotation for x in signature if x.kind == inspect.Parameter.KEYWORD_ONLY}
var_positional = {x.name:x.annotation for x in signature if x.kind == inspect.Parameter.VAR_POSITIONAL}
# Watch out for potential errors
required_positionals = {x.name:x.annotation for x in signature if x.kind == inspect.Parameter.POSITIONAL_ONLY and x.default == inspect.Parameter.empty and x.name != "self"}
# Idea: First check the positional arguments using positional_only and positional_or_keyword.
# Once positional_only runs out, start using positional_or_keyword.
# Once both runs out, use var_positional
# Raise error if there's too many positional arguments (var_positional is empty)
### Check the positional arguments ###
can_be_positional = positional_only | positional_or_keyword
if len(required_positionals) > len(args):
number_of_missing = len(required_positionals) - len(args)
raise TypeError(f"{func.__name__} missing {number_of_missing} positional-only argument{'s' if number_of_missing > 1 else ''}: {', '.join(list(required_positionals)[len(args):])}")
for i in range(min(len(args), len(can_be_positional))):
datatype = list(can_be_positional.values())[i]
if datatype == inspect.Parameter.empty:
try:
converted_value = self.safe_eval(args[i], float, locals)
except:
converted_value = self.safe_eval(args[i], str, locals)
else:
converted_value = self.safe_eval(args[i], datatype, locals)
if list(can_be_positional)[i] == "duration" and func.__name__ not in self.local_funcs:
if converted_value is not None and converted_value < 0:
raise ValueError(f"Positional argument 'duration' should be a non-negative integer")
elif converted_value is None:
converted_value = 1
# elif list(can_be_positional)[i] == "label":
# if converted_value is None:
# converted_value = func.__name__
converted_args.append(converted_value)
if len(args) < len(can_be_positional):
a = len(args) - len(can_be_positional)
can_be_positional = {x:can_be_positional[x] for x in list(can_be_positional)[a:]}
elif var_positional and len(args) > len(can_be_positional):
for j in args[len(can_be_positional):]:
c = self.safe_eval(j, list(var_positional.values())[0], locals)
converted_args.append(c)
elif not var_positional and len(args) > len(can_be_positional):
raise TypeError(f"{func.__name__} accepts at most {len(can_be_positional)} positional arguments, got {len(args)} instead")
can_be_keyword = can_be_positional | keyword_only
### Check the keyword args ###
for kw, value in kwargs.items():
datatype = can_be_keyword.get(kw)
if datatype is None:
raise TypeError(f"{func.__name__} has no keyword argument '{kw}'")
converted_kwargs[kw] = self.safe_eval(value, datatype, locals)
# else:
# converted_kwargs[kw] = datatype(value)
return converted_args, converted_kwargs
def run(self, token: dict):
"""
Runs the token. `token` is a dictionary in the form
```py
{"function": function, "inputs": str, "modifiers": int, "args": list, "kwargs": dict}
```
"""
func = token["function"]
self.inputs = token["inputs"]
self.modifiers = token["modifiers"]
args = token["args"]
kwargs = token["kwargs"]
if isinstance(func, BasePlayer.CustomMothballFunction):
# print(func.sequence)
d = {}
used = []
for i,j in zip(args, func.positional_only + func.positional_or_keyword):
d[j.name] = i
used.append(j.name)
for k in func.positional_or_keyword+func.keyword_only:
if k.name in kwargs:
d[k.name] = kwargs[k.name]
elif k.name not in used:
d[k.name] = k.default
x = self.local_vars | {}
self.local_vars = self.local_vars | d
self.simulate(func.sequence, False ,self.local_vars, suppress_exception=False)
for i in x:
if i in self.local_vars:
x[i] = self.local_vars[i]
self.local_vars = x
else:
func(self, *args, **kwargs)
def simulate(self, sequence: str, return_defaults = True, locals: dict = None, suppress_exception: bool = True):
"Execute Mothball Code. If no output was made and `return_defaults == True`, return the default output (see `show_default_output()`). `locals` is a dict of values for variables."
try:
parsed_tokens = self.parse(sequence)
for token in parsed_tokens:
if self.stop_flag:
raise InterruptedError("Stopped execution")
runnable = self.tokenize(token, locals=locals)
if self.stop_flag:
raise InterruptedError("Stopped execution")
self.run(runnable)
if return_defaults and not self.output:
self.show_default_output()
except Exception as e:
if suppress_exception:
self.add_to_output(ExpressionType.TEXT, string_or_num=f"Error ({type(e).__name__}): {str(e)}")
else:
raise
def show_default_output(self): ...
def show_output(self):
s = ""
for tup in self.output:
ss = "".join(tup[1])
print(ss)
s += ss + "\n"
return s
FUNCTIONS = {"function": function, "func":function, "print": print, "repeat": repeat, "r": repeat, "setprecision":setprecision, "precision":setprecision, "pre":setprecision, "ballhelp": ballhelp, "help": ballhelp, "var": var}
ALIASES = {"function": ["function", "func"], "print": ["print"], "repeat": ["repeat", "r"], "setprecision": ["setprecision", "pre", "precision"], "ballhelp":["ballhelp", "help"], "var": ["var"]}
if __name__ == "__main__":
a = BasePlayer()
# import time
# m = time.perf_counter()
a.simulate(r'func(test, mity, code=print("hi {mity}")) test(3) test("this") help(test) help(print)')
# print(a.local_funcs)
# n = time.perf_counter()
# print(n-m)
b=a.show_output()