updated image generation code [WIP]

This commit is contained in:
weckyy702
2021-04-16 11:46:32 +02:00
parent f4c9c77ad8
commit d8dde66d78
5 changed files with 74 additions and 41 deletions

View File

@@ -8,6 +8,16 @@ import logging
from interpreter.function_scope import Function_scope
from interpreter._token import Token, Token_type
from errors.custom import JavaSyntaxError
def print_arr(arr):
print("[")
for elem in arr:
if isinstance(elem, List):
print_arr(elem)
else:
print(elem)
print("]")
class Lexer:
def __init__(self, tokens: List[Token]) -> None:
self._tokens = tokens
@@ -149,9 +159,10 @@ class Lexer:
def _construct_source_line_from_tokens(self, tokens: List[Token]) -> str:
"""TODO: make this function smarter"""
line = ""
for token in tokens:
if token.type == Token_type.SEMICOLON:
continue
break
line += token.content + ' '
return line[:-1] #ignore the space after the last instruction text
@@ -209,7 +220,6 @@ class Lexer:
return while_instruction_back(condtion_str, loop_instructions)
def _handle_for_construct(self, tokens: List[Token]):
#TODO: change that
logging.debug("Found for construct")
tokens.extend(self.get_tokens_until([Token_type.LEFT_CURLY]))
@@ -390,4 +400,4 @@ def _get_for_arguments_from_tokens(tokens: List[Token]) -> Tuple[List[Token], Li
break
increment_tokens.append(token)
return variable_tokens, condition_tokens, increment_tokens[:-2]
return variable_tokens, condition_tokens, increment_tokens[:-1]

View File

@@ -10,7 +10,8 @@ from enum import IntEnum
import os.path
import secrets
from interpreter.interpret_source import JavaInterpreter
from interpreter.Tokenizer import Tokenizer
from interpreter.Lexer import Lexer
from interpreter.function_scope import Function_scope
from draw.code_to_image_wrapper import NSD_writer
import draw.code_to_image as cti
@@ -38,12 +39,12 @@ class NassiShneidermanDiagram:
@staticmethod
def _save_scope(scope: Function_scope, output_path: str):
y_size = scope.get_height()
x_size = scope.get_width()
y_size = scope.get_height()
with NSD_writer(output_path, x_size, y_size):
x, y = 0, 0
for instruction in scope:
x, y = instruction.to_image(x, y, x_size)
x, y = instruction.to_image(x, y, x_size)[0:2]
@staticmethod
def check_conflicts(filepath:str, behavoiur: Overwrite_behaviour):
@@ -64,6 +65,7 @@ class NassiShneidermanDiagram:
filepath = f"{output_path}/{scope.name}"
filepath = self.check_conflicts(filepath, on_conflict)
if filepath is not None:
logging.info(f"Saving NSD to {filepath}.png...")
@@ -81,9 +83,14 @@ class NassiShneidermanDiagram:
i+=1
def load_from_file(self, filepath:str, itp_custom_tags: Optional[Dict[str, List[str]]]):
itp = JavaInterpreter(filepath)
itp.reset_tags(itp_custom_tags)
self.function_scopes = itp.load_instruction_scopes()
tokenizer = Tokenizer(filepath)
tokens = tokenizer.get_tokens()
lexer = Lexer(tokens)
self.function_scopes = lexer.get_instructions()[:-1]
if not self.function_scopes:
return True

View File

@@ -19,7 +19,7 @@ class Tokenizer:
self.line_number = 1
self.column_number = 0
self.source_text = re.sub("(private)|(public)|(protected)", "", self.source_text)
self.source_text = re.sub("(private)|(public)|(protected)|(final)", "", self.source_text)
self.type_name_pattern = re.compile('(char)|(int)|(void)|(double)|(boolean)|(Pixel)|(String)') #TODO: make this modular