From a2452e72a613e1bd5ec23a9d1b6e2065713c180c Mon Sep 17 00:00:00 2001
From: Ruben Dahl <dahrub@nxi.no>
Date: Sat, 8 Apr 2023 23:12:17 +0200
Subject: [PATCH] Added consts and worked on the parser

Added a DEBUG flag that suppresses the debug decorators's output when set to False.
---
 constants.py  |  1 +
 datatypes.py  | 12 ++++++++++++
 decorators.py | 18 +++++++++++++-----
 pyne.py       | 32 +++++++++++++++++++++++++-------
 4 files changed, 51 insertions(+), 12 deletions(-)
 create mode 100644 constants.py

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