From 57188fca203643f18409b0fa71d730d771faffed Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Sun, 5 Nov 2023 02:39:00 +0800 Subject: chore: add Token --- pdm.lock | 50 ++++++++++++++++++++++++++++++++- psi/Grammar/Token | 7 ++++- psi/__init__.py | 73 ++++--------------------------------------------- psi/psi.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + tests/__init__.py | 23 ++++++++++++++++ tests/get_next_token.py | 2 ++ 7 files changed, 156 insertions(+), 69 deletions(-) create mode 100644 psi/psi.py create mode 100644 tests/get_next_token.py diff --git a/pdm.lock b/pdm.lock index 31140f2..736ebb1 100644 --- a/pdm.lock +++ b/pdm.lock @@ -6,7 +6,7 @@ groups = ["default"] cross_platform = true static_urls = false lock_version = "4.3" -content_hash = "sha256:b629d4cd8e8585cf1128e9dd7f1a585746bc3e94bb588794250bf99dd80d00b3" +content_hash = "sha256:3ff8281ca1b68fe781cdb20ad83bc38ecc74aab23b6382b6d5c92f063123b18c" [[package]] name = "anyio" @@ -285,6 +285,16 @@ files = [ {file = "importlib_metadata-6.8.0.tar.gz", hash = "sha256:dbace7892d8c0c4ac1ad096662232f831d4e64f4c4545bd53016a3e9d4654743"}, ] +[[package]] +name = "iniconfig" +version = "2.0.0" +requires_python = ">=3.7" +summary = "brain-dead simple config-ini parsing" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + [[package]] name = "jinja2" version = "3.1.2" @@ -555,6 +565,16 @@ files = [ {file = "platformdirs-3.10.0.tar.gz", hash = "sha256:b45696dab2d7cc691a3226759c0d3b00c47c8b6e293d96f6436f733303f77f6d"}, ] +[[package]] +name = "pluggy" +version = "1.3.0" +requires_python = ">=3.8" +summary = "plugin and hook calling mechanisms for python" +files = [ + {file = "pluggy-1.3.0-py3-none-any.whl", hash = "sha256:d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7"}, + {file = "pluggy-1.3.0.tar.gz", hash = "sha256:cf61ae8f126ac6f7c451172cf30e3e43d3ca77615509771b3a984a0730651e12"}, +] + [[package]] name = "pygments" version = "2.16.1" @@ -579,6 +599,24 @@ files = [ {file = "pymdown_extensions-10.3.tar.gz", hash = "sha256:94a0d8a03246712b64698af223848fd80aaf1ae4c4be29c8c61939b0467b5722"}, ] +[[package]] +name = "pytest" +version = "7.4.2" +requires_python = ">=3.7" +summary = "pytest: simple powerful testing with Python" +dependencies = [ + "colorama; sys_platform == \"win32\"", + "exceptiongroup>=1.0.0rc8; python_version < \"3.11\"", + "iniconfig", + "packaging", + "pluggy<2.0,>=0.12", + "tomli>=1.0.0; python_version < \"3.11\"", +] +files = [ + {file = "pytest-7.4.2-py3-none-any.whl", hash = "sha256:1d881c6124e08ff0a1bb75ba3ec0bfd8b5354a01c194ddd5a0a870a48d99b002"}, + {file = "pytest-7.4.2.tar.gz", hash = "sha256:a766259cfab564a2ad52cb1aae1b881a75c3eb7e34ca3779697c23ed47c47069"}, +] + [[package]] name = "python-dateutil" version = "2.8.2" @@ -789,6 +827,16 @@ files = [ {file = "sniffio-1.3.0.tar.gz", hash = "sha256:e60305c5e5d314f5389259b7f22aaa33d8f7dee49763119234af3755c55b9101"}, ] +[[package]] +name = "tomli" +version = "2.0.1" +requires_python = ">=3.7" +summary = "A lil' TOML parser" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + [[package]] name = "typing-extensions" version = "4.8.0" diff --git a/psi/Grammar/Token b/psi/Grammar/Token index 61dd757..0de3014 100644 --- a/psi/Grammar/Token +++ b/psi/Grammar/Token @@ -47,4 +47,9 @@ ELLIPSIS '...' COLONEQUAL ':=' EXCLAMATION '!' INTEGER 'INTEGER' -EOF 'EOF' \ No newline at end of file +EOF 'EOF' +SPACE ' ' + + +AWAIT +ASYNC diff --git a/psi/__init__.py b/psi/__init__.py index 330fcd3..b3e08df 100644 --- a/psi/__init__.py +++ b/psi/__init__.py @@ -3,71 +3,10 @@ @BODY 似乎要写的还蛮多的,所以先写几个TODO List """ -__all__ = ['psi'] - -from psi.execution import Execution - -class psi: - """ - A class representing a Psi object. - - Args: - input: The input value for the Psi object. - - Returns: - None - - Example: - ```python - obj = Psi("example") - ``` - """ - - def __init__(self, input): - """ - Initializes a Psi object. - - Args: - input: The input value for the Psi object. - - Returns: - None - """ - self.input = input - self.execution = Execution(input) - self.result = None - - def execute(self): - """ - Executes the Psi object. - - Returns: - The result of the execution. - """ - self.result = self.execution.execute() - return self.result - - def get_result(self): - """ - Retrieves the result of the Psi object. - - Returns: - The result of the execution. - """ - return self.result - - def set_input(self, input): - """ - Sets the input value for the Psi object. - - Args: - input: The new input value. - - Returns: - None - """ - self.input = input - self.execution = Execution(input) - self.result = None - +__all__ = ['psi', 'Exception', 'interpreter', 'lexer', 'Parser'] +from .psi import psi +from .execution import Execution +from .interpreter import Interpreter +from .lexer import Lexer +from .parsers import Parser \ No newline at end of file diff --git a/psi/psi.py b/psi/psi.py new file mode 100644 index 0000000..8df0acd --- /dev/null +++ b/psi/psi.py @@ -0,0 +1,69 @@ + +from psi.execution import Execution + +__all__ = ['psi'] + +class psi: + """ + A class representing a Psi object. + + Args: + input: The input value for the Psi object. + + Returns: + None + + Example: + ```python + obj = Psi("example") + ``` + """ + + def __init__(self, input): + """ + Initializes a Psi object. + + Args: + input: The input value for the Psi object. + + Returns: + None + """ + self.input = input + self.execution = Execution(input) + self.result = None + + def execute(self): + """ + Executes the Psi object. + + Returns: + The result of the execution. + """ + self.result = self.execution.execute() + return self.result + + def get_result(self): + """ + Retrieves the result of the Psi object. + + Returns: + The result of the execution. + """ + return self.result + + def set_input(self, input): + """ + Sets the input value for the Psi object. + + Args: + input: The new input value. + + Returns: + None + """ + self.input = input + self.execution = Execution(input) + self.result = None + + diff --git a/pyproject.toml b/pyproject.toml index bc5709d..a815413 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ dependencies = [ "mkdocs-material-extensions>=1.2", "neoteroi-mkdocs>=1.0.4", "docutils>=0.20.1", + "pytest>=7.4.2", ] requires-python = ">=3.8" readme = "README.md" diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..c9a0e8f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,23 @@ +from os.path import join, abspath, dirname + +DIR = dirname(abspath(__file__)) + +token_dict = {} # 创建一个空字典 + +with open(join(DIR, '..', 'psi', 'Grammar', 'Token'), 'r') as file: + for line in file: + if line := line.strip(): + values = line.split() # 使用空格分割行,得到值列表 + code = values[0] # 第一个值为代码 + symbol = values[1] if len(values) > 1 else None # 第二个值为符号,如果没有第二个值,则设置为None + token_dict[code] = symbol # 将代码和符号添加到字典中 + +# 将字典中的键值对转换为多个变量及其对应的值 +for code, symbol in token_dict.items(): + globals()[code] = symbol + +# 打印变量及其对应的值 +print(LPAR) +print(RPAR) +print(AWAIT) +# 其他变量... \ No newline at end of file diff --git a/tests/get_next_token.py b/tests/get_next_token.py new file mode 100644 index 0000000..2801337 --- /dev/null +++ b/tests/get_next_token.py @@ -0,0 +1,2 @@ +class ABC: + ... \ No newline at end of file -- cgit v1.2.3-70-g09d2