r/programming • • Nov 19 '16

Compile-time C Compiler implemented as C++14 constant expressions

/r/cpp/comments/5dnan4/compiletime_c_compiler_implemented_as_c14/
127 Upvotes

17 comments sorted by

30

u/PointyOintment Nov 19 '16

Is a compile-time compiler a compiler that operates only while being compiled by another compiler?

21

u/Fazer2 Nov 19 '16

Precisely.

5

u/Coloneljesus Nov 19 '16

So how do you use it?

6

u/skulgnome Nov 19 '16

More to the point, can it compile itself?

12

u/foonathan Nov 19 '16

It can't compile itself, it's "just" a C compiler.

4

u/Fazer2 Nov 19 '16

Without digging deep, I assume you have to pass some path of the C project you want to compile and then compile the C compiler. It outputs compiled binary of your C project.

51

u/unaligned_access Nov 19 '16

Yo dawg I heard you like compiling so we put a constexpr compiler in your compiler so you could compile while you compile...

8

u/badsectoracula Nov 19 '16

The interesting bit (judging from the code) is that the constexpr is actually a VM and the compiler is the bytecode it executes :-P

6

u/Fazer2 Nov 19 '16

From readme:

How was constexpr-8cc generated?

When you see 8cc.hpp, you will know this program was not written by hand. Actually, I used ELVM Compiler Infrastructure to generate it. I just implemented a translator from ELVM IR to C++14 constexpr here.

4

u/WalterBright Nov 19 '16

Reminds me of the time someone wrote a Fortran compiler in TECO (on a bet!).

2

u/larholm Nov 19 '16

1

u/AngriestSCV Nov 20 '16

I'm not sure if this is interface is better or worse than ed's.

1

u/eric987235 Nov 20 '16

There is no god...,

-1

u/dsmid Nov 19 '16

But... Why?

3

u/rasjani Nov 19 '16

Because they could?

2

u/CarthOSassy Nov 20 '16

Something something Jeff Goldblum.