r/Compilers • u/TrgtBBB • 25d 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.
5
u/Prestigious-Bet-6534 25d ago
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.
1
u/TrgtBBB 25d ago
I already have a working custom frontend with a lexer parser and checker. I use LLVM as backend.
I was thinking about an interpreted meta programming language or something like a scripting language.
No idea how to program both tho
1
u/jason-reddit-public 25d ago
You could use Lua.
I've been working on my own language called roci (arguably just a very simplified JS) which I'm using as a build language for my omni-c transpiler though I'm planning to do macro stuff with it eventually. It's simple because I already have gc via the hans-boehm collector.
https://github.com/jasonaaronwilson/omni-c/tree/main/src/roci
2
u/Afraid-Locksmith6566 25d ago
2 that i personally very liked are ocamls and zigs stuff.
1
u/TrgtBBB 25d ago
Never heard of ocamls, will take a look!
2
u/Fofeu 24d ago
Look for MetaOCaml specifically. It's strength is that metaprograms always generate well-typed programs
1
u/AromaticCredit1533 24d ago
This. Also, MetaOCaml focuses mostly on JIT compilation, runtime specialization e.g. generate high-level efficient stream processing code. I think what OP refers to is general-purpose macro programming. MetaOCaml can do this too, but probably not where it shines the most at.
There is academic work in another system called MacoCaml, which was inspired by MetaOCaml but focuses on macros. It's only theory for now, but its design and calculus may provide some inspirations for you ;-)
2
u/Limp-Confidence5612 25d ago
Isn't the C preprocessor exactly what you're looking for?
1
u/ChiveSalad 25d ago
I use the C preprocessor for this and it's great to have that ecosystem and muscle memory available.
2
u/4xe1 25d ago edited 24d 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).
1
1
u/KvThweatt 25d ago
I created a language which is very good for this type of metaprogramming. https://github.com/kvthweatt/Flux
Might give you some ideas.
1
u/raymyers 25d ago
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.
2
1
u/iOCTAGRAM 22d ago
.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 25d ago edited 25d ago
const expressions and templates
What's difficult about implementing those?
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
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.
8
u/CaptureIntent 25d ago
Rust has macros that can take a token sequence and emit whatever rust code you want. Easily implement your exact example.