Skip to content

ubugeeei/ubcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ubcc

A toy C Compiler implemented by Rust.

Usage

# first time
make

# build and testing
make build
make e2e

Able to compile

int literals

int main() {
    return 0;
}
int main() {
    return 42;
}

binary expressions

int main() {
    return 5 + 20 - 4;
}
int main() {
    return 5 + 6 * 7;
}
int main() {
    return 5 * (9 - 6);
}
int main() {
    return (3 + 5) / 2;
}

compare

int main() {
    return 0 < 1;
}
int main() {
    return 1 >= 0;
}
int main() {
    return 0 <= 1;
}
int main() {
    return 0 != 1;
}
int main() {
    return 42 == 42;
}

variables

int main() {
    int a = 2;
    return a + 2;
}
int main() {
    int a = 10;
    a = a + 2;
    return a;
}
int main() {
    int x = 2;
    int y = 5;
    return x + y;
}

pointer

int main() {
    int x = 3;
    int *y = &x;
    return *y;
}
int main() {
    int x = 0;
    int *y = &x;
    *y = 3;
    return x;
}
int main() {
    int x = 100;
    int a = 200;

    int *p = &x;
    p = p + 1;
    return *p;
}
int main() {
    int x = 100;
    int a = 200;

    int *p = &a;
    p = p - 1;

    return *p;
}

collections

int main() {
    int a[2];
    *a = 1;
    return *a;
}
int main() {
    int a[2];
    *a = 1;
    return a[0];
}
int main() {
    int a[2];
    a[0] = 1;
    a[1] = 2;
    return a[1];
}
int main() {
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    return a[9];
}
int main() {
    char *a = "ABC";
    return *a;
}
int main() {
    char *a = "ABCDEF";
    return a[5];
}

branches

int main() {
    int foo = 4;
    int z = 5;
    if (foo / 2 == 2) z = 50;
    return foo + z;
}
int main() {
    int foo = 100;
    int z;
    if (foo / 2 == 50) {
        z = 50;
    } else {
        z = 100;
    }
    return foo + z;
}

loop

int main() {
    int i = 0;
    while (i < 10) {
      i = i + 1
    };
    return i;
}
int main() {
    int i = 0;
    for (i = 1; i < 10; i = i + 2) {
        i = i - 1;
    }
    return i;
}

function

int foo(int i) {
    return i;
}

int main() {
    int a = foo(10);
    return 10;
}

builtin function

int main() {
    int x = 0;
    return sizeof(x);
}

comments

int main() {
    /*
     * comment
     */
    return 0;
}
int main() {
    // comment
    return 0;
}

About

A toy C Compiler implemented by Rust.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published