interpreter improvements and error handling in NSD image saving

This commit is contained in:
weckyy702
2020-12-30 14:45:54 +01:00
parent 5dc1f8e19d
commit d68976b55e
5 changed files with 277 additions and 20 deletions

View File

@@ -66,15 +66,20 @@ class NassiShneidermanDiagram:
filepath = self.check_conflicts(filepath, on_conflict)
if filepath is not None:
logging.info(f"Saving NSD to {filepath}...")
print(f"Saving... to {filepath}")
image_y_sz = self._get_image_height(i)
with NSD_writer(filepath, x_size, image_y_sz):
scope = self.function_scopes[i].contents
x, y = 0, 0
for instruction in scope:
x, y = instruction.to_image(x, y, x_size)
logging.info("Done!")
try:
with NSD_writer(filepath, x_size, image_y_sz):
scope = self.function_scopes[i].contents
x, y = 0, 0
for instruction in scope:
x, y = instruction.to_image(x, y, x_size)
logging.info("Done!")
except Exception as e:
logging.error(f"Failed to save image {filepath} with error '{e}'")
except:
logging.error(f"Failed to save image {filepath}. Unknown error")
raise
def load_from_file(self, filepath:str):
self.function_scopes = itp.load_scoped_instructions(filepath)

View File

@@ -7,7 +7,7 @@ from typing import List, Match, Tuple
from errors.custom import InterpreterException, JavaSyntaxError, ScopeNotFoundException
from draw.Iinstruction import *
logging.warning("""As 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
*for loops
Please remove these features from the source code as they will result in incorrect behaviour""")
@@ -23,7 +23,7 @@ class Function_scope(Iterable):
return self.contents.__iter__()
COMMENT_PATTERN = re.compile(r"""^//|^/\*\*|^\*|^--""")
REMOVE_KEYWORDS = [' ', "public", "private", "final"]
REMOVE_KEYWORDS = [' ', ';', "public", "private", "final", "protected"]
VARIABLE_TAGS = ["byte", "short", "int", "long", "float", "double", "boolean", "char", "String"]
FUNCTION_IDENTIFIERS = ["void"]
FUNCTION_IDENTIFIERS.extend(VARIABLE_TAGS)
@@ -34,14 +34,15 @@ REPLACE = dict((re.escape(k), '') for k in REMOVE_KEYWORDS)
remove_pattern = re.compile("|".join(REPLACE.keys()))
variable_regex = "^("
for kw in FUNCTION_IDENTIFIERS:
for kw in VARIABLE_TAGS:
variable_regex += fr"""{kw}|"""
variable_pattern = re.compile(variable_regex[0:-1]+")(.*)")
variable_pattern = re.compile(variable_regex[0:-1]+")(.*=|.*;)(.*)")
print(variable_pattern)
function_regex = "^("
for kw in FUNCTION_IDENTIFIERS:
function_regex += fr"""{kw}|"""
function_regex = function_regex[0:-1]+ r""").*([(].*[)].*)"""
function_regex = function_regex[0:-1]+ ").*([(].*[)].*)"
function_pattern = re.compile(function_regex)
@@ -150,8 +151,12 @@ def handle_do_while(line: str, src: List[str], i: int) -> Tuple[Iinstruction, in
def handle_variable(line:str, src: List[str], i: int) -> Tuple[Iinstruction, int]:
groups = variable_pattern.match(line).groups()
var_type = groups[0]
var_name = groups[1]
return generic_instruction(f"{var_type} {var_name}"), i
var_name = groups[1][:-1]
var_value = groups[2]
if var_value == "":
return generic_instruction(f"declare variable '{var_name}' of type {var_type}"), i
return generic_instruction(f"declare variable '{var_name}' of type {var_type} with value {var_value}"), i
def handle_instruction(line: str, src: List[str], i: int) -> Tuple[Iinstruction, int]:
if line.startswith("while("):
@@ -166,9 +171,9 @@ def handle_instruction(line: str, src: List[str], i: int) -> Tuple[Iinstruction,
logging.debug("Found do-while construct in line: %i", i+1)
return handle_do_while(line, src, i)
# elif variable_pattern.match(line):
# logging.debug("Found variable in line %i", i+1)
# return handle_variable(line, src, i)
elif variable_pattern.match(line):
logging.debug("Found variable in line %i", i+1)
return handle_variable(line, src, i)
else:
logging.debug("found generic instruction in line %i", i+1)