-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
and lots of other stuff
- Loading branch information
Showing
5 changed files
with
74 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters