Skip to content

Commit

Permalink
bug fixes (closes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TabulateJarl8 committed Sep 3, 2021
1 parent d59599a commit 80ae151
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ti842py/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__title__ = "ti842py"
__description__ = "TI-BASIC to Python 3 Transpiler"
__url__ = "https://github.com/TabulateJarl8/ti842py"
__version__ = "0.9.5"
__version__ = "0.9.6"
__author__ = "Tabulate"
__author_email__ = "[email protected]"
__license__ = "GPLv3"
Expand Down
2 changes: 2 additions & 0 deletions ti842py/parsing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,5 @@ def decistmt(s):
# Convert token list back into source code
return tokenize.untokenize(result).decode('utf-8')

def remove_values_from_list(lst, iterable):
return [item for item in lst if item.strip() not in iterable]
60 changes: 55 additions & 5 deletions ti842py/tiParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, basic, multiplication, floating_point, turbo_draw):
self.turbo_draw = turbo_draw

# Utility Functions
self.UTILS = {"wait": {"code": [""], "imports": ["import time"], "enabled": False}, "menu": {"code": [""], "imports": ["import dialog"], "enabled": False}, "math": {"code": [""], "imports": ["import math"], "enabled": False}, 'random': {'code': [''], 'imports': ['import random'], 'enabled': False}}
self.UTILS = {"wait": {"code": [""], "imports": ["import time"], "enabled": False}, "menu": {"code": [""], "imports": ["import dialog"], "enabled": False}, "math": {"code": [""], "imports": ["import math"], "enabled": False}, 'random': {'code': [''], 'imports': ['import random'], 'enabled': False}, 'traceback': {'code': [''], 'imports': ['import traceback'], 'enabled': True}}
here = os.path.abspath(os.path.dirname(__file__))
for file in [file for file in os.listdir(os.path.join(here, "utils")) if os.path.isfile(os.path.join(here, "utils", file))]:
with open(os.path.join(here, "utils", file)) as f:
Expand Down Expand Up @@ -124,6 +124,13 @@ def convertLine(self, index, line):
else:
statement = "input()"
self.UTILS["toNumber"]["enabled"] = True

# stop listening for keyboard input on input
# these statements will be removed in post-processing if getKey isn't used
if not isinstance(statement, list):
statement = [statement]
statement.insert(0, 'listener.stop()')
statement.extend(['listener = pynput.keyboard.Listener(on_press=get_key.set_last_key)', 'listener.start()'])
# For loop
elif line.startswith("For"):
args = line[4:].strip("()").split(",")
Expand Down Expand Up @@ -165,6 +172,13 @@ def convertLine(self, index, line):
statement = "input("
if len(args) == 1:
statement += str(args[0]) + ")"

# stop listening for keyboard input on input
# these statements will be removed in post-processing if getKey isn't used
if not isinstance(statement, list):
statement = [statement]
statement.insert(0, 'listener.stop()')
statement.extend(['listener = pynput.keyboard.Listener(on_press=get_key.set_last_key)', 'listener.start()'])
else:
statement = ["print(" + str(args[0]) + ")", "time.sleep(" + args[1] + ")"]
self.UTILS["wait"]["enabled"] = True
Expand All @@ -187,6 +201,13 @@ def convertLine(self, index, line):
else:
statement = variable + " = toNumber(input(\"" + variable + "=?\"))"
self.UTILS["toNumber"]["enabled"] = True

# stop listening for keyboard input on input
# these statements will be removed in post-processing if getKey isn't used
if not isinstance(statement, list):
statement = [statement]
statement.insert(0, 'listener.stop()')
statement.extend(['listener = pynput.keyboard.Listener(on_press=get_key.set_last_key)', 'listener.start()'])
# Goto (eww)
elif line.startswith("Goto "):
statement = "goto .lbl" + line[5:]
Expand Down Expand Up @@ -362,13 +383,13 @@ def convertLine(self, index, line):
if re.search(r'l[1-6]\([0-9A-Za-z]+\)', ' '.join(statement)):
# List subscription
try:
statement = parsing_utils.noStringReplace(r'(l[1-6])\(([0-9A-Za-z]+)\)', lambda m: m.group(1) + '[' + str(int(m.group(2)) - 1) + ']', statement)
statement = parsing_utils.noStringReplace(r'(l[1-6])\(([0-9A-Z]+)\)', lambda m: m.group(1) + '[' + str(int(m.group(2)) - 1) + ']', statement)
except ValueError:
statement = parsing_utils.noStringReplace(r'(l[1-6])\(([0-9A-Za-z]+)\)', lambda m: m.group(1) + '[' + m.group(2) + ']', statement)
statement = parsing_utils.noStringReplace(r'(l[1-6])\(([0-9A-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'(?<!l[1-6])\[([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*(\[\[.*\]\])', lambda m: m.group(1) + ' = Matrix(' + m.group(2).replace('][', '], [') + ')', statement)
Expand Down Expand Up @@ -460,6 +481,17 @@ def toPython(self):
'\t\tpass'
]

# Remove get_key functions that surround inputs if getKey isn't used
if not self.UTILS['getKey']['enabled']:
self.pythonCode = parsing_utils.remove_values_from_list(
self.pythonCode,
(
'listener.stop()',
'listener = pynput.keyboard.Listener(on_press=get_key.set_last_key)',
'listener.start()'
)
)

# Decorate main with with_goto if goto is used
if self.UTILS["goto"]["enabled"]:
self.pythonCode.insert(0, "@with_goto")
Expand All @@ -468,6 +500,12 @@ def toPython(self):
if self.UTILS['matrix']['enabled']:
one_line_after_main_definition = self.pythonCode.index('def main():') + 1
self.pythonCode.insert(one_line_after_main_definition, '\tmatrix_A, matrix_B, matrix_C, matrix_D, matrix_E, matrix_F, matrix_G, matrix_H, matrix_I, matrix_J = (Matrix() for _ in range(10)) # init matrices')
if self.UTILS['getKey']['enabled']:
# initialize getKey
one_line_after_main_definition = self.pythonCode.index('def main():') + 1
self.pythonCode.insert(one_line_after_main_definition, '\tget_key = GetKey()')
self.pythonCode.insert(one_line_after_main_definition + 1, '\tlistener = pynput.keyboard.Listener(on_press=get_key.set_last_key)')
self.pythonCode.insert(one_line_after_main_definition + 2, '\tlistener.start()')

# Add required utility functions
neededImports = []
Expand All @@ -481,6 +519,18 @@ def toPython(self):
neededImports.append(importedPackage)
self.pythonCode = neededImports + self.pythonCode

self.pythonCode += ["if __name__ == \"__main__\":", "\tmain()"]
self.pythonCode += [
'if __name__ == \'__main__\':',
'\ttry:',
'\t\tmain()',
'\texcept:',
'\t\ttraceback.print_exc()',
'\t\ttry:',
'\t\t\timport sys, termios',
'\t\t\t# clear stdin on unix-like systems',
'\t\t\ttermios.tcflush(sys.stdin, termios.TCIFLUSH)',
'\t\texcept ImportError:',
'\t\t\tpass'
]

return self.pythonCode
6 changes: 1 addition & 5 deletions ti842py/utils/getKey.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self):
"+": 95,
"\"": 95,
"0": 102,
" ": 102,
pynput.keyboard.Key.space: 102,
".": 103,
":": 103,
"?": 104,
Expand All @@ -89,7 +89,3 @@ def get_last_key(self):
key = self.last_key
self.last_key = 0
return key

get_key = GetKey()
listener = pynput.keyboard.Listener(on_press=get_key.set_last_key)
listener.start()

0 comments on commit 80ae151

Please sign in to comment.