diff --git a/draw/code_to_image_wrapper.py b/draw/code_to_image_wrapper.py new file mode 100644 index 0000000..b65896f --- /dev/null +++ b/draw/code_to_image_wrapper.py @@ -0,0 +1,13 @@ +import draw.code_to_image as cti + +class NSD_writer(object): + def __init__(self, filepath: str, x_sz: int, y_sz: int) -> None: + self.filepath = filepath + self.x_sz = x_sz + self.y_sz = y_sz + + def __enter__(self): + cti.NSD_init(self.x_sz, self.y_sz) + + def __exit__(self, _, __, ___): + cti.NSD_save(self.filepath) \ No newline at end of file diff --git a/interpreter/NassiShneidermann.py b/interpreter/NassiShneidermann.py index fc21424..5c2cecf 100644 --- a/interpreter/NassiShneidermann.py +++ b/interpreter/NassiShneidermann.py @@ -3,7 +3,7 @@ import logging from draw.Iinstruction import Iinstruction from interpreter import interpret_source as itp -from draw import code_to_image as cti +from draw.code_to_image_wrapper import NSD_writer class NassiShneidermanDiagram: @@ -29,15 +29,14 @@ class NassiShneidermanDiagram: return int(h) def convert_to_image(self, filename: str, x_size: int=200): - logging.info(f"Saving NSD to {filename}.png") image_y_sz = self.get_image_height() - cti.NSD_init(x_size, image_y_sz) - x, y = 0, 0 + logging.info(f"Saving NSD to {filename}.png...") + with NSD_writer(filename, x_size, image_y_sz): + x, y = 0, 0 - for instruction in self.instructions: - x, y = instruction.to_image(x, y, x_size) - - cti.NSD_save(filename) + for instruction in self.instructions: + x, y = instruction.to_image(x, y, x_size) + logging.info("Done!") def load_from_file(self, filepath:str): instructions = itp.load_instructions(filepath) diff --git a/interpreter/interpret_source.py b/interpreter/interpret_source.py index dadc40a..35c1c62 100644 --- a/interpreter/interpret_source.py +++ b/interpreter/interpret_source.py @@ -114,7 +114,7 @@ def handle_do_while(line: str, src: List[str], i: int) -> Tuple[Iinstruction, in bracket_index = end_line.rindex(')') #throws if contruct id ill-formed instruction_txt = end_line[7:bracket_index] elif check_line_start(src, i+1, "while("): - i += 1 #make sure we return the correct value for i + i += 1 end_line = src[i] bracket_index = end_line.rindex(')') instruction_txt = end_line[6:bracket_index] @@ -167,5 +167,5 @@ def load_instructions(filepath: str) -> List[Iinstruction]: src = load_src(filepath) instructions, i = get_instructions_in_scope(src) if i != len(src): - raise InterpreterError("Unknown error during source interpretation! Unsupported language constructs or ill-formed?") + raise InterpreterError("Unknown error during source interpretation! Unsupported language constructs or ill-formed source?") return instructions \ No newline at end of file