From d340a05b61b6cfd7f4d5c24eae1f44f7ec862aa0 Mon Sep 17 00:00:00 2001 From: Scarlett Date: Tue, 6 May 2025 19:10:18 -0400 Subject: [PATCH] fib --- tests/programs/fib.alpha | 84 ++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/tests/programs/fib.alpha b/tests/programs/fib.alpha index 9105a9a..bbc20f9 100644 --- a/tests/programs/fib.alpha +++ b/tests/programs/fib.alpha @@ -1,39 +1,63 @@ -(* TEST: [-asc -tc -cg -ir] *) - #include "std.alpha" function entry : string2integer -function Fib : integer2integer +function fib : integer2integer -Fib(i) := { - [integer: a; integer: b ; Boolean: d ; integer: c] - (*if( i = 0 ) then { - return 7; - } else { - i := i; - }*) - (*b := b | (a & c); - b := 2 < 3;*) - - (* `if(i = 1) then { return 1; } else { - return Fib(i - 1) + Fib(i - 2); - } -*) +fib(i) := { + [ integer: a; integer: b; integer: count ] + + a := 1; + b := 2; - a := 0; - b := 1; - while(0 < i) { - c := a + b; - a := b; - b := c; - } - return c; + if (i = 0) then { + return 0; + } else { + a := a; + } + + if (i = 1) then { + return 1; + } else { + a := a; + } + + if (i = 2) then { + return 2; + } else { + a := a; + } + + count := 2; + while (count < i) { + + count := count + 1; + } + + return b; } entry (arg) := { - [ integer: x; integer: y ] - x := 2; -(* x := Fib(2);*) - y := printInteger(Fib(3)); - return 1; + [ integer: result; integer: input; integer: fibValue] + + result := printC('E'); + result := printC('n'); + result := printC('t'); + result := printC('e'); + result := printC('r'); + result := printC(' '); + result := printC('N'); + result := printC('u'); + result := printC('m'); + result := printC('b'); + result := printC('e'); + result := printC('r'); + result := printC(':'); + result := printC(' '); + + input := inI(1); + fibValue := fib(input); + + result := printI(fibValue); + + return 0; }