Skip to content

Commit

Permalink
some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mathcat4 committed Mar 10, 2022
1 parent 6c0c504 commit c028699
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__pycache__/
venv/
.venv/
19 changes: 8 additions & 11 deletions element_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,14 @@ def sumascii(a: str):
return sum(bytes(a, encoding="utf-8"))


def intinput():
start = 0
s = ""

for char in stdin.read():
if char in "0123456789":
s += char
else:
break

return int(s)
def general_input():
inp = input("> ")
if inp.isdigit():
return int(inp)
elif inp.replace('.','',1).isdigit():
return float(inp)

return inp


def repeat(a: str, b: int):
Expand Down
4 changes: 2 additions & 2 deletions elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def __call__(self, *args, **kwargs):
# g (and other constants) must be a lambda because the interpreter tries to call it
"¶": Element(arity=1, func=Print),
"Đ": Element(arity=1, func=SumDigits),
"Ŋ": Element(arity=0, func=IntInput),
"Ƣ": Element(arity=2, func=Mod)
"Ŋ": Element(arity=0, func=GeneralInput),
"Ƣ": Element(arity=2, func=Mod),
}
4 changes: 2 additions & 2 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def SumDigits(a, ctx=None):
return sumascii(a)


def IntInput(ctx=None):
return intinput()
def GeneralInput(ctx=None):
return general_input()


def Mod(a, b, ctx=None):
Expand Down
6 changes: 3 additions & 3 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def typecheck(args: list, types: list, *, ordered=True):
if len(args) == len(types):
return all(isinstance(a, b) for a, b in zip(args, types))
elif len(args) == 1 and len(types) > 1:
for t in types:
if isinstance(args[0], t):
return t
for typ in types:
if isinstance(args[0], typ):
return typ
return False
except (TypeError, IndexError):
raise TypeError(f"Did you mean: [{args}]?") from None
Expand Down
3 changes: 2 additions & 1 deletion interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def main(self, tokens: list[Any], ctx):
self.output = []
main_output = self.interprete(tokens, ctx)

self.output.insert(0, main_output) if main_output else None
self.output.insert(0, main_output) if main_output != None else None

return self.output

def interprete(self, tokens: list[Any], ctx) -> Any:
Expand Down
21 changes: 11 additions & 10 deletions structure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tokenizer import Token, tokenize # Token() is used as a annotation
from tokenizer import Token, TokenType, tokenize


class ForLoop:
Expand Down Expand Up @@ -26,31 +26,32 @@ def structure(tokens):

loop_started = 0
loop_content = []
loop_number = None
loop_number = Token(TokenType.NUMBER, 10)

structured_tokens = []

for t in tokens:
if t.value == "↹":
for token in tokens:
if token.value == "↹":
loop_started = 1 # stage 1, outside {}
elif t.value == "{":
elif token.value == "{":
loop_started = 2 # stage 2, inside {}
elif t.value == "}": # end of loop
elif token.value == "}": # end of loop
structured_tokens.append(ForLoop(loop_number, loop_content))

loop_started = 0
loop_content = []
loop_number = None
loop_number = Token(TokenType.NUMBER, 10)
else:
if loop_started == 1:
loop_number = t # loop number
loop_number = token # loop number
elif loop_started == 2:
loop_content.append(t) # loop body
loop_content.append(token) # loop body
elif loop_started == 0: # outside loop
structured_tokens.append(t)
structured_tokens.append(token)

return structured_tokens


if __name__ == "__main__":
print(structure(tokenize("+2 3↹4{¶1} 5")))
print(structure(tokenize("+2 3↹{¶1} 5")))
5 changes: 3 additions & 2 deletions unittests/general_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import unittest, init

from interpreter import run


Expand All @@ -8,5 +9,5 @@ def test_basicops(self):
self.assertEqual(run("-2 1"), [1])


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions unittests/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
import sys
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)
6 changes: 3 additions & 3 deletions unittests/typecheck_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import unittest, init

from helper import typecheck


Expand All @@ -8,6 +9,5 @@ def test_typecheck(self):
self.assertEqual(typecheck(["abc", [1, 2], 9.0], [str, list, float]), True)
self.assertEqual(typecheck([[]], [str, int, float, tuple, list]), list)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit c028699

Please sign in to comment.