Files
BlooMooExperiments/original_code_parsing_reconstr.py
2026-03-05 21:23:58 +00:00

102 lines
2.8 KiB
Python

code = "{@STRING(\"TEST\", \"\");@IF(\"2+3*4'\"2+3*4\"&&1'1\",\"{@RETURN(\"TRUE\");}\",\"{@RETURN(\"FALSE\");}\");@RETURN(TEST);}"
def prepare_input(code: str) -> str:
# original used right and left instructions for striping code from braces (yeah, they are unnecessary)
code = code.strip()
if code[0] == '{':
code = code[1::]
if code[-1] == '}':
code = code[:-1]
code = code.strip() # yep, another strip to remove spaces after removing braces
return code
def initializeBehaviours(code: str) -> str:
code_lines = []
depth = 0
tmp_buffer = ""
for i, c in enumerate(code):
if c == '(':
depth += 1
tmp_buffer += c
continue
elif c == ')':
depth -= 1
tmp_buffer += c
continue
elif c == ';':
if depth == 0:
code_lines.append(tmp_buffer)
tmp_buffer = ""
continue
else:
tmp_buffer += c
continue
elif c == ' ':
continue
else:
tmp_buffer += c
return code_lines
def parse_line(line: str):
if line.endswith(';'):
line = line[:-1] # make sure to remove the trailing semicolon if it still exists
current_char = line[0]
left_paren_index = line.find('(')
if current_char == '@': # special instruction
# get the instruction name
if left_paren_index == -1:
left_paren_index = 1
instruction_name = line[1:left_paren_index]
# assign runner (not implemented here, just a placeholder)
# goto LABEL_40
elif current_char == "*":
close_bracket_index = line.find(']') + 1
caret_index = line.find('^', close_bracket_index)
if close_bracket_index >= left_paren_index:
caret_index = line.find('^')
else:
left_paren_index = line.find('(', caret_index)
target_name = line[1:caret_index]
if target_name[0] == "[":
target_name = target_name[1:-1] # remove the brackets
# object = new CMC_Expression
else:
# object = CMC_ObjectsContainer::getObject
pass
method_name = line[caret_index + 1:left_paren_index]
# goto LABEL_40
elif line.startswith("THIS"):
method_name = line[5:left_paren_index]
# goto LABEL_40
pass
if __name__ == '__main__':
prepared_code = prepare_input(code)
print("Prepared code:")
print(prepared_code)
print("\nCode lines:")
code_lines = initializeBehaviours(prepared_code)
for line in code_lines:
print(line)
for line in code_lines:
parse_line(line)