Skip to content

csmile - a compiler for a subset of the C language.

License

Notifications You must be signed in to change notification settings

JameyWoo/csmile

Repository files navigation

csmile

csmile is a compiler for a subset of the C language. Partial compilation is implemented.

If you try this simple compiler, you will find that your code can't always be successfully compiled here, because I simplified the syntax a lot, it is only a small subset of the C language.

After running this compiler, the assembly code corresponding to the source code will be generated. Yes, I will not generate an executable binary file, but will give the job to gcc. You need to use gcc to convert this assembly code into executable file.

The demo below will give you a good idea of the syntax supported by this compiler.

Environment

You can make the project everywhere.

But the assembly code generated by the compiler needs to run on x86 platform. All tests passed on a 32-bit ubuntu 12.04 system.

Usage

cd to the root path of the project and run

make

you'll get a csmile.exe file, this is the compiler.

then run

./csmile.exe <source_file>

to generate the assembly code, such as

./csmile.exe tests/gcd.c

The output would be saved at Output-assembly.txt.

You can copy the content to a .s file such as test.s

And run

gcc test.s -o test
./test

Good luck!

Demo

A gcd demo

int gcd(int u, int v) {
    int tmp;
    if (v == 0) {
        return u;
    } else {
        /* u-u/v*v == u mod v */
        tmp = u / v;
        tmp = tmp * v;
        return gcd(v, u - tmp);
    }
}

void main(void) {
    int x;
    int y;
    x = input();
    y = input();
    
    output(gcd(x, y));
}

A factorial demo

void main(void) {
    int a;
    int b;
    b = 1;
    a = input();
    while (a > 0) {
        b = b * a;
        a = a - 1;
    }
    output(b);
}

About

csmile - a compiler for a subset of the C language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published