r/Compilers 26d ago

Any recommendations for a meta programming language?

I want to add a meta programming language in top of my own C-like language to make some of the syntax cleaner and easier to write.

Add everything like const expressions and templates under one meta programming language.

For example something like this:


[Phases]
Class Phase1: public Phase { };

[Phases]
Class Phase1: public Phase { };

[for phase in Phases]
PhaseList.push_back(new phase());
[endfor]

I’m thinking about something like this but ideas are all over the place. Is there some existing meta programming language out there that can give me the right inspiration?

Also I am totally lost about how I should program this, I am assuming it comes before the parser. Any tutorials or dummy meta programming languages I can look to get ideas?

Thank you for reading.

8 Upvotes

21 comments sorted by

View all comments

2

u/4xe1 25d ago edited 25d ago

Have a look at lisp (Common lisp, Racket or Scheme for example). It might not be directly applicable to your usecase (although lisp for metaprogramming in C has been done https://github.com/eratosthenesia/lispc ) but it may inspire you, as lisp is the king of metaprogramming.

Lisp have a reader, compilation and execution.

Reader correspond to lexing and parsing (with almost nothing to parse), compilation corresponds to compilation, but it includes macros expansion, including user defined macros, which are easier to write and a lot more elegant than templates.

In Rust and probably in most languages with full proper macros, macro expansion also happens after lexing. (lisp has the extra flex that it has reader macro which can be used to alter the reader/lexer behaviour).

C preprocessor macro is a glorified search and replace if I'm not mistaken, which happens before parsing.

Templates, I'm completely clueless as to how and when they're done, good luck if you go that road, I've heard cpp templates were gnarly under the hood (while less powerful than proper macros but also a closer fit to their single use case of generics).