179 points rramadass 1 week ago 37 comments
rramadass 1 week ago | parent
StilesCrisis 5 days ago | parent
rramadass 5 days ago | parent
WalterBright 5 days ago | parent
int foo(int i) {
int add(int x) { return i + x; } // nested function
return add(3);
}
int bar(int i {
alias lambda = (x) => x + i; // lambda
return lambda(3);
}
}sltkr 5 days ago | parent
Here's an example: https://cppinsights.io/s/f04b6896
(That also shows how C++'s range-based for-loops work, which is similarly some syntactic suger for a fairly mundane for-loop, but details are interesting: unlike a typical for-loop, the loop variable is declared inside the loop body.)
StilesCrisis 5 days ago | parent
frabert 4 days ago | parent
Someone 5 days ago | parent
That’s not true in C++, is it? There, this does not compile
for(int i = 0; i < 3; ++i)
{}
i = i + 1;
(https://cppinsights.io/s/26405687: error: use of undeclared identifier 'i')https://en.cppreference.com/cpp/language/for:
“A for statement equivalent to:
{
init-statement
while ( condition )
{
statement
expression ;
}
}
Except that […]”and
“While in C names declared in the scope of init-statement and condition can be shadowed in the scope of statement, it is forbidden in C++:
for (int i = 0;;)
{
long i = 1; // valid C, invalid C++
// ...
}
“alankarmisra 5 days ago | parent
ranger_danger 4 days ago | parent
jsrcout 5 days ago | parent
f1shy 5 days ago | parent
Like: I wish what is the first interpretation of my code to be obvious. Is not about advanced optimization, or nitty gritty compiler tricks, is about the language. This tool is useful in any C++ compiler. How did we end with such a complex language?
I mean (sorry, very personal take) the programming language is for me a tool to help me think about a problem and its solution. In this era of LLM more than before, is not a tool to get the machine to do something, is a language for formalizing ideas, problems and solutions, like math. Should not be so obscure, that you need to be a lawyer to understand it, right? Or I am very much old and totally in the weeds with my ideas?
rs545837 5 days ago | parent
kccqzy 5 days ago | parent
In the README example, the C++ practitioner just doesn’t think about those automatically synthesized copy constructors and such for POD types, nor do they think about the fact that assignment is desugared to operator= or that an assignment can involve a static_cast.
f1shy 5 days ago | parent
Of course there are people who do not need that tool. The thing is, other languages doesn’t need such an explanation tool at all, is very much enough with reading a small book. I personally would like C++ to not really need it, but clearly as I stated: maybe “the only one”
drysine 5 days ago | parent
Are you sure about that?
kccqzy 5 days ago | parent
Plain old C has -E to expand macros. Rust has cargo expand. Haskell has -ddump-simpl. Common Lisp has macroexpand. Any language that has a complicated syntax or allows the user to customize the syntax needs such a tool. Whether such a tool actually exists reflects more on popularity and community desire.
esikich 5 days ago | parent
f1shy 5 days ago | parent
Compilation per se is not difficult. Doing it with good error recovery, good feedback in case of error, and the optimization is difficult.
This tool does not tell me those parts (for that is gotbolt) this is about how the first layer of interpretation works, and not for making compilers, but for understanding what the code actually means, because is not obvious at first sight.
dasyatidprime 5 days ago | parent
pjmlp 5 days ago | parent
Only if you mean K&R C or the early C89/90 compilers, before the extension explosion, newer language standards, and modern ISAs.
rramadass 5 days ago | parent
Compilers for all languages have the concept of "lowering" the level of source-language abstractions from high-level to low-level viz. Written Source -> Simpler Source -> AST -> IR -> Machine Code. One type is "desugaring" i.e. transforming complex syntax to simpler ones so that the following stages of compilation have to operate only on a smaller and hence more manageable language subset.
This tool shows both desugaring and other lowering (eg. implicit transformations, type deductions etc.) within the C++ source language by regenerating the source from the AST thus giving you full visibility until that stage in the compilation process.
PS: Explanatory video presentation by the author - https://www.youtube.com/watch?v=VJ6ZvDRYzNE&t=558s
jcelerier 4 days ago | parent
tweedler290 5 days ago | parent
abbefaria27 5 days ago | parent
kvemkon 5 days ago | parent
What I'd like to see, is how much time is spent to compile each block of C++ code (sliced as fine as possible).
veexx103 5 days ago | parent
Can these issues be resolved?
kizxc4395 5 days ago | parent
jruohonen 4 days ago | parent
aktenlage 4 days ago | parent
rurban 2 days ago | parent
They do it with AST trees instead.
edgcpp or cfront are also good tools for this kind of lowering.