fedjmike / mini-c Goto Github PK
View Code? Open in Web Editor NEWDr Strangehack, or: how to write a self-hosting C compiler in 10 hours
Dr Strangehack, or: how to write a self-hosting C compiler in 10 hours
do you think there should be tests that test for IF statement? i saw the example only contains while statements but no IF statement.
also, awesome project! :D
main.c
#include <stdio.h>
int main(int argc, char **argv){
int i=0;
while(i<1000000){
printf("\r%i",i);
fflush(stdout);
i=i+1;
}
printf("\n");
return 0;
}
Compiling:
mcc ./main.c
Errors:
mcc ./main.c
./main.c:7: error: no symbol 'fflush' declared
./main.c:7: error: no symbol 'stdout' declared
There is some equivalence to do this?
OS: win10
Version: latest win branch (023992f on Jul 2 2015)
Example: By first assigning a = 9 and b = -2 and then computing a + a*b, the compiler wrongly gives -36, indicating it actually did (a+a)*b.
Anybody who knows C can use your mini-c
as the minimal self-hosting subset of C compiler and can implement the whole C compiler or other languages.
What would also be interesting is to bootstrap mini-c
from scratch. Currently you require some kind of a C compiler like gcc
to be present on the system.
Here is an example of an approach that doesn't require any compiler at all, it starts from a small hand written machine code and then it goes from there to develop a structured programming language called BCC: http://rano.org/bcompiler.html (I made a mirror at github here: https://github.com/certik/bcompiler/blob/master/bcc.bc)
So the goal would be to write a compiler in BCC that can compile mini-c
.
The selftest test case passes just fine. However, when trying to compile cc.c using the ccself
binary, it fails with the following errors.
u@x220 ~/D/c/c/mini-c> make selftest
gcc -m32 -std=gnu11 -Werror -Wall cc.c -o cc
./cc cc.c
gcc -m32 a.s -o ccself
./ccself tests/triangular.c
gcc -m32 a.s -o triangular; ./triangular 5; [ $? -eq 15 ]
u@x220 ~/D/c/c/mini-c> ./ccself cc.c
cc.c:719: error: expected ')', found '�'
cc.c:719: error: expected ';', found '�'
cc.c:719: error: expected '}', found '�'
Note, compiling cc.c with the produced cc
binary works just fine, and compiling triangular.c with ccself
also works just fine.
Is ccself
intended to be able to compile cc.c?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.