c4 README

The project's README from github.com/rswier/c4. As one markdown file: README.md.

c4 - C in four functions

An exercise in minimalism.

Try the following:

gcc -o c4 c4.c
./c4 hello.c
./c4 -s hello.c

./c4 c4.c hello.c
./c4 c4.c c4.c hello.c

c4 (rswier/c4)

educational
Searching Database…

10.8K ·GitHub
What is this?C4 is a tiny, self-hosting compiler and interpreter for a small subset of the C language, written in just four functions as an exercise in extreme minimalism.
Is it a compiler or an interpreter?It's both: c4 parses C source code and then executes it directly through its own built-in virtual machine, rather than producing a separate binary. It's even capable of compiling and running its own source code.
How do I get it running?Compile the single source file with 'gcc -o c4 c4.c', then run it against a C file, for example './c4 hello.c'.
Do I need anything besides a C compiler?No, all you need is a standard C compiler like gcc to build the single c4.c file; there are no other dependencies.
Does it support the full C language?No, c4 only implements the small subset of C needed to compile itself (char, int, pointers, basic control flow), not the full C standard.
Does it work on Linux, macOS, and Windows?Since it's just a standard C source file built with gcc, it runs anywhere a C compiler is available, including Linux, macOS, and Windows.
What do the four functions actually do?The entire compiler is organized into a tokenizer, an expression parser, a statement parser, and a main driver function, together handling lexing, parsing, code generation, and execution.
What does the '-s' flag do?Running './c4 -s hello.c' prints the generated virtual machine instructions alongside the source, letting you see exactly how c4 translates your code.
Why run './c4 c4.c c4.c hello.c'?This chains c4 interpreting itself interpreting itself to run a program, demonstrating that c4 is self-hosting and can compile and execute its own source code.
Who is this project for?It's aimed at people curious about how compilers and interpreters work internally, not for writing real-world C programs.
Creator
rswier
Size
<0.1 MB
Stars
10.8K
Updated
CommitMar 14
Age
11y
Commits · 90 Days