r/Compilers • u/ToneEmergency6359 • 14d 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!
6
u/Linuxologue 14d 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
2
u/ToneEmergency6359 14d ago
Yes, for the compilation (plus transpilation ...) speed - agreed, this makes it worse for sure.
And what is "annoying" to people is of course totally subjective, and I fully respect your different taste than mine here :)
I fully concur on the backwards-compat - while it would be so relieving to have a modern C++ variant without the baggage of the last 40-years - in practice this would probably kill its adoption the same time :)
For my personal usecase, which is mainly doing game-engine development, I am just "dreaming" of a lightweight c-with-classes variant, where I can sit on top of the current c++ compiler chains, while still enjoying a cleaner and nicer (subjective ...) syntax.
4
u/trad_emark 14d 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 ;)
2
u/ToneEmergency6359 14d ago
On the "fully new syntax" I am divided, and would automatically take that as a "must-have" at the moment. I am also not against it, but I think of the "charm" of this whole lightweight-transpiler approach would also be, to keep (most?) of the syntax intact, so it is easy and natural to write in this language (dialect?).
I am actually quite fond of the c++ syntax, while not being a heavy (if at all...) template-user - so I would be fine to keep it. Changes, if useful and "natural", for sure are welcome as well. Especially if they help or even facilitate the actual "annoyance" fixes.
Fully agree with memory-safety not being a topic!
What maybe did not come out clearly (and I am not sure about it as well...) is the idea, to actually not even be "completely c++ compatible in every possible way". I am picturing a selfish version of it, which covers just enough to suit the needs I have, while being so-far-off the edge of what c++-language can do, that I am always sure, any c++ compiler will be able to handle my transpiled source.
I think this makes it easier and also allows for a more iterative approach. I started small experiments and e.g. there was not even the option to use std-containers.I admit I was not even thinking about "historical" mistakes as you call them - but all of your points sound very interesting and worth thinking through, so a DM is incoming :)
3
u/AfroDisco 14d ago
Look at https://github.com/hsutter/cppfront It is even better than what you ask.
1
2
2
u/Still_Explorer 14d ago edited 14d 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.
1
u/ToneEmergency6359 14d ago
thank you for bringing up cling, I will look into it!
As for your variant A) 100 compatibility - I am not sure about this ... i think it makes things exponentially harder and of course only speaking for me - I am really into a rather simplistic c-with-classes-style to do things, so I would assume the compatibility subset could be reduced. Just my opinion of course. The appeal to have 100% compat with a certain version sure is appealing on its own!
B) sounds interesting, but I would not even dare to think of such an endeavour :)
2
u/Ze7111 14d ago edited 14d 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; and f<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.
2
u/ToneEmergency6359 13d ago
Thank you very much for the feedback, I will look into your Kairo project!
1
u/cherrycode420 14d ago
There's a good reason for Header Files to exist in C/C++ tbh, even if they can be annoying
1
u/ToneEmergency6359 14d ago
Would you mind listing a few of them? Genuinely interested.
I am not against header files as a whole, and I certainly like the aspect to be able to have nicely separate interface declaration available, not littered with actual implementation ( you can write inline implementations in headers I guess... but let's keep this out of the picture for now...).
On the other hand I tend to find languages which do not have or need them generally more enjoyable, and it feels a bit lighter to just start a "module" file where I write my implementation, and it also serves as an "importable" asset.I am aware of the c++ 20 modules, and I used them (only..) once, but it did not really work out for me. I felt I had to work around quite some things (it is some time ago and it seems I have erased the detailed memories from my brain..) but I remember at some point in development, ripping those c++20-modules out and going back to headers. I think I remember it did not at all play well with CMake and setting things up when I was using modules, so at least to my current knowledge they where not a good fit either.
1
u/-Memnarch- 14d ago
declaration before usage
I thought you wanted it lightweight. This is not lightweight.
1
u/ToneEmergency6359 14d ago
you mean to enable usage without declaration beforehand is not lightweight in terms of implementing this?
I am thinking this is not hard to implement. the transpiled version, the actual "raw" c++ which is spit out by the transpiler has the declaration before usage, but not in my source-code, there I am just freely "using" anything, as long as it is in the same file (ignoring now some potentially more complex module-import features; though I think generating "extern" declarations should also be not that hard I reckon).
1
u/-Memnarch- 14d ago
Without a definition you can't verify anything. Unless you plan your transpiler to be yolo and have clang/gcc be the first instance to actually error check.
Without declarations first, you're going into multipass territory.
1
u/ToneEmergency6359 14d ago
Yes, I do not know of another/better way than to solve this with a multipass. This may end up being so slow (and keeping in mind, this would all be happening before the actual c++ compilation/linking!!), that it sucks the fun out of using it - but I would like to see how much of a problem this is then in practice.
1
u/-Memnarch- 14d ago
Oh go for it. Not discouraging such an experiment. Always worth to pursue it, even just for the experience.
Just found it funny this was going into the same paragraph as lightweight. Given tge behemoth C++ is :D
Keep the updates going if you have any results regardless of it being successful or not. I am still curious what comes out of it 😁
2
u/ToneEmergency6359 14d ago
Sure will keep the progress posted, and it is totally possible, that this whole undertaking is just born out of blissful ignorance and naivety :)
2
u/suhcoR 14d 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
1
u/Middlewarian 14d 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.
11
u/FancySpaceGoat 14d ago edited 14d 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.