diff --git a/main.py b/main.py index 37c3475..d4b3a40 100644 --- a/main.py +++ b/main.py @@ -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 = "" @@ -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."), @@ -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.", + ), ] @@ -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() @@ -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": @@ -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" ) diff --git a/test.sponge b/test.sponge index 9add62d..b5f6e29 100644 --- a/test.sponge +++ b/test.sponge @@ -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" diff --git a/test2.sponge b/test2.sponge new file mode 100644 index 0000000..95b0b2e --- /dev/null +++ b/test2.sponge @@ -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 . + +print $x +var fun x add $x 1 +run test3.sponge +print test2 +print $y \ No newline at end of file diff --git a/test3.sponge b/test3.sponge new file mode 100644 index 0000000..23d5e91 --- /dev/null +++ b/test3.sponge @@ -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 . + +print test3 +print $x +var int y 91 \ No newline at end of file