pyne/pyne.py

73 lines
2.1 KiB
Python
Raw Normal View History

2023-04-08 20:07:28 +00:00
from argparse import ArgumentParser
from dataclasses import dataclass
from decorators import deprecated, todo, wip
from enum import Enum
from pyne_errors import ParserError, FileError
import datatypes
import re
2023-04-08 20:07:28 +00:00
import sys
@dataclass
class PyneObject:
value: datatypes.Variable
2023-04-08 20:07:28 +00:00
class Pyne:
def __init__(self, path: str, args: list[str]):
self.args = args
self.path = path if self._validate(path) else 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 row, line in enumerate(f.readlines(), 1):
for var in datatypes.VAR_MATCH:
regex = re.compile(var)
2023-04-09 10:59:21 +00:00
res = regex.match(line[:-1])
if res:
objects.append(
PyneObject(
2023-04-09 10:59:21 +00:00
datatypes.Variable(
None, datatypes.VAR_MATCH[var], line[:-1]
)
)
)
break
continue
else:
raise ParserError(f"[{self.path}:{row}] Token {line} not found.")
if len(objects) == 0:
objects.append(datatypes.NONE_TYPE)
return objects
2023-04-08 20:07:28 +00:00
def _validate(self, path: str) -> bool:
"""
Validates the path and checks if it exists or not.
"""
with open(path) as f:
f.seek(1)
return True
@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, help="The input file")
2023-04-08 20:07:28 +00:00
args = parser.parse_args()
print(args)
pyne = Pyne(args.file[0], None)
try:
for obj in pyne.parse():
print(obj)
return 0
except ParserError as e:
print(f"Found invalid token: {e}")
2023-04-08 20:07:28 +00:00
if __name__ == "__main__":
raise SystemExit(main())