diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..51c64dd --- /dev/null +++ b/constants.py @@ -0,0 +1 @@ +DEBUG = True diff --git a/datatypes.py b/datatypes.py index 7e88599..9898d78 100644 --- a/datatypes.py +++ b/datatypes.py @@ -5,6 +5,7 @@ from typing import Any class Datatype(Enum): NONE = auto() + NULL = auto() STRING = auto() INTEGER = auto() FLOAT = auto() @@ -15,3 +16,14 @@ class Variable: name: str type: Datatype value: Any + + +NONE_TYPE = Variable(None, Datatype.NONE, None) + +VAR_MATCH = { + r"null": Datatype.NULL, + r"\".+\"": Datatype.STRING, + r"\d+\.\d{0,}": Datatype.FLOAT, + r"\d+": Datatype.INTEGER, + r".+": Datatype.NONE, +} diff --git a/decorators.py b/decorators.py index 2f5f529..b93be83 100644 --- a/decorators.py +++ b/decorators.py @@ -1,7 +1,11 @@ +from constants import DEBUG + + def todo(msg: str = "Not implemented"): def inner(func): def inner2(*args, **kwargs): - print(f"[{func.__name__}] TODO: {msg}") + if DEBUG: + print(f"[{func.__name__}] TODO: {msg}") return func(*args, **kwargs) return inner2 @@ -11,9 +15,10 @@ def todo(msg: str = "Not implemented"): def wip(func): def inner(*args, **kwargs): - print( - f"Called work in progress function {func.__name__}. Functionality may change in the future." - ) + if DEBUG: + print( + f"Called work in progress function {func.__name__}. Functionality may change in the future." + ) return func(*args, **kwargs) return inner @@ -22,7 +27,10 @@ def wip(func): def deprecated(func): @functools.wraps(func) def inner(*args, **kwargs): - print(f"\x1b[31mWarning:\x1b[0m The function {inner.__name__} is deprecated.") + if DEBUG: + print( + f"\x1b[31mWarning:\x1b[0m The function {inner.__name__} is deprecated." + ) return func(*args, **kwargs) return inner diff --git a/pyne.py b/pyne.py index de61aef..9c5dec5 100644 --- a/pyne.py +++ b/pyne.py @@ -4,13 +4,13 @@ from decorators import deprecated, todo, wip from enum import Enum from pyne_errors import ParserError, FileError import datatypes +import re import sys @dataclass class PyneObject: - value: int - datatype: datatypes.Datatype + value: datatypes.Variable class Pyne: @@ -18,9 +18,26 @@ class Pyne: self.args = args self.path = path if self._validate(path) else None - @todo() - def parse(self) -> PyneObject: - return PyneObject(None, datatypes.Datatype.NONE) + @todo( + "Tokenize the parser so it doesn't try to match the whole line, gotta find a way for it to do the strings though" + ) + def parse(self) -> list[PyneObject]: + objects: list[PyneObject] = [] + with open(self.path) as f: + for line in f.readlines(): + for var in datatypes.VAR_MATCH: + regex = re.compile(var) + res = regex.match(line) + if res: + objects.append( + PyneObject( + datatypes.Variable(None, datatypes.VAR_MATCH[var], line) + ) + ) + break + if len(objects) == 0: + objects.append(datatypes.NONE_TYPE) + return objects def _validate(self, path: str) -> bool: """ @@ -34,12 +51,13 @@ class Pyne: @todo("Project is a work in progress") def main() -> int: parser = ArgumentParser(description="A simple language written in Python") - parser.add_argument("file", nargs=1) + parser.add_argument("file", nargs=1, help="The input file") args = parser.parse_args() print(args) pyne = Pyne(args.file[0], None) - print(pyne.parse()) + for obj in pyne.parse(): + print(obj) return 0