r/Compilers • u/TrgtBBB • Aug 17 '26
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.
5
u/Prestigious-Bet-6534 Aug 17 '26
You could implement a superset of C and a transpiler which converts that syntax into plain C. Not simple but doable, and you'll learn a lot. I recommend starting with the basics of how a compiler/interpreter/transpiler works. for that we have the very good and free book craftinginterperters.com . Also AI is very good at troubleshooting parsers, often finding the bug in seconds instead of hours.
3
u/jcastroarnaud Aug 17 '26
Ruby has metaprogramming out of the box. See the Class documentation. May it inspire you.
2
u/Afraid-Locksmith6566 Aug 17 '26
2 that i personally very liked are ocamls and zigs stuff.
8
u/CaptureIntent Aug 17 '26
Rust has macros that can take a token sequence and emit whatever rust code you want. Easily implement your exact example.
1
u/KvThweatt Aug 17 '26
I created a language which is very good for this type of metaprogramming. https://github.com/kvthweatt/Flux
Might give you some ideas.
2
u/Limp-Confidence5612 Aug 17 '26
Isn't the C preprocessor exactly what you're looking for?
2
u/4xe1 Aug 17 '26 edited Aug 17 '26
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).
2
u/tiajuanat Aug 17 '26
Have you looked at C++ and Template Metaprogramming?
1
u/Jarmsicle Aug 17 '26
Give Nim a look
1
u/raymyers Aug 17 '26
I would use S-expressions and make something like Lisp macros. It’s a notation that makes the syntax tree very visible, so it’s obvious how to transform it.
So that’s a well-studied and easy to implement metaprogramming system, should be a good one to start with.
Scheme is known for having something called hygenic macros, while Common Lisp has the more flexible non-hygenic kind.
https://en.wikibooks.org/wiki/Scheme_Programming/Macros
1
u/iOCTAGRAM Aug 20 '26
.NET has CodeDOM for bytecode, and there should be Source Text DOM. The main problem with what I saw in C++ was inability to inspect the results, and even if compiler can have a shortcut for not dealing with text files, it should be possible to extract text. Source Text DOM should provide means for what is found in hygienic macros. Similar but with ability to output text.
-1
u/EggplantExtra4946 Aug 17 '26 edited Aug 17 '26
What's difficult about implementing those?
This is utterly meaningless, you are not even trying. If you want a real answer ask a real question with an example that contains at least the shadow of the beginning of an idea.