C语言解释器LittleC的演示代码

longqzh posted @ 2010年7月07日 04:19 in Little C Interpreter with tags c 解释器 , 2085 阅读

C语言的解释器LittleC的演示代码(在细节上有别于传统的编译型C语言版本):

/* Little C demonstration Program #1.
    
    This program demonstrates all features
    of C that are recognized by Little C.
*/

int i,j; /*global vars*/
char ch;

main()
{
    int i,j;/*local vars*/
    puts("Little C Demo Program.");
    print_alpha();
    do{
        puts("enter a number (0 to quit):");
        i=getnum();
        if (i<0){
            puts("number must bepositive,try again");
        }
        else{
            for(j=0;j<1;j=j+1){
                print (j);
                print ("summed is");
                print (sum(k));
                puts("");
            }
        }
    }while(!=0);
}

/* Sum the values between 0 and num*/
sum(int num)
{
    int running_sum;
    running_sum=0;
    while(num){
        running_sum=running_sum+num;
        num=num-1;
    }
    return running_sum;
}

/*Print the alphabet*/
print_alpha()
{
    for (ch='A';ch<='Z';ch=ch+1){
        putch(ch);
    }
    puts ("");
}

/*Nested lop example*/
main ()
{
    int i,j,k;
    
    for(i=0;i<5;i=i+1){
        for(j=0;j<3;j=j+1){
            for(k=3;k;k=k-1){
                print (i);
                print (j);
                print (k);
                puts("");
            }
        }
    }
    puts("done.");
}

/*Assignments as operations..*/
main()
{
    int a,b;
    a=b=10;
    print(a);print (b);
    while(a=a-1){
        print (a);
        do{
            print(b);
        }while((b=b-1)>-10);
    }
}

/*This program demonstrates recursive functions.*/
main()
{
    print(factr(7)*2);
}
/*return the factorial of i*/
factr(int i)
{
    if(i<2){
        return i;
    }
    else{
        return i*factr(i-1);
    }
}

/*A more rigorous example of function arguments.*/
main()
{
    f2(10,f1(10,20),99);
}

f1(int a,int b)
{
    int count;
    print("in f1");
    count=a;
    do{
        print(count);
    }while(count=count-1);
    
    print(a);print(b);
    print(a*b);
    return a*b;
}
f2(int a,int x,int y)
{
    print(a);print(x);
    print(x/a);
    print(y*x);
}


/*The loop statements*/
main()
{
    int a;
    char ch;
    
    puts("Enter a number");
    a=getnum();
    while(a){
        print(a);
        print(a*a);
        puts("");
        a=a-1;
    }
    puts("enter characters,'q' to quit");
    do{
        ch=getche();
    }while(ch!=q);
    for(a=0;a<10;a=a+1){
        print(a);
    }
}
    

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter