implemented for loops

This commit is contained in:
weckyy702
2021-01-03 21:48:38 +01:00
parent 04f3efed8e
commit b71ae7ecea
4 changed files with 42 additions and 8 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

@@ -1,5 +1,5 @@
from interpreter.interpret_source import Function_scope from interpreter.interpret_source import Function_scope
from typing import Dict, List from typing import Dict, List, Optional
import logging import logging
from enum import IntEnum from enum import IntEnum
import os.path import os.path
@@ -69,7 +69,7 @@ class NassiShneidermanDiagram:
logging.error(f"Failed to save image {filepath}. Unknown error") logging.error(f"Failed to save image {filepath}. Unknown error")
raise raise
def load_from_file(self, filepath:str, itp_custom_tags: Dict[str, List[str]]): def load_from_file(self, filepath:str, itp_custom_tags: Optional[Dict[str, List[str]]]):
itp = JavaInterpreter(filepath) itp = JavaInterpreter(filepath)
itp.reset_tags(itp_custom_tags) itp.reset_tags(itp_custom_tags)
self.function_scopes = itp.load_instruction_scopes() self.function_scopes = itp.load_instruction_scopes()

View File

@@ -1,6 +1,6 @@
import logging import logging
import re import re
from typing import Dict, List, Match, Tuple from typing import Dict, List, Match, Tuple, Union
from errors.custom import InterpreterException, JavaSyntaxError, ScopeNotFoundException from errors.custom import InterpreterException, JavaSyntaxError, ScopeNotFoundException
from draw.Iinstruction import * from draw.Iinstruction import *
@@ -167,7 +167,37 @@ class JavaInterpreter:
return while_instruction_back(instruction_txt, child_instructions), idx return while_instruction_back(instruction_txt, child_instructions), idx
def _handle_for(self, line: str, idx: int): def _handle_for(self, line: str, idx: int):
return generic_instruction(line), idx #line: for(type|name;condition;increment)
segments = line.split(";")
try:
var = segments[0][4:]
cond = segments[1]
inc = segments[2][:-2]
instructions = []
if cond == "": #did you know test expressions where optional and defaulted to true? Me neither
cond = "true"
if var != "":
variable_instruction = self._handle_variable(var, idx)[0]
instructions.append(variable_instruction)
brace_offset = self._get_scope_start_offset(idx)
child_instructions, idx = self._get_instructions_in_scope(idx+brace_offset)
if inc != "":
increment_instruction = generic_instruction(inc)
child_instructions.append(increment_instruction)
instructions.append(for_instruction("while " + cond, child_instructions))
return instructions, idx
except IndexError:
raise JavaSyntaxError("Ill-formed for loop construct!")
except:
raise
def _handle_variable(self, line: str, idx: int): def _handle_variable(self, line: str, idx: int):
groups = self._variable_pattern.match(line).groups() groups = self._variable_pattern.match(line).groups()
@@ -178,7 +208,7 @@ class JavaInterpreter:
return generic_instruction(f"declare variable '{var_name}' of type {var_type}"), idx return generic_instruction(f"declare variable '{var_name}' of type {var_type}"), idx
return generic_instruction(f"declare variable '{var_name}' of type {var_type} with value {var_value}"), idx return generic_instruction(f"declare variable '{var_name}' of type {var_type} with value {var_value}"), idx
def _handle_instruction(self, line: str, idx:int) -> Tuple[Iinstruction, int]: def _handle_instruction(self, line: str, idx:int) -> Tuple[Union[Iinstruction, List[Iinstruction]], int]:
if line.startswith("while("): if line.startswith("while("):
logging.debug("Found while construct in line: %i", idx+1) logging.debug("Found while construct in line: %i", idx+1)
return self._handle_while(line, idx) return self._handle_while(line, idx)
@@ -213,6 +243,9 @@ class JavaInterpreter:
break break
instruction, i = self._handle_instruction(line, i) instruction, i = self._handle_instruction(line, i)
if isinstance(instruction, List):
scope.extend(instruction)
else:
scope.append(instruction) scope.append(instruction)
i += 1 i += 1

View File

@@ -95,8 +95,9 @@ public class Rover extends Actor
nachricht("Ich muss mindestens drei Zeilen fahren! :("); nachricht("Ich muss mindestens drei Zeilen fahren! :(");
return; return;
} }
int i = 1;
fahreZeileDreheHoch(); fahreZeileDreheHoch();
for(int i = 1; i < anzahlZeilen-1; i++) { for(; i < anzahlZeilen-1; i++) {
fahreZeileDreheRunter(true); fahreZeileDreheRunter(true);
} }
fahreZeileDreheRunter(false); fahreZeileDreheRunter(false);