Mercurial > hg > CbC > CbC_examples
changeset 26:1db97badc4e0
add small example
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Mar 2020 09:15:18 +0900 |
parents | 6324b8df04f1 |
children | 586096c45873 |
files | fact-e.c |
diffstat | 1 files changed, 48 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/fact-e.c Sat Mar 21 09:15:18 2020 +0900 @@ -0,0 +1,48 @@ +#define __environment _CbC_environment +#define __return _CbC_return + +#include "stdio.h" +#include "stdlib.h" + +struct F { int n; int r; int o; void *env; + __code (*next)( struct F ); + __code (*exit)( struct F ); + }; + +__code factorial(struct F arg) +{ + if (arg.n<0) { + printf("#0008:err %d!\n",arg.n); + exit(1); + // goto (*exit1)(0,exit1env); + } + if (arg.n==0) + goto arg.next(arg); + else { + arg.r *= arg.n; + arg.n--; + goto factorial(arg); + } +} + +__code print(struct F arg); + +int main( int ac, char *av[]) +{ + struct F arg; + arg.n = atoi(av[1]); + // arg.n = 10; + arg.r = 1; + arg.o = 1; + arg.next = print; + arg.exit = 0; + arg.env = 0; + goto factorial(arg); +} + +__code print(struct F arg) +{ + printf("#0032:%d! = %d\n",arg.o, arg.r); + exit(0); +} +