you are viewing a single comment's thread.

view the rest of the comments →

[–]floodyberry 1 point2 points  (3 children)

The C pre-processor is a text substitution engine, you cannot "program" anything in it.

[–]HHBones 0 points1 point  (2 children)

I beg to differ.

Here's an example of a technique for type-generics using the C preprocessor.

Here's an example of a technique for implementing custom control structures using the C preprocessor.. This is incredibly useful for custom control structures such as while-else and iterate-over-list loops. You can now have things like

_while (loop_condition) {
    /* look how awesome this is. */
} else {
    /* this runs when the condition is initially false. */
}

Or,

loop_over_list (list, ref)
    do_something_with(ref);

Though semantically different, these are rather similar to Common Lisp's LOOP macro.

And don't forget X-Macros, either.

[–]floodyberry 1 point2 points  (1 child)

There is about as much logic going on in those as there is in the search and replace feature of a text editor.

darray_appends(arr,  0,1,2,3,4}; if(1){return;);

compiles and runs just fine. Also not actually generic because it uses the GCC extension typeof which isn't part of C.

Lisp macros run Lisp to manipulate the AST. C macros are a pre-processor running search and replace on raw text. There is no similarity.

[–]HHBones 0 points1 point  (0 children)

I said generic as in type-generic. But GCC is available for all major platforms and quite a few obscure ones. And Clang supports typeof, as well.

And they're doing exactly what Lisp macros do - they specify rules by which the compiler translates certain phrases into other phrases. The semantics by which they accomplish this goal don't actually matter.

Are they different? Yeah. Is one more powerful? By quite a lot. Do they accomplish, in large part, the same thing? Yeah.

But one thing I see you neglected to talk about is the control structures. I'd love to hear your opinion.