actually fixed the bug

This commit is contained in:
weckyy702
2020-12-21 23:38:29 +01:00
parent 06f6a70909
commit eb0e5fc1be

View File

@@ -1,13 +1,13 @@
from Iinstruction import * from Iinstruction import *
import logging import logging
import re import re
from typing import Any, List from typing import Any, List, Union
class Scope(): class Scope():
def __init__(self, enclosing_scope) -> None: def __init__(self, enclosing_scope) -> None:
self.enclosing_scope = enclosing_scope self.enclosing_scope = enclosing_scope
self.contents = [] self.contents: List = []
def add_instruction(self, instruction) -> None: def add_instruction(self, instruction) -> None:
self.contents.append(instruction) self.contents.append(instruction)
@@ -16,11 +16,11 @@ class Scope():
self.contents.append(subscope) self.contents.append(subscope)
def load_src(filepath: str) -> List[str]: def load_src(filepath: str) -> List[str]:
lines = [] lines: List[str] = []
try: try:
with open(filepath) as file: with open(filepath) as file:
for _line in file: for _line in file:
line:str = _line.strip() line = _line.strip()
if line and not re.match(r"""^//|^#|^COMMENT|^--""", line): if line and not re.match(r"""^//|^#|^COMMENT|^--""", line):
lines.append(line) lines.append(line)
except: except:
@@ -28,7 +28,10 @@ def load_src(filepath: str) -> List[str]:
return lines return lines
def get_scopes(src: list[str]): #TODO: remove debugging-only str
scope_contents = Union[str, Iinstruction, Scope]
def get_scopes(src: List[str]):
global_scope = Scope(None) global_scope = Scope(None)
current_scope = global_scope current_scope = global_scope
@@ -48,7 +51,7 @@ def get_scopes(src: list[str]):
return global_scope return global_scope
def get_instructions(scope: Scope) -> list[Any]: def get_instructions(scope: Scope) -> List[scope_contents]:
instructions = [] instructions = []
for item in scope.contents: for item in scope.contents:
@@ -61,7 +64,7 @@ def get_instructions(scope: Scope) -> list[Any]:
def get_scoped_instructions(filepath:str) -> list[Any]: def get_scoped_instructions(filepath:str) -> List[scope_contents]:
source_code = load_src(filepath) source_code = load_src(filepath)
global_scope = get_scopes(source_code) global_scope = get_scopes(source_code)
@@ -69,16 +72,19 @@ def get_scoped_instructions(filepath:str) -> list[Any]:
return instructions return instructions
if __name__ == "__main__": if __name__ == "__main__":
"""debuging"""
def print_scope(scope: Scope): def print_scope(scope: Scope):
print('[', end='') print('{')
for item in scope.contents: for item in scope.contents:
if isinstance(item, Scope): if isinstance(item, Scope):
print_scope(item) print_scope(item)
else: else:
print(item, end=", ") print(item, end=";\n")
print(']') print('}')
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
inst = get_scoped_instructions("res/input/input.java") #inst = get_scoped_instructions("res/input/input.java")
print(inst) lines = load_src("res/input/input.java")
global_scope = get_scopes(lines)
print_scope(global_scope)