Skip to content

Commit

Permalink
add cli support
Browse files Browse the repository at this point in the history
and lots of other stuff
  • Loading branch information
mathcat4 committed Feb 15, 2022
1 parent 7bbbfcd commit 106847a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 30 deletions.
7 changes: 7 additions & 0 deletions context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""File for all context related stuff, like print str, flags, etc."""


class Context:
def __init__(self):
self.printed = False
self.print = ""
25 changes: 13 additions & 12 deletions functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from tokenizer import Token, TokenType

g = "Hello, World!"
w = "Hello World"
b = "0123456789"
Expand Down Expand Up @@ -27,26 +25,29 @@
p8 = 128


def Add(a1, a2):
def Add(a1, a2, ctx=None):
"""Add two numbers"""
return Token(TokenType.NUMBER, a1.value + a2.value)
return a1 + a2


def Sub(a1, a2):
def Sub(a1, a2, ctx=None):
"""Subtract two numbers"""
return Token(TokenType.NUMBER, a1.value - a2.value)
return a1 - a2


def Mul(a1, a2):
def Mul(a1, a2, ctx=None):
"""Multiplies too numbers"""
return Token(TokenType.NUMBER, a1.value * a2.value)
return a1 * a2


def TrueDiv(a1, a2):
def TrueDiv(a1, a2, ctx=None):
"""Divides two numbers"""
return Token(TokenType.NUMBER, a1.value / a2.value)
return a1 / a2


def Print(a1):
def Print(a1, ctx=None):
"""Prints something"""
print(a1)
ctx.print += str(a1) + "\n"
ctx.printed = True

return a1
23 changes: 8 additions & 15 deletions interpreter.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
from tokenizer import tokenize, Token
from elements import elements
from parse import parse
from helper import TokenList
from typing import Union


def interprete(tokens: list[Token]) -> Union[TokenList, None]:
def interprete(tokens: TokenList, ctx) -> Union[TokenList, None]:
if type(tokens) is list: # If tokens are grouped
if len(tokens) == 1 and type(tokens[0]) is list:
tokens = tokens[0]

func = elements.get(tokens[0].value) #
func = elements.get(tokens[0])

# Exit if func does not exist else get the lambda of the element
if func:
func = func[1]
else:
return

args = map(interprete, tokens[1:]) # Interprete every argument, recursive case
args = [] # Interprete every argument, recursive case
for token in tokens[1:]:
args.append(interprete(token, ctx=ctx))

args.append(ctx)

return func(*list(args)) # Call func on every interpreted argument

else:
return tokens


if __name__ == "__main__":
print(interprete(parse(tokenize("+1 3"))))
print(interprete(parse(tokenize("+1+3 4"))))
print(interprete(parse(tokenize("++1+3 2 5"))))
print(interprete(parse(tokenize("-2 1"))))
print(interprete(parse(tokenize("×7 2"))))
print(interprete(parse(tokenize("÷10 2"))))
interprete(parse(tokenize("¶+1 ×3 3")))
43 changes: 43 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from interpreter import interprete
from tokenizer import tokenize
from parse import parse
from context import Context
import argparse

parser = argparse.ArgumentParser(description="CLI for Excuting Noxan")

# (file name not supported yet)
parser.add_argument(
"--code", "-c", type=str, help="optional argument to pass code/file name"
)


def main(code):
"""Main function, which returns output of code"""
ctx = Context()
output = interprete(parse(tokenize(code)), ctx)
if ctx.printed:
return ctx.print
else:
return output


def cli():
"""Fucntion for CLI support"""
args = parser.parse_args()
if args.code:
print(main(args.code))
else:
shell()


def shell():
while True:
code = input(">>> ")
if code == ":quit:" or code == ":exit:":
break
print(main(code))


if __name__ == "__main__":
cli()
6 changes: 3 additions & 3 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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(token)
parse_list.append(token.value)
break
arity = elements[token.value][0]
temp = parse(list(tokens)) # We call parse on the remaining token
Expand All @@ -28,15 +28,15 @@ def parse(token_list: list[Token]) -> list[Token]:
# 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([token] + temp[:arity]) # Add a list of
parse_list.append([token.value] + temp[:arity]) # Add a list of
# this token plus however many grouped tokens the arity
# requires
parse_list += temp[arity:] # add the rest of the parsed
# tokens
break
# exit the loop because everything is parsed.
elif token.name == TokenType.NUMBER:
parse_list.append(token) # Numbers don't need anything else
parse_list.append(token.value) # Numbers don't need anything else

return parse_list

Expand Down

0 comments on commit 106847a

Please sign in to comment.