implemented support for else if constructs

This commit is contained in:
weckyy702
2021-01-04 16:43:21 +01:00
parent f9e0d7f4b0
commit 8ba7936311
2 changed files with 29 additions and 22 deletions

View File

@@ -10,9 +10,9 @@ if sys.version_info < (3, 9):
raise Exception("Unsupported Python version! Pyton 3.9 is required") raise Exception("Unsupported Python version! Pyton 3.9 is required")
logging.warning("""Because the Interpreter is still WIP, some Java language features are not supported. These include: logging.warning("""Because the Interpreter is still WIP, some Java language features are not supported. These include:
*else if statements
*foreach loops (will throw JavaSyntaxError) *foreach loops (will throw JavaSyntaxError)
*Constructors (will be ignored) *constructors (will be ignored)
*switch statements
Please remove these features from the source code as they will result in incorrect behaviour""") Please remove these features from the source code as they will result in incorrect behaviour""")
class Function_scope(Iterable): class Function_scope(Iterable):
@@ -120,34 +120,44 @@ class JavaInterpreter:
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]
brace_offset = self._get_scope_start_offset(idx) child_instructions, idx = self._get_subscope(idx)
child_instructions, idx = self._get_instructions_in_scope(idx+brace_offset)
return while_instruction_front(("while" + instruction_txt), child_instructions), idx return while_instruction_front(("while" + instruction_txt), child_instructions), idx
def _handle_else(self, idx: int): def _get_subscope(self, idx: int):
brace_offset = self._get_scope_start_offset(idx) brace_offset = self._get_scope_start_offset(idx)
return self._get_instructions_in_scope(idx+brace_offset) return self._get_instructions_in_scope(idx+brace_offset)
def _get_else_scope(self, idx:int): def _get_else_scope(self, idx:int):
instructions = None instructions = None
if self._check_line_start(idx, "}else"): if self._check_line_start(idx, "}else"):
logging.debug("found else construct in line: %i", idx+1) if self._check_src(idx, "if("):
instructions, idx = self._handle_else(idx) logging.debug("found else if construct in line: %i", idx+1)
line = self._lines[idx][5:]
instructions, idx = self._handle_if(line, idx)
instructions = [instructions]
else:
logging.debug("found else construct in line: %i", idx+1)
instructions, idx = self._get_subscope(idx)
elif self._check_line_start(idx+1, "else"): elif self._check_line_start(idx+1, "else"):
logging.debug("found else construct in line: %i", idx+2) if self._check_src(idx+1, "if("):
instructions, idx = self._handle_else(idx+1) logging.debug("found else if construct in line: %i", idx+2)
return instructions line = self._lines[idx+1][4:]
instructions, idx = self._handle_if(line, idx+1)
instructions = [instructions]
else:
logging.debug("found else construct in line: %i", idx+2)
instructions, idx = self._get_subscope(idx+1)
return instructions, idx
def _handle_if(self, line: str, idx: int): def _handle_if(self, line: str, idx: int):
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]
brace_offset = self._get_scope_start_offset(idx) true_instructions, idx = self._get_subscope(idx)
true_instructions, idx = self._get_instructions_in_scope(idx+brace_offset)
false_instructions = self._get_else_scope(idx) false_instructions, idx = self._get_else_scope(idx)
return if_instruction(instruction_txt, true_instructions, false_instructions), idx return if_instruction(instruction_txt, true_instructions, false_instructions), idx
@@ -283,7 +293,6 @@ class JavaInterpreter:
return Function_scope(child_instructions, name, rtype, args) return Function_scope(child_instructions, name, rtype, args)
def _get_function_scopes(self) -> List[Function_scope]: def _get_function_scopes(self) -> List[Function_scope]:
matches = self._function_pattern.finditer(self.src) matches = self._function_pattern.finditer(self.src)
return list(map(self._get_function_scope, matches)) return list(map(self._get_function_scope, matches))

View File

@@ -28,14 +28,12 @@ public class Rover extends Actor
if(richtung.equals("Hoch")) { if(richtung.equals("Hoch")) {
pri = "links"; pri = "links";
sec = "rechts"; sec = "rechts";
} else { } else if (richtung.equals("Runter")) {
if (richtung.equals("Runter")) { pri = "rechts";
pri = "rechts"; sec = "links";
sec = "links"; } else {
} else { nachricht("JUNGE DU SPAST!");
nachricht("JUNGE DU SPAST!"); return;
return;
}
} }
drehe(pri); drehe(pri);
fahre(); fahre();