From 94992ee04e42224e814ccdf32906a4b8d0f5bc68 Mon Sep 17 00:00:00 2001 From: plexx Date: Mon, 21 Dec 2020 11:54:22 +0100 Subject: [PATCH 1/3] . --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 16b6540..15d44ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ __pycache__/ -.vscode -.vscode/ res/output/ res/input/ \ No newline at end of file From 4322a27e5fc5dc4876b4890d330c9bc443172d4b Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Mon, 21 Dec 2020 13:38:43 +0100 Subject: [PATCH 2/3] fixed source scope interpretation --- NassiShneidermann.py | 41 ++------------- __pycache__/Iinstruction.cpython-39.pyc | Bin 0 -> 4926 bytes __pycache__/code_to_image.cpython-39.pyc | Bin 0 -> 3376 bytes interpet_source.py | 64 +++++++++++++++++++++++ res/output/Nina.png | Bin 96 -> 3587 bytes 5 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 __pycache__/Iinstruction.cpython-39.pyc create mode 100644 __pycache__/code_to_image.cpython-39.pyc create mode 100644 interpet_source.py diff --git a/NassiShneidermann.py b/NassiShneidermann.py index 766e210..b7fc90b 100644 --- a/NassiShneidermann.py +++ b/NassiShneidermann.py @@ -1,7 +1,7 @@ +from os import cpu_count from code_to_image import NSD_save from Iinstruction import Iinstruction import logging -import re class NassiShneidermanDiagram: @@ -29,49 +29,16 @@ class NassiShneidermanDiagram: x, y = instruction.to_image(x, y, x_sz, 200) cti.NSD_save(filename) - @staticmethod - def load_code_lines(filepath): - lines = [] - try: - with open(filepath) as file: - for _line in file: - line:str = _line.strip() - if line and not re.match(r"""^//|^#|^COMMENT|^--""", line): - lines.append(line) - except: - logging.error(f"Failed to open input file {filepath}!") - - return lines - - def load_from_file(self, filepath: str): - filtered_lines = self.load_code_lines(filepath) - global_scope = [] - current_scope = global_scope - for line in filtered_lines: - logging.debug(line) - if line.__contains__('}'): - current_scope.append("scope exit") - current_scope = global_scope[-1] # does not get correct parent scope - #TODO: get correct parent scope - if line.__contains__('{'): - current_scope.append("scope enter") - current_scope.append([]) - current_scope = current_scope[-1] - - elif not line.__contains__('}'): - current_scope.append("generic instruction") - print(global_scope) - -"""if __name__ == "__main__": +if __name__ == "__main__": #for debugging from Iinstruction import * 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)""" + NSD.convert_to_image("Nina", 500) diff --git a/__pycache__/Iinstruction.cpython-39.pyc b/__pycache__/Iinstruction.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c1ec0497f33da854ba173426515821d8a99ab940 GIT binary patch literal 4926 zcmcgv&5zs06`vu86h%>5*}Gn^Q+utZO%b*As^1BUAa!a3K>$|{;I=4;6a-f@yUR62 zIzw)}5~$PW&+x%})W5`2fu3?LiXPGr>Ej zhiLJxu=$M(dc8ho&~{V3!Mf-P(lPc`&kOxnhuOA7gKzWg3yl)7^}5T^j~%{!2_6Xx z?4{rheMb7Xz5?ILu&?FTC#jJBVJLF;BuLUe%{%^Kl1hJ+jzxMBbL=yG_Sg%eAT6ZC z|3Zv#eEn*G@(@b$Ayh&|)YUk3b-}pC^;a5a-1w5YhA>6r>b6AV4n*;lp(4mGsN3Wk zSl7&*Pl5=HI2)xw98K^4-cLkt6#7ZhJBnq`55r#VeKMSDdAHW!rQ$rzO)1hd8I3eB zsJu|d@(4byc#o&1icKJ~nL<1g= zk^vWA3_hJeoT9-H%nKeoJBdWVg&h0QgU5k?EdBBL!N($kAg8e$)crbmG081Y1=jOg zXpu6NPH!#sDtpY0oSx@&lCyI!ndWQ)wL~9A22|yRGKHc%WFVk#5@%O_^bOfY`6gD7 z9YpS6bqg!>v#7Q}%B?i^g0X)ru)Q!%TQRNd(YMi&$R7*O%Wcma$NVfr+3~#3&;0P} zO~dmbXwa;~+R83|4lT@$qcHYUiQdaytSU>Cgyu*Dm<1)l%A!o`>Kaw8*RW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Nx>LNlGb2%_U+w?r)F zx_>xQ|G*NVC^72A27!MbYC}{hq|8vcnzK~EkEKe-3Wp0Ha_fWQ-}^%_g(L@1S-@}( i{494`U8nnAce~rQG&~Ea`#+kA&)xrO|7NxgJ--1D(h?W| literal 0 HcmV?d00001 diff --git a/__pycache__/code_to_image.cpython-39.pyc b/__pycache__/code_to_image.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f25e63912fa3d28d86d95c3ed5f08a5bb3cdfceb GIT binary patch literal 3376 zcmai0OK%*<5uWaz*$26#L~)qHOay{`V#uGYN&>+yx_7bYkze86}76kYTR*T9keC64B8FQZkCU1yzu0TTm^OQ z3iXD(3F`V4>Y7{!bwjPnjTcNdhMy z$?3LPQG05X4nK=kTNK{YBpj%sZeNdecr=;*I!n#zvQ~K~8=1qA>72T|hw0#)7wouT zCp}J^O&ZNT^k1Uq4>1_$C6jFIj9H&c{z`=0h?lP6uUP1r%FC)0(tX9lnslI-*Y(9G zPd@a$qhVy!={B9&$LV>eo0)7sO!J*j)L;~cde@l#MFab{>ssJdh*TXFB1#6@#i#FD z{g+CG10s`Fe$D8SGADKtTgZoriTbFid^&_1dU&KbYnO~QYR`&8240qEsoO8bl*Tqq)3hyYAW_Upc z8K9$mwO8;sDLfqwhNkUmvTIQZ)7~)Cg@^M8Dcl>yal5JsB1QdI$31mO_APm<17&h( zS%@S&F6wlAm=0o9tR|z_M09Lm)Ujz-^cv)Kl?L)a;T>B!?}T3dGBE`w0ex5umBS6< zye$zx!GK^9_2B-gi*I2yC%`$(Wi;0K)<< z$Ru@ii;F+QZVR|@o{`rlzL)%M_?3Jp1PS^6D^XvT@VyAg}moif;Oj}XEg+yai zqEZSSM2Q(H3KG2mr(RGdy`WrHMg-H}qj{5tO$>uSeM#+n&*w>^?|{`jxiL`}b5Rh~tTUzEMZIXTxt zWO~vB&R6!FX<`#GfodvI&aT{!LTsMLo2GCV`cfzY+*f$Y9R&ppz>Bf(0-W`(g`NeR zz7KQ%y9C)^_9kfia?c`0KcKaL{GHl=_s%_=Wi2%HkFcWYzh7eNb#U0UaKIlvRzaL) zhe2QCSNs8FazeL-gtyJM*(3G<&m#dq`~>tj1LC7^1%%|KYpvN}sQH{XTeAk5aKa=L zBf^nykeKn1!M#OyAWIY0=iLZ^VZl-kn@X;z}_ zh?Ors?mp?FXjq1{@8;ksE~`WoB1J=jH40QJN64E$1=H1RX3IH}8)7lHwJ+B{$GoTo zK@vu35JV55X!Jh}e$}WPrQ|{TRw43}+^?Ycq>M~*(B^A?60 aYp^D=u}ht~;%2?Mach&FRj None: + self.enclosing_scope = enclosing_scope + self.contents = [] + + def add_instruction(self, instruction) -> None: + self.contents.append(instruction) + + def add_subscope(self, subscope) -> None: + self.contents.append(subscope) + +def load_src(filepath: str) -> list[str]: + lines = [] + try: + with open(filepath) as file: + for _line in file: + line:str = _line.strip() + if line and not re.match(r"""^//|^#|^COMMENT|^--""", line): + lines.append(line) + except: + logging.error(f"Failed to open input file {filepath}!") + + return lines + +def read_scopes(src: list[str]): + global_scope = Scope(None) + current_scope = global_scope + + for line in src: + logging.debug(line) + if line.__contains__('}'): + current_scope.add_instruction("scope exit") + current_scope = current_scope.enclosing_scope + if line.__contains__('{'): + current_scope.add_instruction("scope enter") + subscope = Scope(current_scope) + current_scope.add_subscope(subscope) + current_scope = subscope + + elif not line.__contains__('}'): + current_scope.add_instruction("generic instruction") + + return global_scope + +def print_scope(scope: Scope): + print('[', end='') + for item in scope.contents: + if isinstance(item, Scope): + print_scope(item) + else: + print(item, end=", ") + print(']') + +if __name__ == "__main__": + 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 diff --git a/res/output/Nina.png b/res/output/Nina.png index 6f46d697b3542b884df79378af8b0229ceb35296..987d273523234db0407bfad4ee6b7cf75c386756 100644 GIT binary patch literal 3587 zcmeAS@N?(olHy`uVBq!ia0y~yVEn?s!2E)P2`IAMezpiu{FJAQV@SoEw^t5wGAIZf zFev>$_q;00LKWdJS8a{C8>W4>W@z|+Mpog(9mb|nYBUH&Q^9CP7%dA%i^I_xVYD_J qtqn(O!_nGsv^E^A4F^MQsJC5@>+Ecv*&4vk9fPN2- From 711ceb2567eb882e1f809c79c6036d7498f3be3c Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Mon, 21 Dec 2020 22:07:20 +0100 Subject: [PATCH 3/3] updated .gitignore --- .gitignore | 8 +++-- NassiShneidermann.py | 6 +++- __pycache__/Iinstruction.cpython-39.pyc | Bin 4926 -> 4921 bytes __pycache__/code_to_image.cpython-39.pyc | Bin 3376 -> 3301 bytes interpet_source.py | 42 +++++++++++++++++------ 5 files changed, 41 insertions(+), 15 deletions(-) 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 c1ec0497f33da854ba173426515821d8a99ab940..9cf077e26b9c33f8807a102a083edbb3bb4d19d6 100644 GIT binary patch delta 417 zcmXYrKTpD75XO7Z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f(F;EDzzBn$omJC}x$=4tl5nYTOpbMezs&<=t;F|jItwC;Tl z-ox^$jFM`17^|eZH&nxBJ5DN(qTaLpw$xu?zGIv2oXPtplUD{5NqhwY#_$>x9k7%L zD%gYyzDN!&#ZxQgV|vIH8Cd!|E$&!?3wy%AOb+Z6gi|CbO}WSwNRf1DN)#2Y$O}t& z6B|ehsG`y&WnZte#zM&Ul`KQ(ioT3Gsy~c>gL#S62kc?Vk-n*CZe!Gmo}wlLJUhKe zCH;E-eIAuMyO5);yrKiA0hMXf8N=>%BmVkugz=@`J1dUrasg*< zp!V$)V_rYDvL)mSDmM`}5YX2+UUVP8=2W=Pp>hkEY8p5`I=Rf`j#;S_+ 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