Skip to content

Commit

Permalink
aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSecondComing123 committed Mar 8, 2022
1 parent 4bac5e9 commit 4b4eb0c
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 25 deletions.
33 changes: 23 additions & 10 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions elements.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
from functions import *
from typing import Callable


class Element:
def __init__(self, *, arity: int, func: Callable):
self.arity = arity
self.func = func

def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)


elements = {
"+": [2, Add],
"-": [2, Sub],
"×": [2, Mul],
"÷": [2, TrueDiv],
"ⁱ": [2, Power],
"g": [0, lambda *x: g], # g (and other constants) must be a lambda because the interpreter tries to call it
"¶": [1, Print]
"+": Element(arity=2, func=Add),
"-": Element(arity=2, func=Sub),
"×": Element(arity=2, func=Mul),
"÷": Element(arity=2, func=TrueDiv),
"ⁱ": Element(arity=2, func=Power),
"g": Element(arity=0, func=lambda *x: g),
# g (and other constants) must be a lambda because the interpreter tries to call it
"¶": Element(arity=1, func=Print)
}
8 changes: 8 additions & 0 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@


def gettypes(*a):
"""
Convert a list of objects to their types
"""
return list(map(type, a))


def typecheck(args: list, types: list, *, ordered=True):
"""
Check if the types of args match types. If ordered is true,
the check order is strict.
"""

if not ordered:
return Counter(gettypes(*args)) == Counter(types)
else:
Expand Down
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def cli():


def shell():
"""
Interactive shell
"""

while True:
code = input(">>> ")
if code == ":quit:" or code == ":exit:":
Expand Down
6 changes: 3 additions & 3 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ def parse(token_list: list[Token]) -> list[Token]:
if token.name == TokenType.FUNCTION:
if not tokens: # account for there not being anything
# after this token by just breaking the loop
parse_list.append(elements[token.value][1])
parse_list.append(elements[token.value].func)
break
arity = elements[token.value][0]
arity = elements[token.value].arity
temp = parse(list(tokens)) # This works
# because complete functions (functions and a complete
# number of constants/nilads) form single units, and non-
# complete functions (functions and a non-complete number
# of constants/nilads) can use those single units.
parse_list.append(
[elements[token.value][1]] + temp[:arity]
[elements[token.value].func] + temp[:arity]
) # Arity appended to list because function might have args
parse_list += temp[arity:]
break # break because everything is parsed (complete)
Expand Down
23 changes: 18 additions & 5 deletions structure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
class Structure:
def __repr__(self):
return "Structure()"
from tokenizer import Token # Used as a annotation


class ForLoop:
"""
For loop structure class.
Syntax:
def parse(self, token_list):
return token_list
↹number{body}
(Borrowed from SYNTAX.md)
"""

def __init__(self, number: Token, body: Token):
self.number = number
self.body = body

def __repr__(self):
return f"ForLoop(number={self.number}, body={self.body})"
10 changes: 10 additions & 0 deletions tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def __repr__(self):


def tokenize(text: str) -> list[Token]:
"""
Tokenize text
"""

tokens = []

current_number = False
Expand All @@ -61,6 +65,10 @@ def tokenize(text: str) -> list[Token]:
current_float = False
float_contents = ""

loop_started = False
loop_number = None
loop_contents = ""

for char in text:
if not IsType.number(char):
if char == ".": # decimal point
Expand All @@ -69,6 +77,8 @@ def tokenize(text: str) -> list[Token]:
current_float = True
current_number = False # so the next digits get added to number
number = ""
elif char == "↹": # for loop
loop_started = True

elif current_float:
float_contents += number
Expand Down

0 comments on commit 4b4eb0c

Please sign in to comment.