From 633e438712883bda5dabae706d91387c40fa81ed Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Thu, 11 Dec 2025 14:24:11 +1100 Subject: [PATCH] Update README, new tests --- README.md | 8 ++++---- tests/function.grnd | 13 +++++++++++++ tests/recursivefib.grnd | 24 ++++++++++++++++++++++++ tests/uhoh.grnd | 8 ++++++-- 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 tests/recursivefib.grnd diff --git a/README.md b/README.md index fabf90a..7cd9ef5 100644 --- a/README.md +++ b/README.md @@ -76,12 +76,12 @@ build - [x] Maths - [x] Comparisions - [ ] Type conversions - - [ ] Functions + - [x] Functions - [x] Define functions - [x] Call functions - - [ ] Return values (type checked) - - [ ] Arguments for functions - - [ ] Jumping within functions + - [x] Return values (type checked) + - [x] Arguments for functions + - [x] Jumping within functions - [ ] Custom data structures - [ ] Working with external libraries diff --git a/tests/function.grnd b/tests/function.grnd index c7a01fb..e61f5ce 100644 --- a/tests/function.grnd +++ b/tests/function.grnd @@ -2,5 +2,18 @@ fun !dingus -string return "dingle" endfun +fun !jumpy + set &x 0 + @loop + equal $x 10 &cond + if $cond %endloop + add 1 $x &x + println $x + jump %loop + @endloop +endfun + call !dingus &e println $e + +call !jumpy &x diff --git a/tests/recursivefib.grnd b/tests/recursivefib.grnd new file mode 100644 index 0000000..df24554 --- /dev/null +++ b/tests/recursivefib.grnd @@ -0,0 +1,24 @@ +fun !fib -int -int &n -function &fib + # Base case: fib(0) = 0, fib(1) = 1 + lesser $n 2 &isBase + if $isBase %baseCase + + # Recursive case: fib(n) = fib(n-1) + fib(n-2) + subtract $n 1 &nMinus1 + call !fib $nMinus1 $fib &fib1 + + subtract $n 2 &nMinus2 + call !fib $nMinus2 $fib &fib2 + + add $fib1 $fib2 &result + return $result + + @baseCase + return $n +endfun + +# Main program +println "Computing fib(30) recursively..." +call !fib 30 $fib &answer +println "Result:" $answer +end diff --git a/tests/uhoh.grnd b/tests/uhoh.grnd index 781fde2..200ebeb 100644 --- a/tests/uhoh.grnd +++ b/tests/uhoh.grnd @@ -1,3 +1,7 @@ -fun !dingle -int -function &in - call !in &tmp +fun !dingle -int -function &in -int &counter + add 1 $counter &counter + println "We are" $counter "iterations deep" + call !in $in $counter &out endfun + +call !dingle $dingle 0 &out