-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Rudxain/patch-1
Refactor and bug fix
- Loading branch information
Showing
9 changed files
with
457 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,60 @@ | ||
from sys import stdout | ||
from typing import Final | ||
from enum import Enum | ||
|
||
# Keywords | ||
KW_print = 'ijustwannatelluhowimfeeling' | ||
KW_if = 'andifuaskmehowimfeeling' | ||
|
||
KW_let = 'give' | ||
KW_assign = 'up' | ||
KW_import1 = 'weknowthe' | ||
KW_import2 = "andwe'regonnaplayit" | ||
KW_def = 'gonna' | ||
KW_return1 = 'whenigivemy' | ||
KW_return2 = 'itwillbecompletely' | ||
KW_try = 'thereaintnomistaking' | ||
KW_except = 'iftheyevergetudown' | ||
KW_main = 'takemetourheart' | ||
KW_end = 'saygoodbye' | ||
class KW(Enum): | ||
"""Keywords""" | ||
PRINT = 'ijustwannatelluhowimfeeling' | ||
IF = 'andifuaskmehowimfeeling' | ||
|
||
KW_break = 'desertu' | ||
KW_continue = 'runaround' | ||
KW_endless_loop = 'togetherforeverandnevertopart' | ||
KW_while_loop = 'togetherforeverwith' | ||
LET = 'give' | ||
ASSIGN = 'up' | ||
IMPORT1 = 'weknowthe' | ||
IMPORT2 = "andwe'regonnaplayit" | ||
DEF = 'gonna' | ||
RETURN1 = 'whenigivemy' | ||
RETURN2 = 'itwillbecompletely' | ||
TRY = 'thereaintnomistaking' | ||
EXCEPT = 'iftheyevergetudown' | ||
MAIN = 'takemetourheart' | ||
END = 'saygoodbye' | ||
|
||
KW_L_OP = 'islessthan' | ||
KW_G_OP = 'isgreaterthan' | ||
KW_GOE_OP = 'isgreaterthanorequalto' | ||
KW_LOE_OP = 'islessthanorequalto' | ||
KW_is_not_OP = 'aint' | ||
KW_E_OP = 'is' | ||
BREAK = 'desertu' | ||
CONTINUE = 'runaround' | ||
ENDLESS_LOOP = 'togetherforeverandnevertopart' | ||
WHILE_LOOP = 'togetherforeverwith' | ||
|
||
KW_PY = "py:" | ||
G_OP = 'isgreaterthan' | ||
L_OP = 'islessthan' | ||
GOE_OP = 'isgreaterthanorequalto' | ||
LOE_OP = 'islessthanorequalto' | ||
IS_NOT_OP = 'aint' | ||
E_OP = 'is' | ||
|
||
keywords = [ | ||
KW_print, | ||
KW_if, | ||
KW_let, | ||
KW_assign, | ||
KW_import1, | ||
KW_import2, | ||
KW_def, | ||
KW_return1, | ||
KW_return2, | ||
KW_try, | ||
KW_except, | ||
KW_main, | ||
KW_end, | ||
KW_break, | ||
KW_continue, | ||
KW_endless_loop, | ||
KW_while_loop, | ||
KW_L_OP, | ||
KW_G_OP, | ||
KW_GOE_OP, | ||
KW_LOE_OP, | ||
KW_is_not_OP, | ||
KW_E_OP, | ||
PY = 'py:' | ||
|
||
KW_PY | ||
] | ||
|
||
INDENT_KW = [ | ||
KW_if, KW_def, KW_try, KW_except, KW_while_loop, KW_endless_loop | ||
] | ||
KEYWORDS: Final[list[str]] = [e.value for e in KW] | ||
"""values in `KW`""" | ||
|
||
INDENT_KW: Final = [KW[k].value for k in ['IF', 'DEF', 'TRY', 'EXCEPT', 'WHILE_LOOP', 'ENDLESS_LOOP']] | ||
"""keywords that require indentation in their body (when transpiled to py)""" | ||
|
||
# Tokens that the interpreter will totally ignore | ||
ignore_tokens = {'~', "'"} | ||
IGNORE_TOKENS: Final = set("~'") | ||
"""Tokens that the interpreter will totally ignore""" | ||
|
||
# Characters in numbers | ||
digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'} | ||
DIGITS: Final = set('0123456789.') | ||
"""Characters in numerals""" | ||
|
||
# Separators are used in tokenization | ||
separators = { | ||
SEPARATORS: Final = { | ||
# not using `set`, because readability, and multi-char `str`s may be added in the future | ||
'(', ')', '[', ']', '{', '}', ',', '\n', ' ', '+', '-', '*', '/', '%', '^', '=' | ||
} | ||
"""Separators are used in tokenization""" | ||
|
||
# Operators | ||
operators = { | ||
OPERATORS: Final = { | ||
'+', '-', '*', '/', '%', '^', '=', | ||
'[', ']', '(', ')', '{', '}', ',', | ||
'>', '<', '<=', '>=', '!=', 'is', 'aint' | ||
} | ||
OP_build_in_functions = {'to_string', 'to_int', 'to_float', 'length'} | ||
|
||
def join_list(l): | ||
return ''.join(map(str, l)) | ||
OP_BUILT_IN_FUNCTIONS: Final = {'to_string', 'to_int', 'to_float', 'length'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,69 @@ | ||
from time import time | ||
start = time() # Set and start a timer | ||
|
||
from sys import stdout | ||
from traceback import format_exc | ||
from argparse import ArgumentParser | ||
|
||
|
||
def play_audio(src_file_name): | ||
import AudioGenerator | ||
from pyrickroll import Token | ||
from Lexer import lexicalize | ||
|
||
with open(src_file_name, mode='r', encoding='utf-8') as src: | ||
content = src.readlines() | ||
content[-1] += '\n' | ||
for statement in content: | ||
tokens = lexicalize(statement) | ||
tok = Token(tokens) | ||
|
||
for i in range(len(tok.t_values)): | ||
AudioGenerator.play(tok.t_values[i]) | ||
|
||
def main(): | ||
|
||
arg_parser = ArgumentParser() | ||
arg_parser.add_argument("file", nargs='?', default="") | ||
arg_parser.add_argument("-cpp", action = "store_true") | ||
arg_parser.add_argument("-intpr", action = "store_true") | ||
arg_parser.add_argument("--time", action = "store_true") | ||
arg_parser.add_argument("--audio", action = "store_true") | ||
args = arg_parser.parse_args() | ||
|
||
# Run the RickRoll program | ||
if args.file: | ||
from os.path import exists | ||
# Convert .rickroll to C++ | ||
if args.cpp: | ||
from crickroll import run_in_cpp | ||
run_in_cpp(args.file) | ||
|
||
# Execute .rickroll using the interpreter | ||
elif args.intpr: | ||
from interpreter import run_in_interpreter | ||
run_in_interpreter(args.file) | ||
|
||
else: | ||
try: | ||
from pyrickroll import run_in_py | ||
exec(run_in_py(args.file), globals(), globals()) | ||
except Exception: | ||
error_msg = format_exc().split('File "<string>",')[-1] | ||
stdout.write(f'Exception in{error_msg}') | ||
|
||
else: | ||
stdout.write('Warning: [Not executing any script...]') | ||
|
||
|
||
if args.audio: | ||
play_audio(args.file) | ||
|
||
if args.time: | ||
stdout.write(f'\nExecution Time: [{time() - start}] sec.\n') | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
#!/usr/bin/env python3 | ||
from typing import Final | ||
from traceback import format_exc | ||
|
||
|
||
def play_audio(src_file_name: str): | ||
import AudioGenerator | ||
from pyrickroll import Token | ||
from Lexer import lexicalize | ||
|
||
with open(src_file_name, mode='r', encoding='utf-8') as src: | ||
content = src.readlines() | ||
if len(content) > 0: | ||
content[-1] += '\n' | ||
for statement in content: | ||
tokens = lexicalize(statement) | ||
tok = Token(tokens) | ||
|
||
for v in tok.t_values: | ||
AudioGenerator.play(v) | ||
|
||
|
||
def main(): | ||
from argparse import ArgumentParser | ||
from sys import stdout | ||
from time import time | ||
|
||
arg_parser = ArgumentParser() | ||
arg_parser.add_argument("file", nargs='?', default="") | ||
arg_parser.add_argument("-cpp", action="store_true") | ||
arg_parser.add_argument("-intpr", action="store_true") | ||
arg_parser.add_argument("--time", action="store_true") | ||
arg_parser.add_argument("--audio", action="store_true") | ||
args = arg_parser.parse_args() | ||
|
||
# excludes `def`s, `import`s and `argparse` times | ||
start: Final = time() | ||
# Run the RickRoll program | ||
if args.file: | ||
# Convert .rickroll to C++ | ||
if args.cpp: | ||
from crickroll import run_in_cpp | ||
run_in_cpp(args.file) | ||
|
||
# Execute .rickroll using the interpreter | ||
elif args.intpr: | ||
from interpreter import run_in_interpreter | ||
run_in_interpreter(args.file) | ||
|
||
else: | ||
try: | ||
from pyrickroll import run_in_py | ||
exec(run_in_py(args.file), globals(), globals()) | ||
except Exception: | ||
error_msg = format_exc().split('File "<string>",')[-1] | ||
stdout.write(f'Exception in{error_msg}') | ||
|
||
else: | ||
stdout.write('Warning: [Not executing any script...]') | ||
|
||
if args.audio: | ||
play_audio(args.file) | ||
|
||
if args.time: | ||
stdout.write(f'\nExecution Time: [{time() - start}] sec.\n') | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.