Mercurial > hg > CbC > CbC_gcc
comparison CbC-examples/fact-e.c @ 149:ff10c3399912
add small example
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Mar 2020 09:14:44 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
148:24d2e7cdba67 | 149:ff10c3399912 |
---|---|
1 #define __environment _CbC_environment | |
2 #define __return _CbC_return | |
3 | |
4 #include "stdio.h" | |
5 #include "stdlib.h" | |
6 | |
7 struct F { int n; int r; int o; void *env; | |
8 __code (*next)( struct F ); | |
9 __code (*exit)( struct F ); | |
10 }; | |
11 | |
12 __code factorial(struct F arg) | |
13 { | |
14 if (arg.n<0) { | |
15 printf("#0008:err %d!\n",arg.n); | |
16 exit(1); | |
17 // goto (*exit1)(0,exit1env); | |
18 } | |
19 if (arg.n==0) | |
20 goto arg.next(arg); | |
21 else { | |
22 arg.r *= arg.n; | |
23 arg.n--; | |
24 goto factorial(arg); | |
25 } | |
26 } | |
27 | |
28 __code print(struct F arg); | |
29 | |
30 int main( int ac, char *av[]) | |
31 { | |
32 struct F arg; | |
33 arg.n = atoi(av[1]); | |
34 // arg.n = 10; | |
35 arg.r = 1; | |
36 arg.o = 1; | |
37 arg.next = print; | |
38 arg.exit = 0; | |
39 arg.env = 0; | |
40 goto factorial(arg); | |
41 } | |
42 | |
43 __code print(struct F arg) | |
44 { | |
45 printf("#0032:%d! = %d\n",arg.o, arg.r); | |
46 exit(0); | |
47 } | |
48 |