3 ms·
Having implemented two C compilers, knowing every last detail is sometimes a bit annoying, because one thinks "why the heck is that there!" For example, the syn
by WalterBright 3y ago
Having implemented two C compilers, knowing every last detail is sometimes a bit annoying, because one thinks "why the heck is that there!" For example, the syntax for a cast is indistinguishable from that for a function call for many cases. The only way to parse it correctly is to keep a symbol table of the typedefs. Believe me I tried, and finally threw in the towel and did a special symbol table just for typedefs.
This is just pointless complexity. It's why D's cast syntax looks like this:
cast(T)x;
i.e. cast is a keyword.
- throwawaymaths 3y agoSeems pointlessly complex too. Why didn't you just make it cast(T, x)?
- WalterBright 3y agoNeither is more complex. I just like the former better.
- throwawaymaths 3y agoWell, certainly my eyes need to add an extra parsing case so maybe don't assume it's equally complex. I don't think I'm wrong on that count.
- absdiff 3y agoPython and Haxe have a function-like cast like that, but the argument order differs between the languages. I prefer D's approach because it doesn't have this opportunity for confusion. The prefix operator only has one place for the type to go. https://docs.python.org/3/library/typing.html#typing.cast https://docs.python.org/3/library/typing.html#typing.cast typing.cast(typ, val) https://haxe.org/manual/expression-cast.html https://haxe.org/manual/expression-cast.html cast expr; // unsafe cast cast (expr, Type); // safe cast
- throwawaymaths 3y agoit doesn't have this opportunity for confusion 1) it'll be a compiler error anyways 2) of course there's room for confusion. It could be cast(T) var Or it could be cast(var) T And you must remember which is correct.
- ryao 3y agoWould you give C examples of this? I am having trouble imagining how a cast would look like a function call to the compiler.
- trealira 3y agoAlthough I'm not who you asked, I can think of a slightly contrived example. If a is a variable name, then this is using b as the first argument to call the function a (which is surrounded by unnecessary parentheses). If it's a type name, it's casting b to the type a. result = (a)(b);
- WalterBright 3y agoIt's not contrived enough - it happens in real C code.
- trealira 3y agoMy example is slightly contrived because the parentheses around b are unnecessary to cast it, but it becomes more realistic if it's a more complex expression like this: (foo)(a + b) Or this: // Comma expression or function call arguments? (bar)(first = side_effect++, second)
- WalterBright 3y agoYeah, (a)(b,c) is that a comma-expression or an argument list?
- lifthrasiir 3y agoThe best bit is that the type name resolution depends on the current scope, but the scope itself is very unintuitive on edge cases. Jourdan and Pottier in their paper A Simple, Possibly Correct LR Parser for C11 [1] describe several edge cases, including the following case where scope is not necessarily consecutive: typedef long T, U; // T is an argument T is a typedef // vvvvvvvvvvvvvvvvvvvvvvvvvvv ~~~ enum {V} (*f(T T, enum {U} y, int x[T+U]))(T t) { // T is an argument (until the end of function) // vvvvvvvvvvvvvvvvvvvvvv long l = T+U+V+x[0]+y; return 0; } [1] https://hal.science/hal-01633123/file/jourdan2017simple.pdf https://hal.science/hal-01633123/file/jourdan2017simple.pdf