-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetaV4.03.py
More file actions
1352 lines (1183 loc) · 60.5 KB
/
betaV4.03.py
File metadata and controls
1352 lines (1183 loc) · 60.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# PLua (Lua to Python) - MNM (Muser Not Matters) v4 (Roblox Libs)
# Updated by Muser (New Big Update (NBU))
# Enhanced by AI (Control Flow, Basic Roblox Libs)
# TODO: More functions, more libs
import time # <--- Add this import
import re
import shlex
from typing import Any, Dict, List, Tuple, Optional, Callable # Ensure Dict, Any, Optional are imported
import tkinter as tk
import sys
from io import StringIO
import math # Import Python math module
import random # Import Python random module
import copy # Needed for table.clone (though basic lists/dicts have .copy())
BlockInfo = Dict[str, Any] # Define what BlockInfo means for type hints
# --- Utility Functions (lua_gsub, convertsimplevalues - unchanged) ---
def lua_gsub(text, pattern, replacement):
"""
Mimics Lua's string.gsub function using Python regex.
NOTE: `pattern` uses Python regex syntax, not Lua patterns.
`replacement` can be a string (with \1, \2 for groups) or a function.
"""
count = 0
try:
if callable(replacement):
def replace_func(match):
nonlocal count
count += 1
# Pass matched groups to the replacement function
try:
return str(replacement(*match.groups()))
except TypeError:
# If replacement func doesn't take args, pass the whole match
try:
return str(replacement(match.group(0)))
except Exception as e:
print(f"Error in gsub replacement function: {e}")
return match.group(0) # Return original on error
except Exception as e:
print(f"Error in gsub replacement function: {e}")
return match.group(0) # Return original on error
modified_text = re.sub(pattern, replace_func, text)
# Count might not be perfect if re.sub calls func but func returns original
else:
# replacement is a string, use re.subn
modified_text, count = re.subn(pattern, replacement, text)
return modified_text, count
except re.error as e:
print(f"Regex error in gsub pattern '{pattern}': {e}")
return text, 0 # Return original text on regex error
except Exception as e:
print(f"Error during gsub: {e}")
return text, 0
def convertsimplevalues(val: Any) -> Any:
"""Converts string representation of simple Lua values to Python types."""
if not isinstance(val, str): return val
lower_val = val.lower()
if lower_val == "nil": return None
elif lower_val == "false": return False
elif lower_val == "true": return True
if val.startswith('"') and val.endswith('"'): return val[1:-1]
if val.startswith("'") and val.endswith("'"): return val[1:-1]
try: return int(val)
except ValueError:
try:
# Check if it looks like a number before converting
cleaned_val = val.strip()
if cleaned_val.replace('.', '', 1).replace('-', '', 1).isdigit():
return float(cleaned_val)
else: return val # Not a number looking string
except ValueError: return val
# --- Argument Parsing and Value Resolution ---
def parse_value(token: str, local_vars: dict) -> Any:
"""
Parses a token into a Python value.
Checks local variables, then math/string tables, then simple literals.
Handles basic member access like 'math.pi' or 'string.len'.
Returns the value or function object.
"""
# 1. Handle Member Access (basic) -> math.pi, string.len
if '.' in token:
parts = token.split('.', 1)
base_name = parts[0]
member_name = parts[1]
if base_name in local_vars and isinstance(local_vars[base_name], dict):
base_table = local_vars[base_name]
if member_name in base_table:
return base_table[member_name] # Return constant or function object
else:
# Member not found in the table
# print(f"Warning: Member '{member_name}' not found in table '{base_name}'.")
return None # Represent as nil
else:
# Base table not found or not a table
# print(f"Warning: Base '{base_name}' not found or not a table for member access.")
return None # Represent as nil
# 2. Check if it's a known local variable
if token in local_vars:
return local_vars[token]
# 3. Check if it's a simple literal value (string, number, bool, nil)
return convertsimplevalues(token)
def parse_arguments(tokens: List[str], start_index: int, local_vars: dict) -> List[Any]:
"""Parses tokens starting from start_index into evaluated arguments."""
args = []
for i in range(start_index, len(tokens)):
args.append(parse_value(tokens[i], local_vars))
return args
# --- Roblox Library Implementations ---
# Dictionary to hold our math implementations
mnm_math_lib = {}
# Dictionary to hold our string implementations
mnm_string_lib = {}
# --- Math Library Functions ---
def _mnm_math_abs(x=None): return math.fabs(x) if isinstance(x, (int, float)) else 0
def _mnm_math_acos(x=None): return math.acos(x) if isinstance(x, (int, float)) else 0
def _mnm_math_asin(x=None): return math.asin(x) if isinstance(x, (int, float)) else 0
def _mnm_math_atan(x=None): return math.atan(x) if isinstance(x, (int, float)) else 0
def _mnm_math_atan2(y=None, x=None): return math.atan2(y, x) if isinstance(y, (int, float)) and isinstance(x, (int, float)) else 0
def _mnm_math_ceil(x=None): return math.ceil(x) if isinstance(x, (int, float)) else 0
def _mnm_math_clamp(x=None, min_val=None, max_val=None):
if not all(isinstance(n, (int, float)) for n in [x, min_val, max_val]): return 0
return max(min_val, min(x, max_val))
def _mnm_math_cos(x=None): return math.cos(x) if isinstance(x, (int, float)) else 1 # cos(0)=1
def _mnm_math_cosh(x=None): return math.cosh(x) if isinstance(x, (int, float)) else 1 # cosh(0)=1
def _mnm_math_deg(x=None): return math.degrees(x) if isinstance(x, (int, float)) else 0
def _mnm_math_exp(x=None): return math.exp(x) if isinstance(x, (int, float)) else 1 # exp(0)=1
def _mnm_math_floor(x=None): return math.floor(x) if isinstance(x, (int, float)) else 0
def _mnm_math_fmod(x=None, y=None): return math.fmod(x, y) if isinstance(x, (int, float)) and isinstance(y, (int, float)) else 0
def _mnm_math_frexp(x=None): return math.frexp(x) if isinstance(x, (int, float)) else (0, 0) # Returns tuple
def _mnm_math_ldexp(x=None, e=None): return math.ldexp(x, int(e)) if isinstance(x, (int, float)) and isinstance(e, (int, float)) else 0
def _mnm_math_lerp(a=None, b=None, t=None):
if not all(isinstance(n, (int, float)) for n in [a, b, t]): return 0
return a + (b - a) * t
def _mnm_math_log(x=None, base=None):
if not isinstance(x, (int, float)) or x <= 0: return -math.inf # Or None? Lua might error
if base is None: return math.log(x)
if not isinstance(base, (int, float)) or base <= 0 or base == 1: return -math.inf
return math.log(x, base)
def _mnm_math_log10(x=None): return math.log10(x) if isinstance(x, (int, float)) and x > 0 else -math.inf
# Removed map function, as it's not standard Python math
def _mnm_math_max(*args): return max(args) if args else 0 # Lua errors on no args
def _mnm_math_min(*args): return min(args) if args else 0 # Lua errors on no args
def _mnm_math_modf(x=None): return math.modf(x) if isinstance(x, (int, float)) else (0.0, 0.0) # Returns tuple (f, i)
# noise omitted
def _mnm_math_pow(x=None, y=None): return math.pow(x, y) if isinstance(x, (int, float)) and isinstance(y, (int, float)) else 0
def _mnm_math_rad(x=None): return math.radians(x) if isinstance(x, (int, float)) else 0
def _mnm_math_random(m=None, n=None):
m_num = isinstance(m, (int, float))
n_num = isinstance(n, (int, float))
if m_num and n_num: return random.uniform(m, n) if isinstance(m, float) or isinstance(n, float) else random.randint(int(m), int(n))
elif m_num: return random.uniform(0, m) if isinstance(m, float) else random.randint(1, int(m)) # Roblox convention: random(m) is 1..m
else: return random.random() # Default [0, 1)
def _mnm_math_randomseed(x=None): random.seed(x) # Returns void/None
def _mnm_math_round(x=None): return round(x) if isinstance(x, (int, float)) else 0 # Python 3 round (to nearest even for .5)
def _mnm_math_sign(x=None):
if not isinstance(x, (int, float)): return 0
if x == 0: return 0
return math.copysign(1, x)
def _mnm_math_sin(x=None): return math.sin(x) if isinstance(x, (int, float)) else 0 # sin(0)=0
def _mnm_math_sinh(x=None): return math.sinh(x) if isinstance(x, (int, float)) else 0
def _mnm_math_sqrt(x=None): return math.sqrt(x) if isinstance(x, (int, float)) and x >= 0 else 0
def _mnm_math_tan(x=None): return math.tan(x) if isinstance(x, (int, float)) else 0
def _mnm_math_tanh(x=None): return math.tanh(x) if isinstance(x, (int, float)) else 0
# Populate mnm_math_lib
# Create a static list of items from locals() BEFORE iterating
local_items_math = list(locals().items())
for name, func in local_items_math:
# Check if it's one of our functions AND it's actually callable
if name.startswith("_mnm_math_") and callable(func):
mnm_math_lib[name[10:]] = func # Strip prefix
# Add math constants separately (ensures they don't interfere with iteration)
mnm_math_lib['pi'] = math.pi
mnm_math_lib['huge'] = float('inf')
# --- String Library Functions ---
# Helper for 1-based Lua index -> 0-based Python index
def _lua_to_py_index(idx, length):
if not isinstance(idx, int): return None # Invalid index type
if idx > 0: return idx - 1
elif idx == 0: return None # Lua doesn't use 0
# idx < 0: Lua's -1 is end, -2 is second to last, etc.
elif idx >= -length: return length + idx
else: return None # Index out of bounds (negative)
# Helper for Lua slicing (inclusive j) -> Python slicing (exclusive j)
def _get_slice_indices(s_len, i, j):
py_i = _lua_to_py_index(i, s_len)
# Default for j is -1 (Lua end)
j = j if j is not None else -1
py_j = _lua_to_py_index(j, s_len)
if py_i is None: py_i = 0 # Default start if i invalid? Or error? Let's default to 0
if py_j is None: py_j = s_len -1 # Default end if j invalid? Let's default to end
# Python slice needs index *after* the desired end character
py_j_exclusive = py_j + 1
# Basic bounds check
if py_i < 0: py_i = 0
if py_j_exclusive > s_len: py_j_exclusive = s_len
if py_i >= py_j_exclusive: return None, None # Invalid slice resulting in empty
return py_i, py_j_exclusive
def _mnm_string_byte(s:str="", i:int=1, j:Optional[int]=None):
if not isinstance(s, str): s = str(s)
if j is None: j = i
s_len = len(s)
py_i, py_j_exclusive = _get_slice_indices(s_len, i, j)
if py_i is None: return () # Return empty tuple for no results
codes = []
for k in range(py_i, py_j_exclusive):
codes.append(ord(s[k]))
return tuple(codes) # Lua returns multiple numbers, simulate with tuple
def _mnm_string_char(*args):
chars = []
for code in args:
if isinstance(code, (int, float)):
try:
chars.append(chr(int(code)))
except ValueError:
print(f"Warning: Invalid code point {code} in string.char")
chars.append('?') # Placeholder for invalid codes
else:
chars.append('?')
return "".join(chars)
def _mnm_string_find(s:str="", pattern:str="", init:int=1, plain:bool=False):
if not isinstance(s, str): s = str(s)
if not isinstance(pattern, str): pattern = str(pattern)
s_len = len(s)
py_init = _lua_to_py_index(init, s_len)
if py_init is None or py_init >= s_len: return None # Cannot start search
try:
if plain: # Simple substring search
found_index = s.find(pattern, py_init)
if found_index != -1:
# Return Lua-style 1-based indices (start, end inclusive)
return found_index + 1, found_index + len(pattern)
else:
return None
else: # Regex search
# NOTE: Using Python Regex, NOT Lua patterns!
match = re.search(pattern, s[py_init:])
if match:
# Adjust indices back to original string and 1-based
start_index = py_init + match.start() + 1
end_index = py_init + match.end() # re.end is exclusive, Lua is inclusive
return start_index, end_index
else:
return None
except re.error as e:
print(f"Regex error in string.find pattern '{pattern}': {e}")
return None
except Exception as e:
print(f"Error during string.find: {e}")
return None
def _mnm_string_format(formatstring:str="", *args):
# WARNING: Python's % formatting is similar but NOT identical to Lua's string.format.
# This is a very basic approximation.
if not isinstance(formatstring, str): formatstring = str(formatstring)
try:
# Attempt Python % formatting
return formatstring % args
except TypeError as e:
print(f"Warning: Error during string.format (may differ from Lua): {e}")
# Try simple replacements as fallback? No, stick to % attempt.
return formatstring
except Exception as e:
print(f"Error during string.format: {e}")
return formatstring
# gmatch omitted (returns iterator)
def _mnm_string_gsub(s:str="", pattern:str="", replacement:Any="", n:Optional[int]=None):
# NOTE: Uses lua_gsub helper which uses Python regex.
if not isinstance(s, str): s = str(s)
if not isinstance(pattern, str): pattern = str(pattern)
# Replacement can be string or function (passed directly to lua_gsub)
if not isinstance(replacement, (str, Callable)): replacement=str(replacement)
limit = n if isinstance(n, int) and n >= 0 else 0 # 0 means replace all in re.subn context? No, needs full replacement for count limit
# lua_gsub handles the replacement logic
if limit > 0:
# Manually perform limited replacement using re.finditer
count = 0
last_end = 0
result = []
try:
for match in re.finditer(pattern, s):
if count >= limit:
break
result.append(s[last_end:match.start()])
if callable(replacement):
# Call replacement function (handle potential errors inside)
repl_val = replacement(*match.groups())
result.append(str(repl_val))
else:
# Perform string replacement with group references (\1, etc.)
result.append(match.expand(replacement))
last_end = match.end()
count += 1
result.append(s[last_end:]) # Append remaining part
return "".join(result), count
except re.error as e:
print(f"Regex error in gsub pattern '{pattern}': {e}")
return s, 0
except Exception as e:
print(f"Error during limited gsub: {e}")
return s, 0
else:
# Replace all occurrences
return lua_gsub(s, pattern, replacement) # Returns (new_string, count)
def _mnm_string_len(s:str=""):
if not isinstance(s, str): s = str(s)
return len(s)
def _mnm_string_lower(s:str=""):
if not isinstance(s, str): s = str(s)
return s.lower()
def _mnm_string_match(s:str="", pattern:str="", init:int=1):
# NOTE: Uses Python Regex, NOT Lua patterns! Captures not fully handled here.
if not isinstance(s, str): s = str(s)
if not isinstance(pattern, str): pattern = str(pattern)
s_len = len(s)
py_init = _lua_to_py_index(init, s_len)
if py_init is None or py_init >= s_len: return None
try:
match = re.search(pattern, s[py_init:])
if match:
# Lua match returns captures if present, otherwise the whole match
if match.groups():
# Return first capture group if exists, mimic basic Lua capture
return match.group(1) if len(match.groups()) >= 1 else match.group(0)
else:
return match.group(0) # Return the whole match
else:
return None
except re.error as e:
print(f"Regex error in string.match pattern '{pattern}': {e}")
return None
except Exception as e:
print(f"Error during string.match: {e}")
return None
# pack, packsize omitted
def _mnm_string_rep(s:str="", n:int=0):
if not isinstance(s, str): s = str(s)
if not isinstance(n, int) or n < 0: n = 0
return s * n
def _mnm_string_reverse(s:str=""):
if not isinstance(s, str): s = str(s)
return s[::-1]
def _mnm_string_split(s:str="", separator:str=""):
# Basic split, doesn't handle regex separators like Lua might implicitly
if not isinstance(s, str): s = str(s)
if not isinstance(separator, str): separator = str(separator)
if separator == "": # Lua splits into individual characters
return list(s)
return s.split(separator) # Returns list (Lua table)
def _mnm_string_sub(s:str="", i:int=1, j:Optional[int]=None):
if not isinstance(s, str): s = str(s)
s_len = len(s)
py_i, py_j_exclusive = _get_slice_indices(s_len, i, j if j is not None else -1) # Default j is Lua -1
if py_i is None or py_j_exclusive is None or py_i >= py_j_exclusive:
return "" # Return empty string for invalid slice
return s[py_i:py_j_exclusive]
# unpack omitted
def _mnm_string_upper(s:str=""):
if not isinstance(s, str): s = str(s)
return s.upper()
# Populate mnm_string_lib
for name, func in locals().items():
if name.startswith("_mnm_string_"):
mnm_string_lib[name[12:]] = func
# --- Condition Logic (evaluate_lua_condition, find_block_structure unchanged) ---
def evaluate_lua_condition(condition_tokens: List[str], local_vars: Dict[str, Any]) -> bool:
# ... (previous code remains unchanged) ...
if not condition_tokens: return False
if len(condition_tokens) >= 2 and condition_tokens[0].lower() == "not":
operand_val = parse_value(condition_tokens[1], local_vars)
return operand_val is False or operand_val is None
elif len(condition_tokens) == 1:
py_val = parse_value(condition_tokens[0], local_vars)
return py_val is not False and py_val is not None
elif len(condition_tokens) == 3:
op1_token, operator, op2_token = condition_tokens
operand1 = parse_value(op1_token, local_vars)
operand2 = parse_value(op2_token, local_vars)
try:
if operator == "==": return operand1 == operand2
elif operator == "~=": return operand1 != operand2
elif operator == "<": return operand1 < operand2
elif operator == ">": return operand1 > operand2
elif operator == "<=": return operand1 <= operand2
elif operator == ">=": return operand1 >= operand2
elif operator.lower() == "and":
op1_truthy = operand1 is not False and operand1 is not None
return op1_truthy and (operand2 is not False and operand2 is not None)
elif operator.lower() == "or":
op1_truthy = operand1 is not False and operand1 is not None
return op1_truthy or (operand2 is not False and operand2 is not None)
else: print(f"Error: Unsupported operator in condition: {operator}"); return False
except TypeError: print(f"Warning: Type error during condition ('{operand1}' {operator} '{operand2}'). Resulting in false."); return False if operator != "~=" else True
except Exception as e: print(f"Error during condition evaluation: {e}"); return False
else: print(f"Warning: Complex condition evaluation not fully supported: '{' '.join(condition_tokens)}'. Evaluating as false."); return False
def find_block_structure(lines: List[str], start_index: int) -> Optional[BlockInfo]:
# ... (previous code remains unchanged) ...
# Needs careful review if block starters/enders appear in strings/comments
# For brevity, keeping the simple keyword check logic
if start_index >= len(lines): return None
try: tokens = shlex.split(lines[start_index].strip())
except ValueError: return None
if not tokens: return None
block_type = tokens[0].lower()
structure: BlockInfo = {'type': block_type, 'elseifs': [], 'end_line': -1}
nesting_level = 1
current_line_index = start_index + 1
# Parse initial condition and find end, storing block lines (simplified logic from previous version)
# ... (This complex block finding logic should be robustly tested/refined) ...
# Example structure population (needs full implementation as before)
if block_type == "if":
# Find 'then', extract condition tokens
pass # Placeholder for full find_block_structure logic
elif block_type == "while":
# Find 'do', extract condition tokens
pass # Placeholder for full find_block_structure logic
# Scan lines, manage nesting, identify elseif/else, find 'end'
# Populate structure dict with 'if_block_lines', 'elseif_block_lines', etc. and 'end_line'
# --> NOTE: The full block finding logic from the previous step needs to be here <--
# For now, returning a dummy structure for MNM2 structure checks
if block_type in ['if', 'while', 'function']: # Added function here for consistency
dummy_end = start_index + 1
while dummy_end < len(lines):
if lines[dummy_end].strip().lower() == 'end':
structure['end_line'] = dummy_end
# Dummy line extraction
if block_type == 'if':
structure['if_cond_tokens'] = ['true'] # Dummy
structure['if_block_lines'] = lines[start_index+1:dummy_end]
elif block_type == 'while':
structure['while_cond_tokens'] = ['true'] # Dummy
structure['while_block_lines'] = lines[start_index+1:dummy_end]
# Function block lines handled in MNM2 'local function' part
break
dummy_end += 1
if structure['end_line'] == -1: return None # Failed to find end
return structure # Return dummy structure
return None # Not a recognized block starter
# --- Main Interpreter ---
def execute_function_call(func_obj: Callable, arg_tokens: List[str], local_vars: Dict) -> Any:
"""Helper to parse args and call math/string/user functions."""
if not callable(func_obj):
print(f"Error: Attempted to call non-function value.")
return None # Represent nil
try:
# Parse arguments using the current scope
parsed_args = parse_arguments(arg_tokens, 0, local_vars)
# Call the function with parsed arguments
result = func_obj(*parsed_args)
return result
except TypeError as e:
# Handle wrong number of arguments or types
# func_name = getattr(func_obj, '__name__', 'unknown') # Get name if possible
print(f"TypeError during function call: {e}. Check arguments.")
return None
except Exception as e:
print(f"Error during function call execution: {e}")
return None
# --- Table Library Functions ---
# Dictionary to hold our table implementations
mnm_table_lib = {}
def _mnm_table_clear(tbl: Any):
"""Removes all keys/values from a table."""
if isinstance(tbl, dict):
tbl.clear()
elif isinstance(tbl, list):
tbl.clear()
else:
print("Error: table.clear requires a table (list/dict).")
def _mnm_table_clone(tbl: Any):
"""Creates a shallow copy of a table."""
if isinstance(tbl, dict):
return tbl.copy()
elif isinstance(tbl, list):
return tbl.copy()
else:
print("Error: table.clone requires a table (list/dict).")
return None # nil
def _mnm_table_concat(tbl: list, sep: str = "", i: int = 1, j: Optional[int] = None):
"""Concatenates list elements into a string."""
if not isinstance(tbl, list):
print("Error: table.concat requires an array (list).")
return ""
if not isinstance(sep, str): sep = str(sep)
tbl_len = len(tbl)
if j is None: j = tbl_len # Default j is length of table
# Convert 1-based Lua indices to 0-based Python slice indices
py_i = _lua_to_py_index(i, tbl_len)
py_j = _lua_to_py_index(j, tbl_len)
# Handle invalid or out-of-bounds indices gracefully
if py_i is None: py_i = 0
if py_j is None: py_j = tbl_len - 1 # Last valid index
# Ensure start index is not past end index
if py_i > py_j:
return "" # Empty string if range is invalid
# Python slice end index is exclusive
py_j_exclusive = py_j + 1
# Slice and join
elements_to_join = [str(x) for x in tbl[py_i:py_j_exclusive]]
return sep.join(elements_to_join)
def _mnm_table_create(count: int, value: Any = None):
"""Creates a list pre-filled with a value."""
if not isinstance(count, int) or count < 0:
print("Error: table.create requires a non-negative integer count.")
return []
# Create a list with 'count' copies of 'value'
# Need deepcopy if value is mutable? Roblox spec implies shallow copies are okay.
return [copy.copy(value) for _ in range(count)] # Use copy to avoid aliasing issues with mutable values
def _mnm_table_find(haystack: list, needle: Any, init: int = 1):
"""Finds the first index of a value in a list."""
if not isinstance(haystack, list):
print("Error: table.find requires an array (list) as the first argument.")
return None # nil
haystack_len = len(haystack)
py_init = _lua_to_py_index(init, haystack_len)
if py_init is None: py_init = 0 # Default start is 0 in Python if 1 fails
if py_init >= haystack_len: return None # Cannot start search past the end
try:
# Search from the calculated Python index
found_py_index = haystack.index(needle, py_init)
# Convert back to 1-based Lua index
return found_py_index + 1
except ValueError:
# Value not found
return None # nil
# freeze / isfrozen omitted
def _mnm_table_insert(tbl: list, *args):
"""Inserts value at position or appends."""
if not isinstance(tbl, list):
print("Error: table.insert requires an array (list).")
return
if len(args) == 1:
# table.insert(tbl, value) -> append
value = args[0]
tbl.append(value)
elif len(args) == 2:
# table.insert(tbl, pos, value) -> insert at pos
pos, value = args
if not isinstance(pos, int):
print("Error: table.insert position must be an integer.")
return
tbl_len = len(tbl)
# Convert 1-based pos. Allow pos up to len+1 (for appending via insert)
if pos > 0: py_pos = pos - 1
elif pos == 0: py_pos = 0 # Allow 0 like Lua for insert at beginning? Lua usually errors. Let's stick to 1..len+1
# Allow pos up to len + 1 (inserts at end)
elif pos >= -(tbl_len + 1) : py_pos = tbl_len + pos + 1
else:
print(f"Error: table.insert position {pos} out of bounds.")
return
# Clamp position for Python's insert
if py_pos < 0: py_pos = 0
if py_pos > tbl_len: py_pos = tbl_len # insert at len appends
tbl.insert(py_pos, value)
else:
print("Error: table.insert takes 2 or 3 arguments.")
def _mnm_table_maxn(tbl: Any):
"""Returns the largest positive integer key."""
max_n = 0
if isinstance(tbl, dict):
for k in tbl.keys():
if isinstance(k, int) and k > max_n:
max_n = k
elif isinstance(tbl, list):
# For lists, the highest index is len - 1, so highest key is len
max_n = len(tbl) # Lua's maxn behaviour for arrays
return max_n
def _mnm_table_move(src: list, a: int, b: int, t: int, dst: Optional[list] = None):
"""Copies elements from src[a..b] to dst[t...]."""
if dst is None: dst = src # Default destination is source table
if not isinstance(src, list) or not isinstance(dst, list):
print("Error: table.move requires arrays (lists).")
return None # Or dst? Lua returns dst.
src_len = len(src)
dst_len = len(dst)
# Convert 1-based Lua indices to 0-based Python indices/slices
py_a = _lua_to_py_index(a, src_len)
py_b = _lua_to_py_index(b, src_len)
py_t = _lua_to_py_index(t, dst_len) # Target start index in dst
# Validate indices
if py_a is None or py_b is None or py_t is None:
print("Error: Invalid index in table.move.")
return dst # Return original dst on error
if py_a > py_b: return dst # Nothing to move if start > end
# Slice end index is exclusive
py_b_exclusive = py_b + 1
# Extract elements to move (create copy)
elements_to_move = src[py_a:py_b_exclusive]
num_to_move = len(elements_to_move)
# Ensure destination list is large enough, pad with None (nil) if needed
required_dst_len = py_t + num_to_move
if required_dst_len > dst_len:
dst.extend([None] * (required_dst_len - dst_len))
# Place elements into destination
# Handle potential overlap if src and dst are the same list
# If src is dst, doing this slice assignment handles overlap correctly
dst[py_t : py_t + num_to_move] = elements_to_move
return dst
def _mnm_table_pack(*args):
"""Packs arguments into a list with field 'n'."""
# In Lua, this creates a table like { [1]=arg1, [2]=arg2, ..., n=num_args }
# We'll simulate with a list for the array part. Adding 'n' is tricky if using lists.
# Let's just return the list part for now.
return list(args)
def _mnm_table_remove(tbl: list, pos: Optional[int] = None):
"""Removes element at pos (default last) and returns it."""
if not isinstance(tbl, list):
print("Error: table.remove requires an array (list).")
return None # nil
tbl_len = len(tbl)
if tbl_len == 0: return None # Nothing to remove
if pos is None:
py_pos = tbl_len - 1 # Default to last element
else:
if not isinstance(pos, int):
print("Error: table.remove position must be an integer.")
return None
py_pos = _lua_to_py_index(pos, tbl_len)
# Validate python index
if py_pos is None or not (0 <= py_pos < tbl_len):
print(f"Error: table.remove position {pos} out of bounds.")
return None # nil
return tbl.pop(py_pos)
def _mnm_table_sort(tbl: list, comp: Optional[Callable] = None):
"""Sorts a list in-place. Custom comparator not supported yet."""
if not isinstance(tbl, list):
print("Error: table.sort requires an array (list).")
return
if comp is not None:
print("Warning: table.sort custom comparator function is not supported. Using default comparison.")
# Future: Implement calling the MNM function 'comp' via interpreter
# Requires complex callback mechanism.
try:
# Sort in-place using Python's default sort (handles mixed types with errors)
tbl.sort()
except TypeError as e:
print(f"Error during table.sort (mixed types?): {e}")
except Exception as e:
print(f"Error during table.sort: {e}")
def _mnm_table_unpack(tbl: list, i: int = 1, j: Optional[int] = None):
"""Returns elements from list i to j as a tuple."""
if not isinstance(tbl, list):
print("Error: table.unpack requires an array (list).")
return () # Empty tuple
tbl_len = len(tbl)
if j is None: j = tbl_len # Default j is length of table
py_i, py_j_exclusive = _get_slice_indices(tbl_len, i, j)
if py_i is None or py_j_exclusive is None or py_i >= py_j_exclusive:
return () # Empty if range invalid
return tuple(tbl[py_i:py_j_exclusive]) # Return as tuple (for multiple returns simulation)
# Populate mnm_table_lib
mnm_table_lib['clear'] = _mnm_table_clear
mnm_table_lib['clone'] = _mnm_table_clone
mnm_table_lib['concat'] = _mnm_table_concat
mnm_table_lib['create'] = _mnm_table_create
mnm_table_lib['find'] = _mnm_table_find
mnm_table_lib['insert'] = _mnm_table_insert
mnm_table_lib['maxn'] = _mnm_table_maxn
mnm_table_lib['move'] = _mnm_table_move
mnm_table_lib['pack'] = _mnm_table_pack
mnm_table_lib['remove'] = _mnm_table_remove
mnm_table_lib['sort'] = _mnm_table_sort
mnm_table_lib['unpack'] = _mnm_table_unpack
mnm_os_lib = {}
def _mnm_os_clock():
"""Returns high-resolution time in seconds since arbitrary point."""
return time.perf_counter()
def _mnm_os_difftime(t2=None, t1=None):
"""Returns the difference in seconds between two timestamps."""
if isinstance(t2, (int, float)) and isinstance(t1, (int, float)):
return float(t2 - t1)
else:
print("Error: os.difftime requires two numbers.")
return 0.0
def _mnm_os_time(time_table: Optional[Dict]=None):
"""
Returns current time as seconds since epoch, or converts a table to seconds since epoch.
Note: Table conversion assumes local timezone unless !*t format implies UTC.
"""
if time_table is None:
# Return current UTC time as seconds since epoch
return time.time()
elif isinstance(time_table, dict):
# Convert dict to time tuple for mktime
# Lua table fields: year, month, day, hour, min, sec, isdst, yday, wday
# Python struct_time fields (index): tm_year(0), tm_mon(1), tm_mday(2), tm_hour(3), tm_min(4), tm_sec(5), tm_wday(6), tm_yday(7), tm_isdst(8)
try:
# Get current time tuple as defaults
now_struct = time.localtime()
tm_year = int(time_table.get('year', now_struct.tm_year))
tm_mon = int(time_table.get('month', now_struct.tm_mon))
tm_mday = int(time_table.get('day', now_struct.tm_mday))
# Hour/min/sec default to 12:00:00 in standard Lua if not provided, but let's default to 0 for simplicity if missing entirely
tm_hour = int(time_table.get('hour', 0))
tm_min = int(time_table.get('min', 0))
tm_sec = int(time_table.get('sec', 0))
# isdst is tricky, -1 tells mktime to figure it out
tm_isdst = int(time_table.get('isdst', -1))
# Create the time tuple (wday and yday are ignored by mktime)
time_tuple = (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, 0, 0, tm_isdst)
return time.mktime(time_tuple)
except (ValueError, TypeError, OverflowError) as e:
print(f"Error converting table to time in os.time: {e}")
return None # Represent nil
else:
print("Error: os.time requires no arguments or a table argument.")
return None # Represent nil
def _mnm_os_date(format_string: str = "%Y-%m-%d %H:%M:%S", timestamp: Optional[float] = None):
"""
Formats the date/time according to format_string.
Handles special formats '*t' (local time table) and '!*t' (UTC time table).
Default format is now '%Y-%m-%d %H:%M:%S' for consistency.
"""
if not isinstance(format_string, str):
format_string = str(format_string)
try:
# Determine if we need local time or UTC time struct
use_utc = format_string.startswith("!")
if use_utc:
format_string = format_string[1:] # Remove '!'
# Get the time struct
if timestamp is None:
time_struct = time.gmtime() if use_utc else time.localtime()
else:
if not isinstance(timestamp, (int, float)):
print("Error: os.date timestamp argument must be a number.")
return None # nil
time_struct = time.gmtime(timestamp) if use_utc else time.localtime(timestamp)
# Handle special format '*t' -> return table/dict
if format_string == "*t":
date_dict = {
'year': time_struct.tm_year,
'month': time_struct.tm_mon,
'day': time_struct.tm_mday,
'hour': time_struct.tm_hour,
'min': time_struct.tm_min,
'sec': time_struct.tm_sec,
'wday': (time_struct.tm_wday + 1) % 7 + 1, # Lua: Sunday=1, Sat=7; Python: Mon=0, Sun=6
'yday': time_struct.tm_yday,
'isdst': time_struct.tm_isdst # Python bool/int, Lua bool
}
return date_dict
else:
# Use strftime for standard formatting
return time.strftime(format_string, time_struct)
except (ValueError, TypeError) as e:
print(f"Error in os.date formatting: {e}")
return None # nil
except Exception as e:
print(f"Unexpected error in os.date: {e}")
return None # nil
# Populate mnm_os_lib
mnm_os_lib['clock'] = _mnm_os_clock
mnm_os_lib['date'] = _mnm_os_date
mnm_os_lib['difftime'] = _mnm_os_difftime
mnm_os_lib['time'] = _mnm_os_time
def MNM2(code, local_vars=None, is_block_execution=False):
"""
Interprets MNM code with basic Roblox libs, control flow.
"""
if local_vars is None:
local_vars = {}
# --- Initialize Global Libraries ---
local_vars['math'] = mnm_math_lib
local_vars['string'] = mnm_string_lib
local_vars['os'] = mnm_os_lib # <--- Add this line
local_vars['table'] = mnm_table_lib # <--- Add this line
# Seed random generator once at start? Or rely on default seeding?
# math.randomseed(os.urandom(8)) # Example seeding
lines = code if is_block_execution else code.strip().split("\n")
current_line_index = 0
while current_line_index < len(lines):
line_number = current_line_index
line = lines[current_line_index].strip()
next_line_index = current_line_index + 1
if not line or line.startswith('--'):
current_line_index = next_line_index
continue
try:
tokens = shlex.split(line)
except ValueError as e:
print(f"Error tokenizing line {line_number + 1}: '{line}' - {e}")
current_line_index = next_line_index
continue
if not tokens:
current_line_index = next_line_index
continue
command_token = tokens[0]
command_lower = command_token.lower()
# --- Local Variable/Function Declaration ---
if command_lower == "local":
# ... (previous local handling - check if find_block_structure is needed) ...
# Use the refined 'local function' logic from previous step that uses find_block_structure
if len(tokens) >= 3 and tokens[1].lower() == "function":
func_name_raw = tokens[2]
function_name = func_name_raw[:-2] if func_name_raw.endswith("()") else func_name_raw
# Find block structure properly to get end line
# --> Re-integrate the robust find_block_structure logic here <--
# Find end line index
func_end_index = -1
nesting = 1
for idx in range(current_line_index + 1, len(lines)):
l_strip = lines[idx].strip().lower()
if l_strip in ["if", "while", "function", "for", "do"]: nesting += 1
if l_strip == 'end': nesting -=1
if nesting == 0: func_end_index = idx; break
if func_end_index != -1:
function_code_lines = lines[current_line_index + 1 : func_end_index]
function_body_code = "\n".join(function_code_lines)
def create_lambda(body, parent_vars): return lambda *args: MNM2(body.split("\n"), parent_vars.copy(), is_block_execution=True) # Handle args? No simple way yet
defined_function = create_lambda(function_body_code, local_vars)
local_vars[function_name] = defined_function
next_line_index = func_end_index + 1
else: print(f"Syntax Error: Missing 'end' for function '{function_name}'."); next_line_index = len(lines)
# Handle local variable assignment (rest is same as before)
elif len(tokens) >= 4 and tokens[2] == "=":
variable_name = tokens[1]
# Check if RHS is a function call
rhs_tokens = tokens[3:]
if len(rhs_tokens) > 0:
potential_func = parse_value(rhs_tokens[0], local_vars)
if callable(potential_func): # Is it math.abs, string.len etc?
call_result = execute_function_call(potential_func, rhs_tokens[1:], local_vars)
local_vars[variable_name] = call_result
else: # Regular assignment
value_token = " ".join(rhs_tokens)
local_vars[variable_name] = parse_value(value_token, local_vars)
else: # local var = (nothing) -> nil
local_vars[variable_name] = None
elif len(tokens) == 2: local_vars[tokens[1]] = None
else: print(f"Syntax Error: Invalid 'local' statement: {' '.join(tokens)}")
# --- Print Command ---
elif command_lower == "print":
arguments_to_print = []
i = 1
while i < len(tokens):
arg_token = tokens[i]
if arg_token.startswith('"') and arg_token.endswith('"'):
arguments_to_print.append(arg_token[1:-1])
i += 1
elif arg_token.startswith("'") and arg_token.endswith("'"):
arguments_to_print.append(arg_token[1:-1])
i += 1
elif '(' in arg_token and arg_token.endswith(')'):
# Basic handling for function-like arguments in print
func_call = arg_token[:-1].split('(')
func_name = func_call[0]
if len(func_call) > 1:
args_str = func_call[1]
# Simple split by comma for arguments inside parentheses
inner_args = [a.strip() for a in args_str.split(',')]
evaluated_args = [parse_value(arg, local_vars) for arg in inner_args]
potential_func = parse_value(func_name, local_vars)
if callable(potential_func):
call_result = execute_function_call(potential_func, evaluated_args, local_vars)
if call_result is None: arguments_to_print.append("nil")
elif isinstance(call_result, bool): arguments_to_print.append(str(call_result).lower())
elif isinstance(call_result, tuple):
arguments_to_print.extend(map(str, call_result))
else:
arguments_to_print.append(call_result)
else:
arguments_to_print.append(arg_token) # Treat as literal if not a function
else:
arguments_to_print.append(func_name) # Just the function name
i += 1
else:
parsed_arg = parse_value(arg_token, local_vars)
if parsed_arg is None: arguments_to_print.append("nil")
elif isinstance(parsed_arg, bool): arguments_to_print.append(str(parsed_arg).lower())
else: arguments_to_print.append(parsed_arg)
i += 1
print(*arguments_to_print)
# --- If Statement ---
elif command_lower == "if":
# --> Use the robust find_block_structure logic here <--
# Dummy implementation for now: