diff --git a/.vscode/launch.json b/.vscode/launch.json index 1ded7e8..e80f071 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Python: Aktuelle Datei", "type": "python", "request": "launch", - "program": "run.py", + "program": "debug.py", "console": "integratedTerminal" } ] diff --git a/interpreter/interpret_source.py b/interpreter/interpret_source.py index 5c9b16f..e64e0a7 100644 --- a/interpreter/interpret_source.py +++ b/interpreter/interpret_source.py @@ -6,8 +6,9 @@ from typing import List, Text, Tuple from draw.Iinstruction import * COMMENT_REGEX = r"""^//|^#|^COMMENT|^--""" +REMOVE_KEYWORDS = [' ', "public", "private", "void"] + WHILE_TAG = "solange " #german for 'while'. Change this depending on your language -REMOVE_KEYWORDS = (' ', "public", "private", "void") REPLACE = dict((re.escape(k), '') for k in REMOVE_KEYWORDS) remove_pattern = re.compile("|".join(REPLACE.keys())) @@ -42,21 +43,25 @@ def load_src(filepath: str) -> List[str]: return lines -def get_scope_start_offset(src: List[str], start_idx: int) -> int: +def get_next_occurence_of(src: List[str], start_idx:int, tag:str) -> int: i = start_idx while i < len(src): - line = src[i] - if line.__contains__("{"): - return i - start_idx + 1 + if src[i].__contains__(tag): + return i i += 1 + +def get_scope_start_offset(src: List[str], start_idx: int) -> int: + i = get_next_occurence_of(src, start_idx, '{') + if i != len(src): + return 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]: outer_scope: List[Iinstruction] = [] i = start_idx while i < len(src): line = src[i] - logging.debug(line) try: if line.__contains__('}'): #We exited this scope, return it return outer_scope, i @@ -82,10 +87,16 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[ true_instructions, i = get_instructions_in_scope(src, i+brace_offset) false_instructions = None - if src[i].__contains__("else"): #if there is an else statement, check it + #if there is an else statement, check it + if src[i].__contains__("else"): logging.debug("found else construct in line: %i", i+1) brace_offset = get_scope_start_offset(src, i) - false_instructions, i = get_instructions_in_scope(src, i+2) + false_instructions, i = get_instructions_in_scope(src, i+brace_offset) + elif src[i+1].__contains__("else"): + logging.debug("found else construct in line: %i", i+2) + brace_offset = get_scope_start_offset(src, i+1) + false_instructions, i = get_instructions_in_scope(src, i+1+brace_offset) + outer_scope.append(if_instruction(instruction_txt, true_instructions, false_instructions)) @@ -105,7 +116,7 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[ logging.debug("Found generic instruction in line: %i", i+1) outer_scope.append(generic_instruction(line)) except: - logging.error("Encountered error in line: %i", i) + logging.error("Encountered error in line: %i", i+1) raise # rethrow i += 1 return outer_scope, 0 \ No newline at end of file diff --git a/res/input/input.java b/res/input/input.java index bc08f2c..e4c4f8a 100644 --- a/res/input/input.java +++ b/res/input/input.java @@ -1,65 +1,67 @@ -// fahre1(); -// fahre2(); -// while(shouldNiet()) +// // fahre1(); +// // fahre2(); +// // while(shouldNiet()) +// // { +// // niet4(); +// // niet5(); +// // if(if6) +// // { +// // niet7(); +// // niet8(); +// // } else +// // { +// // niet10(); +// // niet11(); +// // } +// // niet13(); +// // } +// // niet15(); +// // niet16(); + +// // do{ +// // niet21(); +// // niet22(); +// // }while(bool23); + +// //the following code was heavily distorted in order to test the interpreter. Sorry to everyone who has to read this + +// void + +// private void + + + +// drehe("links"); +// while(huegelVorhanden("rechts")) // { -// niet4(); -// niet5(); -// if(if6) -// { -// niet7(); -// niet8(); -// } else -// { -// niet10(); -// niet11(); -// } -// niet13(); +// gesteinSammeln(); +// fahre(); + + + + + // } -// niet15(); -// niet16(); - -// do{ -// niet21(); -// niet22(); -// }while(bool23); - -//the following code was heavily distorted in order to test the interpreter. Sorry to everyone who has to read this - -void - -private void - -drehe("links"); -while(huegelVorhanden("rechts")) -{ - gesteinSammeln(); - fahre(); - - - - - -} -drehe("rechts"); +// drehe("rechts"); - gesteinSammeln(); +// gesteinSammeln(); -fahre(); +// fahre(); - while(huegelVorhanden("rechts")) -{ - gesteinSammeln(); - fahre(); -} -drehe("rechts"); +// while(huegelVorhanden("rechts")) +// { +// gesteinSammeln(); +// fahre(); +// } +// drehe("rechts"); @@ -67,35 +69,36 @@ drehe("rechts"); - gesteinSammeln(); -fahre(); +// gesteinSammeln(); +// fahre(); - while(huegelVorhanden("rechts")) -{ - gesteinSammeln(); - if(!huegelVorhanden("vorne")) +// while(huegelVorhanden("rechts")) +// { +// gesteinSammeln(); +// if(!huegelVorhanden("vorne")) - { +// { - fahre(); - } else +// fahre(); +// } +// else - { - fahre(); - } -} +// { +// fahre(); +// } +// } @@ -104,21 +107,32 @@ fahre(); - drehe("rechts"); +// drehe("rechts"); -do +// do -{ - insideDoWhile(); - insideDoWhile(); +// { +// insideDoWhile(); +// insideDoWhile(); - insideDoWhile(); +// insideDoWhile(); - insideDoWhile(); -}while( !huegelVorhanden( "vorne" ) ) ; \ No newline at end of file +// insideDoWhile(); +// } +// while( !huegelVorhanden( "vorne" ) ) ; + +if(bool) { + true_case; + true_case; +} +else +{ + false_case; + false_case; +} \ No newline at end of file