added support for full dynamic sizing of instruction blocks
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
from typing import Iterable, List
|
||||
from abc import abstractmethod
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from draw import code_to_image as cti
|
||||
|
||||
class Iinstruction:
|
||||
class Iinstruction(metaclass=ABCMeta):
|
||||
"""Base class for all instructions"""
|
||||
|
||||
def __init__(self, instruction_text: str) -> None:
|
||||
@@ -13,16 +13,23 @@ class Iinstruction:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def getblksize(self) -> float:
|
||||
return self._getblksize()
|
||||
def getblkheight(self) -> float:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def getblkwidth(self) -> float:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def __str__(self) -> str:
|
||||
pass
|
||||
|
||||
def _getblksize(self) -> float:
|
||||
def _getblkheight(self) -> float:
|
||||
return cti.get_text_size(self.instruction_text)[1]
|
||||
|
||||
def _getblkwidth(self) -> float:
|
||||
return cti.get_text_size(self.instruction_text)[0]+50 #padding
|
||||
|
||||
|
||||
class generic_instruction(Iinstruction):
|
||||
"""Any instruction that is not a control structure"""
|
||||
@@ -31,7 +38,13 @@ class generic_instruction(Iinstruction):
|
||||
Iinstruction.__init__(self, instruction_text)
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
|
||||
return cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, self.getblksize())
|
||||
return cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, self.getblkheight())
|
||||
|
||||
def getblkheight(self) -> float:
|
||||
return self._getblkheight()
|
||||
|
||||
def getblkwidth(self) -> float:
|
||||
return self._getblkwidth()
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.instruction_text
|
||||
@@ -48,29 +61,52 @@ class if_instruction(Iinstruction):
|
||||
self.true_case = true_case
|
||||
self.false_case = false_case
|
||||
|
||||
def get_truesize(self) -> float:
|
||||
def get_trueheight(self) -> float:
|
||||
sz = 0.0
|
||||
for inst in self.true_case:
|
||||
sz += inst.getblksize()
|
||||
sz += inst.getblkheight()
|
||||
return sz
|
||||
|
||||
def get_falsesize(self) -> float:
|
||||
def get_falseheight(self) -> float:
|
||||
sz = 0.0
|
||||
if self.false_case:
|
||||
for inst in self.false_case:
|
||||
sz += inst.getblksize()
|
||||
sz += inst.getblkwidth()
|
||||
return sz
|
||||
|
||||
def getblksize(self) -> float:
|
||||
return self._getblksize() + max(self.get_truesize(), self.get_falsesize())
|
||||
def getblkheight(self) -> float:
|
||||
return self._getblkheight() + max(self.get_trueheight(), self.get_falseheight())
|
||||
|
||||
def getblkwidth(self) -> float:
|
||||
return max(self._getblkwidth(), self.get_truewidth() + self.get_falsewidth())
|
||||
|
||||
def get_truewidth(self) -> float:
|
||||
w = 0.0
|
||||
|
||||
for inst in self.true_case:
|
||||
w += inst.getblkwidth()
|
||||
|
||||
return w
|
||||
|
||||
def get_falsewidth(self) -> float:
|
||||
w = 0.0
|
||||
|
||||
if self.false_case:
|
||||
for inst in self.false_case:
|
||||
w += inst.getblkwidth()
|
||||
|
||||
return w
|
||||
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
|
||||
true_w = self.get_truewidth()
|
||||
false_w = self.get_falseheight()
|
||||
true_x, true_y, true_sz_x, _, false_x, false_y, false_sz_x, _ = cti.draw_if_statement(
|
||||
self.instruction_text, x, y, x_sz, self.getblksize()
|
||||
self.instruction_text, x, y, true_w, false_w, self.getblkheight()
|
||||
)
|
||||
self.draw_true_case(true_x, true_y, true_sz_x)
|
||||
self.draw_false_case(false_x, false_y, false_sz_x)
|
||||
blk_size = self.getblksize()
|
||||
blk_size = self.getblkheight()
|
||||
return x, y + blk_size
|
||||
|
||||
def draw_true_case(self, x: float, y:float, x_sz:float):
|
||||
@@ -118,26 +154,35 @@ class while_instruction_front(Iinstruction):
|
||||
Iinstruction.__init__(self, condition)
|
||||
self.child_instructions = instructions
|
||||
|
||||
def get_children_size(self) -> float:
|
||||
def get_children_height(self) -> float:
|
||||
children_sz = 0
|
||||
for inst in self.child_instructions:
|
||||
children_sz += inst.getblksize()
|
||||
children_sz += inst.getblkheight()
|
||||
return children_sz
|
||||
|
||||
def getblksize(self) -> float:
|
||||
return self._getblksize() + self.get_children_size()
|
||||
def get_children_width(self) -> float:
|
||||
w = 0.0
|
||||
for inst in self.child_instructions:
|
||||
w += inst.getblkheight()
|
||||
return w
|
||||
|
||||
def getblkheight(self) -> float:
|
||||
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) -> Iterable[float]:
|
||||
children_x, children_y, children_sz_x = cti.draw_while_loop_front(self.instruction_text, x, y, x_sz, self.getblksize())
|
||||
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.getblksize()
|
||||
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)
|
||||
return self.get_children_size()
|
||||
return self.get_children_height()
|
||||
|
||||
def __str__(self) -> str:
|
||||
res = "while(" + self.instruction_text + "){\n"
|
||||
@@ -152,7 +197,7 @@ class while_instruction_back(while_instruction_front):
|
||||
while_instruction_front.__init__(self, condition, instructions)
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int):
|
||||
children_x, children_y, children_sz_x = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, self.getblksize())
|
||||
children_x, children_y, children_sz_x = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, self.getblkheight())
|
||||
self.draw_children(children_x, children_y, children_sz_x)
|
||||
return x, y + self.getblksize()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user