

Ask HN: Why wasn't the C preprocessor written to allow adding language features? - DiabloD3

I ask this question because it would be trivial to add DSL-like features to C (in a lispy sort of way) if CPP was as powerful, as, say, sed.<p>Are there any language aware preprocessors out there that would allow me to define new syntax for C without descending into CPP hell (or, alternatively, not make the code gcc only)?<p>Edit: I'm trying to avoid sed and m4. Please don't make me use m4, I beg of you.
======
lsiebert
Someone should probably point out that C++ started out with CFront
(<http://en.wikipedia.org/wiki/Cfront>) as it's first compiler, which compiled
C++ into C code, which could then be compiled.

Thus the cynic in me wants to answer, "to prevent a C+++."

~~~
DiabloD3
I think this may be what I was asking for. Although, I would have to rewrite
it in C and remove a lot of the unwanted C++ features...

------
codgercoder
If the actual question was "why wasn't C...?", the whole point of C was to
achieve the maximum amount of power with a minimal language system. Making a
preprocessor that is powerful enough to handle lexical and syntactic analysis
of source code was not necessary for that goal. If you really want it, use sed
(and don't forget M4).

Also, IIRC, programming languages with mutable syntax were generally
acknowledged to be a bad idea around the time C was being born. Something
about every program being written in a different language :-)

------
mtdewcmu
Are you aware of the crazy things that have been done with C++ templates? See
boost spirit. It's thought-provoking, but I'm not convinced it's a good idea
to use templates this way. It seems to be too hard to exercise control over
the code that gets generated, and you end up with huge binaries.

That said, metaprogramming certainly has a place, particularly when creating
state machines in code (e.g. yacc, lex, re2c, ragel). It makes you wish for a
standard way, but C is a conservative language, and I think that's been a key
to its success.

------
andrewcooke
i think this is the same reason why go is so conservative in many ways - it's
just their style. see <http://dreamsongs.com/WIB.html> which is the definitive
reflection on why c is not lisp (if you're young enough to not already have
seen it ;o)

i don't know of a good preprocessor/template engine that does what you want (i
had a similar need recently and came to the conclusion that there's a hole
here - but i don't know m4 well enough to know what a replacement should look
like). could you not use some language that compiles to c (gambit springs to
mind) or switch to go (i haven't used go much, but from a little experience it
strikes me as a very good "better c").

i guess another option is to look at the llvm toolchain. maybe you can hack
their c front-end...?

------
javert
I don't see why you couldn't pass your code through sed in the Makefile before
it gets to the compiler. (Other than being an ugly hack, any thoughts?)

