<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christoffer Lernö</title>
    <description>The latest articles on DEV Community by Christoffer Lernö (@lerno).</description>
    <link>https://dev.to/lerno</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F60018%2F58e1d46f-b316-483e-af28-c64a264ddf6e.jpeg</url>
      <title>DEV Community: Christoffer Lernö</title>
      <link>https://dev.to/lerno</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lerno"/>
    <language>en</language>
    <item>
      <title>On arithmetics and overflow</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Tue, 23 Feb 2021 14:12:22 +0000</pubDate>
      <link>https://dev.to/lerno/on-arithmetics-and-overflow-1kob</link>
      <guid>https://dev.to/lerno/on-arithmetics-and-overflow-1kob</guid>
      <description>&lt;p&gt;It is generally understood that overflowing an add or a multiplication is usually a bad thing. This seems to imply that the solution is to detect and trap (quit the program or throw an exception) on such errors. But as we will see, the answer isn't that clear cut.&lt;/p&gt;

&lt;h2&gt;
  
  
  Commutative and associative addition?
&lt;/h2&gt;

&lt;p&gt;Typically when we work with integers, we prefer that the ordering of the operands doesn't matter. For example, if we see &lt;code&gt;a + b + c&lt;/code&gt; we'd prefer that &lt;code&gt;(a + b) + c&lt;/code&gt; is the same as &lt;code&gt;a + (b + c)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately, if we trap for overflow this does not hold. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we trap on overflow then &lt;code&gt;(a + b) + c&lt;/code&gt; would trap, but &lt;code&gt;a + (b + c)&lt;/code&gt; would not. Ooops.&lt;/p&gt;

&lt;p&gt;In C the former is even undefined behaviour. Let's pick an &lt;code&gt;unsigned&lt;/code&gt; example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UINT_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because overflow is wrapping in C, this is well defined and gives the expected result. In languages where even &lt;em&gt;unsigned&lt;/em&gt; overflow is trapped, such as Swift, this will similarly trap or not trap depending on evaluation order. For unsigned we can also design an common overflow (sometimes called an underflow) by having a possible negative intermediate value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case the overflow will happens if we evaluate &lt;code&gt;a + (b - c)&lt;/code&gt;. Again this is not a problem in C, but will be a problem if the language traps the overflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trapping overflow fixes actual bugs
&lt;/h2&gt;

&lt;p&gt;So is trapping overflow bad? If they create subtle problems (in particularly in C where it's undefined behaviour), shouldn't we always wrap?&lt;/p&gt;

&lt;p&gt;Again, the problem is not as simple as that. With trapping overflow we catch this exploit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see here that if there is no trap on overflow, then we can pick &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; so that the allocated size overflows into a small value, and then &lt;code&gt;some_value&lt;/code&gt; actually will be written out of bounds.&lt;/p&gt;

&lt;p&gt;(This is from an actual exploited overflow in the wilds.)&lt;/p&gt;

&lt;p&gt;Some people will point out that with proper bounds checking then the exploit cannot occur either. But that relies on proper bounds being known. It's probably possible to rewrite the code in the example to use &lt;em&gt;slices&lt;/em&gt; (pointer + length) with bounds checking, but in general we can't rely on that to fix all the various overflows.&lt;/p&gt;

&lt;h2&gt;
  
  
  A detour: mixed signedness
&lt;/h2&gt;

&lt;p&gt;A quick question: "What should the type of an unsigned int added to a signed int be?"&lt;/p&gt;

&lt;p&gt;For C the answer is "an unsigned int", in C# the answer is "a long".&lt;/p&gt;

&lt;p&gt;The C answer seems bad, surely we want something signed to be safe? But there's a reason to this apparent madness:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10U&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, is the result well defined? – Well, yes it is! All the operands are converted into unsigned and then wrapping addition is performed, followed by an implicit cast back to an integer value.&lt;/p&gt;

&lt;p&gt;What had happened if C instead had did a cast on the unsigned to a signed integer? Well &lt;code&gt;INT_MAX + 10U&lt;/code&gt; results in a large negative number, we then subtract 10 from that value, which results in a value less than &lt;code&gt;INT_MIN&lt;/code&gt;, which in C is undefined behaviour due to the overflow.&lt;/p&gt;

&lt;p&gt;Because signed ints have undefined behaviour for overflow, it's safer to cast to unsigned in C. Implicit conversion between signed and unsigned representations means that the code looks very simple and is mostly right, even though it does clever things.&lt;/p&gt;

&lt;p&gt;So what about languages with trapping overflow?&lt;/p&gt;

&lt;p&gt;The usual case is that such languages require explicit casts. Let's try that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10U&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// BOOM!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, that didn't work. What about this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10U&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Also BOOM!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second case the unsigned version of &lt;code&gt;b&lt;/code&gt; becomes a large positive number, causing the unsigned add to overflow as well.&lt;/p&gt;

&lt;p&gt;If you think about it this is quite natural: unsigned maths uses 2s complement numbers and wrapping overflow to represent negative numbers, so without wrapping behaviour adding negative numbers can't work.&lt;/p&gt;

&lt;p&gt;Let's look at another common example, now with unsigned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again this "just works" since C gladly converts &lt;code&gt;change&lt;/code&gt; to an unsigned value even if it's negative and it just works. With overflow trap on unsigned again it won't work.&lt;/p&gt;

&lt;p&gt;Let's look at how we currently could solve this in Zig:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;u32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+%&lt;/span&gt; &lt;span class="nb"&gt;@bitCast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down: &lt;code&gt;@bitCast&lt;/code&gt; is needed to convert the &lt;code&gt;i32&lt;/code&gt; in the same way &lt;code&gt;(unsigned)change&lt;/code&gt; would do in C.&lt;/p&gt;

&lt;p&gt;Now once we have this 2s complement unsigned, we request wrapping addition using the &lt;code&gt;+%&lt;/code&gt; operator (Zig has wrapping operator counterparts for all arithmetics operations).&lt;/p&gt;

&lt;p&gt;Problem solved!&lt;/p&gt;

&lt;p&gt;... Or wait? What happened to our overflow check? For example we could set &lt;code&gt;x = 0&lt;/code&gt; and &lt;code&gt;change = -1&lt;/code&gt; and this would happily just work without batting an eye.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;real&lt;/em&gt; solution that works and traps overflow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;@intCast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;@as&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;@as&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we first go to the larger type (&lt;code&gt;i64&lt;/code&gt;) perform the add, then try to narrow that number to an &lt;code&gt;u32&lt;/code&gt;, catching any negative numbers or numbers that exceed the maximum of &lt;code&gt;u32&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we've come full circle:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduce trapping "by default" so it's easy to do the right thing.&lt;/li&gt;
&lt;li&gt;Realize that some cases need to circumvent the trapping to be correct.&lt;/li&gt;
&lt;li&gt;Find a "correct" solution that is very complicated that people are supposed to follow to do the right thing.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Enter the left hand side
&lt;/h2&gt;

&lt;p&gt;Unfortunately we're not done yet listing the problems, what about this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is that UB assuming &lt;code&gt;long long&lt;/code&gt; is larger than &lt;code&gt;int&lt;/code&gt;? In C, sure it is. What the person writing this probably wanted was the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mostly people don't run into this due to C's promotion rules. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x7FFF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will do "the right thing" on all platforms where the &lt;code&gt;int&lt;/code&gt; is at least 32 bits. This is because C implicitly converts all arguments to an &lt;code&gt;int&lt;/code&gt; before performing any arithmetic operation.&lt;/p&gt;

&lt;p&gt;But as demonstrated by the above examples that only gets you so far. There is a recent language trend to perform arithmetics with the native bit width, leading to new unexpected results (this example is Zig):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// BOOM!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the multiplication is performed using signed 8 bits, which will overflow on &lt;code&gt;16 * 16&lt;/code&gt;, even it later would have been promoted to &lt;code&gt;i32&lt;/code&gt; for the addition.&lt;/p&gt;

&lt;p&gt;These are hard to spot errors that will yield runtime errors but look fine to the compiler.&lt;/p&gt;

&lt;p&gt;It would be reasonable that the left hand side guides the type used on the right hand side in the assignment, but that adds complexity that few languages want to take on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Overflow traps cause unexpected and hard-to-spot non commutative and associative behaviour in expressions that otherwise would have been fine.&lt;/li&gt;
&lt;li&gt;Wrapping behaviour enables buffer overflows and similar exploits.&lt;/li&gt;
&lt;li&gt;Overflow traps on unsigned integers makes the mixed signedness case very hard to get right.&lt;/li&gt;
&lt;li&gt;In most languages it doesn't matter if the left hand sign can contain the number, what matters are the types on the right hand side, which isn't always intuitive or desired.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In my next post I'll investigate ways to try to improve on the status quo.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>programminglanguages</category>
    </item>
    <item>
      <title>Implementing "defer"</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Mon, 27 Jul 2020 09:24:54 +0000</pubDate>
      <link>https://dev.to/lerno/implementing-defer-3l76</link>
      <guid>https://dev.to/lerno/implementing-defer-3l76</guid>
      <description>&lt;p&gt;The &lt;code&gt;defer&lt;/code&gt; statement is going mainstream. Go has it's own special defer which only fires on function end, otherwise &lt;code&gt;defer&lt;/code&gt; has consistent "execute at scope end" semantics. Swift, &lt;a href="https://ziglang.org/documentation/master/#defer"&gt;Zig&lt;/a&gt;, Jai, &lt;a href="https://nim-lang.org/docs/manual.html#exception-handling-defer-statement"&gt;Nim&lt;/a&gt; and &lt;a href="https://odin-lang.org/docs/overview/#defer-statement"&gt;Odin&lt;/a&gt; all use defer in this manner.&lt;/p&gt;

&lt;p&gt;The problems with implementing &lt;code&gt;defer&lt;/code&gt; is similar to implementing destructors for stack allocated objects in C++, although the presence of virtual functions complicates things.&lt;/p&gt;

&lt;p&gt;I couldn't find anyone describing how &lt;code&gt;defer&lt;/code&gt; is done in other compilers so when working on a version of it for &lt;a href="http://www.c2lang.org"&gt;C2&lt;/a&gt; I had to make it up as I went along.&lt;/p&gt;

&lt;p&gt;For posterity's sake I thought it might be interesting to do a writeup on how defer was implemented.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting the rules
&lt;/h2&gt;

&lt;p&gt;First up there are lots of different possible rules to adapt defer to. The original draft of this article would handle &lt;code&gt;goto&lt;/code&gt; &lt;em&gt;across&lt;/em&gt; defers. C2 retains &lt;code&gt;goto&lt;/code&gt;, and for a long time, so did &lt;a href="http://www.c3-lang.org"&gt;C3&lt;/a&gt; – so this was important to make the article complete. However &lt;code&gt;goto&lt;/code&gt; adds much complexity to defer which made the article both much longer and harder to follow.&lt;/p&gt;

&lt;p&gt;For that reason we'll limit ourselves to &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;continue&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt; and labelled versions of the latter. If there is interest I can go into details on how to add defer for &lt;code&gt;goto&lt;/code&gt; in another article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling early exit
&lt;/h2&gt;

&lt;p&gt;The first issue in &lt;code&gt;defer&lt;/code&gt; is the early exit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;return&lt;/code&gt; needs to inline the defer at the end, so this is lowered to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; this is handled similar to &lt;code&gt;return&lt;/code&gt; but only part of the defers may be inlined at the point of the break:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And the inlined version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also have the labelled version of break. (I'll stick to the java-style labelled break syntax here)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;FOO:&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;FOO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is again lowered to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;FOO:&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;FOO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So as we see it's sufficient to keep a list of the defers and then inline the defer statements in reverse order where we encounter a &lt;code&gt;break&lt;/code&gt; &lt;code&gt;continue&lt;/code&gt; or &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it all together
&lt;/h2&gt;

&lt;p&gt;So now we've listed all the things we need to solve. How do we put it together? Here's the algorithm I used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For each defer, keep a pointer back to the previously active defer.&lt;/li&gt;
&lt;li&gt;For each dynamic scope, keep track of the current defer.&lt;/li&gt;
&lt;li&gt;On break/continue/return AST nodes, store 2 AST nodes.&lt;/li&gt;
&lt;li&gt;On each scoping AST node (while, for, compound statement etc) store 2 AST nodes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Algorithm
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Set &lt;code&gt;current_scope-&amp;gt;current_defer = NULL&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Traverse the AST-tree.&lt;/li&gt;
&lt;li&gt;When pushing a new dynamic scope
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When encountering a defer:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;defer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When encountering a scoped statement:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;scoped_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;push_new_current_scope&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;recursively&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;scoped_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;end_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pop_current_scope&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When encountering a break or continue:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;Ast&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;target_ast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_target&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;break_stmt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;break_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;end_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;break_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target_ast&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When encountering a return:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;return_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_scope&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This results in us being able to use each defer as the top of a linked list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current_defer
   |
   v
current_defer-&amp;gt;prev_defer
   |
   v
current_defer-&amp;gt;prev_defer-&amp;gt;prev_defer
   |
   v
  NULL
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Codegen is now easy.&lt;/p&gt;

&lt;p&gt;We introduce a helper function to inline defers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;codegen_defers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Defer&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Defer&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;codegen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;current_defer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_defer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prev_defer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When doing codegen for a scoped statement:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;codegen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scoped_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;inner_stmt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;codegen_defers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scoped_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;end_defer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scoped_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start_defer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When doing codegen for &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;continue&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;codegen_defers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;break_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;end_defer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;break_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start_defer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;codegen_break&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;break_stmt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Codegen for &lt;code&gt;return&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;codegen_defers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;return_stmt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;defer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;codegen_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;return_stmt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Going further
&lt;/h2&gt;

&lt;p&gt;Ok, so now we're done? Not quite, if we want to go beyond C syntax. We can imagine something looking a bit like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getFile&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case we actually have two scopes: one inner scope (between &lt;code&gt;{}&lt;/code&gt;) and the outer one that starts in the conditional.&lt;/p&gt;

&lt;p&gt;The principle is the same so we can reuse the same solution as above, but it's worth taking note of this case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defer after if
&lt;/h3&gt;

&lt;p&gt;We have other questions to answer as well. What does this code do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"x was 0&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Some people have suggested that this should be treated as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;defer&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"x was 0&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I am strongly against that idea, as it would mean that compound statements suddenly have a different meaning than regular statements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defer as part of the function
&lt;/h3&gt;

&lt;p&gt;Another interesting thing one can do with defer is the idea that a function may contain an implicit &lt;code&gt;defer&lt;/code&gt; that is added to the scope which invokes it. &lt;a href="https://odin-lang.org"&gt;Odin&lt;/a&gt; has that feature using "deferred attributes" (see further down from &lt;a href="https://odin-lang.org/docs/faq/#does-odin-have-c-style-destructors"&gt;this link&lt;/a&gt;). This is simple to tie into the defer machinery.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defers &amp;amp; goto
&lt;/h3&gt;

&lt;p&gt;Handling &lt;code&gt;goto&lt;/code&gt; with defers is a bit more complicated as one need to conditionally invoke defers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;FOO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// When is this called?&lt;/span&gt;
  &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;FOO:&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The lowered code needs to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;_defer_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;FOO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;_defer_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;FOO:&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_defer_1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since B can jump &lt;em&gt;into&lt;/em&gt; scopes as well as out of scopes, this adds another dimension to the analysis. The solution is not &lt;em&gt;hard&lt;/em&gt; but definitely not as straightforward as the structured jumps of &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Non-local jumps of &lt;code&gt;setjmp&lt;/code&gt; are not possible to handle at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  Go style defers
&lt;/h3&gt;

&lt;p&gt;Go has a different style of defer. Go's defers actually store the defer code like a closure that is queued and invoked &lt;em&gt;at function end&lt;/em&gt; rather than at scope end. This means a defer actually needs to allocate memory for itself. A loop like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Would queue up all the defers generated in the loop in a long list and release them at function end. If the &lt;code&gt;defer&lt;/code&gt; is releasing something limited like db connections then this is a bad idea. For various "gotchas" in Go due to this style of defer, see &lt;a href="https://blog.learngoprogramming.com/gotchas-of-defer-in-go-1-8d070894cb01"&gt;this&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While Go defers work nicely with exceptions and &lt;code&gt;goto&lt;/code&gt;, it has quite a bit of quirks as well as the need to reserve memory to store the defers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defers and errors
&lt;/h3&gt;

&lt;p&gt;Sometimes one would prefer for defers to only occur on error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;getAndCheckFile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getFile&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;// We want to close if we return with error.&lt;/span&gt;
   &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;fileIsValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;NuLL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;readHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mh"&gt;0xdeadbeef&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;// oops, we will be closing f!&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For this reason Zig introduces &lt;code&gt;errdefer&lt;/code&gt;, and C3 has &lt;code&gt;defer catch&lt;/code&gt; / &lt;code&gt;defer try&lt;/code&gt; statements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Being able to cancel defers
&lt;/h3&gt;

&lt;p&gt;As an alternative (and complement) to special forms of defer is being able to &lt;em&gt;cancel&lt;/em&gt; defers. So far I've only seen this functionality on defer implemented as RAII. Theoretically it could look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;getAndCheckFile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getFile&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nl"&gt;FOO:&lt;/span&gt; &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;fileIsValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;NuLL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;readHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mh"&gt;0xdeadbeef&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;undefer&lt;/span&gt; &lt;span class="n"&gt;FOO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Defer is useful functionality for languages that lack either &lt;code&gt;finally&lt;/code&gt; or RAII. With structured jumps it is straightforward to implement with zero overhead.&lt;/p&gt;

</description>
      <category>compilers</category>
      <category>programminglanguagedesign</category>
      <category>defer</category>
      <category>c3</category>
    </item>
    <item>
      <title>Macros in C3 - a status update</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Sat, 18 Jul 2020 19:29:33 +0000</pubDate>
      <link>https://dev.to/lerno/macros-in-c3-a-status-update-1m1n</link>
      <guid>https://dev.to/lerno/macros-in-c3-a-status-update-1m1n</guid>
      <description>&lt;p&gt;I'm going to share a bit of the C3 design process here for people who might be interested.&lt;/p&gt;

&lt;p&gt;Like error handling, macros are one of the few truly &lt;em&gt;new&lt;/em&gt; things in C3 compared to C. Consequently I've been going back and forth with the design trying to cover all angles. &lt;/p&gt;

&lt;p&gt;I always wanted to make macros sufficiently safe that people could use them without worries, which means that some macro use from C would have to go, but which one?&lt;/p&gt;

&lt;p&gt;After doing an inventory of what macros &lt;em&gt;could&lt;/em&gt; do, I roughly end up with this "feature ladder" for macros – from easily understandable and readable to more "dangerous" in terms of how easy it would be to abuse:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inlining&lt;/li&gt;
&lt;li&gt;Lazy evaluation of arguments&lt;/li&gt;
&lt;li&gt;Polymorphic parameters&lt;/li&gt;
&lt;li&gt;Non-local jumps&lt;/li&gt;
&lt;li&gt;Implicit capture&lt;/li&gt;
&lt;li&gt;Declarations escaping scope&lt;/li&gt;
&lt;li&gt;Arbitrary code generation&lt;/li&gt;
&lt;li&gt;Code fragment replacement&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One has to make the cut somewhere, and for C3 I think it's reasonable to either stop at (4) or (5).&lt;/p&gt;

&lt;p&gt;(5) - implicit capture - is a bit related to (8) but can often be extremely useful in local code. &lt;/p&gt;

&lt;p&gt;One hard-to-place feature is taking a name or a function invocation and then generating statements from that.&lt;/p&gt;

&lt;p&gt;Consider the following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#define FOO(X) do { X(0); X(1); X(2); } while 0;
void doX(int i) { ... }
FOO(doX);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In C3 this is sort of covered at the (2) level, even though for C that would be (7).&lt;/p&gt;

&lt;p&gt;Because macros are mainstream tools in C3 rather than advanced tools it’s important that the syntax is geared towards writing code for 1-3 in particular.&lt;/p&gt;

&lt;p&gt;This naturally makes it more natural to require that the macros should resemble functions as much as possible.&lt;/p&gt;

&lt;p&gt;(6), (7) and (8) are, when used, usually clever ways to twist C into being more brief or to have an in-code DSL.&lt;/p&gt;

&lt;p&gt;This flexibility can create pretty neat hacks, but it’s unclear whether this is a good idea in the large. Are these just clever solutions or are they important ones? My bet is on the former: that the legitimate uses more are about closing holes in C. And if it is, then the macros are basically a poor man's syntax extensions. &lt;/p&gt;

&lt;p&gt;If syntax extensions are desired, &lt;a href="https://www.kitlang.org"&gt;Kit&lt;/a&gt; shows how that can be done in a very elegant manner. However, syntax extensions will always sacrifice readability for power, and here C3 makes a different tradeoff so that no matter what macro you see, you should be able to make a good guess as to what it could be doing.&lt;/p&gt;

&lt;p&gt;For comparison, here are some C macros and their counterpart in C3 (as the design currently stands):&lt;/p&gt;

&lt;p&gt;C:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define nodesGet(nodes, index) ((INode**)((nodes)+1))[index]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;C3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;macro&lt;/span&gt; &lt;span class="n"&gt;INode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;nodesGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;INode&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;C3 allows trailing body in macros, which makes for slightly different look from C in "foreach" style macros:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define namespaceFor(ns) for (size_t __i = 0; __i &amp;lt; (ns)-&amp;gt;avail; ++__i)
&lt;/span&gt;
&lt;span class="n"&gt;namespaceFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;NameNode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;namenodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;__i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;nametblHookNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;C3 (note that the declaration for trailing body is very much undecided):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;macro&lt;/span&gt; &lt;span class="n"&gt;namespaceFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usize&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usize&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;avail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;namespaceFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;usize&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;NameNode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;namenodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;nametblHookNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using implicit capture of variables from the surrounding scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define lexReturnPuncTok(tok, skip) { \
  lex-&amp;gt;toktype = tok; \
  lex-&amp;gt;tokp = srcp; \
  lex-&amp;gt;srcp = srcp + (skip); \
  return; \
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;C3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;macro&lt;/span&gt; &lt;span class="n"&gt;lexReturnPuncTok&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;srcp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toktype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tok&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tokp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;srcp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;srcp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;srcp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Creating a good macro system that is simple enough not to be dangerous requires difficult trade offs, and it's easy to just make it as flexible as possible. That might be a mistake though, with macros becoming an advanced feature reserved for special situations instead of a regular tool in the toolkit.&lt;/p&gt;

</description>
      <category>c3</category>
      <category>compilers</category>
      <category>macros</category>
    </item>
    <item>
      <title>A zoo of casts</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Tue, 14 Jul 2020 20:35:52 +0000</pubDate>
      <link>https://dev.to/lerno/a-zoo-of-casts-4bob</link>
      <guid>https://dev.to/lerno/a-zoo-of-casts-4bob</guid>
      <description>&lt;p&gt;I recently made a post on Reddit to ask about various types of cast syntax. For posterity's sake I'm recording them here.&lt;/p&gt;

&lt;p&gt;Note that I'm ignoring the behaviour of the cast. Some languages have different syntax for upcasts, downcasts, bitcasts etc. I'm not concerned with that here. This is merely a list of variants of visual syntax. Consequently I list &lt;code&gt;:&amp;gt;&lt;/code&gt; even though that's only a special form of cast for F#, and only one of the many &lt;code&gt;keyword&amp;lt;type&amp;gt;(x)&lt;/code&gt; casts for C++, even though there are many variants.&lt;/p&gt;

&lt;p&gt;Also I apologize in advance if the attribution is incorrect somewhere. I don't know all the languages I list here.&lt;/p&gt;

&lt;p&gt;(The list of languages for each is also incomplete – it's just a sample)&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast(x, int)        MATLAB
int(x)              Pascal
&amp;lt;int&amp;gt;x              Typescript
(int)x              C/C++/Java/Beef/C#
static_cast&amp;lt;int&amp;gt;    C++
x as int            C#/Swift/Rust
x as! int           Swift
cast(x as int)      SQL
cast(int)x          D, Jai
@as(int, x)         Zig
[int]x              Pike
(int)(x)            Go
x :&amp;gt;                F#
cast[int](x)        Nim
x.as(int)           Crystal/Ecstasy
x-&amp;gt;(int)            Frost
(x: int)            Flow
cast&amp;lt;int&amp;gt;(x)        C2
x.asInstanceOf(int) Scala
x.(int)             Go
x $ int             ChucK
int'(x)             Verilog
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For fun, here are other permutations of the cast syntax that may or may not be useful:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(x as int)       
(x, int)
x&amp;lt;int&amp;gt;
x::int
(int : x)
(int x)
int::x
(x :: int)
cast(x -&amp;gt; int)
x to int
x#int
int:x
x.as[int]
x[int]
x.int
(int &amp;gt;&amp;gt; x)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;C3 is currently using &lt;code&gt;cast(x, int)&lt;/code&gt; but that might change.&lt;/p&gt;

&lt;p&gt;When evaluating syntax, readability is important and it is always nice if the precedence is crystal clear.&lt;/p&gt;

&lt;p&gt;As an example: &lt;code&gt;x as Foo[4]&lt;/code&gt; – would that be &lt;code&gt;x as (Foo[4])&lt;/code&gt; or &lt;code&gt;(x as Foo)[4]&lt;/code&gt;? Precedence rules will obviously decide, but if we compare with &lt;code&gt;cast&amp;lt;Foo[4]&amp;gt;(x)&lt;/code&gt; the latter is much clearer because there is no need to know the precedence. &lt;/p&gt;

&lt;p&gt;But length also matters: &lt;code&gt;x = int(y) + int(z)&lt;/code&gt; is succinct while &lt;code&gt;x = cast(y, int) + cast(z, int)&lt;/code&gt; feels quite a bit more wordy.&lt;/p&gt;

&lt;p&gt;Picking a good cast syntax for a language is clearly one of difficult trade-offs.&lt;/p&gt;

</description>
      <category>syntax</category>
      <category>compilers</category>
    </item>
    <item>
      <title>More on error handling in C3</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Fri, 03 Jul 2020 19:40:18 +0000</pubDate>
      <link>https://dev.to/lerno/more-on-error-handling-in-c3-3bee</link>
      <guid>https://dev.to/lerno/more-on-error-handling-in-c3-3bee</guid>
      <description>&lt;p&gt;When we left off, C3 was looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int! index = atoi(readLine());
if (index) {
  printf("Thx for the number\n");
  // Index is now int.
  ... 
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I somewhat off-handedly mentioned that some sort of &lt;code&gt;guard&lt;/code&gt; statement would be needed to extract the error and the need to handle things like &lt;code&gt;index &amp;amp;&amp;amp; index &amp;gt; 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As usual in language design, things become less easy the more you flesh out the spec.&lt;/p&gt;

&lt;p&gt;The first obvious problem is using &lt;code&gt;if (index)&lt;/code&gt; for unwrapping.&lt;/p&gt;

&lt;p&gt;Here's a problematic piece of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool! b = someCall();
// Is this checking if b is true or non error?
if (b) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A way around this would be to explicitly indicate the success check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool! b = someCall();
// Use the ? to indicate unwrapping
if (b?) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This seems fine, but now that we made &lt;code&gt;b?&lt;/code&gt; doing implicit unwrapping we're making pretty complicated things possible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool! b = someCall();
if (i &amp;gt; 0 &amp;amp;&amp;amp; b? &amp;amp;&amp;amp; ((b = someCall())? || i &amp;gt; 100) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the example above the compiler has to figure out that b might possibly have an error...&lt;/p&gt;

&lt;p&gt;To deal with this we need to do real full &lt;a href="https://en.wikipedia.org/wiki/Flow-sensitive_typing"&gt;flow typing&lt;/a&gt;, which increases the complexity of implementing the compiler by quite a bit. That's not the only problem: flow typing means types implicitly change. A quick look at the code above - is it easy to see that &lt;code&gt;b&lt;/code&gt; will not be unwrapped in the body?&lt;/p&gt;

&lt;p&gt;So flow typing has both advantages and disadvantages.&lt;/p&gt;

&lt;p&gt;One of the core principles I try to follow building this language is that it should not be hard to write a compiler for it. It's by necessity a multi pass compiler, but other things it's nice to keep simple.&lt;/p&gt;

&lt;p&gt;There are ways to do so. For example, unwrapping might require what in other languages are called a "if-let":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool! b = someCall();
if (bool b1 = b?) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here there is no implicit unwrap, it's just another variable introduced in the scope. This is all well, but pretty verbose. It would be nice to have a shortcut for the &lt;code&gt;bool b = b?&lt;/code&gt; case.&lt;/p&gt;

&lt;p&gt;Again the language design becomes more complex than one likes. C3 has a pretty flexible &lt;code&gt;if&lt;/code&gt; statement that allows you to write things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (int a = foo(), b = bar(), int c = baz()) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However in this case only the final result (that of &lt;code&gt;baz()&lt;/code&gt;) counts. If it looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (int a = foo()?, b = bar(), int c = baz()) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We'd have to make sure that the call to &lt;code&gt;foo()&lt;/code&gt; didn't return an error AND that &lt;code&gt;baz()&lt;/code&gt; was non zero.&lt;/p&gt;

&lt;p&gt;So what should we do?&lt;/p&gt;

&lt;p&gt;It's time to take a step back and review our options without making assumptions that we unwrap things with &lt;code&gt;if&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First let us construct our &lt;code&gt;guard&lt;/code&gt; statement – the one taking  a block to execute if there is an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int! i = ...
catch (err = i)
{
   ... handle the error ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can do some very simple flow typing here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If a variable is caught using a &lt;code&gt;catch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;And the catch has a jump at all exits&lt;/li&gt;
&lt;li&gt;Then the variable can be types to the non failable version of it after the catch.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int! i = ...
catch (err = i)
{
   ...
   return;
}
// i is int here
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So that works. This is much easier than if we hade overloaded &lt;code&gt;if&lt;/code&gt; to handle error unwrapping. What if we introduce &lt;code&gt;try&lt;/code&gt; to be like &lt;code&gt;if&lt;/code&gt; but only for unwrapping:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try (int j = i) 
{ 
 ... only executes if i is not an error ...
}
try (i) 
{ 
 ... i is implicitly unwrapped to int ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So to wrap up, here are some elements of the error handling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int! i = ...

// Default value if it is an error
int j = i else 0;

// Jump on error
int k = i else return;

// Check error
try (i)
{
  printf("i was: %d\n", i);
}

// Conditional execution
// this line is only called
// if i is not an error.
printf("i was: %d\n", i);

// Composition:
bool! b = checkFoo(getFoo(i));
int! l = i + 1;

// Returning something that may be an error
if (z &amp;gt; 0) return i;

// Check if error
bool wasError = check(i);

// Check if success
bool wasSuccess = try(i);

// Returning an error
return MyError!;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The error handling still has some ways to go, but it's getting closer to something that also handles the various possible corner cases and not just the simplest use cases.&lt;/p&gt;

</description>
      <category>compilers</category>
      <category>c3</category>
      <category>errors</category>
    </item>
    <item>
      <title>A new error handling paradigm for C3</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Sun, 14 Jun 2020 12:44:12 +0000</pubDate>
      <link>https://dev.to/lerno/a-new-error-handling-paradigm-for-c3-2geo</link>
      <guid>https://dev.to/lerno/a-new-error-handling-paradigm-for-c3-2geo</guid>
      <description>&lt;p&gt;The &lt;a href="http://www.c3-lang.org"&gt;C3&lt;/a&gt; programming language is getting increasingly more complete (try it out &lt;a href="https://ide.judge0.com/?YOhO"&gt;here&lt;/a&gt;!), it's a language very close to C similar to the C2 language.&lt;/p&gt;

&lt;h4&gt;
  
  
  The current state of C3
&lt;/h4&gt;

&lt;p&gt;C3 of today has an error system inspired by Midori and Herb Sutter's C++ error proposal. It has lots of similarities with Zig's errors as well.&lt;/p&gt;

&lt;p&gt;Here is an example from the documentation:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error RandomError {
  NORMAL,
  EXCEPTIONAL
}

func int mayThrowError() throws RandomError {
  if (rand() &amp;gt; 0.5) throw RandomError.NORMAL;
  if (rand() &amp;gt; 0.99) throw RandomError.EXCEPTIONAL;
  return 1;
}

func void testMayError() throws
{
  // all throwable sites must be annotated with "try"
  try mayThrowError(); 
}

func void testWithoutError() {
  try testMayError();

  // Catching will catch any try above in the scope.
  catch (error e) {
    case RandomError.NORMAL:
      io.printf("Normal Error\n");
    case RandomError.EXCEPTIONAL:
      io.printf("You win!\n");
    default:
      io.printf("What is this error you're talking about?\n");                 
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This might at first glance look like exceptions, but it is value based and a function like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func int getFoo() throws RetrieveError;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Corresponds to the C code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RetrieveError getFoo(int *result);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So throws are really return values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why isn't this sufficient?
&lt;/h4&gt;

&lt;p&gt;Compared to exceptions I find this pretty good. Places where errors occur are clearly marked and we're using value based return values under the sheets.&lt;/p&gt;

&lt;p&gt;However, the flow here is clearly exception-style. Personally I like the explicit control given by C return values. However, they are not always convenient. For a function in C you usually end up with one of four cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No errors returned, just return the result.&lt;/li&gt;
&lt;li&gt;It may fail, so return boolean, result as "out" parameter. Maybe use errno or similar to get details.&lt;/li&gt;
&lt;li&gt;It may fail in many ways, so return the error code, result as an "out" parameter.&lt;/li&gt;
&lt;li&gt;It may fail in many ways, return the result (usually a pointer), the error is an out parameter and will be set if it fails.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are typically only simple in the case that no result is needed.&lt;/p&gt;

&lt;p&gt;Go improves on this by using tuple returns, which folds 2-4 into a single case. However there's another problem – that of having multiple calls which would throw errors. In C it might end up looking like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (doSomething() != OK) goto ERR;
if (doSomethingElse() != OK) goto ERR;
cannotFailProc();
if (blah() != OK) goto ERR;
return true;
ERR:
... error handling ...
return false;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This contrasts with exception style code which can be much easier to read:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try
{
  doSomething();
  doSomethingElse();
  cannotFailProc();
  blah();
  return true;
}
catch
{
   ... error handling ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Go has tried to make some efforts to improve the rather infamous cascade of &lt;code&gt;if (err != nil) { ... }&lt;/code&gt; code but hasn't really made any major progress.&lt;/p&gt;

&lt;p&gt;Designing a new language this has frustrated me: exceptions are known to have issues, but so do "return values".&lt;/p&gt;

&lt;p&gt;There's also the idea to use sum types, e.g. &lt;code&gt;Result&amp;lt;MyResult, Error&amp;gt;&lt;/code&gt; and pass them around. Swift was even built with optionals where "no value" meant an error... but that was so unergonomic that they later introduced an exception style error handling very similar to what C3 currently provides.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Result&lt;/code&gt; would usually mean using things like &lt;code&gt;getMaybeThrowingInt().flatMap(i =&amp;gt; get(i)).flatMap(val =&amp;gt; val.openFile)&lt;/code&gt; where each invocation only conditionally happens if the result is a non-error.&lt;/p&gt;

&lt;p&gt;However, this &lt;code&gt;Result&lt;/code&gt; based would often look rather different from the normal "error free" code. So that looked like a dead end as well.&lt;/p&gt;

&lt;h4&gt;
  
  
  Frustrations and an idea
&lt;/h4&gt;

&lt;p&gt;Trying out the error handling in C3 I was frustrated with how ugly simple functions would look in the case they had a single error.&lt;/p&gt;

&lt;p&gt;Consider the simple task of looking up the index of an item in an array and using it.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error SearchError {
  ELEMENT_NOT_FOUND;
}
func int indexOfFoo(Foo[] f, int i) throws SearchError
{ .... }

func void test(Foo[] f)
{
  int i = try indexOfFoo(f, 1);
  printf("Name1: %s", f[i].name);
  catch (SearchError e)
  { 
    printf("Name1 could not be found\n");
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using an error here like this feels all wrong. (Java infamously returns -1 rather than using an exception on this sort of code).&lt;/p&gt;

&lt;p&gt;Some Go style tuple return would probably have given us:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func void test(Foo[] f)
{
  int i, bool success = indexOfFoo(f, 1);
  if (!success)
  {
    printf("Name1 could not be found\n");
    return;
  }    
  printf("Name1: %s", f[i].name);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And this feels more reasonable. Not because of the code size, but because it feels weird to introduce jumps in the code just to handle the fact that there is the possibility of a special "not found" index.&lt;/p&gt;

&lt;p&gt;So what do I really want? The Go version translates everything to &lt;em&gt;values&lt;/em&gt; rather than implicit jumps. This is similar to how &lt;code&gt;Result&lt;/code&gt; works. Maybe there is a way?&lt;/p&gt;

&lt;p&gt;What if we introduce a built in sum type: "a type + error". For example &lt;code&gt;int!&lt;/code&gt; would be the same as &lt;code&gt;Result&amp;lt;int, Error&amp;gt;&lt;/code&gt; in languages using &lt;code&gt;Result&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we rewrite our code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error ElementNotFoundError;

func int! indexOfFoo(Foo[] f, int i)
{ .... }

func void test(Foo[] f)
{
  int! i = indexOfFoo(f, 1);
  guard (i) // Only called on i is error
  {
    printf("Name1 could not be found\n");
    return;
  }
  // i implicitly becomes "int" due to the guard.
  printf("Name1: %s", f[i].name);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can do more with this though! If we define that a statement relying on a "Result" also becomes a "Result" we get this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func void test(Foo[] f)
{
  int! i = indexOfFoo(f, 1);
  Foo! foo = f[i];
  printf("Name1: %s", foo.name);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In a "Result" based language that would translate to something like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Result&amp;lt;int, Error&amp;gt; i = indexOfFoo(f, 1);
Result&amp;lt;Foo, error&amp;gt; foo = i.flatMap(i =&amp;gt; f[i]);
Result&amp;lt;void, error&amp;gt; res = foo.flatMap(foo =&amp;gt; 
  printf("Name1: %s", foo.name)
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let's look at another example. Here is a sample C# program to illustrate it's exceptions:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static void Main(string[] args)
{
  int index;
  int value = 100;
  int[] arr = new int[10];
  try
  {
    Console.Write("Enter a number: ");
    index = Convert.ToInt32(Console.ReadLine());
    arr[index] = value;
  }
  catch (FormatException e)
  {
    Console.Write("Bad Format ");
  }
  catch (IndexOutOfRangeException e)
  {
    Console.Write("Index out of bounds ");
  }
  Console.Write("Remaining program ");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;C3 has no out of bounds error, but we can make a method for it:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error IndexOutOfBoundsError;

func void! int[].set(int[]* array, int index, int value)
{
  if (index &amp;lt; 0 || index &amp;gt;= array.size) return! IndexOutOfBoundsError;
  array[index] = value;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let's assume &lt;code&gt;atoi&lt;/code&gt; returns a &lt;code&gt;ConversionError&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func void main()
{
  int index;
  int value = 100;
  int[100] arr;
  console::write("Enter a number: ");
  int! index = atoi(readLine());
  guard (arr.set(index, value))
  {
    case ConversionError:
      printf("This is not a number %s\n", error.string);
    case EofError:
      printf("Input closed.\n");
    case IndexOutOfBoundsError:
      printf("Index out of bounds.\n");
    default:
      printf("Unknown error.\n");
  }
  printf("Remaining program\n");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we just want to ignore all errors:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func void main()
{
  int index;
  int value = 100;
  int[100] arr;
  console::write("Enter a number: ");
  int! index = atoi(readLine());
  arr.set(index, value);
  printf("Remaining program\n");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we want to be explicit about following the happy case with nesting, here's a variant:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func void main()
{
  int index;
  int value = 100;
  int[100] arr;
  console::write("Enter a number: ");
  int! index = atoi(readLine());
  if (index) {
    printf("Thx for the number\n");
    // Index is now int.
    // catch (index) - Invalid
    if (arr.set(index, value)) {
        printf("All worked fine!\n")
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;
  
  
  Some unresolved questions
&lt;/h4&gt;

&lt;p&gt;In the text above I use &lt;code&gt;guard&lt;/code&gt; to "get" the error from the "Result". Other variants could be to use &lt;code&gt;catch&lt;/code&gt; or &lt;code&gt;iferr&lt;/code&gt; as a keyword. Maybe even use unrolling with &lt;code&gt;!&lt;/code&gt;, e.g. &lt;code&gt;if (i!)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly the conditional extraction in the &lt;code&gt;if&lt;/code&gt; might have issues. If we have &lt;code&gt;Foo*! f&lt;/code&gt; then &lt;code&gt;if (f)&lt;/code&gt; might assume that &lt;code&gt;f&lt;/code&gt; is also not null, where the correct check would be &lt;code&gt;if (f &amp;amp;&amp;amp; f)&lt;/code&gt;(!).&lt;/p&gt;

&lt;p&gt;Shortcuts for defaults on error are needed. Some possibilities:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int i = atoi(readLine()) ?: 0
int i = atoi(readLine()) else 0;
int i = atoi(readLine()) !! 0;
int i = atoi(readLing()) || 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And for rethrows:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int i = atoi(readLine())!;
int i = atoi(readLine()) else return!;
int i = atoi(readLine())!!;
int i = try atoi(readLine());
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I use &lt;code&gt;return!&lt;/code&gt; for returning errors. Again, it could use &lt;code&gt;raise&lt;/code&gt; &lt;code&gt;throw&lt;/code&gt; or &lt;code&gt;exit&lt;/code&gt; instead. Or an exclamation mark after the error, e.g. &lt;code&gt;return SomeError!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While there is a lot of things left to figure out I at least feel like there is a real alternative here that might be a candidate to replace the current error handling in C3.&lt;/p&gt;

</description>
      <category>compilers</category>
      <category>c3</category>
      <category>errors</category>
    </item>
    <item>
      <title>Thoughts on numeric literal type inference rules for a C-like programming language</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Sun, 03 May 2020 10:58:08 +0000</pubDate>
      <link>https://dev.to/lerno/thoughts-on-numeric-literal-type-inference-rules-for-a-c-like-programming-language-4fpb</link>
      <guid>https://dev.to/lerno/thoughts-on-numeric-literal-type-inference-rules-for-a-c-like-programming-language-4fpb</guid>
      <description>&lt;p&gt;For the &lt;a href="http://www.c3-lang.org/"&gt;C3&lt;/a&gt; language I’m working on I wanted to improve on &lt;em&gt;C&lt;/em&gt;’s integers.&lt;/p&gt;

&lt;p&gt;Recent languages have gravitated towards removing many implicit casts: &lt;em&gt;Rust&lt;/em&gt;, &lt;em&gt;Swift&lt;/em&gt;, &lt;em&gt;Go&lt;/em&gt;, &lt;em&gt;Zig&lt;/em&gt; and &lt;em&gt;Odin&lt;/em&gt; all fall into this camp.&lt;/p&gt;

&lt;p&gt;Studying &lt;em&gt;Go&lt;/em&gt; in particular is illuminating: they recognize that removing implicit casts creates usability issues, and changes &lt;em&gt;numeric literals&lt;/em&gt; to be &lt;em&gt;BigInts&lt;/em&gt;, implicitly convertible into any sufficiently large integer type.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Swift&lt;/em&gt;, &lt;em&gt;Zig&lt;/em&gt; and &lt;em&gt;Odin&lt;/em&gt; all pick up this idea, but in slightly different ways. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Zig&lt;/em&gt; uses what it calls &lt;a href="https://ziglang.org/documentation/master/#Peer-Type-Resolution"&gt;peer type resolution&lt;/a&gt; to describe how the conversion from “compile time integer” (the &lt;em&gt;BigInt&lt;/em&gt; representation) occurs in various circumstances, such as in binary expressions. This is basically picking a common type that all sub expressions can coerce into. Here are some examples: As an example, adding a variable of type &lt;code&gt;i32&lt;/code&gt; with a constant “123” will convert the constant from &lt;em&gt;BigInt&lt;/em&gt; to &lt;code&gt;i32&lt;/code&gt;. The common type here is &lt;code&gt;i32&lt;/code&gt; and this is the type the constant will be cast to. When adding &lt;code&gt;i32&lt;/code&gt; and &lt;code&gt;i64&lt;/code&gt; the common type is instead &lt;code&gt;i64&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;This peer type resolution breaks down in expressions like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var y : i32 = if (x &amp;gt; 2) 1 else 2;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example 1 and 2 are both &lt;em&gt;BigInt&lt;/em&gt; types, but the expression is a runtime one and needs a definite integer type.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;Go&lt;/em&gt;, this situation is resolved by falling back on a default type size: &lt;code&gt;int&lt;/code&gt;. In &lt;em&gt;Go&lt;/em&gt; this is a 32-bit or 64-bit value depending on platform.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Zig&lt;/em&gt; doesn't have that, so other strategies must be employed. As of 0.6.0, &lt;em&gt;Zig&lt;/em&gt; will parse the above, but will not accept either of the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (x &amp;gt; 2) 1 else 2;
var y : i32 = 1 + if (x &amp;gt; 2) 1 else 2;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Peer type resolution might also at times create odd results. Consider the following &lt;em&gt;Zig&lt;/em&gt; code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var foo : i16 = 0x7FFF;
var bar : i32 = foo + 1;

// The above is equivalent to:
var foo : i16 = 0x7FFF;
var temp : i16 = foo + 1;
var bar : i32 = temp;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because of peer type resolution this would overflow. One could imagine a behaviour more similar to what &lt;em&gt;C&lt;/em&gt; would often give you by resolving the add &lt;code&gt;foo + 1&lt;/code&gt; &lt;em&gt;after&lt;/em&gt; casting both to the type of &lt;code&gt;bar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For &lt;em&gt;Odin&lt;/em&gt;, &lt;em&gt;Swift&lt;/em&gt; and &lt;em&gt;Go&lt;/em&gt; this example is more obvious, because there are no widening conversions. In &lt;em&gt;Go&lt;/em&gt; for example we would have to write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var foo int16 = 0x7FFF
var bar int32 = int32(x + 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case it's clear that in &lt;code&gt;x + 1&lt;/code&gt; neither &lt;code&gt;x&lt;/code&gt; nor &lt;code&gt;1&lt;/code&gt; are int32, so the fact that &lt;code&gt;bar&lt;/code&gt; returns &lt;code&gt;-32768&lt;/code&gt; is expected.&lt;/p&gt;

&lt;p&gt;It's important to emphasize that these are conventions, there is not strictly any right or wrong, rather it's about a trade off between convenience and how prone it is to cause bugs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;C3&lt;/em&gt; – like &lt;em&gt;Zig&lt;/em&gt; – allows safe widening conversions, and so it needs to decide whether it should follow the &lt;em&gt;Zig&lt;/em&gt; behaviour or the "convert first" approach. To me the &lt;em&gt;Zig&lt;/em&gt; behaviour is a bit counter-intuitive. I'd prefer the widening to happen before the addition. Here we might consider &lt;code&gt;int a = b + c&lt;/code&gt; where &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are signed chars. Is it intuitive that this add should overflow on &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; both equal to &lt;code&gt;64&lt;/code&gt;? I think it's better to avoid possible overflows if possible, and doing widening first helps that.&lt;/p&gt;

&lt;p&gt;To achieve this we use &lt;em&gt;bi-directional type checking&lt;/em&gt;. This works by "pushing down" the expected type and optionally casting to the expected type. As we saw in the &lt;em&gt;Zig&lt;/em&gt; example, it does that for assignment, but retains peer type resolution when we nest deeper into expressions.&lt;/p&gt;

&lt;p&gt;To illustrate this, here is some &lt;em&gt;C3&lt;/em&gt; code, it should look fairly familiar – note that like in &lt;em&gt;Java&lt;/em&gt;, &lt;em&gt;C#&lt;/em&gt; and &lt;em&gt;D&lt;/em&gt; the sizes of the types are well defined. An &lt;code&gt;int&lt;/code&gt; is always 32 bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;short foo = 0x7FFF;
int bar = foo + 1;
// The above is equivalent to:
short foo = 0x7FFF;
int bar = cast(foo, int) + cast(1, int);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What happens during semantic analysis in &lt;em&gt;C3&lt;/em&gt; is this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Found declaration of &lt;code&gt;bar&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Analyse the init expression to the declaration, pass down the type &lt;code&gt;int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The init expression is a binary add, analyse left side, pass down the type &lt;code&gt;int&lt;/code&gt; to the analysis.&lt;/li&gt;
&lt;li&gt;Left side is smaller than &lt;code&gt;int&lt;/code&gt;, promote to &lt;code&gt;int&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Analyse right side, passing down the type &lt;code&gt;int&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Right side is compile time integer, try to convert to &lt;code&gt;int&lt;/code&gt; (this will be a compile time error if the value does not fit in an integer).&lt;/li&gt;
&lt;li&gt;Binary sub expressions are resolved. Find a common type of both sides, which in this case is &lt;code&gt;int&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Implicitly cast binary expression to &lt;code&gt;int&lt;/code&gt; if necessary (not necessary in this case).&lt;/li&gt;
&lt;li&gt;Check the type of the binary expression if it matches &lt;code&gt;int&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So let's have a look at a case when conversion &lt;em&gt;isn't&lt;/em&gt; possible and a compile time error occurs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;long foo = 1;
int bar = foo + 2;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Semantic analysis is in this case.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Found declaration of &lt;code&gt;bar&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Analyse the init expression to the declaration, pass down the type &lt;code&gt;int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The init expression is a binary add, analyse left side, pass down the type &lt;code&gt;int&lt;/code&gt; to the analysis.&lt;/li&gt;
&lt;li&gt;Left side is &lt;code&gt;long&lt;/code&gt;, it cannot be implicitly cast to &lt;code&gt;int&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Analyse right side, passing down the type &lt;code&gt;int&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Right side is compile time integer, try to convert to &lt;code&gt;int&lt;/code&gt; (this will be a compile time error if the value does not fit in an integer).&lt;/li&gt;
&lt;li&gt;Binary sub expressions are resolved. Find if the common type which is &lt;code&gt;long&lt;/code&gt; in this case.&lt;/li&gt;
&lt;li&gt;Since the right hand side is &lt;code&gt;int&lt;/code&gt;, cast to &lt;code&gt;long&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The resulting binary expression now has type &lt;code&gt;long&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check the type of the binary expression if it matches &lt;code&gt;int&lt;/code&gt;, it doesn't and cannot be implicitly converted to &lt;code&gt;int&lt;/code&gt; either. A compile time error "&lt;code&gt;long&lt;/code&gt; cannot be implicitly converted to &lt;code&gt;int&lt;/code&gt;" is displayed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Given the above examples it might seem like one could simply do away with any "peer type resolution".&lt;/p&gt;

&lt;p&gt;However, there are cases where top down resolution fails. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;short x = ...
if (x + 1 &amp;lt; 100) { ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case we don't have a type hint. This is what happens when resolving the comparison.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Analyse left hand size, with type passed down as &lt;code&gt;NULL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Analyse &lt;code&gt;x + 1&lt;/code&gt; with type passed down as &lt;code&gt;NULL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Left hand side is &lt;code&gt;short&lt;/code&gt; and right hand side is &lt;code&gt;compile time integer&lt;/code&gt;. The common type is &lt;code&gt;short&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Right hand side in addition is implicitly cast to &lt;code&gt;short&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Left hand side in the comparison has the type &lt;code&gt;short&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Analyse right hand side of comparison, this is a &lt;code&gt;compile time integer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Find a common type between &lt;code&gt;short&lt;/code&gt; and &lt;code&gt;compile time integer&lt;/code&gt;, which is &lt;code&gt;short&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Implicitly cast right hand side in comparison to &lt;code&gt;short&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Analysis is complete.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is not to say that these two methods are sufficient(!) here is an example from &lt;strong&gt;gingerBill&lt;/strong&gt; (author of &lt;em&gt;Odin&lt;/em&gt;): &lt;code&gt;((x &amp;gt; 1 ? 3000 : 2 + 1000) == 2)&lt;/code&gt;. The problem here is that all values are compile time integers, so no type hint can be found anywhere.&lt;/p&gt;

&lt;p&gt;This can either be considered a compile time error because it's "under typed", or default to some integer type: either the register sized integer (&lt;em&gt;Go&lt;/em&gt;'s strategy) or some other similar strategy. &lt;em&gt;C3&lt;/em&gt; currently picks the first option (compile time error) because of the difficulty of picking a good default that doesn't accidentally cause odd behaviour and due to how uncommon this is.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>An evolution of macros for C</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Tue, 28 Jan 2020 09:00:12 +0000</pubDate>
      <link>https://dev.to/lerno/an-evolution-of-macros-for-c-59b5</link>
      <guid>https://dev.to/lerno/an-evolution-of-macros-for-c-59b5</guid>
      <description>&lt;p&gt;&lt;em&gt;(This text was previously published on Medium)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I’ve been trying for a long time to think up a good macro system that could replace or extend the C preprocessor and yet be as easy and approachable.&lt;/p&gt;

&lt;p&gt;There has recently been a lot of interesting work on alternatives to C and C++, and consequently those languages have tried to fix both macros and templates. In some languages, like Rust, there’s a very rich set of tools to extend the language, the inspiration here has clearly come from languages like LISP where macros have been a way to expand the language itself. There are other approaches though: both Zig and Jai uses compile time execution of the language to avoid any specialized macro syntax. Zig is notable for making this a large part of the language.&lt;/p&gt;

&lt;p&gt;In many ways those macro systems are to C’s as a strong typed language is to C’s weakly typed one. The increased type and error control also means added complexity in defining macros. If we write a language that overall is much stricter than C, then this is both fine and necessary. But for C, do we really want to constrict ourselves?&lt;/p&gt;

&lt;p&gt;What would a sort of “incremental” improvement for a macro system for C look like? Can we make a minimal extension that doesn’t feel like we’re making a whole new language?&lt;/p&gt;

&lt;p&gt;Let us make an attempt!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;, we could make multiline &lt;code&gt;#define&lt;/code&gt;s more readable by adding &lt;code&gt;{ }&lt;/code&gt; to implicitly allow row breaks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#define foo(a, b) \
  int x = run_foo(a, b); \
  if (x &amp;gt; 0) printf("We got foo!\n");

// =&amp;gt; 

#define foo(a, b) {
  int x = run_foo(a, b);
  if (x &amp;gt; 0) printf("We got foo!\n");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;, accidentally shadowing other variables is bad. Let’s create unique variable names on demand by a prefix of &lt;code&gt;$&lt;/code&gt; inside a &lt;code&gt;#define&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#define foo(a, b) {
  int $x = run_foo(a, b);
  if ($x &amp;gt; 0) printf("We got foo!\n")
}

foo(1, 2); // $x expands to __foo_x_1
foo(100, 20); // $x expands to __foo_x_2 (increment by one for each time expanded)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;, &lt;code&gt;__typeof__&lt;/code&gt; is needed to do a lot of nice macros, let’s lift it to a sanctioned &lt;code&gt;typeof&lt;/code&gt; function. We can now rewrite GCC’s MAX:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a &amp;gt; _b ? _a : _b; })

#define max(a, b) {
  ({
    typeof(a) $a = (a);
    typeof(b) $b = (b);
    $a &amp;gt; $b ? $a : $b;  
  })
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is about as far as we should take &lt;code&gt;#define&lt;/code&gt;. For more advanced macros we need a new syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;, let’s define non-preprocessor macros using a new &lt;code&gt;macro&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;macro foo(&amp;amp;a, &amp;amp;b) {
  int $x = run_foo(a, b);
  if ($x &amp;gt; 0) printf("We got foo!\n")
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note the slightly odd “&amp;amp;” prefix. This means we import the entire expression or variable into the scope. Without we simply use the value, so here are the two equivalent versions of &lt;code&gt;max&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;macro max(&amp;amp;a, &amp;amp;b) {
  typeof(a) $a = (a); // (1)
  typeof(b) $b = (b); // (2)
  return $a &amp;gt; $b ? $a : $b; // Return automatically makes this an expression statement.
}

// If we do not use &amp;amp;a, &amp;amp;b then we get evaluated values 
// instead, making it look like
// an untyped version of a static inlined function. The macro 
// below is exactly equivalent to the one at the top.
macro max(a, b) {
  return a &amp;gt; b ? a : b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;macro&lt;/code&gt; is largely hygienic, &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; are meaningless in the top scope of the body. For that reason we can reuse &lt;code&gt;return&lt;/code&gt; to indicate that the macro returns a value, that is, it should be treated as an expression. This allows us to skip &lt;code&gt;({ })&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Wrapping something "inside" of a macro is a pain, so for our final extension, let’s define a trailing body parameter that can be expanded:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;macro for_from_to(a, b, macro body) {
  for (typeof(a) $x = a; $x &amp;lt;= b; $x++) {
    body();
  }    
}

for_from_to(1, 100) {
  printf("Again!\n"); 
}

// expands to:

for (int __for_from_to_x_1 = 1; __for_from_to_x_1 &amp;lt;= 100; __for_from_to_x_1++) {
  printf("Again!\n");
}

macro for_from_to(a, b, macro($v) body) {
  for (typeof(a) $x = 0; $x &amp;lt; a; $x++) {
    body($x);
  }    
}

times_do(1, 100) {
  printf("Loop: %d\n", $v);
}

// expands to:

for (int __for_from_to_x_1 = 1; __for_from_to_x_1 &amp;lt;= 100; __for_from_to_x_1++) {
  printf("Loop: %d\n", __for_from_to_x_1);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The trailing body is expanded inside of the macro as if it was a macro itself.&lt;/p&gt;




&lt;p&gt;In the examples above I’ve tried to extend and expand on the C macros rather than replacing it. The new &lt;code&gt;macro&lt;/code&gt; function is simply an evolved subset of &lt;code&gt;#define&lt;/code&gt; that can be parsed as normal C except for the lack of types (giving a compiler the ability to issue a lot more errors directly at the macro definition).&lt;/p&gt;

&lt;p&gt;This is not the only direction we could have taken the language. Another approach could have been to make it possible to parameterize static inlined functions instead. Those are steps instead of solving a part of the macro problem domain using generic function. In this direction we also have parameterized (generic) structs.&lt;/p&gt;

&lt;p&gt;That, however, would bring a significant change to the language. Similarly, a “Rust-like” macro system could offer both expressiveness and safety, but it would be more of a revolution than an evolution.&lt;/p&gt;

&lt;p&gt;Sometimes the latter is what you want.&lt;/p&gt;

</description>
      <category>programminglanguagedesign</category>
      <category>c</category>
      <category>evolvingc</category>
    </item>
    <item>
      <title>How to procrastinate while working hard</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Sat, 18 Jan 2020 20:07:35 +0000</pubDate>
      <link>https://dev.to/lerno/how-to-procrastinate-while-working-hard-4l5f</link>
      <guid>https://dev.to/lerno/how-to-procrastinate-while-working-hard-4l5f</guid>
      <description>&lt;p&gt;Refactoring is an important part of programming. If you are maintaining a non-trivial code base you need to constantly remove cruft and improve on solutions otherwise the code will slowly rot.&lt;/p&gt;

&lt;p&gt;Working with improving abstractions and code quality there is also a lure which is mostly ignored, which is over-engineering. The urge to add code that feels “magical” and just does things in an extremely elegant way. You can find examples in amazing C++ templates, or some awesomely elegant Swift code that might use some combination of operator overloading, generics and pattern matching. It might look cool, but over-engineering is dangerous.&lt;/p&gt;

&lt;p&gt;It’s dangerous because you can spend days on that “perfect abstraction” which might be elegant on the surface — but your teammates will have a less pleasant time trying to figure out how to debug or extend it later on.&lt;/p&gt;

&lt;p&gt;It’s dangerous because all that time you spent might make you reluctant to find easier solutions, or throw it away when it’s no longer needed.&lt;/p&gt;

&lt;p&gt;It’s dangerous because that complexity disguised as abstraction is making your code less maintainable and also less easy to understand.&lt;/p&gt;

&lt;p&gt;It’s dangerous because you might have thrown away bug free code and replaced it with something new and untested because you thought it might look more elegant.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But most of all it’s dangerous because it’s so damned satisfying to just find those beautiful abstractions. It’s so much fun that we forget how dangerous it is.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So when you feel the urge — remember restraint. The “magically cool things” your language can do are usually exactly those parts that you should stay clear of.&lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>To OO or not to OO</title>
      <dc:creator>Christoffer Lernö</dc:creator>
      <pubDate>Sat, 18 Jan 2020 20:03:06 +0000</pubDate>
      <link>https://dev.to/lerno/to-oo-or-not-to-oo-5dm5</link>
      <guid>https://dev.to/lerno/to-oo-or-not-to-oo-5dm5</guid>
      <description>&lt;p&gt;&lt;em&gt;(a previous version of this text was posted on Medium)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Around 2018, partly due to following Jonathan Blow’s work on Jai and the obvious lack of OO in the language, I started to re-think about whether OO is a good thing or not. I ended up listening to — and reading — various criticisms leveled towards OO and pondered the problem quite a bit. I had also started following Bob Nystrom’s &lt;a href="https://craftinginterpreters.com"&gt;“Crafting Interpreters”&lt;/a&gt; series that had the second part written in a very clear and easy to understand procedural C. It reminded me how directly aimed at the problem code used to be for me.&lt;/p&gt;

&lt;h3&gt;
  
  
  Before OO there was just writing code
&lt;/h3&gt;

&lt;p&gt;I started programming in 1982. I was 9 years old and programming on home computers was almost always BASIC (or assembler, when you wanted to get some speed). It was the era of the first “home computers”. Computer magazines would print listings of games and applications. This was part “getting programs on the cheap” and part “learning to program”.&lt;/p&gt;

&lt;p&gt;BASIC isn’t exactly structured programming, at least not those early versions. “Structured programming” was limited to statements like &lt;code&gt;GOSUB 1340&lt;/code&gt;. Still, you could definitely build things with it. Games and applications, all were written in BASIC. The limitation was usually the memory of the machine (typically 16 kb) rather than the structure. It might not have been elegant code, but it got things done.&lt;/p&gt;

&lt;p&gt;Eventually I would pick up Pascal, and even though later iterations of BASIC would improve much of the 8-bit implementations, Pascal was just so much better. It was more powerful, a more importantly it was really easy to write clear and structured code. But even so, program design just wasn’t much different from writing assembler or BASIC. You started in one end and built things until they were done. It was really easy to just "get things done".&lt;/p&gt;

&lt;p&gt;I eventually learned a bit C++, but the OO part of the language mostly escaped me. It wasn’t until I got to Java that things changed — and at the time I believed it to be for the better...&lt;/p&gt;

&lt;h3&gt;
  
  
  Java and “real” OO
&lt;/h3&gt;

&lt;p&gt;I used to tell people that I didn’t understand object oriented programming until I learned Java. That might not be quite accurate, but it’s true that I didn’t attempt any “Object Oriented Design” until I learned Java.&lt;/p&gt;

&lt;p&gt;Java really forced you to do objects. I myself started out when applets was the new hot thing, and the language was still in its 1.0.2 version. It was cool and magical. Objects where these small units you could craft to do things, almost like programming small robots. Instead of just plain programming you did this &lt;em&gt;design&lt;/em&gt; where independent objects were talking and producing a result. It was awesome. And of course, it was also a lie.&lt;/p&gt;

&lt;p&gt;With Java going mainstream, the articles and books on how to do &lt;em&gt;real OO&lt;/em&gt; flourished. The point seemed to be that one should throw away most things about procedural programming. It was supposedly bad in the same way unstructured programming was to the procedural programming. The more we thought in terms of objects, the better things were. It was clear that &lt;em&gt;nirvana&lt;/em&gt; was near — accessible to those lucky people who could get program Java for a living.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object Oriented modelling nightmares
&lt;/h3&gt;

&lt;p&gt;In my free time I was working on the next iteration of a complex online game that was initially written as BBS door (BBS online games back when we used dial-up modems). The original version had been written in QBasic, and I also had done a rewrite in Turbo Pascal that covered about 80% of the game. Writing the game had taken evenings and weekends spread over about half a year.&lt;/p&gt;

&lt;p&gt;The QBasic version was tricky as you had to pass all global data explicitly between implementation files, and each file had a size limit — so it had to be split with this huge declaration of globals on top of each file. The Pascal version was so much easier to write. You didn’t need to explicitly pass globals and it was straightforward to pass things to procedures — whereas for QBasic you had to pass parameters and results in global variables you especially set aside for the purpose. Obviously the Java version — with OO goodness — must be even &lt;em&gt;easier&lt;/em&gt; than Pascal to implement!&lt;/p&gt;

&lt;p&gt;That turned out to be “not quite true”. I wrote page after page on the design of the classes, how each entity would know what, and what state each would contain and how to act on other entities. It felt really cool, but it was also complex and every version of the object model seemed to have &lt;em&gt;some&lt;/em&gt; problem where eventually everything needed to know everything and every single class would be hideously complex with little way to ensure consistency. It felt like an impossible task.&lt;/p&gt;

&lt;h3&gt;
  
  
  Doing real things
&lt;/h3&gt;

&lt;p&gt;Meanwhile I started working professionally as a programmer. I wrote in Perl, Java, learned Objective-C and Ruby. With Objective-C I discovered that the “OO” of Java/C++ was just one brand of OO. In Java the idea was to create tiny classes that were assembled to a whole, but in ObjC objects were instead used as high level “glue” between larger components written internally in straight procedural C code. The fine grained classes of Java would be considered the very opposite of good design for Objective-C. So, if OO was to be to procedural programming as procedural was to unstructured — how come there wasn’t even a consensus on how to do “proper OO”?&lt;/p&gt;

&lt;p&gt;My pet project was still a failure. I still tried to model things the Java way and I kept failing. But then I tried writing it in Ruby and something unexpected happened.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting things done
&lt;/h3&gt;

&lt;p&gt;In Java it’s easy to just get caught in the work of writing your classes. Java is comparatively verbose, so just writing a bunch of classes, writing getters and setters seems like you got quite a bit of work done. That’s why it was easy just to waste time testing out models and still feel like you’re getting somewhere.&lt;/p&gt;

&lt;p&gt;In Ruby, on the other hand, writing a class is trivial. Even modelling a lot of them isn’t much work. If you’re lazy you can even generate a bunch of them using metaprogramming. Ruby is very, very quick to do prototyping in.&lt;/p&gt;

&lt;p&gt;Suddenly I couldn’t fool myself anymore. After implementing parts of the model in Ruby it was suddenly clear that I wasn’t adding anything of value by creating those game model classes. It was work, but it wasn’t real work. I needed a new idea.&lt;/p&gt;

&lt;p&gt;At the time I was working professionally on poker servers, and it was clear a poker game instance was simply a data structure with the deck, the current bets and players. Player actions were simply commands acting on this data according to certain rules. Maybe this idea could work…?&lt;br&gt;
As a prototype in my Ruby code I simply used a nested hash map — no model objects at all. Each action the player would simply invoke the corresponding method which would directly edit values of the branches of the map. A very procedural approach — even though I didn’t think of it like that at the time.&lt;/p&gt;

&lt;p&gt;Some good things immediately resulted from this design: it was dead simple to add an “undo”-functionality, where the changes to the data could be rolled back. The data tree was naturally dead simple to serialize and deserialize (compare the headache of my old designs where each single class needed to implement serialization/deserialization on its own), and I could also track exactly what changes were made in order to assemble updates to logged in players.&lt;/p&gt;

&lt;p&gt;I had solved my big OO problem… by adopting a more procedural mindset.&lt;/p&gt;

&lt;h3&gt;
  
  
  Not quite there yet
&lt;/h3&gt;

&lt;p&gt;Despite solving the problem, I didn’t actually &lt;em&gt;realize&lt;/em&gt; that the problem was OO. I just thought I had found a very clever solution.&lt;/p&gt;

&lt;p&gt;Professionally, even though I could do all the “fancy” OO solutions with reflection, polymorphism etc, my own OO style tended to favour simple, explicit and &lt;em&gt;obvious&lt;/em&gt; solutions. But I would pick the solutions because my experience showed me they were the best, not because I realized there was any problems with OO.&lt;/p&gt;

&lt;h3&gt;
  
  
  A new problem
&lt;/h3&gt;

&lt;p&gt;I had built my game server and it was fine. It was trivial to extend and dead simple to add more features. There was just one problem: the client.&lt;/p&gt;

&lt;p&gt;I had written clients before, but on a smaller. As long as you have no more than say 10–15 screens you can get away with most designs. My game client had over 50 distinct dialog screens and many different states. Things were getting messy.&lt;/p&gt;

&lt;p&gt;I had my model and my views and my controllers and still I felt I had no control there &lt;em&gt;was just so much state everywhere&lt;/em&gt;. Because this is the essence of Java/C++ style OO: split state into small pieces and let each object manage their specific part of the application state. It’s really a spectacularly bad idea as complexity roughly correspond to the square of different interacting states. In addition, it’s very tempting to simply let the state be implicit in some (combination of) member variable values. “Why have an explicit state when you can check the value of a variable to figure it out?”&lt;/p&gt;

&lt;h3&gt;
  
  
  A conclusion
&lt;/h3&gt;

&lt;p&gt;In procedural programming you tend to keep your state where you can see it. Unlike OO, where you’re encouraged to split the state and hide it, you pretty much have to keep it explicit and that really is a good thing.&lt;br&gt;
That’s not to say that using objects is necessarily a bad thing. It’s a very powerful tool for building UI rendering hierarchies for one thing, and the &lt;em&gt;namespacing&lt;/em&gt; together with chaining can create very smooth and readable code, compare: &lt;code&gt;urlescape(substr(string, 0, strlen(string) - 2)&lt;/code&gt; to &lt;code&gt;string.substr(0, -2).urlescape()&lt;/code&gt; (there can still be an argument that the former is clearer though!). However, the object oriented &lt;em&gt;design&lt;/em&gt; with objects that keep state or act on other objects — here is where OO goes all wrong.&lt;/p&gt;

&lt;p&gt;There is also the (mostly forgotten) Objective-C style of OO which happens to be even better for building GUIs than Java/C++, as the late binding of the dispatch and the runtime pushes it significantly closer to being a scripting language. Sadly Apple, the former champions of Objective-C, have largely forgotten what the idea behind ObjC really was, and are now replacing it with Swift which adopts the OO style of Java/C++.&lt;/p&gt;

&lt;p&gt;Still, there are languages trying to get back to the basics. Golang is one, and many of the other new “system programming languages” also qualifies. Go in particular (despite my reservations regarding the language) disproves the myth that “it’s not possible to build large scale products with procedural programming”. The increasing popularity of the language might create a crack in the idea that OO is “inevitable”&lt;/p&gt;

&lt;p&gt;However, Java-style OO is deeply entrenched and shows little inclination of disappearing anytime soon. It will be interesting to see what the future brings.&lt;/p&gt;

</description>
      <category>objectoriented</category>
      <category>procedural</category>
      <category>programminglanguages</category>
      <category>java</category>
    </item>
  </channel>
</rss>
