initial commit

This commit is contained in:
2025-08-31 13:48:32 +10:00
parent ea93fdb4f2
commit 0b6d74d3a9
5 changed files with 129 additions and 0 deletions

4
console.py Normal file
View File

@@ -0,0 +1,4 @@
from rich.console import Console
console = Console()

23
error.py Normal file
View File

@@ -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}")

19
main.py Normal file
View File

@@ -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()

79
test.grnd Normal file
View File

@@ -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 &currentChar
getstrcharat $determiner $detIndex &currentDetChar
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 &currentLoop "" # 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 &currentChar
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 &currentLoop ""
set &detIndex 0
@next
add $currentLoop $currentChar &currentLoop
# next character in the string
add $counter 1 &counter
inequal $length $counter &notAtEnd
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 &currentItem
stdlnout $currentItem
add $counter 1 &counter
getlistsize *listOut &length
inequal $counter $length &notDone
if $notDone %loopOverList

4
test2.grnd Normal file
View File

@@ -0,0 +1,4 @@
set &x 0
@loop
add $x 1 &x
jump %loop