Skip to content

Commit

Permalink
add for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mathcat4 committed Mar 11, 2022
1 parent 1edac9a commit eaff587
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 11 deletions.
7 changes: 5 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Update 3/9/2022:
    Added the int input element (Ŋ) and the sum digits/sum code points element. (Đ)
- Added the int input element (Ŋ) and the sum digits/sum code points element. (Đ)

Update 3/8/2022:
    Changed elements.py and parser.py to use the Element(arity, func) syntax instead of a list.
- Changed elements.py and parser.py to use the Element(arity, func) syntax instead of a list.

Update 3/11/2022:
- Add For Loop support with structure.py and unpack.py
2 changes: 1 addition & 1 deletion functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def TrueDiv(a, b, ctx=ctx):

def Print(a, ctx=ctx):
"""Print with newline"""
ctx.print = print_with_newline(a)
ctx.print += print_with_newline(a)
ctx.printed = True
return a

Expand Down
4 changes: 3 additions & 1 deletion interpreter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any
from structure import structure

from tokenizer import tokenize
from parse import parse
from context import Context
from unpack import unpack


class Interprete:
Expand Down Expand Up @@ -50,4 +52,4 @@ def interprete(self, tokens: list[Any], ctx) -> Any:


def run(text, ctx=Context()):
return Interprete().main(parse(tokenize(text)), ctx)
return Interprete().main(parse(unpack(structure(tokenize(text)))), ctx)
5 changes: 4 additions & 1 deletion structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __repr__(self):
return f"ForLoop(number={self.number}, body={self.body})"


def structure(tokens):
def structure_forLoop(tokens):
"""
Groups tokens together to structures
"""
Expand Down Expand Up @@ -51,6 +51,9 @@ def structure(tokens):

return structured_tokens

def structure(tokens):
return structure_forLoop(tokens)


if __name__ == "__main__":
print(structure(tokenize("+2 3↹4{¶1} 5")))
Expand Down
6 changes: 1 addition & 5 deletions tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,4 @@ def tokenize(text: str) -> list[Token]:


if __name__ == "__main__":
print(tokenize("+7 59*89 / 207"))
print(tokenize("+1 +3 4"))
print(tokenize('2 "s1" 3 "s2" "s3'))
print(tokenize('12.34 2 "abc" 3.7'))
print(tokenize("↹2{¶1}"))
print(tokenize("+1 2"))
14 changes: 14 additions & 0 deletions unittests/noxan_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest, init

from interpreter import run
from tokenizer import tokenize, Token, TokenType
from structure import structure, ForLoop


class TokenizerTests(unittest.TestCase):
def test_tokenizer(self):
pass


if __name__ == "__main__":
unittest.main()
11 changes: 10 additions & 1 deletion unpack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from tokenizer import Token
from tokenizer import Token, tokenize
from structure import ForLoop
from structure import structure


def unpack_forLoop(tokens):
Expand All @@ -13,3 +14,11 @@ def unpack_forLoop(tokens):
unpacked_tokens.extend(token.body)

return unpacked_tokens


def unpack(tokens):
return unpack_forLoop(tokens)


if __name__ == "__main__":
print(unpack(structure(tokenize("+2 3↹4{¶1} 5"))))

0 comments on commit eaff587

Please sign in to comment.