started cleaning source code interpretation
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,5 @@ res/output/
|
|||||||
res/input/
|
res/input/
|
||||||
.vscode/
|
.vscode/
|
||||||
*.pyc
|
*.pyc
|
||||||
*.png
|
*.png
|
||||||
|
debug.py
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
from typing import Iterable, List
|
from typing import Iterable, List
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from draw import code_to_image as cti
|
from draw import code_to_image as cti
|
||||||
|
|||||||
@@ -4,14 +4,10 @@ from typing import List, Tuple
|
|||||||
|
|
||||||
from draw.Iinstruction import *
|
from draw.Iinstruction import *
|
||||||
|
|
||||||
class Scope():
|
COMMENT_REGEX = r"""^//|^#|^COMMENT|^--"""
|
||||||
|
|
||||||
def __init__(self, enclosing_scope) -> None:
|
class JavaSyntaxError(Exception):
|
||||||
self.enclosing_scope = enclosing_scope
|
pass
|
||||||
self.contents: List = []
|
|
||||||
|
|
||||||
def add_instruction(self, instruction: Iinstruction) -> None:
|
|
||||||
self.contents.append(instruction)
|
|
||||||
|
|
||||||
def load_src(filepath: str) -> List[str]:
|
def load_src(filepath: str) -> List[str]:
|
||||||
lines: List[str] = []
|
lines: List[str] = []
|
||||||
@@ -20,68 +16,97 @@ def load_src(filepath: str) -> List[str]:
|
|||||||
with open(filepath) as file:
|
with open(filepath) as file:
|
||||||
for _line in file:
|
for _line in file:
|
||||||
line = _line.strip().replace(' ', '')
|
line = _line.strip().replace(' ', '')
|
||||||
if line and not re.match(r"""^//|^#|^COMMENT|^--""", line):
|
if line and not re.match(COMMENT_REGEX, line):
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
if line.__contains__('{'):
|
if line.__contains__('{'):
|
||||||
brace_open_count += 1
|
brace_open_count += 1
|
||||||
if line.__contains__('}'):
|
if line.__contains__('}'):
|
||||||
brace_closed_count += 1
|
brace_closed_count += 1
|
||||||
except:
|
except:
|
||||||
logging.error(f"Failed to open input file {filepath}!")
|
raise FileNotFoundError(f"File {filepath} was not found!")
|
||||||
|
|
||||||
if brace_open_count != brace_closed_count:
|
if brace_open_count != brace_closed_count:
|
||||||
raise Exception("Number of opened braces does not match number of closed ones. Program is illformed!")
|
raise JavaSyntaxError("Number of opened braces does not match number of closed ones. Program is ill-formed!")
|
||||||
|
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
def get_scope_start_offset(src: List[str], start_idx: int) -> int:
|
||||||
|
i = start_idx
|
||||||
|
while i < len(src):
|
||||||
|
line = src[i]
|
||||||
|
if line.__contains__("{"):
|
||||||
|
return i - start_idx + 1
|
||||||
|
raise Exception("Unable to find scope start. Is the program ill-formed?")
|
||||||
|
|
||||||
def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[Iinstruction], int]:
|
def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[Iinstruction], int]:
|
||||||
outer_scope = Scope(None)
|
outer_scope: List[Iinstruction] = []
|
||||||
i = start_idx
|
i = start_idx
|
||||||
while i < len(src):
|
while i < len(src):
|
||||||
line = src[i]
|
line = src[i]
|
||||||
try:
|
try:
|
||||||
if line.__contains__('}'): #We exited this scope, return it
|
if line.__contains__('}'): #We exited this scope, return it
|
||||||
return outer_scope.contents, i
|
return outer_scope, i
|
||||||
|
|
||||||
if line.startswith("while("):
|
if line.startswith("while("):
|
||||||
|
#construct:
|
||||||
|
#while(...)
|
||||||
|
#{
|
||||||
|
#...
|
||||||
|
#}
|
||||||
logging.debug("Found while instruction in line: %i", i+1)
|
logging.debug("Found while instruction in line: %i", i+1)
|
||||||
bracket_idx = line.rindex(')') # throws if the while is illformed
|
|
||||||
|
bracket_idx = line.rindex(')') # throws if while contruct is illformed
|
||||||
|
|
||||||
instruction_txt = line[6:bracket_idx]
|
instruction_txt = line[6:bracket_idx]
|
||||||
child_instructions, i = get_instructions_in_scope(src, i+1)
|
child_instructions, i = get_instructions_in_scope(src, i+2)
|
||||||
outer_scope.add_instruction(while_instruction_front(instruction_txt, child_instructions))
|
|
||||||
|
outer_scope.append(while_instruction_front(instruction_txt, child_instructions))
|
||||||
|
|
||||||
elif line.startswith("if("):
|
elif line.startswith("if("):
|
||||||
|
#line: if(...)
|
||||||
|
|
||||||
logging.debug("Found if instruction in line: %i", i+1)
|
logging.debug("Found if instruction in line: %i", i+1)
|
||||||
|
|
||||||
bracket_idx = line.rindex(')') # throws if the contruct is illformed
|
bracket_idx = line.rindex(')') # throws if the contruct is illformed
|
||||||
|
|
||||||
instruction_txt = line[3:bracket_idx]
|
instruction_txt = line[3:bracket_idx]
|
||||||
true_instructions, i = get_instructions_in_scope(src, i+2)
|
brace_offset = get_scope_start_offset(src, i)
|
||||||
|
true_instructions, i = get_instructions_in_scope(src, i+brace_offset)
|
||||||
|
|
||||||
false_instructions = None
|
false_instructions = None
|
||||||
if src[i].__contains__("else"): #if there is an else statement, check it
|
if src[i].__contains__("else"): #if there is an else statement, check it
|
||||||
false_instructions, i = get_instructions_in_scope(src, i+2)
|
false_instructions, i = get_instructions_in_scope(src, i+2)
|
||||||
outer_scope.add_instruction(if_instruction(instruction_txt, true_instructions, false_instructions))
|
|
||||||
|
outer_scope.append(if_instruction(instruction_txt, true_instructions, false_instructions))
|
||||||
|
|
||||||
elif line.startswith("do{"):
|
elif line.startswith("do{"):
|
||||||
|
#construct:
|
||||||
|
#do{
|
||||||
|
#...
|
||||||
|
#}while(...);
|
||||||
logging.debug("Found start of do-while instruction in line: %i", i)
|
logging.debug("Found start of do-while instruction in line: %i", i)
|
||||||
|
|
||||||
child_instructions, i = get_instructions_in_scope(src, i+1)
|
child_instructions, i = get_instructions_in_scope(src, i+1)
|
||||||
|
|
||||||
end_line = src[i]
|
end_line = src[i]
|
||||||
#line: }while(...);
|
|
||||||
bracket_idx = end_line.rindex(");")
|
bracket_idx = end_line.rindex(");")
|
||||||
instruction_txt = end_line[7: bracket_idx]
|
instruction_txt = end_line[7: bracket_idx]
|
||||||
outer_scope.add_instruction(while_instruction_back(instruction_txt, child_instructions))
|
|
||||||
|
outer_scope.append(while_instruction_back(instruction_txt, child_instructions))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.debug("Found generic instruction in line: %i", i+1)
|
logging.debug("Found generic instruction in line: %i", i+1)
|
||||||
outer_scope.add_instruction(generic_instruction(line))
|
outer_scope.append(generic_instruction(line))
|
||||||
except:
|
except:
|
||||||
logging.error("Encountered error in line: %i", i)
|
logging.error("Encountered error in line: %i", i)
|
||||||
raise # rethrow
|
raise # rethrow
|
||||||
i += 1
|
i += 1
|
||||||
return outer_scope.contents, 0
|
return outer_scope, 0
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
"""debuging"""
|
"""debuging"""
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
#inst = get_scoped_instructions("res/input/input.java")
|
|
||||||
lines = load_src("res/input/input.java")
|
lines = load_src("res/input/input.java")
|
||||||
global_scope = get_instructions_in_scope(lines)
|
global_scope = get_instructions_in_scope(lines)
|
||||||
print(global_scope)
|
print(global_scope)
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
fahre1();
|
fahre1();
|
||||||
fahre2();
|
fahre2();
|
||||||
while(shouldNiet()) {
|
while(shouldNiet())
|
||||||
|
{
|
||||||
niet4();
|
niet4();
|
||||||
niet5();
|
niet5();
|
||||||
if(if6)
|
if(if6)
|
||||||
|
|||||||
Reference in New Issue
Block a user