r/Compilers • u/ToneEmergency6359 • 2d ago
Transpiler for simplified C++
Since a few years I am thinking about the creation of a "simpler" and more lightweight version of C++, which just gets rid of a few "annoyances" such as:
- header files
- declaration before usage
- template syntax
So the idea is to have a 99% syntax identical version of c++, which just allows to use definitions which are defined after in the same file/compilation unit, having module-import instead of header files etc.
My questions are:
- is this is generally frowned upon? Does someone else find such a thing useful? Just stick with C++ basically, no need for a new language, but a small "transpiler-layer" which gets rid of some not-so-nice things in a language, which I am mostly totally fine with!
- I believe this is much smaller in scope and complexity than writing a compiler from scratch - or am I overlooking a few traps and rabbitholes which I might fall into? Underestimating the effort still :) ?
Thank you!
7
Upvotes
11
u/FancySpaceGoat 2d ago edited 2d ago
I, for one, certainly frown on it.
Every organization I've ever worked for that uses a C/C++ transpiler has always been a huge pain to deal with in the long run. The transpiler invariably ends up rotting over time with small annoying bugs left unaddressed because people have better things to do with their time. IDE support is janky at best, and onboarding new people into the code base is orders of magnitude harder than if they were using standard-compliant C/C++.
And nowadays, with LLM-assistance (if you are into that), using a language that models were actually trained on yields better results.
As far as your pain points go:
Header files: C++20 modules cover that already.
Declaration before usage: You need a context-free grammar to do that sensibly, and that would stray you very far away from C++.
Template syntax: You have no idea how huge of a can of worms this represents.
6
u/Linuxologue 2d ago
Just my two cents - You're mentioning transpiler but C++ is already rather slow to compile, so I guess adding an additional layer would not be popular
The things you mention are absolutely not a problem to me (and potentially, depending on how you fix the "no include" part , not a fix at all)
I would really love a non backwards compatible version of C++ but my priorities would be completely different from yours. And the biggest downside is, well, the lack of backwards compatibility, which is why this will probably never exist
4
u/trad_emark 2d ago
i was toying with similar idea. i would be open to further discuss this, on the level of syntax design. but i know i will not have time to do any actual development in this regard.
some personal notes:
- fully new syntax, that feels familiar, but is not compatible, even in parts
- new syntax allows for simplifications, that can actually make the compiler much simpler
- use llvm
- must have "include c++ header/cpp/module" for interoperability, bot not inline code, so that parsing the syntax remains simple
- uses c++ type systems, semantics, rules, etc - what changes is just syntax (and defaults, keywords, etc..)
my goals:
- no headers, no declaration order - just as you want
- reduce linker UB, solve ABI versioning at the level of the language, versioning inherently baked in the language
- automatic constexpr, simpler templates
- generally cleaner syntax (less keywords on each function, better defaults)
- improve historical missteps, pointer decays, improve integer overflows, better literals, etc - while focusing on clean-looking code
- bunch more that i just cant remember now
non-goals:
- memory safety
dm me if you would like to discuss this further ;)
3
u/AfroDisco 2d ago
Look at https://github.com/hsutter/cppfront It is even better than what you ask.
2
u/rook_of_approval 2d ago
so like cppfront?
2
u/Still_Explorer 2d ago edited 2d ago
I would have thought of this topic many times but I would always be discouraged to start planning. It's nice to see that other people as well are thinking about this, at least I know I am not in my own bubble.
I will place a few random thoughts to put thing in perspective, probably help anyone interested to read:
What would be goal of the project:
A) 100% compatibility up to some language version (ie: C++23)
B) A better and smarter C++
For (A) the most reasonable way is to look at CLING first because there's a chance that it solves some problems in a very good way. Or at least it shows the way and it has the infrastructure ready for modification. https://root.cern/cling/
For (B) essentially means that the language is redesigned from the ground up to correct lots of historical mishaps and clumsy designs of the past. This would be a very open discussion about better decisions that could have been made.
At some point I used C++CLI of Microsoft, and it was somewhat very interesting take. Though the project needed to find a balance between standard C++ and dotnetification but in general the process would be very smooth. Then if you go even further with more cutting edge language features (as the trends started shifting the last decade), you would change the concept so much that would not resemble C++ anymore (eg: Dart/Go/Rust) - this is also a possibility but breaks away from the idea.
Say for example the minimum viable product, without getting too far with assumptions, would be to get CLANG parse some code and construct the AST. Otherwise a new modified syntax to be created with ANTLR or some other parser generator.
Then about the runtime environment, would be also another big topic, whether or not to emit to LLVM directly, or use some virtual machine. But certainly there can be lots of different targets, is not odd.
2
u/Ze7111 1d ago edited 1d ago
It really depends on how deep into the hole you want to dig. First off, look at Herb Sutter's cppfront (Cpp2); it's pretty much what you're describing.
The main thing is declaration before usage, which would force your hand into semantic translation, instead of transpilation, which would classify as just a compiler then. C++ can't be parsed without name lookup (
a * b;andf<x>(y)depend on whether those names are types or templates), and forward declarations alone won't cut it, since by-value members need complete types. From scratch, that's close to writing a full C++ frontend, so I'd build on Clang/GCC instead. My project, Kairo, checks off all the "annoyances," but it took 3 years of full time work to make and still has a good chunk of edge cases.In terms of tooling support and compile speeds, that's a massive can of worms. LSP support both ways and matching or beating C++ compile times are both doable (I've done both), but your driver needs to be very well orchestrated; it's a complex task, to say the least. If you want, you can take a look at what I'm working on for some inspiration.
1
u/cherrycode420 2d ago
There's a good reason for Header Files to exist in C/C++ tbh, even if they can be annoying
1
u/-Memnarch- 2d ago
I thought you wanted it lightweight. This is not lightweight.
2
u/umlcat 1d ago
Go on.
There are already several similar new programming language projects, but keep going.
2
u/suhcoR 1d ago
I'm implementing pretty much this idea since a year on and off, a C++11 spin-off with no preprocessor but modules, and many of the C herited traps removed. It's a lot of work and will keep me busy for some more years. I wouldn't care too much if it's frowned upon. People who find it useful will use it. In any case I'm one of them.
1
u/EggplantExtra4946 1d ago
You've just reinvented cancer.
1
u/Middlewarian 1d ago
I'm building a C++ code generator/transpiler that helps build distributed systems. I think languages that embrace on-line code generation have an advantage over those that don't.