From b92f99cc70d753fdac1a1b6126c0d680e527ec54 Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Sat, 13 Sep 2025 10:10:21 +1000 Subject: [PATCH] Added programs/bf.grnd --- packages/lists/lists.grnd | 24 ++++ packages/lists/readme.md | 1 + programs/bf.grnd | 225 ++++++++++++++++++++++++++++++++++++++ programs/test.grnd | 1 + 4 files changed, 251 insertions(+) create mode 100644 packages/lists/lists.grnd create mode 100644 packages/lists/readme.md create mode 100644 programs/bf.grnd create mode 100644 programs/test.grnd diff --git a/packages/lists/lists.grnd b/packages/lists/lists.grnd new file mode 100644 index 0000000..3617706 --- /dev/null +++ b/packages/lists/lists.grnd @@ -0,0 +1,24 @@ +# Converts list to string +fun -string !ltos -list &inlist + getlistsize *inlist &listlen + set &idx 0 + set &ans "" + add $ans '[' &ans + @stdoutloop + getlistat *inlist $idx &store + add $ans $store &ans + add $idx 1 &idx + equal $idx $listlen &store + if $store %trueend + add $ans ", " &ans + jump %stdoutloop + + @trueend + add $ans "]" &ans + return $ans +endfun + +setlist *store 4 2 "Hello" +pusharg *store +call !ltos &store +stdlnout $store \ No newline at end of file diff --git a/packages/lists/readme.md b/packages/lists/readme.md new file mode 100644 index 0000000..bdf86e8 --- /dev/null +++ b/packages/lists/readme.md @@ -0,0 +1 @@ +### Due to the current limitations of ground, this package is not ready for use. \ No newline at end of file diff --git a/programs/bf.grnd b/programs/bf.grnd new file mode 100644 index 0000000..2887d30 --- /dev/null +++ b/programs/bf.grnd @@ -0,0 +1,225 @@ +stdout "Brainf*ck Code: " +stdin &str +stdout "Input: " +stdin &input +set &idx 0 +setlist *cells 0 +setlist *startloop +set &loopdepth 0 +set &ignore false +set &selectedcell 0 +set &inputidx 0 +getstrsize $str &len + +setlist *ascii ' ' '!' '"' '#' '$' '%' '&' "'" '(' ')' '*' '+' ',' '-' '.' '/' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ':' ';' '<' '=' '>' '?' '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' '[' '\' ']' '^' '_' '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '{' '|' '}' '~' + +@loop + equal $idx $len &store + if $store %end + + getstrcharat $str $idx &idxchar + + equal $idxchar '+' &store + if $store %plus + + equal $idxchar '-' &store + if $store %minus + + equal $idxchar '>' &store + if $store %right + + equal $idxchar '<' &store + if $store %left + + equal $idxchar '[' &store + if $store %leftbracket + + equal $idxchar ']' &store + if $store %rightbracket + + equal $idxchar ',' &store + if $store %input + + equal $idxchar '.' &store + if $store %output + + jump %increment + + @plus + if $ignore %increment + lesser $selectedcell 0 &store + if $store %rangeerror + getlistat *cells $selectedcell &store + add $store 1 &store + setlistat *cells $selectedcell $store + inequal $store 256 &store + if $store %increment + setlistat *cells $selectedcell 0 + jump %increment + + @minus + if $ignore %increment + lesser $selectedcell 0 &store + if $store %rangeerror + getlistat *cells $selectedcell &store + subtract $store 1 &store + setlistat *cells $selectedcell $store + inequal $store -1 &store + if $store %increment + setlistat *cells $selectedcell 255 + jump %increment + + @right + if $ignore %increment + add $selectedcell 1 &selectedcell + + # Check if new cell needs to be added + getlistsize *cells &store + lesser $selectedcell $store &store + if $store %increment + listappend *cells 0 + jump %increment + + @left + if $ignore %increment + subtract $selectedcell 1 &selectedcell + jump %increment + + @leftbracket + if $ignore %customignoreleft + listappend *startloop 0 + setlistat *startloop $loopdepth $idx + set &ignoreidx $loopdepth + add $loopdepth 1 &loopdepth + + getlistat *cells $selectedcell &store + inequal $store 0 &store + if $store %increment + + # If selected cell is 0, jump to the matching right bracket (by ignoring all other instructions) + set &ignore true + jump %increment + + @customignoreleft + add $loopdepth 1 &loopdepth + jump %increment + + @rightbracket + subtract $loopdepth 1 &loopdepth + if $ignore %continue + getlistat *startloop $loopdepth &store + set &idx $store + # Don't increment + jump %loop + + @continue + inequal $loopdepth $ignoreidx &store + if $store %increment + set &ignore false + jump %increment + + @input + if $ignore %increment + lesser $selectedcell 0 &store + if $store %rangeerror + + getstrsize $input &store + equal $inputidx $store &store + if $store %inputzero + + getstrcharat $input $inputidx &inputstore + tostring $inputstore &store + equal $store "'" &store + if $store %singlequote + set &asciiidx 0 + @findascii + getlistsize *ascii &store + equal $store $asciiidx &store + if $store %noascii + + equal $asciiidx 7 &store + if $store %asciiincrement + getlistat *ascii $asciiidx &store + equal $store $inputstore &store + if $store %exitascii + @asciiincrement + add $asciiidx 1 &asciiidx + jump %findascii + + @exitascii + add $asciiidx 32 &asciiidx + setlistat *cells $selectedcell $asciiidx + + add $inputidx 1 &inputidx + jump %increment + + @singlequote + setlistat *cells $selectedcell 39 + add $inputidx 1 &inputidx + jump %increment + + @inputzero + setlistat *cells $selectedcell 0 + add $inputidx 1 &inputidx + jump %increment + + @noascii + error "Error: Invalid ASCII output code" + + @output + if $ignore %increment + lesser $selectedcell 0 &store + if $store %rangeerror + + # 0-31 is not doing anything, except for 10 which is newline + getlistat *cells $selectedcell &store + equal $store 10 &store + if $store %newline + + getlistat *cells $selectedcell &store + lesser $store 32 &store + if $store %blank + + getlistat *cells $selectedcell &store + greater $store 126 &store + if $store %blank + + getlistat *cells $selectedcell &store + subtract $store 32 &store + getlistat *ascii $store &store + stdout $store + jump %increment + + @newline + stdlnout "" + jump %increment + + @blank + stdout " " + jump %increment + + + @increment + add $idx 1 &idx + jump %loop + + @rangeerror + error "Cannot edit garbage data (negative cell index)" +@end + +# Output list +stdlnout "" +getlistsize *cells &listlen +set &idx 0 +stdout "[" +@stdoutloop + getlistat *cells $idx &store + stdout $store + add $idx 1 &idx + equal $idx $listlen &store + if $store %trueend + stdout ", " + jump %stdoutloop + +@trueend +stdout "]" \ No newline at end of file diff --git a/programs/test.grnd b/programs/test.grnd new file mode 100644 index 0000000..289adc5 --- /dev/null +++ b/programs/test.grnd @@ -0,0 +1 @@ +'\'' \ No newline at end of file