From 1779d5ab3e6e7436d32e317e4a2a74e7ee36d403 Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Sat, 26 Dec 2020 19:42:38 +0100 Subject: [PATCH] improved interpreter --- interpreter/interpret_source.py | 13 +++++++++++-- res/input/input.java | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/interpreter/interpret_source.py b/interpreter/interpret_source.py index c679822..5c9b16f 100644 --- a/interpreter/interpret_source.py +++ b/interpreter/interpret_source.py @@ -1,11 +1,16 @@ import logging +from os import remove import re -from typing import List, Tuple +from typing import List, Text, Tuple from draw.Iinstruction import * COMMENT_REGEX = r"""^//|^#|^COMMENT|^--""" 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())) class JavaSyntaxError(Exception): pass @@ -13,13 +18,16 @@ class JavaSyntaxError(Exception): class ScopeNotFoundException(Exception): pass +def replace_all_tags(org: str): + return remove_pattern.sub(lambda m: REPLACE[re.escape(m.group(0))], org) + def load_src(filepath: str) -> List[str]: lines: List[str] = [] brace_open_count, brace_closed_count = 0,0 try: with open(filepath, encoding="utf-8") as file: for _line in file: - line = _line.strip().replace(' ', '') + line = replace_all_tags(_line.strip()) if line and not re.match(COMMENT_REGEX, line): lines.append(line) if line.__contains__('{'): @@ -48,6 +56,7 @@ def get_instructions_in_scope(src: List[str], start_idx: int = 0) -> Tuple[List[ 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 diff --git a/res/input/input.java b/res/input/input.java index 8aeb5ff..bc08f2c 100644 --- a/res/input/input.java +++ b/res/input/input.java @@ -27,6 +27,10 @@ //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")) {