This commit is contained in:
oleting
2020-12-26 15:53:32 +01:00
3 changed files with 14 additions and 21 deletions

2
.vscode/launch.json vendored
View File

@@ -8,7 +8,7 @@
"name": "Python: Aktuelle Datei", "name": "Python: Aktuelle Datei",
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"program": "run.py", "program": "debug.py",
"console": "integratedTerminal" "console": "integratedTerminal"
} }
] ]

View File

@@ -9,11 +9,14 @@ COMMENT_REGEX = r"""^//|^#|^COMMENT|^--"""
class JavaSyntaxError(Exception): class JavaSyntaxError(Exception):
pass pass
class ScopeNotFoundException(Exception):
pass
def load_src(filepath: str) -> List[str]: def load_src(filepath: str) -> List[str]:
lines: List[str] = [] lines: List[str] = []
brace_open_count, brace_closed_count = 0,0 brace_open_count, brace_closed_count = 0,0
try: try:
with open(filepath) as file: with open(filepath, encoding="utf-8") as file:
for _line in file: for _line in file:
line = _line.strip().replace(' ', '') line = _line.strip().replace(' ', '')
if line and not re.match(COMMENT_REGEX, line): if line and not re.match(COMMENT_REGEX, line):
@@ -36,7 +39,8 @@ def get_scope_start_offset(src: List[str], start_idx: int) -> int:
line = src[i] line = src[i]
if line.__contains__("{"): if line.__contains__("{"):
return i - start_idx + 1 return i - start_idx + 1
raise Exception("Unable to find scope start. Is the program ill-formed?") i += 1
raise ScopeNotFoundException("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: List[Iinstruction] = [] outer_scope: List[Iinstruction] = []
@@ -48,23 +52,17 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
return outer_scope, 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 while contruct 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+2) brace_offset = get_scope_start_offset(src, i)
child_instructions, i = get_instructions_in_scope(src, i+brace_offset)
outer_scope.append(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
@@ -86,7 +84,8 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
#}while(...); #}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) brace_offset = get_scope_start_offset(src, i)
child_instructions, i = get_instructions_in_scope(src, i+brace_offset)
end_line = src[i] end_line = src[i]
bracket_idx = end_line.rindex(");") bracket_idx = end_line.rindex(");")
@@ -102,11 +101,3 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[
raise # rethrow raise # rethrow
i += 1 i += 1
return outer_scope, 0 return outer_scope, 0
if __name__ == "__main__":
"""debuging"""
logging.basicConfig(level=logging.DEBUG)
lines = load_src("res/input/input.java")
global_scope = get_instructions_in_scope(lines)
print(global_scope)

View File

@@ -1,6 +1,8 @@
fahre1(); fahre1();
fahre2(); fahre2();
while(shouldNiet()) while(shouldNiet())
{ {
niet4(); niet4();
niet5(); niet5();