Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed May 31, 2022
1 parent 0d395e9 commit 61c526b
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 8 deletions.
3 changes: 3 additions & 0 deletions mlogpp/arrows.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
def generate(pos: int, len_: int):
"""
generate error arrows
"""
return (" " * pos) + ("^" * len_)
32 changes: 31 additions & 1 deletion mlogpp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .linker import Linker
from . import __version__

# input/output method
class IOMethod(Enum):
FILE = 0
STD = 1
Expand Down Expand Up @@ -40,56 +41,82 @@ class IOMethod(Enum):

verbose = False

# parse arguments
for k, v in vars(args).items():
if v:
if k.startswith("output"):
# output method

omethod = IOMethod.FILE if k.endswith("file") else IOMethod.STD if k.endswith("stdout") else IOMethod.CLIP
if omethod == IOMethod.FILE:
ofile = v
elif k.startswith("optimize"):
# optimization level

optlevel = int(k[-1])
elif k == "verbose":
# verbose

verbose = v

# check if files exist
for fn in args.file:
# exists or is `@clip`
if not os.path.isfile(fn) and fn != "@clip":
print(f"Error: input file \"{fn}\" does not exist")
sys.exit(1)

# read files
datas = []
for fn in args.file:
if fn == "@clip":
# clipboard

datas.append((fn, pyperclip.paste()))

else:
# file

with open(fn, "r") as f:
datas.append((fn, f.read()))

# optimization level presets to pass to the optimizer
optimization_levels = {
0: {"enable": False},
1: {"unused": False},
2: {}
}

# compile all files
outs = []
for data in datas:
if data[0].endswith(".mind"):
# skip if already compiler
if data[0].endswith(".mind") or data[0].endswith(".masm"):
outs.append(data[1])
continue

out = Lexer.preprocess(Lexer.lex(Lexer.resolve_includes(data[1])))
out = Parser().parse(out)
out = Generator().generate(out, optimization_levels[optlevel])
# add to compiled files
outs.append(out)

# link the compiled files
if len(outs) > 1:
out = Linker.link(outs).strip()
else:
out = outs[0].strip()

if omethod == IOMethod.FILE:
# output to file

with open(ofile, "w+") as f:
f.write(out)

elif omethod == IOMethod.STD:
# output to stdout

# check if line numbers should be prined
if vars(args)["lines"]:
lines = out.splitlines()
maxline = len(str(len(lines)))
Expand All @@ -100,7 +127,10 @@ class IOMethod(Enum):

if verbose:
print()

elif omethod == IOMethod.CLIP:
# output to clipboard

pyperclip.copy(out)

if verbose:
Expand Down
11 changes: 11 additions & 0 deletions mlogpp/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
from . import arrows
from .formatting import Format

# debug flags
PARSE_ERROR_DEBUG = False
LINK_ERROR_DEBUG = False

def parse_error(tok: Token, msg: str) -> None:
"""
raise parser error
"""

# debug
if PARSE_ERROR_DEBUG:
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
Expand All @@ -19,6 +25,11 @@ def parse_error(tok: Token, msg: str) -> None:
sys.exit(1)

def link_error(pos: Position, msg: str) -> None:
"""
raise linker error
"""

# debug
if LINK_ERROR_DEBUG:
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
Expand Down
4 changes: 4 additions & 0 deletions mlogpp/formatting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Format:
"""
formatting codes
"""

RED = "\033[91m"
BLUE = "\033[94m"
GREEN = "\033[92m"
Expand Down
10 changes: 10 additions & 0 deletions mlogpp/functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
def gen_signature(name: str, params: list):
"""
generate a function signature
"""
return f"{name}:{len(params)}"

# native functions
native = [
"read", "write",
"draw", "drawflush",
Expand All @@ -15,6 +19,7 @@ def gen_signature(name: str, params: list):
"ubind", "ucontrol", "uradar", "ulocate"
]

# number of parameters to native functions
native_params = {
"read": 3, "write": 3,
"draw": 7, "drawflush": 1,
Expand All @@ -29,6 +34,7 @@ def gen_signature(name: str, params: list):
"ubind": 1, "ucontrol":6, "uradar": 6, "ulocate": 8
}

# native subcommands
native_sub = {
"draw": {
"clear": 3,
Expand Down Expand Up @@ -77,6 +83,7 @@ def gen_signature(name: str, params: list):
}
}

# builtin operators
builtin = [
"mod",
"pow",
Expand All @@ -91,6 +98,7 @@ def gen_signature(name: str, params: list):
"len"
]

# number of parameters to builtin operators
builtin_params_default = 1
builtin_params = {
"mod": 2,
Expand All @@ -103,6 +111,8 @@ def gen_signature(name: str, params: list):
"len": 2
}

# special keywords
keywords = ["if", "else", "while", "for", "function", "repeat"]

# special identifiers
special = native + builtin + keywords
Loading

0 comments on commit 61c526b

Please sign in to comment.