Skip to content

Commit

Permalink
final commit?
Browse files Browse the repository at this point in the history
I guess
  • Loading branch information
Rudxain committed Nov 24, 2022
1 parent 26d9d64 commit 753f1f7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src-py/Lexer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# the type-checker complains about the re-export, for some reason,
# so we must explicitly import it here.
from typing import Final

from Keywords import *
from helpers import remove_all

ALL_KW_STR = ','.join(KEYWORDS)
ALL_KW_STR: Final = ','.join(KEYWORDS)

def lexicalize(stmt: str):
SP_LN: Final = {' ', '\n'}

current_token = ''
quote_count = 0
tokens: list[str] = []
Expand All @@ -14,9 +20,9 @@ def lexicalize(stmt: str):
continue

if char in SEPARATORS and quote_count % 2 == 0:
if current_token not in {' ', '\n'}:
if current_token not in SP_LN:
tokens.append(current_token)
if char not in {' ', '\n'}:
if char not in SP_LN:
tokens.append(char)

current_token = ''
Expand All @@ -30,7 +36,7 @@ def order_words(tokens: list[str]):
if current `token+kw_in_statement` not in all keyword string, add `kw_in_statement` to `final_token`
if statement is ended, add `kw_in_statement` to `final_token`
"""
final_token: list[str] = []
final_token: Final[list[str]] = []
kw_in_statement = ''
temp = False
for tok in tokens:
Expand Down
3 changes: 2 additions & 1 deletion src-py/RickRoll.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from typing import Final
from traceback import format_exc


Expand Down Expand Up @@ -33,7 +34,7 @@ def main():
args = arg_parser.parse_args()

# excludes `def`s, `import`s and `argparse` times
start = time()
start: Final = time()
# Run the RickRoll program
if args.file:
# Convert .rickroll to C++
Expand Down
7 changes: 3 additions & 4 deletions src-py/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

from typing import Callable, Final


def starts_ends(container: str | list, x):
def starts_ends(s: str, char: str):
"""
Check if it starts and ends with the same value.
Check if it starts and ends with the same char.
Examples:
```
Expand All @@ -19,7 +18,7 @@ def starts_ends(container: str | list, x):
"""
# for some reason, type-inference only works if it explicitly returns a bool
# WTF?
return True if container[0] == x and container[-1] == x else False
return True if s[0] == char and s[-1] == char else False


join_list: Final[Callable[[list], str]] = lambda l: ''.join(map(str, l))
Expand Down
20 changes: 11 additions & 9 deletions src-py/interpreter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Final # explanation at Lexer.py

from sys import stdout
from time import time

from Keywords import *
from Lexer import lexicalize
from helpers import filter_str, precedence, starts_ends

start = time()
start: Final = time()

class AST:
def print_node(Node: list, args):
Expand Down Expand Up @@ -47,7 +49,7 @@ def __init__(self, tokens: list[list[str]], Node: list):
self.tokens = tokens

self.pos = 0
self.stmt = []
self.stmt: Final = []

while self.pos < len(self.tokens):
self.parse()
Expand Down Expand Up @@ -98,7 +100,7 @@ def parse(self):
Parser(tokens=child_stmts, Node=while_nodes)
AST.while_node(self.Node, cond, while_nodes)

def applyOp(a, b, op: str):
def applyOp(a: int | str, b: int | str, op: str) -> int | str:
if op == '+': return a + b
if op == '-': return a - b
if op == '*': return a * b
Expand All @@ -109,12 +111,12 @@ def applyOp(a, b, op: str):
or op==KW.GOE_OP.value and a>=b or op==KW.LOE_OP.value and a<=b \
else 'False'

def evaluate(tokens: list[str]):
def evaluate(tokens: str):
if len(tokens) == 1 and starts_ends(tokens[0], '"'):
return filter_str(tokens[0])

values = []
ops: list[str] = []
values: Final[list[int | str]] = []
ops: Final[list[str]] = []

for i in range(len(tokens)):
if not tokens[i]: return
Expand Down Expand Up @@ -153,13 +155,13 @@ def evaluate(tokens: list[str]):
values.append(applyOp(val1, val2, op))
return values[-1]

variables = {}
variables: Final[dict[str, int | str | None]] = {}

class Interpreter:
def __init__(self):
self.idx = 0

def interpret(self, nodes: list[list]):
def interpret(self, nodes: list | str):
for node in nodes:
self.idx += 1
if node[0] == "print_node":
Expand All @@ -179,7 +181,7 @@ def interpret(self, nodes: list[list]):

def run_in_interpreter(src_file_name: str):
intpr = Interpreter()
Node: list[list] = []
Node = []

with open(src_file_name, mode='r', encoding='utf-8') as src:
content = src.readlines()
Expand Down
2 changes: 1 addition & 1 deletion src-py/pyrickroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __make_token(self, tok: str):
class TranslateToPython:
def __init__(self):
# tokens
self.values = []
self.values: list[str] = []
self.is_main = False
self.is_function = False
self.indent_count = 0
Expand Down
4 changes: 2 additions & 2 deletions src-py/rickvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from helpers import filter_str, precedence, starts_ends


def applyOp(a: float | str, b: float | str, op: str):
def applyOp(a: int | str, b: int | str, op: str) -> int | str:
if op == '+': return a + b
if op == '-': return a - b
if op == '*': return a * b
Expand All @@ -27,7 +27,7 @@ def evaluate(tokens: list[str]):
return filter_str(tokens[0])
return tokens[0]

values: Final[list[float | str]] = []
values: Final[list[int | str]] = []
ops: Final[list[str]] = []

for i in range(len(tokens)):
Expand Down

0 comments on commit 753f1f7

Please sign in to comment.