Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Magma: Extending C with macros (github.com/eudoxia0)
57 points by eudox on June 10, 2015 | hide | past | favorite | 14 comments


A cool idea, though the it's in need of some better examples to show its true power. Several of the examples can be accomplished with the traditional C preprocessor. For example:

    #define with_open_file(fp, path, mode)   \
        for (FILE *fp = fopen(path, mode) ; fp ; fclose(fp), fp = 0)


Very interesting, thanks! Do you use many of these macros in your code? Does you find it makes it easier to read? I know some macro substitution can be impossible to read due to the CRAZY_CAPITALNAMES_THEY_GIVE_THEM


I don't write C code, isn't this idle code ?


No, it's the head of a for-loop. (The statement or block that follows this macro invocation becomes the body of the loop.) The third clause of the loop head guarantees that the loop can execute at most once.


I see, it's partially syntactically correct then, you'll fill in the loop body as if it was the with' one.

    with_open_file(...) {
       ...
       seek(fp,...)
       read...
       ...
    }
// nothing remains.

I misinterpreted it as some kind of readall | map online, with actual code as 2nd clause of the for loop; reading fp as short for function_pointer at first.


Said another way, the for loop can be written as FILE *fp = fopen( path, mode ); while( fp ) { fclose( fp ); fp = 0; }

NULL (and 0) evaluate to false in C.


No, it's not an infinite loop. If fp comes back as NULL from the initializer part of the loop, then the for loop exits.


And if it doesn't come back NULL, it... immediately `fclose`s the pointer and sets it to zero.


Immediately after executing the loop body, you mean.


Unless someone used continue inside that loop by mistake...


Using "continue" in that loop is harmless, it causes the file pointer to be closed and the loop to terminate. Calling "break" is more harmful though, as it would cause the loop to terminate without closing the file, a memory leak.


Just to completely hijack this whole thought... Bjarne S. has pitched 'smart references' as a library feature for C++, for the fourth time. Smart references require adding overload capability for operator..

It seems that by adding operator;, operator if, operator for, etc., we could use Veldhuizen-style expression-templates to have a two-stage macro-system in C++.

Obviously, a two-stage macro-system is not as capable as fully general macro system. However, it seems to me that we get the most bang for the buck from the first lift, and lifts above the first are not as common. Although, my use of Lisp-like macros is very limited—perhaps I'm just wrong?


Nice implementation, I like how the toplevel lifting is done. I was exploring similar things from a slightly different angle (using PEG natural extensibility): https://github.com/combinatorylogic/clike


implemented with a Common Lisp preprocessor: https://github.com/eudoxia0/cmacro




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: