rewrote instruction classes

This commit is contained in:
weckyy702
2021-04-25 14:21:54 +02:00
parent d8dde66d78
commit 45cb5c0467
3 changed files with 140 additions and 193 deletions

View File

@@ -1,233 +1,180 @@
"""Iinstruction.py: Instruction classes to turn the lexing results into an image""" """Interafce for all instruction types"""
__author__ = "Weckyy702" __author__ ="Weckyy702"
from typing import Iterable, List, Tuple
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from draw import code_to_image as cti from typing import Tuple, List, Optional
class Iinstruction(metaclass=ABCMeta): from . import code_to_image as cti
"""Base class for all instructions"""
class instruction(metaclass=ABCMeta):
def __init__(self, instruction_text: str) -> None: def __init__(self, instruction_text: str) -> None:
self.instruction_text = instruction_text self.instruction_text = instruction_text
@abstractmethod @abstractmethod
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float, float, float]: def convert_to_image(self, x: int, y: int, width: int) -> int:
pass pass
@abstractmethod @abstractmethod
def getblkheight(self) -> float: def get_block_width(self):
pass return self.get_text_width()
@abstractmethod @abstractmethod
def getblkwidth(self) -> float: def get_block_height(self):
pass return self.get_text_height()
@abstractmethod
def __str__(self) -> str:
pass
def _getblkheight(self) -> float:
return cti.get_text_size(self.instruction_text)[1] + cti.PADDING_Y #padding
def _getblkwidth(self) -> float:
return cti.get_text_size(self.instruction_text)[0] + cti.PADDING_X #padding
class generic_instruction(Iinstruction):
"""Any instruction that is not a control structure"""
def get_text_width(self):
return cti.get_text_size(self.instruction_text)[0]
def get_text_height(self):
return cti.get_text_size(self.instruction_text)[1]
class generic_instruction(instruction):
def __init__(self, instruction_text: str) -> None: def __init__(self, instruction_text: str) -> None:
super().__init__(instruction_text) super().__init__(instruction_text)
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float, float, float]:
return cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, self.getblkheight())
def getblkheight(self) -> float: def convert_to_image(self, x: int, y: int, width: int) -> int:
return self._getblkheight() height = self.get_block_height()
return cti.draw_generic_instruction(self.instruction_text, x, y, width, height)
def getblkwidth(self) -> float:
return self._getblkwidth()
def __str__(self) -> str: def get_block_width(self):
return self.instruction_text return super().get_block_width()
class if_instruction(Iinstruction): def get_block_height(self):
"""Conditional structurewdas return super().get_block_height()
NOT.
A.
LOOP
"""
def __init__(self, instruction_text: str, true_case: List[Iinstruction], false_case: List[Iinstruction]=None) -> None:
class if_instruction(instruction):
def __init__(self, instruction_text: str, true_case: List[instruction], false_case: Optional[List[instruction]]) -> None:
super().__init__(instruction_text) super().__init__(instruction_text)
self.true_case = true_case self.true_case = true_case
self.false_case = false_case self.false_case = false_case
def get_trueheight(self) -> float: def convert_to_image(self, x: int, y: int, width: int) -> int:
sz = 0.0 true_width = self.get_true_width()
for inst in self.true_case:
sz += inst.getblkheight()
return sz
def get_falseheight(self) -> float: true_x, true_y, false_x, false_y = cti.draw_if_statement(self.instruction_text, x, y, true_width, width)
sz = 0.0
if self.false_case:
for inst in self.false_case:
sz += inst.getblkheight()
return sz
def get_truewidth(self) -> float: self.draw_children(true_x, true_y, true_width, false_x, false_y, width-true_width)
w = 200.0
for inst in self.true_case: return y + self.get_block_height()
w = max(w, inst.getblkwidth())
return w
def get_falsewidth(self) -> float:
w = 200.0
if self.false_case: def draw_children(self, true_x:int, true_y:int, true_width:int, false_x:int, false_y:int, false_width:int):
for inst in self.false_case:
w = max(w, inst.getblkwidth())
return w
def getblkheight(self) -> float:
return self._getblkheight() + max(self.get_trueheight(), self.get_falseheight())
def getblkwidth(self) -> float:
text_width, true_width, false_width = self.get_widths()
return max(text_width, true_width+false_width)
def get_widths(self) -> Tuple[float, float, float]:
text_width = self._getblkwidth()
true_width = self.get_truewidth()
false_width = self.get_falsewidth()
true_width = max(text_width/2, true_width)
false_width = max(text_width/2, false_width)
true_width = max(text_width-false_width, true_width)
return text_width, true_width, false_width
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float]:
_, true_w, false_w = self.get_widths()
true_x, true_y, true_w, false_x, false_y, false_w = cti.draw_if_statement(
self.instruction_text, x, y, true_w, false_w, self.getblkheight()
)
self.draw_true_case(true_x, true_y, true_w)
self.draw_false_case(false_x, false_y, false_w)
blk_size = self.getblkheight()
return x, y + blk_size
def draw_true_case(self, x: float, y:float, x_sz:float):
for instruction in self.true_case: for instruction in self.true_case:
x, y = instruction.to_image(x, y, x_sz)[0:2] true_y = instruction.convert_to_image(true_x, true_y, true_width)
def draw_false_case(self, x: float, y:float, x_sz:float):
if self.false_case: if self.false_case:
for instruction in self.false_case: for instruction in self.false_case:
x, y = instruction.to_image(x, y, x_sz)[0:2] false_y = instruction.convert_to_image(false_x, false_y, false_width)
def __str__(self) -> str: def get_block_width(self) -> int:
res = f"if({self.instruction_text}) {'{'}\n" text_width = self.get_text_width()
for inst in self.true_case:
res += '\t'+str(inst)+";\n" true_width = self.get_true_width()
res += "}" false_width = self.get_false_width()
return max(text_width, true_width + false_width)
def get_block_height(self):
text_height = self.get_text_height()
true_height = self.get_true_height()
false_height = self.get_false_height()
return text_height + max(true_height, false_height)
def get_text_width(self):
return int(super().get_text_width() * 1.5)
def get_true_width(self) -> int:
width = 200
for instruction in self.true_case:
width = max(width, instruction.get_block_width())
return width
def get_false_width(self) -> int:
width = 200
if self.false_case: if self.false_case:
res += " else {\n" for instruction in self.false_case:
for inst in self.true_case: width = max(width, instruction.get_block_width())
res += '\t'+str(inst)+";\n" return width
res += "}"
return res
class while_instruction_front(Iinstruction): def get_true_height(self) -> int:
height = 0
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None: for instruction in self.true_case:
super().__init__(condition) height += instruction.get_block_height()
self.child_instructions = instructions
def get_children_height(self) -> float: return height
children_sz = 0
for inst in self.child_instructions:
children_sz += inst.getblkheight()
return children_sz
def get_children_width(self) -> float: def get_false_height(self) -> int:
w = 0.0 height = 0
for inst in self.child_instructions:
w = max(w, inst.getblkheight()) if self.false_case:
return w for instruction in self.false_case:
height += instruction.get_block_height()
def getblkheight(self) -> float: return height
return self._getblkheight() + self.get_children_height()
def getblkwidth(self) -> float:
return max(self._getblkwidth(), self.get_children_width())
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float]:
children_x, children_y, children_sz_x = cti.draw_while_loop_front(self.instruction_text, x, y, x_sz, self.getblkheight())
self.draw_children(children_x, children_y, children_sz_x)
return x, y + self.getblkheight()
def draw_children(self, x:float, y:float, x_sz:float):
for inst in self.child_instructions:
x, y = inst.to_image(x, y, x_sz)[0:2]
return self.get_children_height()
def __str__(self) -> str: class while_instruction_front(instruction):
res = "while(" + self.instruction_text + "){\n" def __init__(self, condition_text: str, child_instructions: List[instruction]) -> None:
for inst in self.child_instructions: super().__init__(condition_text)
res += '\t'+str(inst)+";\n" self.children = child_instructions
res += '}'
return res
def convert_to_image(self, x: int, y: int, width: int) -> int:
block_height = self.get_block_height()
children_x, children_y, children_width = cti.draw_while_loop_front(self.instruction_text, x, y, width, block_height)
self.draw_children(children_x, children_y, children_width)
return y + block_height
def draw_children(self, children_x:int, children_y:int, children_width:int):
for instruction in self.children:
children_y = instruction.convert_to_image(children_x, children_y, children_width)
def get_block_width(self):
width = self.get_text_width()
for instruction in self.children:
width = max(width, instruction.get_block_width() / (1 - cti.BLOCK_OFFSET_RATIO)) #instructions inside a bock take up more space, so compensate for that
return int(width)
def get_block_height(self):
height = self.get_text_height()
for instruction in self.children:
height += instruction.get_block_height()
return height
class while_instruction_back(while_instruction_front): class while_instruction_back(while_instruction_front):
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None: def __init__(self, condition_text: str, child_instructions: List[instruction]) -> None:
super().__init__(condition, instructions) super().__init__(condition_text, child_instructions)
def to_image(self, x:int, y:int, x_sz: int): def convert_to_image(self, x: int, y: int, width: int) -> int:
children_x, children_y, children_sz_x = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, self.getblkheight()) block_height = self.get_block_height()
self.draw_children(children_x, children_y, children_sz_x) children_x, children_y, children_width = cti.draw_while_loop_back(self.instruction_text, x, y, width, block_height)
return x, y + self.getblksize()
self.draw_children(children_x, children_y, children_width)
return y + block_height
def __str__(self) -> str:
res = "do{\n"
for inst in self.child_instructions:
res += '\t' +str(inst) + ";\n"
res += f"{'}'}while({self.instruction_text});"
return res
class for_instruction(while_instruction_front): class for_instruction(while_instruction_front):
def __init__(self, variable_def: str, condition: str, instructions: List[Iinstruction]) -> None: def __init__(self, variable_text: str, condition_text: str, child_instruction: List[instruction]) -> None:
super().__init__(condition, instructions) super().__init__(condition_text, child_instruction)
self.variable_instruction = generic_instruction(variable_def) if variable_def else None self.variable_instruction = generic_instruction(variable_text)
def to_image(self, x: int, y: int, x_sz: int) -> Tuple[float]: def convert_to_image(self, x: int, y: int, width: int) -> int:
if self.variable_instruction: y = self.variable_instruction.convert_to_image(x, y, width)
x, y, _ = self.variable_instruction.to_image(x, y, x_sz) return super().convert_to_image(x,y, width)
return super().to_image(x, y, x_sz)
def getblkheight(self) -> float:
if self.variable_instruction:
return super().getblkheight()+self.variable_instruction.getblkheight()
return super().getblkheight()
def getblkwidth(self) -> float:
if self.variable_instruction:
return max(super().getblkwidth(), self.variable_instruction.getblkwidth())
return super().getblkwidth()
def __str__(self) -> str:
if self.variable_instruction:
return self.variable_instruction.__str__()+';\n' + super().__str__()
return super().__str__()

View File

@@ -39,12 +39,12 @@ class NassiShneidermanDiagram:
@staticmethod @staticmethod
def _save_scope(scope: Function_scope, output_path: str): def _save_scope(scope: Function_scope, output_path: str):
x_size = scope.get_width() width = scope.get_width()
y_size = scope.get_height() height = scope.get_height()
with NSD_writer(output_path, x_size, y_size): with NSD_writer(output_path, width, height):
x, y = 0, 0 y = 0
for instruction in scope: for instruction in scope:
x, y = instruction.to_image(x, y, x_size)[0:2] y = instruction.convert_to_image(0, y, width)
@staticmethod @staticmethod
def check_conflicts(filepath:str, behavoiur: Overwrite_behaviour): def check_conflicts(filepath:str, behavoiur: Overwrite_behaviour):

View File

@@ -3,7 +3,7 @@
__author__ = "Weckyy702" __author__ = "Weckyy702"
from typing import Iterable, List from typing import Iterable, List
from draw.Iinstruction import Iinstruction from draw.Iinstruction import instruction
class Function_scope(Iterable): class Function_scope(Iterable):
"""This class serves as a container for Instructions""" """This class serves as a container for Instructions"""
@@ -17,19 +17,19 @@ class Function_scope(Iterable):
def get_height(self) -> int: def get_height(self) -> int:
h = 0.0 h = 0.0
for inst in self.contents: for inst in self.contents:
h += inst.getblkheight() h += inst.get_block_height()
return int(h) return int(h)
def get_width(self) -> int: def get_width(self) -> int:
w = 200.0 #minimum width for every block w = 200 #minimum width for every block
for inst in self.contents: for inst in self.contents:
w = max(w, inst.getblkwidth()) w = max(w, inst.get_block_width())
return int(w) return w
def _add_instruction(self, inst: Iinstruction): def _add_instruction(self, inst: instruction):
self.contents.append(inst) self.contents.append(inst)
def _add_instructions(self, inst: List[Iinstruction]): def _add_instructions(self, inst: List[instruction]):
self.contents.extend(inst) self.contents.extend(inst)
def __iter__(self): def __iter__(self):