Skip to content

Commit

Permalink
Better error report, escape
Browse files Browse the repository at this point in the history
  • Loading branch information
BeOnDrag committed Dec 12, 2022
1 parent 7ce933b commit dd791b1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ main.spec
dist-*/
build/
assets/
run-miled.spec
run-miled.spec
pyinstaller-build
2 changes: 2 additions & 0 deletions interpreter/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def map_over(f: Caller, old_list: list, table: Env):
"wS": Caller(1, lambda x, _: (x[0][1].split(), None)),
",S": Caller(1, lambda x, _: (x[0][1].split(","), None)),

"join": Caller(2, lambda x, _: (x[0][1].join(x[1][1]), None)),

"map": Caller(2, lambda x, t: (map_over(x[0][1], x[1][1], t), None)),

# * ARITHMETIC FUNCTIONS
Expand Down
9 changes: 8 additions & 1 deletion interpreter/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def to_token(buf: str) -> Token:
if buf[0] == "\"" and buf[-1] == "\"":
return Token(
Token.VAL,
buf[1:-1].replace("\\\"", "\"").replace("\\\\", "\\")
buf[1:-1]
.replace(r"\"", "\"")
.replace(r"\\", "\\")
.replace(r"\n", "\n")
.replace(r"\t", "\t")
.replace(r"\r", "\r")
)
elif is_int(buf):
return Token(Token.VAL, int(buf))
Expand All @@ -45,6 +50,8 @@ def to_token(buf: str) -> Token:
return Token(Token.VAL, False)
elif buf == "??":
return Token(Token.VAL, None)
elif buf == "[]":
return Token(Token.VAL, [])
elif buf in KEYWORDS:
return Token(Token.KW, buf)
else:
Expand Down
13 changes: 6 additions & 7 deletions interpreter/readable_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ def test_interpreter(self):
)
self.assertEqual(
# A FizzBuzz Example
" ".join([
"\n".join([
"FizzBuzz" if i % 3 == 0 and i % 5 == 0 else
"Fizz" if i % 3 == 0 else
"Buzz" if i % 5 == 0
else str(i)
for i in range(1, 101)
]) + " ",
Interpreter("""
]),
Interpreter(r"""
:= x 0
:= s ""
:= s []
while: ~ += x 1 != x 101 ;
+= s if::
push s if::
& div 3 x div 5 x "FizzBuzz"
else
if:: div 3 x "Fizz"
Expand All @@ -131,9 +131,8 @@ def test_interpreter(self):
:fi
:fi
:fi
+= s " "
:ihw
s
join "\n" s
""").run()
)
self.assertEqual(
Expand Down
26 changes: 15 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from interpreter.interpret import Interpreter
from argparse import ArgumentParser


def try_run(code: str):
itpr = Interpreter(code)
try:
print(itpr.run())
except Exception as IE:
print("Cannot run program, an error occurred:")
print(IE)
print("near: \"" + str(itpr.tokens[itpr.i].value) + "\" at word #" + str(itpr.i))
print("Check your code. If you confirm there shouldn't be any problem, report an issue.")


if __name__ == "__main__":
parser = ArgumentParser("Miled language interpreter")
parser.add_argument("-f", "--file")
Expand All @@ -10,19 +22,11 @@
try:
if args.file is not None:
file = open(args.file, "r").read()
print(Interpreter(file).run())
try_run(file)
elif args.code is not None:
print(Interpreter(args.code).run())
try_run(args.code)
except FileNotFoundError:
print("File not found.")
except IndexError as E:
print(E)
print("An IndexError occurred. It's likely that you've used an index out of range, or you have too many "
"enclosing marks. Check your code.")
except TypeError as E:
print(E)
print("A TypeError occurred. It's likely that you've forgot to enclose a Caller, or you messed "
"up the types. Use \"->...\" commands to convert types.")
except Exception as E:
print("An error occurred.")
print("Unknown error occurred:")
print(E)

0 comments on commit dd791b1

Please sign in to comment.