145 lines
4.8 KiB
Python
145 lines
4.8 KiB
Python
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 os
|
|
import re
|
|
import sys
|
|
|
|
|
|
def debugprint(output):
|
|
if "PYNE_DEBUG" in os.environ:
|
|
print(output)
|
|
|
|
|
|
@dataclass
|
|
class PyneObject:
|
|
value: datatypes.Variable
|
|
|
|
|
|
VAR_STORE: list[PyneObject] = []
|
|
|
|
|
|
class Pyne:
|
|
def __init__(self, path: str, args: list[str]):
|
|
self.args = args
|
|
self.path = path if self._validate(path) else None
|
|
|
|
@wip
|
|
def parse(self) -> list[PyneObject]:
|
|
# VAR_STORE: list[PyneObject] = []
|
|
col = 1
|
|
is_str_begin = False
|
|
with open(self.path) as f:
|
|
for row, line in enumerate(f.readlines(), 1):
|
|
varname = ""
|
|
strbuf = ""
|
|
debugprint(line)
|
|
if "=" in line:
|
|
debugprint("if '=' in line")
|
|
varname = line.split("=")[0].split(" ")[0]
|
|
debugprint(f"{varname = }")
|
|
col += len(varname) + line.find("=")
|
|
splitline = (
|
|
line[line.find("=") + 1 if varname != "" else 0 :]
|
|
.strip()
|
|
.split(" ")
|
|
)
|
|
debugprint(f"{splitline = }")
|
|
for token in splitline:
|
|
debugprint(f"{token = }")
|
|
if is_str_begin:
|
|
debugprint("is_str_begin = true")
|
|
strbuf += token
|
|
if datatypes.STR_MATCH_BEGIN.match(token):
|
|
debugprint("Matched opening quote")
|
|
is_str_begin = True
|
|
strbuf += token
|
|
if datatypes.STR_MATCH_END.match(token):
|
|
debugprint("Matched end quote")
|
|
is_str_begin = False
|
|
VAR_STORE.append(
|
|
PyneObject(
|
|
datatypes.Variable(varname, datatypes.STRING, strbuf)
|
|
)
|
|
)
|
|
for var in datatypes.VAR_MATCH:
|
|
if token == varname:
|
|
break
|
|
if token == "":
|
|
break
|
|
if is_str_begin:
|
|
break
|
|
res = var.match(token)
|
|
if res:
|
|
VAR_STORE.append(
|
|
PyneObject(
|
|
datatypes.Variable(
|
|
varname, datatypes.VAR_MATCH[var], token
|
|
)
|
|
)
|
|
)
|
|
break
|
|
else:
|
|
raise ParserError(
|
|
f"{self.path}:{row}:{col}: Invalid token `{token}`"
|
|
)
|
|
col += len(token)
|
|
debugprint(f"{VAR_STORE=}")
|
|
# for var in datatypes.VAR_MATCH:
|
|
# res = var.match(line[:-1])
|
|
# if res:
|
|
# VAR_STORE.append(
|
|
# PyneObject(
|
|
# datatypes.Variable(
|
|
# None, datatypes.VAR_MATCH[var], line[:-1]
|
|
# )
|
|
# )
|
|
# )
|
|
# break
|
|
# continue
|
|
# else:
|
|
# raise ParserError(
|
|
# f"[{self.path}:{row}] Token `{line[:-1]}` not found."
|
|
# )
|
|
return VAR_STORE
|
|
|
|
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")
|
|
args = parser.parse_args()
|
|
print(args)
|
|
# obj = PyneObject(
|
|
# datatypes.Variable(
|
|
# "test",
|
|
# datatypes.Datatype.STRING,
|
|
# datatypes.Data(datatypes.Datatype.STRING, '"Hello World"'),
|
|
# )
|
|
# )
|
|
# # dt = datatypes.Data(datatypes.Datatype.INTEGER, 24)
|
|
# # obj.value.value = dt
|
|
# dt = datatypes.Data(datatypes.Datatype.STRING, '"Goodbye World"')
|
|
# obj.value.value = dt
|
|
# print(obj.value.value.type, obj.value.value.value)
|
|
# return 0
|
|
pyne = Pyne(args.file[0], None)
|
|
for obj in pyne.parse():
|
|
print(obj)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|