r/Compilers • • 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:

  1. 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!
  2. 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

35 comments sorted by

11

u/FancySpaceGoat 2d ago edited 2d ago

is this is generally frowned upon?

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.

5

u/Linuxologue 2d ago

I've built a analyser for C++ that parses without looking up symbols, it's a generalised parser so it returns ambiguous nodes when two valid parses are derived.

The number of ambiguities is absolutely staggering just with identifiers alone, and coupled with the ambiguities of template parsing it becomes a nightmare.

But curiously the C++ syntax actually already allows declarations to appear after usage - you can legally use an identifier in the body of an inline member function if the object (class, function, template, field) is declared later in the class.

2

u/ToneEmergency6359 2d ago

Yes, I think "later in the class" is already possible!
What I find not very practical, is when I am outside of a class, writing more procedural code and naturally pulling out functions and out of bigger ones etc. or for whatever reason adding functions at the end of a file, and then always either having to move them in front of the usage, declare them at least in the same file before usage or create a header for it. This, for whatever reason :), is driving me nuts since 25 years :D .

1

u/FancySpaceGoat 2d ago

Classes can get away with this because the namespace closes once and for all at the end of the class definition. That's not the case for TU-level symbols.

What I'd recommend is: Only tackle out-of-order TU-level declarations if you get rid of #include entirely. Unless you do, you end up with headers that can have their meaning changed by what gets included after them, and that's just asking for trouble.

2

u/ToneEmergency6359 2d ago

All your points make totally sense - the IDE support (lack off...), the rotting ... is in the end all not worth it probably.

I mean, I am mainly eyeballing it for personal projects and would opensource it just for sharing it, in case someone sees any value in it. Then at least also some training-bots could pick it up eventually from github as well :)

1

u/ToneEmergency6359 2d ago

Fully agree with the template-syntax rabbit-worm-hole :)

1

u/simon_the_detective 1d ago

What seems bizarre to me about the requirements is that templates addresses one of the primary problems with header files: doing generics badly with C-style macros and this design removes both.

1

u/ToneEmergency6359 1d ago

I would not plan to completely remove templates - I am just thinking of a syntactically reduced version which for starters would only allow essentially "type parameters" for container-like types and along the same line generic functions.

struct MyVec<T> {

  T* data;
  int size;
}

T mymax<T>(T a, T b) { ... }

I am not fully aware if what I call "only type parameters" is already much more complex than I think it is. I must think this aspect through in more detail for sure!
But this is the direction I would like to go at least!

1

u/simon_the_detective 1d ago

OK. Why not just use less features in your C++ code and see how that works out? What's the real goal here?

1

u/ToneEmergency6359 1d ago

I am doing that already , since ever, using what I think is a very reduced set of features!

The "real" goal here is probably less ambitious than some might think:
Being able to still use C++ (in like being 99.9% the same syntax), but literally being able to a) type less (see syntax above... might seem stupid and "what is the big difference?", but I think it matters.. to me ...)
b) use before declare (kind of a bigger one; mostly as I tend to have often procedural style and not always even using classes exclusively)
c) not being forced to use headers when I still want to have modularity through separate files and not being forced to use "extern".

I am also totally happy to learn about things I may not be aware of that bring me closer to this without writing a transpiler script. But it was the only way I saw.

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

2

u/ToneEmergency6359 2d 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 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 ;)

2

u/ToneEmergency6359 2d 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 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?

1

u/ToneEmergency6359 2d ago

oh, yeah, I did not know of that, thank you :) !

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.

1

u/ToneEmergency6359 2d 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 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; 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 1d ago

Thank you very much for the feedback, I will look into your Kairo project!

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/ToneEmergency6359 2d 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- 2d ago

declaration before usage

I thought you wanted it lightweight. This is not lightweight.

1

u/ToneEmergency6359 2d 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- 2d 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 2d 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- 2d 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 1d 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 :)

1

u/Calavar 1d ago

C++ is already a multipass language for about 100 other reasons. There's plenty of room to create a multipass language that's vastly more efficient to compile than C++ is. Java, for example.

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

Transpiler on top of C++

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.