Added consts and worked on the parser

Added a DEBUG flag that suppresses the debug decorators's output when set to False.
master
Ruben Dahl 2023-04-08 23:12:17 +02:00
parent 22da694daf
commit a2452e72a6
No known key found for this signature in database
GPG Key ID: D9B193810A18673E
4 changed files with 51 additions and 12 deletions

1
constants.py 100644
View File

@ -0,0 +1 @@
DEBUG = True

View File

@ -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,
}

View File

@ -1,6 +1,10 @@
from constants import DEBUG
def todo(msg: str = "Not implemented"):
def inner(func):
def inner2(*args, **kwargs):
if DEBUG:
print(f"[{func.__name__}] TODO: {msg}")
return func(*args, **kwargs)
@ -11,6 +15,7 @@ def todo(msg: str = "Not implemented"):
def wip(func):
def inner(*args, **kwargs):
if DEBUG:
print(
f"Called work in progress function {func.__name__}. Functionality may change in the future."
)
@ -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

32
pyne.py
View File

@ -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