Added consts and worked on the parser
Added a DEBUG flag that suppresses the debug decorators's output when set to False.master
parent
22da694daf
commit
a2452e72a6
|
@ -0,0 +1 @@
|
||||||
|
DEBUG = True
|
12
datatypes.py
12
datatypes.py
|
@ -5,6 +5,7 @@ from typing import Any
|
||||||
|
|
||||||
class Datatype(Enum):
|
class Datatype(Enum):
|
||||||
NONE = auto()
|
NONE = auto()
|
||||||
|
NULL = auto()
|
||||||
STRING = auto()
|
STRING = auto()
|
||||||
INTEGER = auto()
|
INTEGER = auto()
|
||||||
FLOAT = auto()
|
FLOAT = auto()
|
||||||
|
@ -15,3 +16,14 @@ class Variable:
|
||||||
name: str
|
name: str
|
||||||
type: Datatype
|
type: Datatype
|
||||||
value: Any
|
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,
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
|
from constants import DEBUG
|
||||||
|
|
||||||
|
|
||||||
def todo(msg: str = "Not implemented"):
|
def todo(msg: str = "Not implemented"):
|
||||||
def inner(func):
|
def inner(func):
|
||||||
def inner2(*args, **kwargs):
|
def inner2(*args, **kwargs):
|
||||||
print(f"[{func.__name__}] TODO: {msg}")
|
if DEBUG:
|
||||||
|
print(f"[{func.__name__}] TODO: {msg}")
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
return inner2
|
return inner2
|
||||||
|
@ -11,9 +15,10 @@ def todo(msg: str = "Not implemented"):
|
||||||
|
|
||||||
def wip(func):
|
def wip(func):
|
||||||
def inner(*args, **kwargs):
|
def inner(*args, **kwargs):
|
||||||
print(
|
if DEBUG:
|
||||||
f"Called work in progress function {func.__name__}. Functionality may change in the future."
|
print(
|
||||||
)
|
f"Called work in progress function {func.__name__}. Functionality may change in the future."
|
||||||
|
)
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
@ -22,7 +27,10 @@ def wip(func):
|
||||||
def deprecated(func):
|
def deprecated(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def inner(*args, **kwargs):
|
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 func(*args, **kwargs)
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
32
pyne.py
32
pyne.py
|
@ -4,13 +4,13 @@ from decorators import deprecated, todo, wip
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pyne_errors import ParserError, FileError
|
from pyne_errors import ParserError, FileError
|
||||||
import datatypes
|
import datatypes
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PyneObject:
|
class PyneObject:
|
||||||
value: int
|
value: datatypes.Variable
|
||||||
datatype: datatypes.Datatype
|
|
||||||
|
|
||||||
|
|
||||||
class Pyne:
|
class Pyne:
|
||||||
|
@ -18,9 +18,26 @@ class Pyne:
|
||||||
self.args = args
|
self.args = args
|
||||||
self.path = path if self._validate(path) else None
|
self.path = path if self._validate(path) else None
|
||||||
|
|
||||||
@todo()
|
@todo(
|
||||||
def parse(self) -> PyneObject:
|
"Tokenize the parser so it doesn't try to match the whole line, gotta find a way for it to do the strings though"
|
||||||
return PyneObject(None, datatypes.Datatype.NONE)
|
)
|
||||||
|
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:
|
def _validate(self, path: str) -> bool:
|
||||||
"""
|
"""
|
||||||
|
@ -34,12 +51,13 @@ class Pyne:
|
||||||
@todo("Project is a work in progress")
|
@todo("Project is a work in progress")
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
parser = ArgumentParser(description="A simple language written in Python")
|
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()
|
args = parser.parse_args()
|
||||||
print(args)
|
print(args)
|
||||||
pyne = Pyne(args.file[0], None)
|
pyne = Pyne(args.file[0], None)
|
||||||
|
|
||||||
print(pyne.parse())
|
for obj in pyne.parse():
|
||||||
|
print(obj)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue