Merge branch 'main' of https://github.com/plexx-dev/Nassi-Shneiderman-Diagramm-Generator into main
This commit is contained in:
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
__pycache__/
|
||||
.vscode
|
||||
.vscode/
|
||||
res/output/
|
||||
res/input/
|
||||
__pycache__/*
|
||||
res/output/*
|
||||
res/input/*
|
||||
.vscode/*
|
||||
*.pyc
|
||||
@@ -1,7 +1,8 @@
|
||||
from os import cpu_count
|
||||
from code_to_image import NSD_save
|
||||
from Iinstruction import Iinstruction
|
||||
import interpet_source as itp
|
||||
import logging
|
||||
import re
|
||||
|
||||
class NassiShneidermanDiagram:
|
||||
|
||||
@@ -29,44 +30,14 @@ 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)
|
||||
source_code = itp.load_src(filepath)
|
||||
instructions = itp.get_scoped_instructions(filepath)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"""if __name__ == "__main__":
|
||||
if __name__ == "__main__":
|
||||
#for debugging
|
||||
from Iinstruction import *
|
||||
|
||||
@@ -74,4 +45,4 @@ class NassiShneidermanDiagram:
|
||||
|
||||
NSD.load_from_file("res/input/input.java")
|
||||
|
||||
NSD.convert_to_image("Nina", 500)"""
|
||||
NSD.convert_to_image("Nina", 500)
|
||||
|
||||
BIN
__pycache__/Iinstruction.cpython-39.pyc
Normal file
BIN
__pycache__/Iinstruction.cpython-39.pyc
Normal file
Binary file not shown.
BIN
__pycache__/code_to_image.cpython-39.pyc
Normal file
BIN
__pycache__/code_to_image.cpython-39.pyc
Normal file
Binary file not shown.
84
interpet_source.py
Normal file
84
interpet_source.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from Iinstruction import *
|
||||
import logging
|
||||
import re
|
||||
from typing import Any, Iterator
|
||||
|
||||
class Scope():
|
||||
|
||||
def __init__(self, enclosing_scope) -> 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 get_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 get_instructions(scope: Scope) -> list[Any]:
|
||||
instructions = []
|
||||
|
||||
for item in scope.contents:
|
||||
if isinstance(item, Scope):
|
||||
instructions.extend(get_instructions(item))
|
||||
else:
|
||||
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)
|
||||
inst = get_scoped_instructions("res/input/input.java")
|
||||
print(inst)
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 96 B After Width: | Height: | Size: 3.5 KiB |
Reference in New Issue
Block a user