posts from @JoshJers tagged #coding

also: #software development, #programming

One thing I'm having trouble with in my programming language dev project is scoped vs. unscoped blocks.

For instance, normally if you have variables inside of a { } block, they're scoped to that block (when the block ends they no longer exist).

However, I have need of an unscoped block, mainly for compile-time conditions.

In C/C++, these conditions are usually written as:

#if fooo
...
#endif 

but I don't actually have a preprocessor so it'd be nice to have something closer to the rest of the language. something like:

if const(foo)
#{
  // do something only if foo is true at compile time
}#

the only problem is I don't love the #{ }# bracing that I have there - but I haven't come up with anything better.

[] and () are already widely used, <> is a parsing nightmare (since they're used in other places as unbalanced operators), I don't really want to use : since it's also widely in use.

Has anyone seen a language that has distinct scoped and unscoped blocks? What did that look like?



The question here is "what's your favorite non-C-like flow control statement/construct that you've seen in a programming language" but I'm gonna start with a little preamble.


I'm very familiar with C-style flow control:

if (condition)
  { thing; }
else 
  { otherThing; }


// And, thanks to C++17:
if (auto v= initializer(); condition) 
  { thing(v); }
else 
  { otherThing(v); }
for (int i = 0; i < 5; i++)
  { thing(); }
for (var e : elements)
  { thing(e); }
while (condition)
  { thing(); }

(plus, ya know, do/while and goto)

I've seen a few others, like Rust's loop (basically a while(true)):

loop
{ 
  thing(); 
  if (something) 
    { break; }
}

and Swift's ranges, which are nice:

for i in 0..<4
  { thing(i); }

What I'm wondering is: what are your favorite bits of flow control logic (loops, jumps, conditionals, etc) that don't show up in C or C++? Are there loop forms that you think are truly elegant? Is there a switch-like construct that doesn't suck like C's switch statement does?



(This is mostly going to be a ramble that is me sort of trying to think out loud but I'll post it to the internet for some reason)

Been poking at my toy language idea in flagrant disregard for all sensible advice to, uh, checks notes "don't do that", and I made what I feel is a pretty good decision in terms of type syntax, but it's made a different piece of the syntax a little uglier.



(This post follows Part 1: 32-bit floats and will make very little sense without having read that one first. Hell, it might make little sense HAVING read that one first, I dunno!)

Last time we went over how to calculate the results of the FMAdd instruction (a fused-multiply-add calculated as if it had infinite internal precision) for 32-bit single-precision float values:

  • Calculate the double-precision product of a and b
  • Add this product to c to get a double-precision sum
  • Calculate the error of the sum
  • Use the error to odd-round the sum
  • Round the double-precision sum back down to single precision

This requires casting up to a 64-bit double-precision float to get extra bits of precision. But what if you can't do that? What if you're using doubles? You can't just (in most languages) cast up to a quad-precision float. So what do you do?