view CbC-examples/goto.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 5d30d517ebed
children
line wrap: on
line source

#define __environment _CbC_environment
#define __return _CbC_return

extern int printf(const char *,...);

__code (*conv)(int,__code (*)());
__code a2(int i,__code conv());
__code a3(int i,__code conv());
__code a4(int i,__code conv());
__code a5(int i,__code conv());
__code a6();
__code a7();
__code a8();
__code a9();

char* print_conv(__code conv());

__code (*exit0)(int,void*);
void *env;

__code
a2(int i,__code conv())
{
    printf("#0023:a2 %d %s\n",i,print_conv(conv));
    goto conv(i+1,a4);
}

__code
a3(int i,__code (*conv)())
{
    printf("#0030:a3 %d %s\n",i,print_conv(conv));
    goto conv(i+1,a5);
}

__code
a4(int i,__code conv())
{
    printf("#0037:a4 %d %s\n",i,print_conv(conv));
    goto (*conv)(i+1,a6);
}

__code
a5(int i,__code (*conv)())
{
    printf("#0044:a5 %d %s\n",i,print_conv(conv));
    goto (*conv)(i+1,i+2,i+3,a7);
}

__code
a6(int i,int j,int k,__code conv())
{
    printf("#0051:a6 %d %s\n",i,print_conv(conv));
    goto conv(i+1,j,k,a8);
}

__code
a7(int i,int j,int k,__code (*conv)())
{
    printf("#0058:a7 %d %s\n",i,print_conv(conv));
    goto conv(i+1,j,k,a9);
}

__code
a8(int i,int j,int k,__code conv())
{
    printf("#0065:a8 %d %s\n",i,print_conv(conv));
    goto (*conv)(i+1,j,k,exit0);
}

__code
a9(int i,int j,int k,__code (*conv)())
{
    printf("#0072:a9 %d %s\n",i,print_conv(conv));
    goto (*conv)(0,env);
}

int main(int ac,char *av[]) {
    exit0 = __return;
    env   = __environment;
    conv = a2;
    goto conv(1,a3);
}

char*
print_conv(__code conv())
{
    if(conv==a2) return "a2";
    if(conv==a3) return "a3";
    if(conv==a4) return "a4";
    if(conv==a5) return "a5";
    if(conv==a6) return "a6";
    if(conv==a7) return "a7";
    if(conv==a8) return "a8";
    if(conv==a9) return "a9";
    else return "xx";
}