diff --git a/.gitignore b/.gitignore index 15d44ee..b526ca6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ -__pycache__/ -res/output/ -res/input/ \ No newline at end of file +__pycache__/* +res/output/* +res/input/* +.vscode/* +*.pyc \ No newline at end of file diff --git a/NassiShneidermann.py b/NassiShneidermann.py index b7fc90b..685eb86 100644 --- a/NassiShneidermann.py +++ b/NassiShneidermann.py @@ -1,6 +1,7 @@ from os import cpu_count from code_to_image import NSD_save from Iinstruction import Iinstruction +import interpet_source as itp import logging class NassiShneidermanDiagram: @@ -29,6 +30,9 @@ class NassiShneidermanDiagram: x, y = instruction.to_image(x, y, x_sz, 200) cti.NSD_save(filename) + def load_from_file(self, filepath:str): + source_code = itp.load_src(filepath) + instructions = itp.get_scoped_instructions(filepath) @@ -39,6 +43,6 @@ if __name__ == "__main__": NSD = NassiShneidermanDiagram(True) - #NSD.load_from_file("res/input/input.java") + NSD.load_from_file("res/input/input.java") NSD.convert_to_image("Nina", 500) diff --git a/__pycache__/Iinstruction.cpython-39.pyc b/__pycache__/Iinstruction.cpython-39.pyc index c1ec049..9cf077e 100644 Binary files a/__pycache__/Iinstruction.cpython-39.pyc and b/__pycache__/Iinstruction.cpython-39.pyc differ diff --git a/__pycache__/code_to_image.cpython-39.pyc b/__pycache__/code_to_image.cpython-39.pyc index f25e639..a01154a 100644 Binary files a/__pycache__/code_to_image.cpython-39.pyc and b/__pycache__/code_to_image.cpython-39.pyc differ diff --git a/interpet_source.py b/interpet_source.py index 03ddb4f..a5939dd 100644 --- a/interpet_source.py +++ b/interpet_source.py @@ -1,6 +1,7 @@ +from Iinstruction import * import logging import re -from typing import Iterator +from typing import Any, Iterator class Scope(): @@ -27,7 +28,7 @@ def load_src(filepath: str) -> list[str]: return lines -def read_scopes(src: list[str]): +def get_scopes(src: list[str]): global_scope = Scope(None) current_scope = global_scope @@ -47,18 +48,37 @@ def read_scopes(src: list[str]): return global_scope -def print_scope(scope: Scope): - print('[', end='') +def get_instructions(scope: Scope) -> list[Any]: + instructions = [] + for item in scope.contents: if isinstance(item, Scope): - print_scope(item) + instructions.extend(get_instructions(item)) else: - print(item, end=", ") - print(']') + instructions.append(item) + + return instructions + + + +def get_scoped_instructions(filepath:str) -> list[Any]: + source_code = load_src(filepath) + global_scope = get_scopes(source_code) + + instructions = get_instructions(global_scope) + return instructions if __name__ == "__main__": + + def print_scope(scope: Scope): + print('[', end='') + for item in scope.contents: + if isinstance(item, Scope): + print_scope(item) + else: + print(item, end=", ") + print(']') + logging.basicConfig(level=logging.DEBUG) - lines = load_src("res/input/input.java") - scope = read_scopes(lines) - - print_scope(scope) \ No newline at end of file + inst = get_scoped_instructions("res/input/input.java") + print(inst) \ No newline at end of file