Skip to content

Commit

Permalink
3 birds, one stone...
Browse files Browse the repository at this point in the history
  • Loading branch information
Butterroach committed Jun 15, 2024
1 parent 3088d80 commit e8ad623
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
63 changes: 59 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ class NoSuchVariable(SlangError):
"""


class FileNotFound(SlangError):
"""
File [msg] doesn't exist
"""


class PermissionDenied(SlangError):
"""
Couldn't do operation on file [msg]
"""


class Teenager(SlangError):
"""
A script can't run itself
"""


fileslines = {}


def split_args(text):
words = []
current_word = ""
Expand Down Expand Up @@ -255,6 +276,33 @@ def slang_input(msg: str = None):
return input(msg)


def run(filename: str):
global linec, currentfile, currentfilecons
try:
with open(filename, "r") as f:
slang_code = f.read()
except PermissionError:
raise PermissionDenied(filename)
except FileNotFoundError:
raise FileNotFound(filename)
fileslines[filename] = 0
fileslines[currentfile] = linec
prevfile = currentfile
currentfile = filename
linec = fileslines[filename]
prevfilecons = currentfilecons
currentfilecons = slang_code
try:
if slang_code == fread:
raise Teenager()
except NameError:
pass
process_function_calls(slang_code, functions)
currentfile = prevfile
linec = fileslines[currentfile]
currentfilecons = prevfilecons


slang_vars = {}
functions = [
("print", slang_print, "Prints output."),
Expand Down Expand Up @@ -312,6 +360,11 @@ def slang_input(msg: str = None):
slang_input,
"Reads input from stdin. Can take an argument specifying a message to display to the user.",
),
(
"run",
run,
"Runs another Slang file. This can be used for functions AND modules at the same time. Variables are shared, so that's function parameters for ya. 3 birds with one stone.",
),
]


Expand Down Expand Up @@ -343,6 +396,7 @@ def replace_error_msg(e: SlangError, code: str):
and (sys.argv[1] in ("--shh", "-s") if len(sys.argv) > 1 else True)
)
):
currentfile = "///SHELL"
if "--shh" not in sys.argv and "-s" not in sys.argv:
print(f"Slang v{__version__}")
print()
Expand All @@ -364,8 +418,7 @@ def replace_error_msg(e: SlangError, code: str):
print(code)
print(f"\u001b[31mERROR! {replace_error_msg(e, code)}\u001b[0m")
except Exception as e:
# print(f"\u001b[31mUH OH! PYTHON ERROR!\t{type(e).__name__}: {e}\u001b[0m")
raise e
print(f"\u001b[31mUH OH! PYTHON ERROR!\t{type(e).__name__}: {e}\u001b[0m")


if __name__ == "__main__" and len(sys.argv) > 1 and sys.argv[1] == "help":
Expand Down Expand Up @@ -402,18 +455,20 @@ def replace_error_msg(e: SlangError, code: str):


if __name__ == "__main__" and len(sys.argv) > 1:
currentfile = sys.argv[1]
try:
with open(sys.argv[1], "r") as f:
fread = f.read()
except FileNotFoundError:
print("that file doesn't exist dumbass")
os._exit(1)
currentfilecons = fread
try:
process_function_calls(fread, functions)
except SlangError as e:
print(
f"\u001b[1m\u001b[31mLINE {linec+1}: \u001b[0m\u001b[4m\u001b[32m{fread.splitlines()[linec]}\u001b[0m"
f"\u001b[1m\u001b[31mLINE {linec+1} IN {currentfile}: \u001b[0m\u001b[4m\u001b[32m{currentfilecons.splitlines()[linec]}\u001b[0m"
)
print(
f"\u001b[1m\u001b[31mERROR! {replace_error_msg(e, fread.splitlines()[linec])}\u001b[0m"
f"\u001b[1m\u001b[31mERROR! {replace_error_msg(e, currentfilecons.splitlines()[linec])}\u001b[0m"
)
4 changes: 4 additions & 0 deletions test.sponge
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ eval_py "print(\"Python expression!\")"
exec_py "print(\"what the fuck is this -> \\\"\\\\\\\\\\\"\")"
var fun out pow 2 7
print $out

var int x 1
print test1
run "test2.sponge"
20 changes: 20 additions & 0 deletions test2.sponge
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Slang -- A simple scripting language
# Copyright (C) 2024 Butterroach

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

print $x
var fun x add $x 1
run test3.sponge
print test2
print $y
18 changes: 18 additions & 0 deletions test3.sponge
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Slang -- A simple scripting language
# Copyright (C) 2024 Butterroach

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

print test3
print $x
var int y 91

0 comments on commit e8ad623

Please sign in to comment.