eudoxia0 / cmacro

Lisp macros for C
884 stars 29 forks source link

Extract macro definitions into a separate file #7

Closed 6D65 closed 10 years ago

6D65 commented 10 years ago

Hi,

I was wondering if it's possible to extract all the specific macros for a DSL into a separate file, so it wouldn't be a need to duplicate the macros for each file.

For example, the defs file would contain the macro definitions for a particular dsl, and code would contain the code that uses these macros. I have tried this now, and it doesn't seem to work.

cmc defs.cm code.cm -o code.c

Maybe i'm missing something.

Thanks

eudoxia0 commented 10 years ago

The files with the macro definitions can be #included just like any other C file. The 'root' file (The file that includes all others) should then go through cmacro and then to the C compiler.

6D65 commented 10 years ago

Thanks, it works.

Still, there are some issues with the compiler(clang) that doesn't like the way cmacro is pulling in all the definitions from all includes into one file. I haven't had the chance to look into this though.

But, I was thinking if it's possible to write a macro that would be replaced with "#include" after macro expansion, "using" for example. I have tried myself, but it seems that cmacro treats #include specially, no matter where it's in the code.

using "foo.h"

// Transform to

#include "foo.h"
eudoxia0 commented 10 years ago

cmacro treats include specially because the files go through the preprocessor before being parsed. I think you can write a macro that produces an #include, to include files (eg the standard library) that don't have macros but that should be included in the final code (This would speed up macroexpansion, since you wouldn't be parsing those huge header files).

Scratch that. That's the intended behaviour, I just noticed I made a mistake in the definition of the parser (It processes everything, rather than just the input).

6D65 commented 10 years ago

So, it's not possible to redefine the include?

I was thinking about the use of macros per file.

For example.

Having an existing codebase. And creating a file.

// app.cm
#include "macrodefs.dsl" // this is the file with the DSL

import "existing.h"

// function 
fn do_something(int b) -> int { return b; }

So i could just add the cmacro into the build process, to just find the files with the .cm extensions, and just do macro expansion on those. Which then i could feed into the compiler. What makes cmacro attractive is the ability to have as many DSL's as I would like, with clean syntax, but at the same time, integrated with the existing code, which will make the transition easy.

Btw, one more question, is it something in the implementation of the cmacro that requires it to be written in SBCL? Would it be possible for me to try and understand it, and maybe rewrite it in Clojure or something? I mean, a jar file would be easier to distribute and integrate in, than installing the SBCL and its dependencies.

Thanks

eudoxia0 commented 10 years ago

Version 0.2 adds a cmacro_import directive for importing files with macro definitions, and doesn't use the preprocessor. Example:

cmacro_import "macros/anaphoric.c"

aif(...)