From 4322a27e5fc5dc4876b4890d330c9bc443172d4b Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Mon, 21 Dec 2020 13:38:43 +0100 Subject: [PATCH] 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_pkB8Xx zw^02bESi8vM5I+m;sS@;$GCFA2o|Y=3;Jc7(rIO^-U4tbgHap}MG)_hUrO~0`b zXs!{7m)?2tn5_DQ{@j6*V0;MOqtmrsRnU{Sp`YBt3dhN}u=*ZW7@k}YU^x`D?qb_U zeBFb$m=Fn~r@)uQw$12D;cXpVye*gje!D}8TJauJyl1$8u&cW%>?N$Vgk9c>3vu^g zu2Z%F!y`lD9>UzV;>S+j367Q^`MXDP!~-NtzXm+JcXbV){_$X;d{l`~o<99lwGaDD ztyYy&S(j|q2AyTi%+6Yw19~SlvUb+F&}LhZEM~TKn^Xy6%6<4senTY)mvI&r4zAUahF=SjiK& zrRF;fpTmygI>oRn%`Bd)3xZkn6DSGBmQV(_^}ij~rCn-YUz##kHO#ZqD1Z2LhNQnU{(V6!79^lN_E4MeKrBFl#h|s?I8yKhoOCnQmlDBHa zzUt&VFv+i>sDlDJ*#YNv>Ga+T@|Kr#vy%^DF6AUSCGvf66&U}NELcyS+!iYqX!xo^B>?=~d1Z)V#k@U~=I0jEk~0E<$tz0$eP8F1s`T*-}BjGMdx zZEz+pZ*BpL7Tjv9GjJ=v{qiJ$V@eGI-jRgedpOtgCT!ycqvlSnLsC#7#-O}VUQi(d z?>ZG$B&b)B%Wsb0^Su7~WUd!k{{W_u2Ux8vD(2f?pj_>a4xRQkII$qaIa_zyTY@Q` zA{PgN_d!|1kgR0L6=rOz!$Pypf2`oYTPiUM^TIfOQ3SW(z_xm)0w6^+w>OiWU3LO? ztK^B>UbHJdTT&;krFbitiq!z(0)2)7#BH9?yTDkRLs$~m^`jGxUPSB{plo0+BQw@8 z*G|T+4pXq;DvJj&g-YeQxiJ*-$Jl8F5Wj>rABs8_lQP5K*#wH^u(wvTTpgR%B{+sn z;yy3&gEb?nw8s75&rpg3l(x%u42f;uX-e;2cl^*FJs--Sq9vYF*DX;0!wr#N0=;~1dAE@v2LNLm{@afI zAo9Galbpn7As+@JeYZa~%jb#GgMY%0_g7!Ti2jYdJ&L*T7BNx>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..f25e63912fa3d28d86d95c3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!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-