From b71ae7eceabd95737569ed2ccb6cc29dd8f8a433 Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Sun, 3 Jan 2021 21:48:38 +0100 Subject: [PATCH] implemented for loops --- .vscode/launch.json | 2 +- interpreter/NassiShneidermann.py | 4 ++-- interpreter/interpret_source.py | 41 ++++++++++++++++++++++++++++---- res/input/input.java | 3 ++- 4 files changed, 42 insertions(+), 8 deletions(-) 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/NassiShneidermann.py b/interpreter/NassiShneidermann.py index cc4e973..ce966a1 100644 --- a/interpreter/NassiShneidermann.py +++ b/interpreter/NassiShneidermann.py @@ -1,5 +1,5 @@ from interpreter.interpret_source import Function_scope -from typing import Dict, List +from typing import Dict, List, Optional import logging from enum import IntEnum import os.path @@ -69,7 +69,7 @@ class NassiShneidermanDiagram: logging.error(f"Failed to save image {filepath}. Unknown error") 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.reset_tags(itp_custom_tags) self.function_scopes = itp.load_instruction_scopes() \ No newline at end of file diff --git a/interpreter/interpret_source.py b/interpreter/interpret_source.py index 40c4140..86b0690 100644 --- a/interpreter/interpret_source.py +++ b/interpreter/interpret_source.py @@ -1,6 +1,6 @@ import logging 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 draw.Iinstruction import * @@ -167,7 +167,37 @@ class JavaInterpreter: return while_instruction_back(instruction_txt, child_instructions), idx 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): 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} 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("): logging.debug("Found while construct in line: %i", idx+1) return self._handle_while(line, idx) @@ -213,7 +243,10 @@ class JavaInterpreter: break instruction, i = self._handle_instruction(line, i) - scope.append(instruction) + if isinstance(instruction, List): + scope.extend(instruction) + else: + scope.append(instruction) i += 1 return scope, i diff --git a/res/input/input.java b/res/input/input.java index 5527716..48845b2 100644 --- a/res/input/input.java +++ b/res/input/input.java @@ -95,8 +95,9 @@ public class Rover extends Actor nachricht("Ich muss mindestens drei Zeilen fahren! :("); return; } + int i = 1; fahreZeileDreheHoch(); - for(int i = 1; i < anzahlZeilen-1; i++) { + for(; i < anzahlZeilen-1; i++) { fahreZeileDreheRunter(true); } fahreZeileDreheRunter(false);