From 0b6d74d3a9deaa8c3b79a0c1b1e0705b3e4f7358 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Sun, 31 Aug 2025 13:48:32 +1000 Subject: [PATCH] initial commit --- console.py | 4 +++ error.py | 23 ++++++++++++++++ main.py | 19 +++++++++++++ test.grnd | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test2.grnd | 4 +++ 5 files changed, 129 insertions(+) create mode 100644 console.py create mode 100644 error.py create mode 100644 main.py create mode 100644 test.grnd create mode 100644 test2.grnd diff --git a/console.py b/console.py new file mode 100644 index 0000000..6c3f330 --- /dev/null +++ b/console.py @@ -0,0 +1,4 @@ +from rich.console import Console + + +console = Console() \ No newline at end of file diff --git a/error.py b/error.py new file mode 100644 index 0000000..959f52a --- /dev/null +++ b/error.py @@ -0,0 +1,23 @@ +from console import console + + +def traceback(code: str, error_type: str, error_message: str, line: int | None = None, start_column: int | None = None, end_column: int | None = None): + if line != None: + console.print(f"[bold red]{error_type} on line {line}: [/]{error_message}\n") + lines = code.split("\n")[line-1:line+2] + + console.print(f"{line } > " + lines[0], highlight=False) + if start_column != None and end_column != None: + console.print(" " + (" " * start_column) + "[bold red]" + ("^" * (end_column-start_column+1))) + + try: + console.print(f"{line+1} " + lines[1], highlight=False) + console.print(f"{line+2} " + lines[2], highlight=False) + console.print(" ...", highlight=False) + except IndexError: # the file is less than 3 lines i guess + pass + else: + console.print(f"[bold red]{error_type}: {error_message}") + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..fdbd177 --- /dev/null +++ b/main.py @@ -0,0 +1,19 @@ +from tokenizer import tokenize +from rich import print +from time import time + + +def main(): + start = time() + file = open("test.grnd", "r") + code = file.read() + file.close() + + tokens = tokenize(code) + compile_time = time()-start + print(tokens) + print(f"Compiled in {compile_time} seconds.") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/test.grnd b/test.grnd new file mode 100644 index 0000000..33337bf --- /dev/null +++ b/test.grnd @@ -0,0 +1,79 @@ +# checks if the current index in the determiner matches the current index in the target string +fun -bool !currentCharsMatch -string &str -string &determiner -int &counter -int &detIndex + getstrcharat $str $counter ¤tChar + getstrcharat $determiner $detIndex ¤tDetChar + equal $currentChar $currentDetChar &equals + return $equals +endfun + +fun -list !split -string &str -string &determiner + # create an empty list + setlist *output "" + set &counter 0 + set &detIndex 0 + set ¤tLoop "" # basically we build out the current token until we reach the determiner + getstrsize $str &length + getstrsize $determiner &determinerLength + + # go through each char in the string, does it match the first char in our determiner? + #@loop + # we are technically getting the current char twice + # 1. inside the currentCharsMatch function + # 2. here + # but oh well, it wont be that bad (i hope) + getstrcharat $str $counter ¤tChar + + pusharg $str + pusharg $determiner + pusharg $counter + pusharg $detIndex + call !currentCharsMatch &equals + not $equals &doesntMatch + if $doesntMatch %next + + stdlnout "WE HAVE A MATCH" + set &detIndex 0 + # WE HAVE A MATCH BABY, we gotta make sure it matches the whole determiner tho + @innerLoop + + pusharg $str + pusharg $determiner + pusharg $counter + pusharg $detIndex + call !currentCharsMatch &equals + add $detIndex 1 &detIndex + equal $detIndex $determinerLength &endOfDet + if $endOfDet %append + if $equals %innerLoop + + @append + listappend *output $currentLoop + set ¤tLoop "" + set &detIndex 0 + + @next + add $currentLoop $currentChar ¤tLoop + # next character in the string + add $counter 1 &counter + inequal $length $counter ¬AtEnd + stdlnout $notAtEnd + if $notAtEnd %loop + + return *output +endfun + +pusharg "split this string" +pusharg " " +call !split *listOut +#getlistsize *listOut &length +#stdlnout $length + +set &counter 0 + +@loopOverList +getlistat *listOut $counter ¤tItem +stdlnout $currentItem +add $counter 1 &counter +getlistsize *listOut &length +inequal $counter $length ¬Done +if $notDone %loopOverList \ No newline at end of file diff --git a/test2.grnd b/test2.grnd new file mode 100644 index 0000000..16717a5 --- /dev/null +++ b/test2.grnd @@ -0,0 +1,4 @@ +set &x 0 +@loop +add $x 1 &x +jump %loop \ No newline at end of file