Skip to content

Commit d5d9ec1

Browse files
committed
fib added
1 parent 0ab9cc9 commit d5d9ec1

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

build.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cd c64/hello
77
bash build.sh
88
ls -lh
99
cd $ROOTPATH
10-
# cd c64/fibonacci
11-
# bash build.sh
12-
# ls -lh
13-
# cd $ROOTPATH
10+
cd c64/fibonacci
11+
bash build.sh
12+
ls -lh
13+
cd $ROOTPATH

c64/fibonacci/build.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
ldmd2 -i \
44
-betterC \
5-
-I ../../modules/common \
6-
hello.d \
5+
-I$PWD/../../modules \
6+
src/fib.d \
77
-mtriple=mos-unknown-unknown \
88
--release \
99
-O \
10+
-L-lprintf_flt \
1011
-of=fib.prg \
1112
-gcc=mos-c64-clang \
1213
-linker=lld

c64/fibonacci/src/fib.d

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import common : printf;
2+
3+
T fibonacci(T)(T n)
4+
{
5+
static if (is(T == int))
6+
{
7+
T go(T n, T prev, T curr)
8+
{
9+
if (n == 0)
10+
return prev;
11+
else
12+
return go(n - 1, curr, prev + curr);
13+
}
14+
15+
return go(n, 0, 1);
16+
17+
}
18+
else
19+
{
20+
static assert(0, "fibonacci only supports integers");
21+
}
22+
}
23+
24+
extern (C)
25+
void main()
26+
{
27+
auto n = 100;
28+
printf("Fibonacci number %d is %d\n", n, fibonacci(n));
29+
}

c64/hello/src/hello.d

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import common: printf;
1+
import common : printf;
22

3-
extern(C)
4-
void main() {
3+
extern (C)
4+
void main()
5+
{
56
printf("Hello World in D!\n");
6-
}
7+
}

0 commit comments

Comments
 (0)