From c0540d1e1a00a18efb7b5b7e442d37f0562be787 Mon Sep 17 00:00:00 2001 From: TabulateJarl8 Date: Tue, 20 Jul 2021 12:13:38 -0400 Subject: [PATCH] Lots of bug fixes and add matrices --- setup.py | 33 ++++++++-------- ti842py/__version__.py | 2 +- ti842py/tiParser.py | 83 ++++++++++++++++++++++++++++------------- ti842py/utils/matrix.py | 8 ++-- 4 files changed, 81 insertions(+), 45 deletions(-) diff --git a/setup.py b/setup.py index f4e6aac..93e80aa 100644 --- a/setup.py +++ b/setup.py @@ -17,26 +17,29 @@ exec(f.read(), about) setuptools.setup( - name=about["__title__"], - version=about["__version__"], - author=about["__author__"], - author_email=about["__author_email__"], - description=about["__description__"], - long_description=long_description, - long_description_content_type="text/markdown", - url=about["__url__"], + name=about["__title__"], + version=about["__version__"], + author=about["__author__"], + author_email=about["__author_email__"], + description=about["__description__"], + long_description=long_description, + long_description_content_type="text/markdown", + url=about["__url__"], install_requires=install_requires, entry_points={ 'console_scripts': [ 'ti842py = ti842py.main:main' ] }, - packages=setuptools.find_packages(), - classifiers=[ - "Programming Language :: Python :: 3", - "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", - "Operating System :: OS Independent", - ], - python_requires='>=3.6', + packages=setuptools.find_packages(), + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", + "Operating System :: OS Independent", + "Development Status :: 4 - Beta", + "Intended Audience :: End Users/Desktop", + "Intended Audience :: Developers" + ], + python_requires='>=3.6', include_package_data=True ) diff --git a/ti842py/__version__.py b/ti842py/__version__.py index 2b0e39e..ac7fd38 100644 --- a/ti842py/__version__.py +++ b/ti842py/__version__.py @@ -1,7 +1,7 @@ __title__ = "ti842py" __description__ = "TI-BASIC to Python 3 Transpiler" __url__ = "https://github.com/TabulateJarl8/ti842py" -__version__ = "0.8.5" +__version__ = "0.9.0" __author__ = "Tabulate" __author_email__ = "tabulatejarl8@gmail.com" __license__ = "GPLv3" diff --git a/ti842py/tiParser.py b/ti842py/tiParser.py index 8202927..ac8e20c 100644 --- a/ti842py/tiParser.py +++ b/ti842py/tiParser.py @@ -64,17 +64,6 @@ def convertLine(self, index, line): elif line.startswith("Disp "): statement = re.search("Disp (.*[^)])", line).groups(1)[0] statement = "print(" + parsing_utils.closeOpen(statement) + ", sep=\"\")" - # Variables - elif "->" in line or "→" in line: - statement = re.split("->|→", line) - statement.reverse() - - if statement[0] == "rand": - # seeding rand - statement = "random.seed(" + statement[-1] + ")" - self.UTILS["random"]["enabled"] = True - else: - statement = " = ".join(statement) # If elif line.startswith("If "): try: @@ -101,6 +90,7 @@ def convertLine(self, index, line): statement = ["if " + parsing_utils.fixEquals(line.lstrip("If ").split(":", 1)[0]) + ":", "\t" + self.convertLine(index + 1, line.lstrip("If ").split(":", 1)[1])] elif line == "Then": return None + # Elif elif line == "Else" and self.basic[index + 1].startswith("If"): statement = "elif " + self.basic[index + 1].lstrip("If ") @@ -137,19 +127,37 @@ def convertLine(self, index, line): # For loop elif line.startswith("For"): args = line[4:].strip("()").split(",") - statement = "for " + args[0] + " in range(" + args[1] + ", " + args[2] + statement = "for " + args[0] + " in range(" + args[1] + ", " + str(int(args[2]) + 1) if len(args) == 4: statement += ", " + args[3] statement += "):" self.indentIncrease = True # While loop elif line.startswith("While "): - statement = "while " + parsing_utils.fixEquals(line[6:]) + ":" - self.indentIncrease = True + if re.search(r"While.*[^\"]:", line) is None: + statement = "while " + parsing_utils.fixEquals(line[6:]) + ":" + self.indentIncrease = True + else: + # while statement on 1 line + statement = ["while " + parsing_utils.fixEquals(line.lstrip("While ").split(":", 1)[0]) + ":", "\t" + self.convertLine(index + 1, line.lstrip("While ").split(":", 1)[1])] + # Repeat loop (tests at bottom) elif line.startswith("Repeat "): statement = ["firstPass = True", "while firstPass is True or not (" + parsing_utils.fixEquals(line[7:]) + "):", "\tfirstPass = False"] self.indentIncrease = True + + # Variables + elif "->" in line or "→" in line: + statement = re.split("->|→", line) + statement.reverse() + + if statement[0] == "rand": + # seeding rand + statement = "random.seed(" + statement[-1] + ")" + self.UTILS["random"]["enabled"] = True + else: + statement = " = ".join(statement) + # Pause elif line.startswith("Pause"): args = line[5:].strip().split(",") @@ -281,7 +289,7 @@ def convertLine(self, index, line): self.UTILS['draw']['enabled'] = True elif line == 'ClrAllLists': - statement = 'l1 = l2 = l3 = l4 = l5 = l6 = [None for _ in range(0, 999)]' + statement = 'l1, l2, l3, l4, l5, l6 = ([None for _ in range(0, 999)] for _ in range(6))' # Clr single list elif line.startswith('ClrList '): @@ -289,7 +297,13 @@ def convertLine(self, index, line): else: # Things that can be alone on a line - if line.startswith("getKey") or line.startswith("abs") or line.startswith("sqrt") or line.startswith("toString(") or line.startswith('randInt(') or line.startswith('rand'): + if line.startswith("getKey") or \ + line.startswith("abs") or \ + line.startswith("sqrt") or \ + line.startswith("toString(") or \ + line.startswith('randInt(') or \ + line.startswith('rand') or \ + re.search(r'^\[[A-J]\]', line): statement = line else: statement = "# UNKNOWN INDENTIFIER: {}".format(line) @@ -298,6 +312,12 @@ def convertLine(self, index, line): if isinstance(statement, str): statement = [statement] + statement = parsing_utils.noStringReplace(r'{', '[', statement) + statement = parsing_utils.noStringReplace(r'}', ']', statement) + statement = parsing_utils.noStringReplace('≠', '!=', statement) + statement = parsing_utils.noStringReplace('≤', '<=', statement) + statement = parsing_utils.noStringReplace('≥', '>=', statement) + if "getKey" in ' '.join(statement): # Replace getKey with get_key.get_last_key() if getKey is not inside of quotes statement = parsing_utils.noStringReplace(r'getKey(?!\()+', "get_key.get_last_key()", statement) @@ -328,7 +348,7 @@ def convertLine(self, index, line): statement = parsing_utils.noStringReplace(r'toString\(([^\)]+)\)', r'str(\1)', statement) if "rand" in ' '.join(statement): # Replace rand with random.random() if rand is not inside of quotes - statement = parsing_utils.noStringReplace(r'rand(?!\(|I|o)+', 'random.random()', statement) + statement = parsing_utils.noStringReplace(r'rand(?!\(|I|i|o)+', 'random.random()', statement) statement = parsing_utils.noStringReplace(r'rand\(([0-9])\)', r'[random.random() for _ in range(\1)]', statement) self.UTILS['random']['enabled'] = True if 'dayOfWk(' in ' '.join(statement): @@ -346,8 +366,16 @@ def convertLine(self, index, line): except ValueError: statement = parsing_utils.noStringReplace(r'(l[1-6])\(([0-9A-Za-z]+)\)', lambda m: m.group(1) + '[' + m.group(2) + ']', statement) + if re.search(r'\[[A-J]\]', ' '.join(statement)): + # Matrices + statement = parsing_utils.noStringReplace(r'\[([A-J])\]', r'matrix_\1', statement) + statement = parsing_utils.noStringReplace(r'(matrix_[A-J])\((.+),(.+?)\)', lambda m: m.group(1) + '[' + m.group(2) + ' - 1][' + m.group(3) + ' - 1]', statement) + statement = parsing_utils.noStringReplace(r'len\((matrix_[A-J])\)\s*=\s*\[(.+),(.+?)\]', r'\1.reshape(\2, \3)', statement) + statement = parsing_utils.noStringReplace(r'(matrix_[A-J])\s*=\s*(\[\[.*\]\])', r'\1 = Matrix(\2)', statement) + self.UTILS['matrix']['enabled'] = True + if 'int(' in ' '.join(statement): - statement = parsing_utils.noStringReplace('int(', 'math.floor(', statement) + statement = parsing_utils.noStringReplace(r'(?