<?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: Evan Typanski</title>
    <description>The latest articles on DEV Community by Evan Typanski (@evantypanski).</description>
    <link>https://dev.to/evantypanski</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%2F18263%2F73b9207b-9f83-41e0-85ed-ef9999217e15.png</url>
      <title>DEV Community: Evan Typanski</title>
      <link>https://dev.to/evantypanski</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/evantypanski"/>
    <language>en</language>
    <item>
      <title>This C++ code doesn't look ambiguous to me</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Mon, 23 Oct 2023 15:05:33 +0000</pubDate>
      <link>https://dev.to/evantypanski/this-c-code-doesnt-look-ambiguous-to-me-3he0</link>
      <guid>https://dev.to/evantypanski/this-c-code-doesnt-look-ambiguous-to-me-3he0</guid>
      <description>&lt;p&gt;Look at this C++ code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;);};&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;public&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;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&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;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;C&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="n"&gt;b&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="n"&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;Do you think this compiles? Well, probably not because I'm asking. So here's the error message from Clang 13:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;etyp:fun/ &lt;span class="nv"&gt;$ &lt;/span&gt;clang++ test.cpp
test.cpp:10:3: error: call to &lt;span class="s1"&gt;'f'&lt;/span&gt; is ambiguous
  f&lt;span class="o"&gt;(&lt;/span&gt;b&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  ^
test.cpp:5:6: note: candidate &lt;span class="k"&gt;function
&lt;/span&gt;void f&lt;span class="o"&gt;(&lt;/span&gt;A&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
     ^
test.cpp:6:6: note: candidate &lt;span class="k"&gt;function
&lt;/span&gt;void f&lt;span class="o"&gt;(&lt;/span&gt;C&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
     ^
1 error generated.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay so it's ambiguous which conversion gets done for the object &lt;code&gt;b&lt;/code&gt; (of type &lt;code&gt;B&lt;/code&gt;). So what options are there?&lt;/p&gt;

&lt;p&gt;1) Call &lt;code&gt;f(A)&lt;/code&gt;. We can call &lt;code&gt;A&lt;/code&gt;'s constructor which takes a reference to a &lt;code&gt;B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2) Call &lt;code&gt;f(C)&lt;/code&gt;. We can call &lt;code&gt;C&lt;/code&gt;'s constructor which takes a reference to a &lt;code&gt;B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3) Call &lt;code&gt;f(A)&lt;/code&gt;. We can call &lt;code&gt;operator A()&lt;/code&gt; in the class &lt;code&gt;B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But, 1 and 3 are ambiguous because we can't find a better one to call &lt;code&gt;f(A)&lt;/code&gt;. But &lt;code&gt;f(C)&lt;/code&gt; is still not ambiguous - we know how to call it without any ambiguity. Trying to call &lt;code&gt;f(A)&lt;/code&gt; is ambiguous so &lt;em&gt;obviously&lt;/em&gt; it shouldn't be chosen.&lt;/p&gt;

&lt;p&gt;It turns out in this case, the ambiguity between 1 and 3 is ranked equal priority as the user defined conversion in 2. From the C++20 standard section 12.4.2.10:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For the purpose of ranking implicit conversion sequences as described in 12.4.3.2, the ambiguous conversion sequence is treated as a user-defined conversion sequence that is indistinguishable from any other user-defined conversion sequence.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I find this surprising because I could easily determine that option 2 is the unambiguous way to make this compile.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not all compilers...
&lt;/h2&gt;

&lt;p&gt;It turns out, MSVC actually &lt;a href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:12,positionColumn:1,positionLineNumber:12,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:'class+B%3B%0Aclass+A+%7B+public:+A+(B%26)%3B%7D%3B%0Aclass+B+%7B+public:+operator+A()%3B+%7D%3B%0Aclass+C+%7B+public:+C+(B%26)%3B+%7D%3B%0Avoid+f(A)+%7B+%7D%0Avoid+f(C)+%7B+%7D%0A%0Aint+main()+%7B%0A++B+b%3B%0A++f(b)%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:vcpp_v19_37_x64,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x64+msvc+v19.37+(Editor+%231)',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+gcc+13.1',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x64+msvc+v19.37+(Compiler+%231)',t:'0')),k:33.33333333333333,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4"&gt;accepts the code&lt;/a&gt;. You can even see in the assembly it calls &lt;code&gt;f(C)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I believe no matter how silly the standard is with its rules (which this particular point may have a very good reason), C++ compilers should aim to conform to the standard. This case was pulled exactly from the standard as a case that should not compile. MSVC should not compile this.&lt;/p&gt;

&lt;p&gt;But that's a whole other rant.&lt;/p&gt;

&lt;p&gt;Thanks for looking at that C++ code.&lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
    <item>
      <title>Zig is for me - is it for you?</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Tue, 04 Apr 2023 15:54:41 +0000</pubDate>
      <link>https://dev.to/evantypanski/zig-is-for-me-is-it-for-you-2dp2</link>
      <guid>https://dev.to/evantypanski/zig-is-for-me-is-it-for-you-2dp2</guid>
      <description>&lt;p&gt;There has been &lt;a href="https://matklad.github.io/2023/03/26/zig-and-rust.html"&gt;a&lt;/a&gt; &lt;a href="https://www.openmymind.net/Zig-Quirks/"&gt;ton&lt;/a&gt; &lt;a href="https://www.infoworld.com/article/3689648/meet-the-zig-programming-language.html"&gt;of&lt;/a&gt; &lt;a href="https://zackoverflow.dev/writing/unsafe-rust-vs-zig/"&gt;Zig&lt;/a&gt; &lt;a href="https://kristoff.it/blog/zig-multi-sequence-for-loops/"&gt;content&lt;/a&gt; lately. So I'll add to the swarm beating this dead horse, but only because I want to introduce some stuff that I'm working on :)&lt;/p&gt;

&lt;p&gt;Also, I wanted to write more off-the-cuff style posts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Zig?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://ziglang.org/"&gt;Zig&lt;/a&gt; is a modern programming language meant to "replace C." There are a ton of those. There does seem to be a worrying trend where every programmer under the sun believes that they will be the one to destroy C. Despite that, I see legacy C code every day that is run in safety-critical contexts. That code is &lt;em&gt;untouchable&lt;/em&gt;. We all know that the second you touch something, it all will crumble. So why would you look at Zig?&lt;/p&gt;

&lt;p&gt;There are a lot of reasons. One of the posts I linked above probably does a better job than I will at explaining this. Let's go through some requirements in a C successor:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A successor must have simple, easy control over memory. I believe this one of the main reasons C is so prominent - few other languages let you do what C does with memory with such simplicity. Most C applications work so closely with buffers and hardware that this is essential.&lt;/li&gt;
&lt;li&gt;A successor must do number 1) more safely than C. If you've ever touched a C codebase, you've seen crazy segfaults where one corner of the application doesn't properly set memory where the other side assumes it's set. To make a language more appealing than C, you must make it easier to be safe.&lt;/li&gt;
&lt;li&gt;A successor must be able to interact easily with C. If you are looking for a language to take over C, you probably have a lot of C sitting around. You don't want to rewrite all of that.&lt;/li&gt;
&lt;li&gt;A successor must be able to compile to a lot of hardware, including proprietary hardware. Many devices you see use C for firmware. Many of these have no other choice. The ability to whip up a C compiler for your specific hardware is very important.&lt;/li&gt;
&lt;li&gt;A successor must not be ugly. Ugly code isn't fun to write.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How does Zig deal with each of these?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Zig gives you control over &lt;a href="https://ziglang.org/documentation/master/std/#A;std:mem.Allocator"&gt;allocators&lt;/a&gt; to chose how and where memory gets allocated. You also have access to suspiciously C like control over pointers...&lt;/li&gt;
&lt;li&gt;Zig still requires manual memory management. If you allocate something, you free it. Or, if another function returns allocated memory, you will free it. Zig's allocators help you do this. First, the allocator pattern is generally implemented by you passing an allocator into a function, so you know it may allocate memory. Programmers should also make it clear that their function allocates memory. Second, you can have special allocators, like &lt;a href="https://ziglang.org/documentation/master/std/#A;std:testing.allocator"&gt;&lt;code&gt;std.testing.allocator&lt;/code&gt;&lt;/a&gt;, which will tell you if you have memory leaks in a unit test. Neat!&lt;/li&gt;
&lt;li&gt;Zig has my favorite thing: a transpiler from C (I &lt;a href="https://etyp.dev/posts/art-of-transpilers/"&gt;really like&lt;/a&gt; those). Just run &lt;code&gt;zig translate-c&lt;/code&gt; and you get Zig code where you had C code. You can use this to include C libraries or upgrade your code base to Zig!&lt;/li&gt;
&lt;li&gt;Zig is based on LLVM, so you get a lot of hardware for free. I'd worry a bit about this point, since it seems a lot harder to make a good Zig compiler than a good C compiler. It's not super common to make a brand new C compiler for each piece of hardware (that's what a compiler backend is for), but there certainly are specialized C compilers. Stay tuned for this one.&lt;/li&gt;
&lt;li&gt;Zig is mostly pretty. You have to get over stuff like &lt;code&gt;.{}&lt;/code&gt; for anonymous structs (I find the anonymous structs syntax satisfying). But some things are great. Say your program returns some enum that can be &lt;code&gt;RED&lt;/code&gt;, &lt;code&gt;BLUE&lt;/code&gt;, or &lt;code&gt;GREEN&lt;/code&gt;. You can return just by saying &lt;code&gt;return .BLUE;&lt;/code&gt; - I think that's really cute. No need to even specify the enum name! (which is really convenient considering the enum could be anonymous...)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Zig is special
&lt;/h2&gt;

&lt;p&gt;I admit, not everyone has my sensibilities. Zig isn't the next Python, and many people conflate high level web development with programming in general. So you may hate it. That's okay. Any worthy art has those who don't like it.&lt;/p&gt;

&lt;p&gt;What contexts would you use Zig? Obviously wherever C would be used. That could be operating systems, microcontrollers, or even game development. Zig also seems to be adding good support for WebAssembly targets - great for efficient web code. If you're like me, it may also be a good language for compilers, as it gives you a no nonsense, quick language for a compiler or interpreter. Certainly better than Python for that, in my opinion.&lt;/p&gt;

&lt;p&gt;Zig is beautiful. It has cool ideas, almost none of which I covered. Just take a look at it, I think it has room to make a splash in certain spaces.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;p&gt;I've done a few things in Zig so far, but not as much as I want.&lt;/p&gt;

&lt;p&gt;My current pet project takes Zig and puts it on the Arduino. I've gotten LEDs blinking, LCD panels with easy to use libraries, reading from sensors, pulse width modulation... all of that. Under the hood it uses &lt;a href="https://github.com/ZigEmbeddedGroup/microzig"&gt;microzig&lt;/a&gt; to provide the heavy lifting, I'm just providing an abstraction on top of that abstraction.&lt;/p&gt;

&lt;p&gt;Right now, Zig is in a pretty unstable place (it is pre-1.0 after all). Libraries you use won't always work with a new release. But, I think this time is crucial to build libraries and tools. Even if the project isn't a brand new library, just using Zig is extremely helpful for the community. If you have a new project, consider using Zig, to give exposure to the language. Maybe not for your fancy new website, though, unless you're &lt;a href="https://dhh.dk/"&gt;DHH&lt;/a&gt; (for all of you Rails lovers).&lt;/p&gt;

&lt;p&gt;I plan on making &lt;a href="https://github.com/evantypanski/arduzig"&gt;arduzig&lt;/a&gt; just as easy to use as the Arduino IDE for whatever Arduino projects you have. I will probably (?) write examples and more posts to make it as understandable as possible. Probably don't try it yet. But once Zig 0.11 is out and I've brought it up to date? Please.&lt;/p&gt;

&lt;p&gt;By the way, take a look at the posts I linked in the first sentence if you haven't. They're better than this one.&lt;/p&gt;

</description>
      <category>zig</category>
      <category>c</category>
    </item>
    <item>
      <title>Rust will `never` do that!</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Fri, 02 Sep 2022 17:09:01 +0000</pubDate>
      <link>https://dev.to/evantypanski/rust-will-never-do-that-216c</link>
      <guid>https://dev.to/evantypanski/rust-will-never-do-that-216c</guid>
      <description>&lt;p&gt;That is, the Rust &lt;a href="https://doc.rust-lang.org/reference/types/never.html"&gt;&lt;code&gt;never&lt;/code&gt; type&lt;/a&gt; (&lt;code&gt;!&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Everything is an expression
&lt;/h2&gt;

&lt;p&gt;Rust is kind of an "everything is an expression" type of language. An expression is something that produces a value. Not everything is an expression, but basically everything that is in a Rust body (executable code) is an expression. Actually, one thing that isn't are locals, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&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;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is its own statement! But you can have an expression with a &lt;code&gt;let&lt;/code&gt; statement, just take:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func_that_returns_option&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 &lt;code&gt;let&lt;/code&gt; statement in there isn't a local, it's a &lt;a href="https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.Let.html"&gt;&lt;code&gt;let&lt;/code&gt; expression&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's go over a few other things that are also expressions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="mi"&gt;1&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;This last one is what we'll be interested in.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;never&lt;/code&gt; type
&lt;/h2&gt;

&lt;p&gt;So if we look into the last example I gave, you know the &lt;code&gt;if&lt;/code&gt; block evaluates to &lt;code&gt;1&lt;/code&gt;. But what about the &lt;code&gt;else&lt;/code&gt; block? &lt;code&gt;return;&lt;/code&gt; doesn't produce a value, it actually leaves the function. So what is its type?&lt;/p&gt;

&lt;p&gt;That's right, the &lt;code&gt;never&lt;/code&gt; type!&lt;/p&gt;

&lt;p&gt;The point of the &lt;code&gt;never&lt;/code&gt; type is to say "this computation will not complete." That way, if you assign something to that value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;The type of &lt;code&gt;x&lt;/code&gt; is based on the &lt;code&gt;if&lt;/code&gt; block. But, that doesn't mean the &lt;code&gt;never&lt;/code&gt; type is ignored. How the compiler figures that out is &lt;code&gt;never&lt;/code&gt; can be coerced into any type. That means, when trying to infer the type for &lt;code&gt;x&lt;/code&gt;, the compiler sees the &lt;code&gt;if&lt;/code&gt; block has type &lt;code&gt;i32&lt;/code&gt;. Then the &lt;code&gt;else&lt;/code&gt; block has type &lt;code&gt;never&lt;/code&gt;, so to make it consistent, it says "&lt;code&gt;never&lt;/code&gt; can convert to &lt;code&gt;i32&lt;/code&gt;" and it's all okay!&lt;/p&gt;

&lt;h2&gt;
  
  
  What does this mean for the user?
&lt;/h2&gt;

&lt;p&gt;Well, not much. It's mostly a fun compiler intricacy. But, if you want to see it mentioned in your diagnostics, try the new &lt;code&gt;let else&lt;/code&gt; syntax (currently only available with nightly). This syntax lets you use a pattern for a &lt;code&gt;let&lt;/code&gt; binding that may not always be true, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&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="nf"&gt;returns_result&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nd"&gt;warn!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is bad!"&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;So, if &lt;code&gt;returns_result&lt;/code&gt; returns &lt;code&gt;Ok&lt;/code&gt;, then we assign &lt;code&gt;x&lt;/code&gt; to the value in &lt;code&gt;Ok&lt;/code&gt;. But, if it's not, we warn and return. Pretty easy!&lt;/p&gt;

&lt;p&gt;The type of that &lt;code&gt;else&lt;/code&gt; block has to be &lt;code&gt;never&lt;/code&gt;, though.&lt;/p&gt;

&lt;p&gt;So try making it not, with this minimal example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#![feature(let_else)]&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;opt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&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="nf"&gt;opt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&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 will error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;E0308&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="n"&gt;clause&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;diverge&lt;/span&gt;
 &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;letelse&lt;/span&gt;&lt;span class="py"&gt;.rs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;     &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&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="nf"&gt;opt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&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="n"&gt;expected&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="n"&gt;integer&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
             &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;help&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;adding&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;diverging&lt;/span&gt; &lt;span class="n"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;such&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="nd"&gt;panic!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;help&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="n"&gt;instead&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;

&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;aborting&lt;/span&gt; &lt;span class="n"&gt;due&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;

&lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt; &lt;span class="n"&gt;about&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;rustc&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;explain&lt;/span&gt; &lt;span class="n"&gt;E0308&lt;/span&gt;&lt;span class="err"&gt;`.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We expect that &lt;code&gt;else&lt;/code&gt; block to diverge, so it has to be the &lt;code&gt;never&lt;/code&gt; type!&lt;/p&gt;

&lt;p&gt;Cool.&lt;/p&gt;

&lt;p&gt;Rust will &lt;code&gt;never&lt;/code&gt; return from that.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Art of Transpilers - From One Programming Language to Another</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Thu, 30 Jun 2022 14:35:27 +0000</pubDate>
      <link>https://dev.to/evantypanski/the-art-of-transpilers-from-one-programming-language-to-another-5419</link>
      <guid>https://dev.to/evantypanski/the-art-of-transpilers-from-one-programming-language-to-another-5419</guid>
      <description>&lt;p&gt;Compilers are cool, they take code you write and make code the machine can understand. Transpilers are cooler, they take code you write and make code in another programming language that machines can't understand (yet). But why?&lt;/p&gt;

&lt;p&gt;Let's take a quick tour around the world of transpilers!&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Clones
&lt;/h2&gt;

&lt;p&gt;JavaScript is one of the top use cases for transpilers. JavaScript can run natively in any major browser. So, instead of making a new language and asking browsers to support that natively in the browser, many want to reuse what's already there.&lt;/p&gt;

&lt;p&gt;The problem is, &lt;a href="https://blog.andrasbacsai.com/javascript-is-going-crazy-1"&gt;JavaScript is crazy&lt;/a&gt;. There are many more examples of that. Even with perfect knowledge of JavaScript internals, bugs will happen because of just how dynamic the language is. So, to get around that, we have...&lt;/p&gt;

&lt;h3&gt;
  
  
  TypeScript
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt; is a superset of JavaScript that allows explicit type information. TypeScript solves many of JavaScript's biggest woes with dynamic typing. I will not provide examples so I don't go insane.&lt;/p&gt;

&lt;p&gt;So, how does TypeScript do it? It's a transpiler! You can just transpile your TypeScript to JavaScript then run &lt;em&gt;that&lt;/em&gt; in the web browser.&lt;/p&gt;

&lt;p&gt;So take this example in TypeScript, ruthlessly stolen from the &lt;a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html"&gt;handbook&lt;/a&gt; on everyday types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// test.ts&lt;/span&gt;
&lt;span class="c1"&gt;// Parameter type annotation&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;!!&lt;/span&gt;&lt;span class="dl"&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;So we can see our &lt;code&gt;name&lt;/code&gt; parameter has the type &lt;code&gt;string&lt;/code&gt;. We can then compile this with &lt;code&gt;tsc test.ts&lt;/code&gt; to get...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// test.js&lt;/span&gt;
&lt;span class="c1"&gt;// Parameter type annotation&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;!!&lt;/span&gt;&lt;span class="dl"&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;... The same thing without the type in &lt;code&gt;name&lt;/code&gt;! Okay that's not interesting, but try adding a call like &lt;code&gt;greet(1);&lt;/code&gt; and you'll see an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;test.ts:6:7 - error TS2345: Argument of &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt; is not assignable to parameter of &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

6 greet&lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        ~


Found 1 error.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type guarantee lets you call &lt;code&gt;toUpperCase()&lt;/code&gt; and declines anything that's not a &lt;code&gt;string&lt;/code&gt; type. You may be able to see the bugs that could be caught from this. That's why TypeScript has made such a splash.&lt;/p&gt;

&lt;h3&gt;
  
  
  CoffeeScript
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://coffeescript.org/"&gt;CoffeeScript&lt;/a&gt; tries to solve a slightly different problem. Instead of providing a superset that makes your code less prone to bugs, CoffeeScript makes JavaScript prettier.&lt;/p&gt;

&lt;p&gt;However, I've always considered CoffeeScript its own language. Once learned, it's a pleasure to write, which is a nice break from JavaScript, in my opinion. But, it seems to be in some middle ground between JavaScript and a standalone language.&lt;/p&gt;

&lt;p&gt;In fact, as stated on the CoffeeScript site, NodeJS can natively run CoffeeScript. It doesn't need to transpile down to JavaScript; it is its own language. What if we went more pure, with no intent to replace the syntax of some other language?&lt;/p&gt;

&lt;h2&gt;
  
  
  Nim
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nim-lang.org/"&gt;Nim&lt;/a&gt; can transpile to C, C++, or JavaScript. That would make Nim our lone example of a language whose purpose is to transpile to multiple other languages. For systems programming, use its C support with dependency-free executables. For frontend development, target JavaScript.&lt;/p&gt;

&lt;p&gt;Let's try an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// test.nim
echo "Hello World!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we compile this to C with &lt;code&gt;nim cc --nimcache:nimcache test.nim&lt;/code&gt;, we get.. an executable. So did it compile? Or transpile?&lt;/p&gt;

&lt;p&gt;Well, if we check the nimcache directory I specifically included, we'll find a fair few C files. These C files are what get compiled to create the executable we see. I won't include these, since they're a fair bit of code.&lt;br&gt;
But, we see a pretty major difference from the JavaScript-like transpilers we looked at: Nim only uses the transpilation as a means to an end.&lt;/p&gt;

&lt;h2&gt;
  
  
  C2Rust
&lt;/h2&gt;

&lt;p&gt;But what if you want to permanently change your codebase? That's the idea behind &lt;a href="https://c2rust.com/"&gt;C2Rust&lt;/a&gt;. There's value in updating legacy codebases to safer, more secure languages like Rust. So, if you want to do it, try a transpiler!&lt;/p&gt;

&lt;p&gt;The value in transpilers stretches far beyond just writing simpler code, or code that targets some other language for backend work. Transpilers can change your codebase and make modernizing the code far easier. In the process, your code becomes safer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Many frontends, one backend
&lt;/h2&gt;

&lt;p&gt;Imagine you have a tool that you want to support many languages, like for static analysis. But, you don't want to write different analyses for every language's internal representation. What can you do? Introduce a new language, then transpile to that!&lt;/p&gt;

&lt;p&gt;That's what I currently work on every day. I make translators that go from many source languages and produce one proprietary language as its output. Then, we run checks based on that internal language.&lt;/p&gt;

&lt;p&gt;This can have a pretty high barrier to entry because you have to design a new language &lt;em&gt;and&lt;/em&gt; your transpiler. But, depending on the use case, it may be an interesting solution.&lt;/p&gt;

&lt;p&gt;Those familiar with LLVM may wonder why this is different from compilation? Many compilers, from Rust to Clang, generate LLVM from your source code. Then, the LLVM generates the machine code. But, that's a compiler, not a transpiler. Don't be silly.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why?
&lt;/h1&gt;

&lt;p&gt;I don't really have profound conclusions about transpilers. I just think they're cool and underappreciated. And, when you work with them, you get to see the internals of two languages at once, rather than just one!&lt;/p&gt;

&lt;p&gt;Two is better than one!&lt;/p&gt;

&lt;p&gt;So write more transpilers... And compilers.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>rust</category>
      <category>javascript</category>
      <category>compilers</category>
    </item>
    <item>
      <title>References to Literals in Rust?!</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Sat, 08 Jan 2022 17:59:22 +0000</pubDate>
      <link>https://dev.to/evantypanski/references-to-literals-in-rust-4kol</link>
      <guid>https://dev.to/evantypanski/references-to-literals-in-rust-4kol</guid>
      <description>&lt;p&gt;One day messing around with Rust, I found that the following code is valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's assigning a variable to a reference to the literal &lt;code&gt;0&lt;/code&gt; - how?! Why?! This absolutely shocked me. Just try doing this in C++ and you'll see why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;non&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;lvalue&lt;/span&gt; &lt;span class="n"&gt;reference&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nv"&gt;'int&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;temporary&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nv"&gt;'int&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;
    &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;r&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="o"&gt;^&lt;/span&gt;   &lt;span class="o"&gt;~&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The literal is a temporary - you can't have a reference to that! String literals are lvalues in C++, but that's a weird special case. That's why you can assign it to a pointer like &lt;code&gt;const char *&lt;/code&gt;, but can't get a &lt;code&gt;const int *&lt;/code&gt; from an integer literal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is this shocking?
&lt;/h2&gt;

&lt;p&gt;This may not seem that shocking to some. Literals are generally temporary and don't really live anywhere in memory - they're essentially hard coded constants in the program. A reference points to some place in memory. How do we point to something that doesn't live in memory? Well, we can't, and we don't!&lt;/p&gt;

&lt;h2&gt;
  
  
  Rvalue Static Promotion
&lt;/h2&gt;

&lt;p&gt;This concept in Rust is called &lt;a href="https://rust-lang.github.io/rfcs/1414-rvalue_static_promotion.html"&gt;rvalue static promotion&lt;/a&gt;. We can look at each part to see what that means:&lt;/p&gt;

&lt;p&gt;Rvalue: Something that can only be on the right hand side of an assignment. For example, you can't do &lt;code&gt;1 = x&lt;/code&gt; because the literal &lt;code&gt;1&lt;/code&gt; is an rvalue.&lt;/p&gt;

&lt;p&gt;Static: Something that is valid for the whole lifetime of the program.&lt;/p&gt;

&lt;p&gt;So we promote the rvalue to a static value in order to take a reference to it. Looking at the program earlier, we can see this in action in &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021&amp;amp;gist=9fe3d6deffc976b8394163f6008b9093"&gt;Rust's playground&lt;/a&gt;. We can see the MIR (one of the intermediate representations of Rust) is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;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;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;_0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;                      &lt;span class="c1"&gt;// return place in scope 0 at src/main.rs:1:11: 1:11&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// in scope 0 at src/main.rs:2:9: 2:10&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;_2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// in scope 0 at src/main.rs:2:13: 2:15&lt;/span&gt;
    &lt;span class="n"&gt;scope&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;debug&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// in scope 1 at src/main.rs:2:9: 2:10&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;bb0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="nn"&gt;main&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;promoted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;    &lt;span class="c1"&gt;// scope 0 at src/main.rs:2:13: 2:15&lt;/span&gt;
                                         &lt;span class="c1"&gt;// ...&lt;/span&gt;
        &lt;span class="n"&gt;_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// scope 0 at src/main.rs:2:13: 2:15&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                          &lt;span class="c1"&gt;// scope 0 at src/main.rs:3:2: 3:2&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;promoted&lt;/span&gt;&lt;span class="p"&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;in&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;_0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// return place in scope 0 at src/main.rs:2:13: 2:15&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;_1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// in scope 0 at src/main.rs:2:14: 2:15&lt;/span&gt;

    &lt;span class="n"&gt;bb0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="mi"&gt;0_i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// scope 0 at src/main.rs:2:14: 2:15&lt;/span&gt;
        &lt;span class="n"&gt;_0&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;_1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// scope 0 at src/main.rs:2:13: 2:15&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                          &lt;span class="c1"&gt;// scope 0 at src/main.rs:2:13: 2:15&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 a little weird to look at if you've never seen MIR before, but the important part is the line &lt;code&gt;promoted[0] in main: &amp;amp;i32&lt;/code&gt; - that's where we see the promoted variable! Then in the main program we assign with &lt;code&gt;_2 = const main::promoted[0];&lt;/code&gt;. So we lift the literal out to a static lifetime in order to return a reference, pretty neat.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why did they do this?
&lt;/h3&gt;

&lt;p&gt;I find this the interesting part. We can see a lot of the motivation for this in the feature:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The necessary changes in the compiler did already get implemented as part of codegen optimizations (emitting references-to or memcopies-from values in static memory instead of embedding them in the code).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It seems like it was just an easy thing to implement, so they did it. Their drawback is pretty interesting:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One more feature with seemingly ad-hoc rules to complicate the language...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I found this funny. Seems like they just thought "it's easy enough, could be useful, why not?" So, they added a new feature to the Rust language. So many languages get by without this, but the Rust devs said, why not?&lt;/p&gt;

&lt;h3&gt;
  
  
  It's useful!
&lt;/h3&gt;

&lt;p&gt;You can see this exact thing in action in Rust's source code! At the time of writing, you can see this &lt;a href="https://github.com/rust-lang/rust/blob/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/compiler/rustc_borrowck/src/nll.rs#L74"&gt;here&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="nf"&gt;dump_mir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;infcx&lt;/span&gt;&lt;span class="py"&gt;.tcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"renumber"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&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;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fourth parameter is a reference to the literal &lt;code&gt;0&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Well, I don't know how useful you'd say it is. But, it's an interesting thing in a common compiler that not many languages have.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>compilers</category>
    </item>
    <item>
      <title>Writing a Simple Programming Language from Scratch - Part 3</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Thu, 30 Dec 2021 22:47:49 +0000</pubDate>
      <link>https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-3-1d7l</link>
      <guid>https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-3-1d7l</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-1-54a2"&gt;Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-2-2522"&gt;Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source code found &lt;a href="https://github.com/evantypanski/ectlang"&gt;here&lt;/a&gt;, for much easier running and compiling.&lt;/p&gt;

&lt;p&gt;We've gotten a compiler to bring code in our source language, ectlang, to a language most are familiar with, C++. Now, we're going to change our target to something a real compiler may use: LLVM!&lt;/p&gt;

&lt;h2&gt;
  
  
  What's LLVM?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://www.aosabook.org/en/llvm.html"&gt;LLVM&lt;/a&gt; is an &lt;a href="https://en.wikipedia.org/wiki/Intermediate_representation"&gt;intermediate representation&lt;/a&gt; (or IR) of the source code. Think of the C++ code we wrote earlier: some other tool grabbed it from that point to get it down to the machine code we can run. Now, we'll target LLVM, a language that's meant to be used as an IR.&lt;/p&gt;

&lt;p&gt;This is a common pattern in compilers because it allows for you to separate your concerns. The compiler we're writing is actually only the front-end: it changes based on the language (language specific), but doesn't change when we move it over to some other architecture (platform agnostic). LLVM is one of the most common ways to bring your compiler from its frontend to people's computers. Thus, we don't have to worry about targeting all of those different architectures, it's done for us!&lt;/p&gt;

&lt;p&gt;And, luckily for us, LLVM has its own API, easily usable from C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using LLVM
&lt;/h2&gt;

&lt;p&gt;There are many tutorials, such as the amazing &lt;a href="https://llvm.org/docs/tutorial/"&gt;Kaleidoscope&lt;/a&gt;, that does a much better job of explaining LLVM and what it can do for you. The goal for me is to stick our foot in the door. LLVM is actually pretty accessible - far more than assembly! It's just, at this point, we're writing code with code.&lt;/p&gt;

&lt;p&gt;So, to start, you'll need to install LLVM to your computer - like on Ubuntu, you can use &lt;code&gt;sudo apt install llvm-12-dev&lt;/code&gt;. If that doesn't work, just google it, it shouldn't be too hard. We'll take a break from our compiler for a bit and just compile some straight up C++ code.&lt;/p&gt;

&lt;p&gt;I'm going to take this bit by bit, then dump the full code for our prototype at the end, just to not overwhelm anybody.&lt;/p&gt;

&lt;p&gt;First, LLVM programs will start with three main objects: &lt;code&gt;LLVMContext&lt;/code&gt;, &lt;code&gt;Module&lt;/code&gt;, and &lt;code&gt;IRBuilder&lt;/code&gt;. Here's about how you'll instantiate each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Context - Owns the types and constants we'll use.&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LLVMContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Module - Contains functions and global variables that we generate.&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;mod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ECTLang module"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// IR Builder - what we'll add instructions to. More of a helper class.&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IRBuilder&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding each of these isn't incredibly important, but I'll explain some. The context is where we'll grab, say, an int type, or float type, or the constant &lt;code&gt;1&lt;/code&gt;. We treat it as a black box: it knows how to make things we want, that's about it.&lt;/p&gt;

&lt;p&gt;The module stores the information that we make. So, if we make a function, that function is owned by the module.&lt;/p&gt;

&lt;p&gt;The IR builder is just an object we use to make creating instructions for basic blocks easier. To create a basic block...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Function returns void.&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;functionReturnType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getVoidTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Our main function.&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mainFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;functionReturnType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ExternalLinkage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="s"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Now make a basic block inside of mainFunction.&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BasicBlock&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BasicBlock&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mainFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetInsertPoint&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy enough. We make a function, call it &lt;code&gt;main&lt;/code&gt;, then make a new basic block with that function as its parent. Then, we let our builder know we're going to insert instructions in the new basic block.&lt;/p&gt;

&lt;p&gt;Basic blocks are hugely important it control flow analysis and compilers. Rust explains them extremely concisely &lt;a href="https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg"&gt;here&lt;/a&gt;. All they are is a bunch of commands that run sequentially. When leaving a basic block, then it can jump to some other basic block based on whatever conditions. But, we're writing a language that doesn't even have &lt;code&gt;if&lt;/code&gt; statements, we'll be fine without much more than knowing to put our instructions in a basic block.&lt;/p&gt;

&lt;p&gt;Now, we have a function that contains a basic block. Let's write our first instructions!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Constant&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;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantInt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getSigned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Constant&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantInt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getSigned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateAdd&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"addtest"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's how you'd make a simple add instruction. Easy, right? The first argument get &lt;code&gt;getSigned&lt;/code&gt; shows how the context is used to get types. To finally see your output LLVM, you can add a return statement and output the program to standard out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateRetVoid&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;outs&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run what we have so far (which is just a function with &lt;code&gt;5 + -6&lt;/code&gt; in it) you get the following LLVM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight llvm"&gt;&lt;code&gt;&lt;span class="c1"&gt;; ModuleID = 'ECTLang module'&lt;/span&gt;
&lt;span class="k"&gt;source_filename&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ECTLang module"&lt;/span&gt;

&lt;span class="k"&gt;define&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="vg"&gt;@main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;body:&lt;/span&gt;
  &lt;span class="k"&gt;ret&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our main function doesn't have anything resembling what we wrote in it! That's because we never actually added our add to the body. We're going to settle this by printing it.&lt;/p&gt;

&lt;p&gt;First, we'll make the function prototype. Here's what &lt;code&gt;printf&lt;/code&gt; looks like in C:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;format&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 here's how we can put that in our LLVM program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt8PtrTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;printfType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/*isVarArg=*/&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printfType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ExternalLinkage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"printf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's pretty trivial to use a search engine to find whatever function you may need's prototype. Just figure out how they map to LLVM and you have an easy job. For the function &lt;code&gt;llvm::FunctionType::get&lt;/code&gt;, the first argument is the return type (&lt;code&gt;int&lt;/code&gt;), the second is a vector of parameters (this can be omitted if there are no parameters), and the last is whether the function is variadic or not.&lt;/p&gt;

&lt;p&gt;We created the actual prototype in our module with the function call &lt;code&gt;llvm::Function::Create&lt;/code&gt;. The only really interesting part here is that we specified the function is externally linked (via &lt;code&gt;llvm::Function::ExternalLinkage&lt;/code&gt;). That just specifies that the linker will find the correct function to use, given we gave the correct information to find it (that is, the prototype).&lt;/p&gt;

&lt;p&gt;Now for the fun part, we need to output our add call to &lt;code&gt;printf&lt;/code&gt;. You can construct this just like you would in C, just via code. We want to make the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's how we construct that function call in LLVM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;formatStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateGlobalStringPtr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&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="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formatStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"printf"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty straightforward! We create an argument for the format string, an argument for the add instruction that we created earlier, then create the function call. The module holds the function that we need, so we can just grab it by name.&lt;/p&gt;

&lt;p&gt;Finally, just add a return statement and output the program the standard output. Here's the full program, for reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.cpp&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"llvm/IR/Module.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"llvm/IR/IRBuilder.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LLVMContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;mod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ECTLang module"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Build IR for this block.&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IRBuilder&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Function returns void.&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;functionReturnType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getVoidTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Our main function.&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mainFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;functionReturnType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                               &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ExternalLinkage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                               &lt;span class="s"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                               &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Now make a basic block inside of mainFunction.&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BasicBlock&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;BasicBlock&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mainFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetInsertPoint&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;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;intTy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Make a simple add with two constants.&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Constant&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;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantInt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getSigned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Constant&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantInt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getSigned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateAdd&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"addtest"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Pointer to int8 would be like char *&lt;/span&gt;
    &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt8PtrTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;printfType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/*isVarArg=*/&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printfType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ExternalLinkage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"printf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Function call arguments&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;formatStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateGlobalStringPtr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&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="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formatStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"printf"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;printArgs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateRetVoid&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;outs&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to compile this program, use your favorite compiler with some extra LLVM tooling help. When you install LLVM, you also get a tool &lt;code&gt;llvm-config&lt;/code&gt; which will help with compiler and linker options for your LLVM install. You can use it with &lt;code&gt;GCC&lt;/code&gt; like so (mine is called &lt;code&gt;llvm-config-12&lt;/code&gt; for the LLVM version I installed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;g++ main.cpp &lt;span class="si"&gt;$(&lt;/span&gt;llvm-config-12 &lt;span class="nt"&gt;--ldflags&lt;/span&gt; &lt;span class="nt"&gt;--libs&lt;/span&gt; &lt;span class="nt"&gt;--cxxflags&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; testllvm.out
&lt;span class="nv"&gt;$ &lt;/span&gt;./testllvm.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the output, we get the following LLVM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight llvm"&gt;&lt;code&gt;&lt;span class="c1"&gt;; ModuleID = 'ECTLang module'&lt;/span&gt;
&lt;span class="k"&gt;source_filename&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ECTLang module"&lt;/span&gt;

&lt;span class="vg"&gt;@0&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;unnamed_addr&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="s"&gt;c"%d\0A\00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;align&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;define&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="vg"&gt;@main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;body:&lt;/span&gt;
  &lt;span class="nv"&gt;%0&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;call&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;*,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="vg"&gt;@printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="k"&gt;getelementptr&lt;/span&gt; &lt;span class="k"&gt;inbounds&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;]*&lt;/span&gt; &lt;span class="vg"&gt;@0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="m"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;ret&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="vg"&gt;@printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i8&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;Being able to read that LLVM isn't important, but we can see the structure seems about right. We have a call to &lt;code&gt;printf&lt;/code&gt; with &lt;code&gt;-1&lt;/code&gt; as our add output being passed in. We get the result of the add as an argument because we used constants, which works for our super simple language well enough.&lt;/p&gt;

&lt;p&gt;We can compile this LLVM directly with Clang into something we can run! Put that code in a file (I'll call it &lt;code&gt;test.ll&lt;/code&gt;) and compile it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;clang++ test.ll &lt;span class="nt"&gt;-o&lt;/span&gt; test.out
&lt;span class="nv"&gt;$ &lt;/span&gt;./test.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see &lt;code&gt;-1&lt;/code&gt; is printed, which happens to be the result of &lt;code&gt;5 + -6&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Since our language has floats, let's also make sure we can do that. If we want to add two floats, it's actually pretty easy. We need to change &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; to be &lt;code&gt;double&lt;/code&gt; types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Constant&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;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantFP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getDoubleTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mf"&gt;5.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Constant&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantFP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getDoubleTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;6.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Switch our &lt;code&gt;add&lt;/code&gt; call to be the floating variant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateFAdd&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"addtest"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then change our format string to accept &lt;code&gt;%f&lt;/code&gt; to format it at as a float:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;formatStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateGlobalStringPtr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%f&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;And if we run/compile that like before, we get a similar result, but floaty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-1.000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generating LLVM
&lt;/h2&gt;

&lt;p&gt;Now that we understand LLVM a bit better, we need to generate LLVM from our small compiler. We'll use the visitor that we made in part 2 to generate some LLVM!&lt;/p&gt;

&lt;p&gt;I'm going to skip setup such as initializing the visitor with the required LLVM context and module, along with other minor details. I'll use code to explain concepts, but if you want working code, check out the Github repo at the top.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building "return" Values
&lt;/h3&gt;

&lt;p&gt;Our visitor we made in the last part doesn't have a return type! Because of this, the more obvious pattern of returning the &lt;code&gt;llvm::Value&lt;/code&gt; at each step isn't feasible. But, we can make each visit make a "promise" to fill a variable. Then, we can just treat that variable as the return value. Here's an example for the integer literal type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// llvmvisitor.cpp&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;LLVMVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Return the LLVM int value.&lt;/span&gt;
    &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConstantInt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getSigned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getInt32Ty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getValue&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;Then, if we want to use that value in the visit function for plus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// llvmvisitor.cpp&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;LLVMVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PlusNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Get the return value from the left side.&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getLeft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Get the return value from the right side.&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getRight&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rhs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Return the add.&lt;/span&gt;
    &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateAdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rhs&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 with that we can have add working! Just do that for the rest of the operations, then we come into another problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dealing with floats
&lt;/h3&gt;

&lt;p&gt;Floats are weird. We can't add an int and a float natively in LLVM. So, instead, we're going to promote everything as soon as we see a float. The logic around this can be a bit more complicated, but we'll try to keep it simple for demonstration purposes. So, if we see a float, we need to promote both sides to a float, then use the float method associated with the operation (eg &lt;code&gt;builder.CreateFAdd&lt;/code&gt;). This is what that may look like for add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// llvmvisitor.cpp&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;floatInst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Promote RHS or LHS if we're dealing with floats and they're not a float.&lt;/span&gt;
        &lt;span class="c1"&gt;// (except we use doubles)&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;lhs&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isDoubleTy&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateSIToFP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getDoubleTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&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;rhs&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getType&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isDoubleTy&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="n"&gt;rhs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateSIToFP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getDoubleTy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

        &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateFAdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Otherwise we're just doing an integer add.&lt;/span&gt;
        &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateAdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rhs&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;Easy! We have some variable &lt;code&gt;floatInst&lt;/code&gt; which gets set when we see a float, then gets reset when we see a &lt;code&gt;StatementNode&lt;/code&gt;. This tells us we need to promote the remaining values that are not floats and use float instructions. We check each side to see if it's a double (since we're setting the type to &lt;code&gt;llvm::Type::getDoubleTy(context)&lt;/code&gt;, works the same with &lt;code&gt;Float&lt;/code&gt; and &lt;code&gt;isFloatTy()&lt;/code&gt;), then we promote if it's not.&lt;/p&gt;

&lt;p&gt;Note, remember to change the format string in the &lt;code&gt;printf&lt;/code&gt; call when using a float.&lt;/p&gt;

&lt;h3&gt;
  
  
  A complete visitor
&lt;/h3&gt;

&lt;p&gt;With that, we have a complete visitor! I put maybe a quarter of the work in this post, with the remaining work already explained, just slightly different. If it's unclear, feel free to check out the repo at the top.&lt;/p&gt;

&lt;p&gt;Here's how you can compile it and run some program you make &lt;code&gt;source.ect&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bison parser.ypp
&lt;span class="nv"&gt;$ &lt;/span&gt;flex &lt;span class="nt"&gt;-o&lt;/span&gt; scanner.c scanner.lex
&lt;span class="nv"&gt;$ &lt;/span&gt;g++ main.cpp parser.tab.cpp nodes/&lt;span class="k"&gt;*&lt;/span&gt;.cpp &lt;span class="si"&gt;$(&lt;/span&gt;llvm-config-12 &lt;span class="nt"&gt;--ldflags&lt;/span&gt; &lt;span class="nt"&gt;--libs&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;llvm-config-12 &lt;span class="nt"&gt;--cxxflags&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; ectfrontend.out
&lt;span class="nv"&gt;$ &lt;/span&gt;./ectfrontend.out &amp;lt; source.ect
&lt;span class="nv"&gt;$ &lt;/span&gt;clang++ test.ll
&lt;span class="nv"&gt;$ &lt;/span&gt;./a.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  This Language Sucks
&lt;/h2&gt;

&lt;p&gt;Okay, now that we have a "working" language, I want to explain many of the reasons this language sucks. The fundamentals I tried to show in this post remain valid, but everything else is just a tool for getting those fundamentals out with the least amount of background knowledge I could muster. But, knowing some of the shortcomings could help us all not make these mistakes when we actually decide to build a compiler.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No semantic analysis

&lt;ul&gt;
&lt;li&gt;We don't check to see that the code is valid, other than it parses. Turns out this could not be horrible for our toy language, but checking for overflows, valid casts, etc. are super important for a compiler.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;No warnings, diagnostics, assertions...

&lt;ul&gt;
&lt;li&gt;If your program is ill formed, you probably should tell the user why. Depending on how it's ill formed, our compiler may say "syntax error" or just segfault. That's pretty bad design.&lt;/li&gt;
&lt;li&gt;We also don't have assertions. These are pretty useful in any program, so when you build debug you can make sure your assumptions are valid. For example, if we trust our assumption that &lt;code&gt;ret&lt;/code&gt; is set for each arithmetic operation, we should add an assertion to express that assumption.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;No unary minus

&lt;ul&gt;
&lt;li&gt;Yeah, you can't add &lt;code&gt;5 + -6&lt;/code&gt;. Even though I use that as an example up when explaining the LLVM API. Oops. You can still have negative numbers, though?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;We expose the IR

&lt;ul&gt;
&lt;li&gt;Not a big deal, but most people don't want to compile LLVM directly from a file afterwards. Ideally, you'd continue to use the API for LLVM (or whatever intermediate representation you're using) to create the executable.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Bad CLI

&lt;ul&gt;
&lt;li&gt;Our command line interface sucks. You have to pass the program directly in rather than just calling with the file name. You can't specify the output file's name. There's no command line options, even like &lt;code&gt;--help&lt;/code&gt;. If anyone ever uses your CLI, these are not just helpful, they're necessary.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;We abuse C++

&lt;ul&gt;
&lt;li&gt;I purposefully did this to rant about C++ a little. If you noticed, we actually assume that &lt;code&gt;ret&lt;/code&gt; is set when we set the &lt;code&gt;lhs&lt;/code&gt; and &lt;code&gt;rhs&lt;/code&gt; variables. If it's not set, we'd segfault. This is such an incredible amount of trust to give to the functions you're calling. If you get a pointer, always check for null.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I could have fixed these, but I tried to keep everything bite sized for the average reader. It already took me years (!) to write these posts because I had to decide what to keep and what to cut, using experience working with a few real compilers in my job and class to help with that. There are plenty of things that discuss every little detail or have extremely well written, correct code. Those are generally textbooks.&lt;/p&gt;

&lt;p&gt;But, thank you so much for reading! Feel free to contact me and get in touch if you have questions or concerns. I hope that compilers seem a little more like regular code now.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>c</category>
      <category>compilers</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Behind C++ Lambda Functions</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Sat, 12 Jun 2021 16:45:55 +0000</pubDate>
      <link>https://dev.to/evantypanski/behind-c-lambda-functions-2169</link>
      <guid>https://dev.to/evantypanski/behind-c-lambda-functions-2169</guid>
      <description>&lt;p&gt;Lambda functions are not just pointers to a function. This is obvious when you think about it: if they're anonymous, and declared completely differently, something must be different. But, we can assign a pointer to a function to a lambda easily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&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;So how does that work? We know we're just assigning to a pointer. But a lambda function isn't just a pointer. So what is it?&lt;/p&gt;

&lt;p&gt;Note, we're going to use Clang as an example. A lot of things in C++ are up to compiler dev's imagination, also known as implementation specific. So if I say "lambdas have 12 toes," that is because Clang defines lambdas as having 12 toes, not because they have to have 12 toes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lambdas are classes
&lt;/h2&gt;

&lt;p&gt;When you define a lambda function, you're really making a C++ class. This class implements the method &lt;code&gt;operator()&lt;/code&gt;. When you call the lambda, you're just calling that operator implementation.&lt;/p&gt;

&lt;p&gt;Here's a simple C++ program to show that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;That line in main is just defining a lambda with no captures (&lt;code&gt;[]&lt;/code&gt;), that takes no argument (&lt;code&gt;()&lt;/code&gt;), with no body (&lt;code&gt;{}&lt;/code&gt;), then calling it (&lt;code&gt;()&lt;/code&gt;). We can actually look at what Clang develops as the AST with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clang++ &lt;span class="nt"&gt;-Xclang&lt;/span&gt; &lt;span class="nt"&gt;-ast-dump&lt;/span&gt; lambda.cpp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll walk through the lambda function piece by piece.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looking at the AST
&lt;/h2&gt;

&lt;p&gt;The AST is just the internal representation of the code that the compiler looks at. Let's look at the most obvious part of the Lambda shenanigans (note, obvious is relative):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CXXOperatorCallExpr&lt;/span&gt; 0x17238c8 &amp;lt;col:3, col:10&amp;gt; &lt;span class="s1"&gt;'void'&lt;/span&gt;:&lt;span class="s1"&gt;'void'&lt;/span&gt;
  |-ImplicitCastExpr 0x1723858 &amp;lt;col:9, col:10&amp;gt; &lt;span class="s1"&gt;'void (*)() const'&lt;/span&gt; &amp;lt;FunctionToPointerDecay&amp;gt;
  | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-DeclRefExpr&lt;/span&gt; 0x17237d8 &amp;lt;col:9, col:10&amp;gt; &lt;span class="s1"&gt;'void () const'&lt;/span&gt; lvalue CXXMethod 0x1723190 &lt;span class="s1"&gt;'operator()'&lt;/span&gt; &lt;span class="s1"&gt;'void () const'&lt;/span&gt;
  &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-ImplicitCastExpr&lt;/span&gt; 0x17238b0 &amp;lt;col:3, col:8&amp;gt; &lt;span class="s1"&gt;'const (lambda at lambda.cpp:2:3)'&lt;/span&gt; lvalue &amp;lt;NoOp&amp;gt;
    &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-MaterializeTemporaryExpr&lt;/span&gt; 0x1723898 &amp;lt;col:3, col:8&amp;gt; &lt;span class="s1"&gt;'(lambda at lambda.cpp:2:3)'&lt;/span&gt; lvalue
      &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-LambdaExpr&lt;/span&gt; 0x17236a0 &amp;lt;col:3, col:8&amp;gt; &lt;span class="s1"&gt;'(lambda at lambda.cpp:2:3)'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright. What we have here is a call expression - that's the last &lt;code&gt;()&lt;/code&gt; in our lambda. Then, we have some gibberish that is Clang doing internal conversions in order to make the types right. Finally, we have our trusted &lt;code&gt;LambdaExpr&lt;/code&gt; - our lambda! Let's look at what's in that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-LambdaExpr&lt;/span&gt; 0x17236a0 &amp;lt;col:3, col:8&amp;gt; &lt;span class="s1"&gt;'(lambda at lambda.cpp:2:3)'&lt;/span&gt;
  |-CXXRecordDecl 0x1723050 &amp;lt;col:3&amp;gt; col:3 implicit class definition
  | |-DefinitionData lambda pass_in_registers empty standard_layout trivially_copyable can_const_default_init
  | | |-DefaultConstructor defaulted_is_constexpr
  | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
  | | |-MoveConstructor exists simple trivial needs_implicit
  | | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
  | | |-MoveAssignment
  | | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-Destructor&lt;/span&gt; simple irrelevant trivial
  | |-CXXMethodDecl 0x1723190 &amp;lt;col:6, col:8&amp;gt; col:3 used operator&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="s1"&gt;'void () const'&lt;/span&gt; inline
  | | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0x1723240 &amp;lt;col:7, col:8&amp;gt;
  | |-CXXConversionDecl 0x1723538 &amp;lt;col:3, col:8&amp;gt; col:3 implicit operator void &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)()&lt;/span&gt; &lt;span class="s1"&gt;'void (*() const noexcept)()'&lt;/span&gt; inline
  | |-CXXMethodDecl 0x17235e8 &amp;lt;col:3, col:8&amp;gt; col:3 implicit __invoke &lt;span class="s1"&gt;'void ()'&lt;/span&gt; static inline
  | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CXXDestructorDecl&lt;/span&gt; 0x17236d0 &amp;lt;col:3&amp;gt; col:3 implicit referenced ~ &lt;span class="s1"&gt;'void () noexcept'&lt;/span&gt; inline default trivial
  &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0x1723240 &amp;lt;col:7, col:8&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, gosh. We'll go piece by piece through this, but here's our class. &lt;a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html"&gt;A &lt;code&gt;CXXRecordDecl&lt;/code&gt; in Clang&lt;/a&gt; represents a C++ struct, union, or class. So our lambda actually contains that class in its node.&lt;/p&gt;

&lt;p&gt;We can even see that in Clang documentation. If we go to the &lt;a href="https://clang.llvm.org/doxygen/classclang_1_1LambdaExpr.html"&gt;documentation for &lt;code&gt;LambdaExpr&lt;/code&gt; in Clang&lt;/a&gt;, we can actually see a bunch of useful functions. Notably, we can see &lt;code&gt;getLambdaClass()&lt;/code&gt;, which will "retrieve the class that corresponds to the lambda." Neat!&lt;/p&gt;

&lt;p&gt;We'll skip the &lt;code&gt;DefinitionData&lt;/code&gt; part of the class, since that's just defining some default constructors and whatnot. Let's look at the &lt;code&gt;operator()&lt;/code&gt; definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;| |-CXXMethodDecl 0x1723190 &amp;lt;col:6, col:8&amp;gt; col:3 used operator&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="s1"&gt;'void () const'&lt;/span&gt; inline
| | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0x1723240 &amp;lt;col:7, col:8&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have our call operator. It's empty, and returns void, so it's not like we expect to see much. But, let's make it have something. If we change our lambda function to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;[](){&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have something in our function body, so we expect to see it in the call operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;| |-CXXMethodDecl 0x171f190 &amp;lt;col:6, col:18&amp;gt; col:3 used operator&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="s1"&gt;'void () const'&lt;/span&gt; inline
| | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0x171f2f8 &amp;lt;col:7, col:18&amp;gt;
| |   &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-DeclStmt&lt;/span&gt; 0x171f2e0 &amp;lt;col:8, col:17&amp;gt;
| |     &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-VarDecl&lt;/span&gt; 0x171f258 &amp;lt;col:8, col:16&amp;gt; col:12 i &lt;span class="s1"&gt;'int'&lt;/span&gt; cinit
| |       &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-IntegerLiteral&lt;/span&gt; 0x171f2c0 &amp;lt;col:16&amp;gt; &lt;span class="s1"&gt;'int'&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there we have it, our call operator has a body, just nested in the AST. How about a return type?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;[](){&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&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 AST:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;| |-CXXMethodDecl 0x1f88190 &amp;lt;col:6, col:17&amp;gt; col:3 used operator&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="s1"&gt;'int () const'&lt;/span&gt; inline
| | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0x1f883b8 &amp;lt;col:7, col:17&amp;gt;
| |   &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-ReturnStmt&lt;/span&gt; 0x1f883a8 &amp;lt;col:8, col:15&amp;gt;
| |     &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-IntegerLiteral&lt;/span&gt; 0x1f88240 &amp;lt;col:15&amp;gt; &lt;span class="s1"&gt;'int'&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, our &lt;code&gt;CXXMethodDecl&lt;/code&gt; changed from &lt;code&gt;void () const&lt;/code&gt; to &lt;code&gt;int () const&lt;/code&gt;, showing our change in return type!&lt;/p&gt;

&lt;p&gt;Finally, how about adding a parameter?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;){}(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;| |-CXXMethodDecl 0xe84220 &amp;lt;col:11, col:13&amp;gt; col:3 used operator&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="s1"&gt;'void (int) const'&lt;/span&gt; inline
| | |-ParmVarDecl 0xe83fc8 &amp;lt;col:6, col:10&amp;gt; col:10 i &lt;span class="s1"&gt;'int'&lt;/span&gt;
| | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0xe842d8 &amp;lt;col:12, col:13&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that the &lt;code&gt;CXXMethodDecl&lt;/code&gt; is now &lt;code&gt;void (int) const&lt;/code&gt; and we have a &lt;code&gt;ParmVarDecl&lt;/code&gt; (just a function parameter) inside of the method decl. Awesome.&lt;/p&gt;

&lt;p&gt;But, lambdas also let you capture a variable. This means that if I have a variable &lt;code&gt;i&lt;/code&gt; outside of the lambda, capture it, change it, then call the lambda, we should see that variable change. Let's try it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;lambda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;](){&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"i is: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;lambda&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 here's our output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;i is: 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome. How does this work in the AST? If we look at it, we actually have one key difference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;| |-FieldDecl 0x18ec440 &amp;lt;col:5&amp;gt; col:5 implicit &lt;span class="s1"&gt;'int &amp;amp;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a field inside of our lambda's class now! It's a reference to an int, namely to &lt;code&gt;i&lt;/code&gt; declared outside. Then, when we use our lambda, we just use that field! Neat.&lt;/p&gt;

&lt;p&gt;Now, we don't take a parameter, we still return void, can we assign this to a C function pointer like we did at the beginning?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;i&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler errors!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lambda.cpp:5:10: error: no viable conversion from &lt;span class="s1"&gt;'(lambda at lambda.cpp:5:19)'&lt;/span&gt; to &lt;span class="s1"&gt;'void (*)()'&lt;/span&gt;
  void &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;ptr&lt;span class="o"&gt;)()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&amp;amp;i]&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;It's weird because we still have all the qualifications of that function pointer. But, if we look at the AST, we'll see something missing from the Lambda's class, particularly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;| |-CXXConversionDecl 0x142f7b8 &amp;lt;col:19&amp;gt; col:19 implicit used operator void &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)()&lt;/span&gt; &lt;span class="s1"&gt;'void (*() const noexcept)()'&lt;/span&gt; inline
| | &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-CompoundStmt&lt;/span&gt; 0x142fba0 &amp;lt;col:19&amp;gt;
| |   &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-ReturnStmt&lt;/span&gt; 0x142fb90 &amp;lt;col:19&amp;gt;
| |     &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-ImplicitCastExpr&lt;/span&gt; 0x142fb78 &amp;lt;col:19&amp;gt; &lt;span class="s1"&gt;'void (*)()'&lt;/span&gt; &amp;lt;FunctionToPointerDecay&amp;gt;
| |       &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nt"&gt;-DeclRefExpr&lt;/span&gt; 0x142fb58 &amp;lt;col:19&amp;gt; &lt;span class="s1"&gt;'void ()'&lt;/span&gt; lvalue CXXMethod 0x142f868 &lt;span class="s1"&gt;'__invoke'&lt;/span&gt; &lt;span class="s1"&gt;'void ()'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clang actually refuses to generate a conversion to the function pointer if we have any captures! That makes sense because the function now relies on that variable &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's say we're really curious and need to know why this happened. How can we do that?&lt;/p&gt;

&lt;h2&gt;
  
  
  Diving into Clang's code
&lt;/h2&gt;

&lt;p&gt;Luckily, Clang's source code is open source. You can find it, along with LLVM and more, &lt;a href="https://github.com/llvm/llvm-project"&gt;on Github&lt;/a&gt;. Furthermore, if you want to know something high level about Clang's code, check out the &lt;a href="https://clang.llvm.org/docs/InternalsManual.html"&gt;Clang internals manual&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But, our job is simple: find out where it refuses to generate the conversion function.&lt;/p&gt;

&lt;p&gt;The first place to start requires a bit of compiler knowledge. Clang has a Sema library for certain semantic actions. This Sema library is what builds the AST. Considering that, we probably have a good place to start, since we noticed a difference in the AST.&lt;/p&gt;

&lt;p&gt;At this time, you can find the Sema library on the Github under &lt;code&gt;llvm-project/clang/lib/Sema/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that we're here, we want to see what creates the lambda function. A solid way to do this would be to look up the constructor in the documentation and see what calls it and when. But, our case is lucky: there's a file called &lt;code&gt;SemaLambda.cpp&lt;/code&gt;, so let's check that out.&lt;/p&gt;

&lt;p&gt;If we dig around here, we find the function &lt;code&gt;BuildLambdaExpr&lt;/code&gt;. Seems promising.&lt;/p&gt;

&lt;p&gt;From there, let's just look through the function.&lt;/p&gt;

&lt;p&gt;First we do some setup from previously gathered info:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;CallOperator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;CallOperator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Lambda&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;IntroducerRange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;IntroducerRange&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ExplicitParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ExplicitParams&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ExplicitResultType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;HasImplicitReturnType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;LambdaCleanup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Cleanup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ContainsUnexpandedParameterPack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LSI&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ContainsUnexpandedParameterPack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;IsGenericLambda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isGenericLambda&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we do some logic checking if captures are used and getting them in a good form for our AST.&lt;/p&gt;

&lt;p&gt;Next, we can see why we don't get our conversion function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&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;Captures&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;CaptureDefault&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;LCD_None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;addFunctionPointerConversions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IntroducerRange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                &lt;span class="n"&gt;CallOperator&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 only add the function pointer conversion if we have no captures and the capture default is none!&lt;/p&gt;

&lt;p&gt;If you really want to dive into the weeds, the Clang devs also often comment the part of the standard they took from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// C++11 [expr.prim.lambda]p6:&lt;/span&gt;
&lt;span class="c1"&gt;//   The closure type for a lambda-expression with no lambda-capture&lt;/span&gt;
&lt;span class="c1"&gt;//   has a public non-virtual non-explicit const conversion function&lt;/span&gt;
&lt;span class="c1"&gt;//   to pointer to function having the same parameter and return&lt;/span&gt;
&lt;span class="c1"&gt;//   types as the closure type's function call operator.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find this yourself too! Just look around for C++ standards (like through &lt;a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/"&gt;open-std.org&lt;/a&gt; or just a search engine) and find the section with expr.prim.lambda. You'll see numbering, find number 6, and you'll find identical or similar wording to that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Was this about lambdas?
&lt;/h2&gt;

&lt;p&gt;Not really. Lambdas were just a mechanism for showing how to find out more about C++ by digging into it. This applies to any language or compiler too, especially if they let you dump ast (or some intermediate representation). It's also just fun to look at complex code.&lt;/p&gt;

&lt;p&gt;So there you go. Try this out the next time you see a weird construct in your favorite language. Sometimes, you'll get surprised and fall into a fun rabbit hole. It's worth every hour.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>clang</category>
    </item>
    <item>
      <title>Writing a Simple Programming Language from Scratch - Part 2</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Mon, 03 May 2021 23:29:56 +0000</pubDate>
      <link>https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-2-2522</link>
      <guid>https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-2-2522</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-1-54a2"&gt;Part 1 found here!!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source code found &lt;a href="https://github.com/evantypanski/ectlang"&gt;here&lt;/a&gt;, for much easier running and compiling.&lt;/p&gt;

&lt;p&gt;Up to this point, we made part of a compiler front end, namely the lexer and parser. Now, we'll adapt the parser and add some steps to make.. more code! Also known as an intermediate representation of the source code. More on that later.&lt;/p&gt;

&lt;p&gt;To do this, we'll use a visitor pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Visitors
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Visitor_pattern"&gt;visitor software pattern&lt;/a&gt; is very useful, especially in compiler design. Visitors can essentially implement new methods on each node while traversing a tree. Or a not tree, but we'll use a tree.&lt;/p&gt;

&lt;p&gt;Let's make a basic visitor!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nodes.cpp&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Visitor&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IntegerNode&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;IntegerNode&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;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&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;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Visitor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="o"&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;span class="n"&gt;IntegerNode&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&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;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;IntegerNode&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Visitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="o"&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;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&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;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="n"&gt;visitor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visitor&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;Then here's the output when this program is called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;g++ nodes.cpp
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;./a.out
5
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening here is pretty simple. The node "accepts" the visitor, which prompts the visitor to visit that particular node. &lt;code&gt;accept&lt;/code&gt; will call &lt;code&gt;IntegerNode&lt;/code&gt;'s accept because of the &lt;code&gt;virtual&lt;/code&gt; method. Then, the visitor can do whatever it wants with that object, knowing its type exactly. If we had a &lt;code&gt;PlusNode&lt;/code&gt; that accepted a visitor, then we would call that accept method, thus we would visit the &lt;code&gt;PlusNode&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;I won't bore by going through all of the nodes, it's mostly just busy work to set it all up. There are ways to write those classes without all of the busy work (tools, metaprogramming). Useful, but writing a simple set of nodes isn't too much work. Sometimes people go through a lot of effort to automate a process that would take less time to just do manually.&lt;/p&gt;

&lt;p&gt;So now we can add any number of nodes that we want. I'll show how we could do some recursion in this using the plus node, then hopefully extrapolating out will be a bit more simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nodes.cpp&lt;/span&gt;
&lt;span class="cm"&gt;/* Keeping all IntegerNode references in code */&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlusNode&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;PlusNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Visitor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PlusNode&lt;/span&gt; &lt;span class="o"&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;span class="n"&gt;PlusNode&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PlusNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;PlusNode&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Visitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PlusNode&lt;/span&gt; &lt;span class="o"&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;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"+"&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"="&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&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;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="n"&gt;leftInt&lt;/span&gt;&lt;span class="p"&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;rightInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PlusNode&lt;/span&gt; &lt;span class="n"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leftInt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="n"&gt;visitor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visitor&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 will recursively use your new &lt;code&gt;PlusNode&lt;/code&gt; and output the added numbers! Here's the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;g++ nodes.cpp
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;./a.out
1+2&lt;span class="o"&gt;=&lt;/span&gt;3
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only nodes we'll add that aren't obvious are &lt;code&gt;ProgramNode&lt;/code&gt; and &lt;code&gt;StatementNode&lt;/code&gt;. A "program" will be a collection of statements for our simple language, and a "statement" is a bunch of math followed by a semicolon. Really a statement can be seen as a command, like &lt;code&gt;return 0;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So how will we use this? The program can actually be represented easily as a tree. This is often represented as an abstract syntax tree (AST). We will then visit each node in the AST, and the visitor will generate some code. This generated code is an &lt;em&gt;intermediate representation&lt;/em&gt; of the code - we don't write this code by hand, but the computer also doesn't use it. It's mainly used by the language to facilitate optimizations and compiling for different computers with different architectures.&lt;/p&gt;

&lt;p&gt;We can also have other kinds of visitors! One class &lt;code&gt;Visitor&lt;/code&gt; could be inherited by your type checker, or what makes your symbol table, then each of these visitors can be run. This example will only use one visitor - the one that generates code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lexer and Parser Updates
&lt;/h3&gt;

&lt;p&gt;Now let's update our parser from part 1 to return nodes instead of doing the calculations itself. We'll also rename the file the &lt;code&gt;parser.ypp&lt;/code&gt; to generate C++. Here is what we'll use for our C declarations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// parser.ypp&lt;/span&gt;

&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;assert&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/node.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/minusnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/plusnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/multnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/divnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/expnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/integernode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/floatnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/programnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/statementnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"scanner.c"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
  &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;yylex&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="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;yyerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cerr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;ProgramNode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="p"&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;There are some basic changes here, but two main ones for understanding how this works: first, we import all of the nodes we'll use inside the parser. Second, we declare a variable &lt;code&gt;*program&lt;/code&gt; to point to the initial node. We'll build everything off of this, then use it in a different file.&lt;/p&gt;

&lt;p&gt;Here are the Bison declarations that we'll use. We changed the union to include nodes that we want to be exchanged between grammar rules, and put the return values in their type definitions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// parser.ypp&lt;/span&gt;

&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="k"&gt;union&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;intVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;floatVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExpNode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;expNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StatementNode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;statementNode&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="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;

&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;intVal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;INTEGER_LITERAL&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;floatVal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FLOAT_LITERAL&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="n"&gt;SEMI&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;expNode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;statementNode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;   &lt;span class="n"&gt;PLUS&lt;/span&gt; &lt;span class="n"&gt;MINUS&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;   &lt;span class="n"&gt;MULT&lt;/span&gt; &lt;span class="n"&gt;DIV&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is similar to what we did in part 1 - just including the nodes!&lt;/p&gt;

&lt;p&gt;Finally, here are the grammar rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// parser.ypp&lt;/span&gt;

&lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ProgramNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&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="n"&gt;program&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;addStatement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&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="nl"&gt;statement:&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt; &lt;span class="n"&gt;SEMI&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StatementNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&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="nl"&gt;exp:&lt;/span&gt;
   &lt;span class="n"&gt;INTEGER_LITERAL&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;IntegerNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&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="n"&gt;FLOAT_LITERAL&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FloatNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&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="n"&gt;exp&lt;/span&gt; &lt;span class="n"&gt;PLUS&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;PlusNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&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="n"&gt;exp&lt;/span&gt; &lt;span class="n"&gt;MINUS&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;MinusNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&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="n"&gt;exp&lt;/span&gt; &lt;span class="n"&gt;MULT&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;MultNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&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="n"&gt;exp&lt;/span&gt; &lt;span class="n"&gt;DIV&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;   &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;DivNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yylineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&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;Each rule returns a new node that we can access in another program as an object - after we finish parsing, we'll only touch these nodes, we don't care about the source code. So, when we parse a division, we'll create a &lt;code&gt;DivNode&lt;/code&gt; to represent that division, then don't touch that source code again.&lt;/p&gt;

&lt;p&gt;Now we also add a &lt;code&gt;yylineno&lt;/code&gt; variable to keep track of line numbers. Most programming languages have some way of managing where in the source file certain nodes came from in order to emit diagnostics, telling the programmer what they did wrong. We're not going to do much with that, but if you ever want good diagnostics, consider including some source information.&lt;/p&gt;

&lt;p&gt;Finally, &lt;code&gt;program&lt;/code&gt; has a bit of interesting logic. If we get to the first, empty case for the &lt;code&gt;program&lt;/code&gt; nonterminal, we make a new &lt;code&gt;ProgramNode&lt;/code&gt; to use. If we get to the point where we have a statement, we'll also have a program, so we add the statement (where a &lt;code&gt;program&lt;/code&gt; is just a &lt;code&gt;std::vector&lt;/code&gt; of statements in implementation). We also assert if we don't have a &lt;code&gt;program&lt;/code&gt; - some kind of &lt;code&gt;null&lt;/code&gt; check is SO important in C++ code, preferably more than an &lt;code&gt;assert&lt;/code&gt; since it doesn't apply in production! Never underestimate the power of humans to segfault a C/C++ program.&lt;/p&gt;

&lt;p&gt;Our lexer didn't change much, just adding a couple options, but I'll put it here anyway:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// scanner.lex&lt;/span&gt;

&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"parser.tab.hpp"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;  &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;yylex&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="n"&gt;noyywrap&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="n"&gt;yylineno&lt;/span&gt;

&lt;span class="o"&gt;%%&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;yylval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;atoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yytext&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;INTEGER_LITERAL&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="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;.[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;yylval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;floatVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;atof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yytext&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;FLOAT_LITERAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s"&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;PLUS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s"&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;MINUS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s"&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;MULT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s"&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;DIV&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s"&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;SEMI&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;t&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="err"&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="cm"&gt;/* ignore whitespace */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have that, let's make a file that runs this! This was moved from the bottom of the parser from part 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.cpp&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/programnode.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"nodes/visitor.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="n"&gt;ProgramNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;yyparse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;yyparse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Visitor&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All this does is it calls some method &lt;code&gt;yyparse()&lt;/code&gt; which we get as an extern function from the Bison files we generated. That populates our &lt;code&gt;program&lt;/code&gt; variable. From this, we can visit the program we parsed!&lt;/p&gt;

&lt;p&gt;Let's run it on a program with one line - &lt;code&gt;3 + 3.2;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;flex &lt;span class="nt"&gt;-o&lt;/span&gt; scanner.c scanner.lex
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;bison parser.ypp
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;g++ parser.tab.cpp main.cpp nodes/&lt;span class="k"&gt;*&lt;/span&gt;.cpp
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;./a.out &amp;lt; source.ect
Program
STATEMENT
3+3.2%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is good! We visited the program, then the statement, then the &lt;code&gt;3+3.2&lt;/code&gt; in our code. This proves we have a parser which creates the AST, then a visitor which goes through and visits the nodes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Generation
&lt;/h2&gt;

&lt;p&gt;Now, we have an accurate AST for our program. We want to use this AST in order to write code that our computer can run. Normally, we'll do this with a true intermediate representation (like LLVM). We won't be doing that quite yet. Instead, we'll go with something easier to understand, that way the concept of code generation and visitors aren't blurred by the complexity of another foreign language. So, instead, we'll just turn our code into C++. It pains me to do this, but it's a good tool to use since we know what proper C++ code looks like.&lt;/p&gt;

&lt;p&gt;Another consequence of writing C++ code is we're going to make it human readable. This won't be the case for most languages - once it gets to this point, the machine tends to be one of the only things actually reading the code, so it won't care much for readability. We're humans and C++ is a human programming language, so it's relatively easy to understand! LLVM is somewhat readable, but it's not super easy.&lt;/p&gt;

&lt;p&gt;How will we do this? We'll make a new visitor that generates &lt;code&gt;.cpp&lt;/code&gt; files! Here's the implementation file for that (let's ignore the header files):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// cppvisitor.cpp&lt;/span&gt;

&lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Open the file we are going to write to.&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fstream&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ProgramNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Write out the information we need to actually run the C program&lt;/span&gt;
    &lt;span class="c1"&gt;// This is hardcoded. It shouldn't be in most cases, but education!!&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"#include &amp;lt;iostream&amp;gt;&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="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"int main() {&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="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getStatement&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"}&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="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StatementNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// We want to print our statements when the generated C++ code is run.&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;std::cout &amp;lt;&amp;lt; "&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getExp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" &amp;lt;&amp;lt; std::endl;&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;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IntegerNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Append the integer to the file.&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FloatNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Append the float to the file.&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PlusNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Append the addition to the file.&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getLeft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" + "&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getRight&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MinusNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Append the subtraction to the file.&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getLeft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" - "&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getRight&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MultNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Append the multiplication to the file.&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getLeft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" * "&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getRight&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CPPVisitor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DivNode&lt;/span&gt; &lt;span class="o"&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;span class="c1"&gt;// Append the division to the file.&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getLeft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" / "&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getRight&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;this&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;Basically, if you understood what was going on before with us printing out each statement, you'll understand this. Instead of using &lt;code&gt;std::cout&lt;/code&gt; directly, we make a C++ program that calls them. Each statement (which, in our language, is just some math followed by a semicolon) will go into an &lt;code&gt;std::cout&lt;/code&gt; call in another file - very, very useful! Okay maybe not but you can see the potential.&lt;/p&gt;

&lt;p&gt;What's amazing about this isn't that we wrote a program that (kind of) writes C++ programs, it's that you can write your visitor to generate many different languages - it's not too much harder to just make it generate assembly! It's really easy to compile/run C++ code to see the fruits of your labor, so that's what we use here.&lt;/p&gt;

&lt;p&gt;Let's run on this file (called &lt;code&gt;source.ect&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight r"&gt;&lt;code&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;3.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One last run!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;flex &lt;span class="nt"&gt;-o&lt;/span&gt; scanner.c scanner.lex
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;bison parser.ypp
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;g++ parser.tab.cpp main.cpp nodes/&lt;span class="k"&gt;*&lt;/span&gt;.cpp
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;./a.out &amp;lt; source.ect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From that, we get the following file created (in &lt;code&gt;test.cpp&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;3.2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&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;3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&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 if we run that...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;g++ test.cpp &lt;span class="nt"&gt;-o&lt;/span&gt; ect-program
evan:ectlang/ &lt;span class="nv"&gt;$ &lt;/span&gt;./ect-program
6.2
3
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exactly as we'd expect. Good job, folks!&lt;/p&gt;

&lt;h2&gt;
  
  
  So What?
&lt;/h2&gt;

&lt;p&gt;So we created C++ code. Who cares? Why didn't we just write the C++ program? Well, the intermediate representation won't really be so high level - it'd be more like assembly in the end. We don't want to constantly write assembly for obvious reasons, so we write programs that transform our code into that - the whole purpose of a compiler. While here we made the intermediate representation a C++ file, that will never happen in a real language, but it's not too far off from it. That's why there's a bit more to the puzzle we have yet to cover.&lt;/p&gt;

&lt;p&gt;With that being said, that's everything! You now should at least have a basis for doing some operations on your source code, eventually creating an intermediate representation that can be used. Congrats!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-3-1d7l"&gt;Onwards to part 3!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>c</category>
      <category>compilers</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Writing a Simple Programming Language from Scratch - Part 1</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Sun, 07 Jul 2019 13:03:45 +0000</pubDate>
      <link>https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-1-54a2</link>
      <guid>https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-1-54a2</guid>
      <description>&lt;p&gt;If you're a developer, you've used programming languages. They're awesome ways to make a computer do what you want it to. Maybe you've even dove deep and programmed in assembly or machine code. Many never want to come back. But some wonder, how can I torture myself more by doing more low level programming? I want to know more about how programming languages are made! All joking aside, writing a new language isn't as bad as it sounds, so if you have even a mild curiosity, I would suggest you stick around and see what it's about.&lt;/p&gt;

&lt;p&gt;This post is meant to give a simple dive into how a programming language can be made, and how you can make your own special language. Maybe even name it after yourself. Who knows.&lt;/p&gt;

&lt;p&gt;I also bet that this seems like an incredibly daunting task to take on. Do not worry, for I have considered this. I did my best to explain everything relatively simply without going on &lt;em&gt;too&lt;/em&gt; many tangents. By the end of this post, you will be able to create your own programming language (there will be a few parts), but there's more. Knowing what goes on under the hood will make you better at debugging. You'll better understand new programming languages and why they make the decisions that they do. You can have a programming language named after yourself, if I didn't mention that before. Also, it's really fun. At least to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compilers and Interpreters
&lt;/h2&gt;

&lt;p&gt;Programming languages are generally high-level. That is to say, you're not looking at 0s and 1s, nor registers and assembly code. But, your computer only understands 0s and 1s, so it needs a way to move from what you read easily to what the machine can read easily. That translation can be done through compilation or interpretation.&lt;/p&gt;

&lt;p&gt;Compilation is the process of turning an entire source file of the source language into a target language. For our purposes, we'll think about compiling down from your brand new, state of the art language, all the way down to runnable machine code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkk5latqahdwgwbc7jcl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkk5latqahdwgwbc7jcl.png" alt="Simple Compilation Diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My goal is to make the "magic" disappear&lt;/p&gt;

&lt;p&gt;Interpretation is the process of executing code in a source file more or less directly. I'll let you think that's magic for this.&lt;/p&gt;

&lt;p&gt;So, how do you go from easy-to-read source language to hard-to-understand target language?&lt;/p&gt;

&lt;h2&gt;
  
  
  Phases of a Compiler
&lt;/h2&gt;

&lt;p&gt;A compiler can be split up into phases in various ways, but there's one way that's most common. It makes only a small amount of sense the first time you see it, but here it goes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4s0dv8f74q0macn3fd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4s0dv8f74q0macn3fd2.png" alt="Fake Phases of a Compiler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oops, I picked the wrong diagram, but this will do. Basically, you get the source file, you put it into a format that the computer wants (removing white space and stuff like that), change it into something the computer can move around well in, and then generate the code from that. There's more to it. That's for another time, or for your own research if your curiosity is killing you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  AKA "Making source code pretty"
&lt;/h3&gt;

&lt;p&gt;Consider the following completely made up language that's basically just a calculator with semicolons:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 r
  // source.ect
  3 + 3.2;
  5.0 / 1.9;
  6 * 2;


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

&lt;/div&gt;

&lt;p&gt;The computer doesn't need all of that. Spaces are just for our petty minds. And new lines? No one needs those. The computer turns this code that you see into a stream of tokens that it can use instead of the source file. Basically, it knows that &lt;code&gt;3&lt;/code&gt; is an integer, &lt;code&gt;3.2&lt;/code&gt; is a float, and &lt;code&gt;+&lt;/code&gt; is something that operates on those two values. That's all the computer &lt;em&gt;really&lt;/em&gt; needs to get by. It's the &lt;strong&gt;lexical analyzer&lt;/strong&gt;'s job to provide these tokens instead of a source program.&lt;/p&gt;

&lt;p&gt;How it does that is really quite simple: give the lexer (a less pretentious sounding way of saying lexical analyzer) some stuff to expect, then tell it what to do when it sees that stuff. These are called &lt;em&gt;rules&lt;/em&gt;. Here's an example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cpp
int     cout &amp;lt;&amp;lt; "I see an integer!" &amp;lt;&amp;lt; endl;


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

&lt;/div&gt;

&lt;p&gt;When an int comes through the lexer and this rule is executed, you will be greeted with a quite obvious "I see an integer!" exclamation. That's not how we'll be using the lexer, but it's useful to see that the code execution is arbitrary: there aren't rules that you have to make some object and return it, it's just regular old code. Can even use more than one line by surrounding it with braces.&lt;/p&gt;

&lt;p&gt;By the way, we'll be using something called &lt;a href="https://github.com/westes/flex" rel="noopener noreferrer"&gt;FLEX&lt;/a&gt; to do our lexing. It makes things pretty easy, but nothing is stopping you from just making a program that does this yourself.&lt;/p&gt;

&lt;p&gt;To get an understanding of how we'll use flex, look at this example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cpp
    // scanner.lex
    /* Definitions */
    %{
      #include &amp;lt;iostream&amp;gt;
      using namespace std;
      extern "C" int yylex();
    %}

    /* Rules next */
    %%
    [0-9]+.[0-9]+ cout &amp;lt;&amp;lt; "FLOAT: (" &amp;lt;&amp;lt; yytext &amp;lt;&amp;lt; ")" &amp;lt;&amp;lt; endl;
    [0-9]+        cout &amp;lt;&amp;lt; "INT: (" &amp;lt;&amp;lt; yytext &amp;lt;&amp;lt; ")" &amp;lt;&amp;lt; endl;
    "+"           cout &amp;lt;&amp;lt; "PLUS" &amp;lt;&amp;lt; endl;
    "-"           cout &amp;lt;&amp;lt; "MINUS" &amp;lt;&amp;lt; endl;
    "*"           cout &amp;lt;&amp;lt; "TIMES" &amp;lt;&amp;lt; endl;
    "/"           cout &amp;lt;&amp;lt; "DIVIDED BY" &amp;lt;&amp;lt; endl;
    ";"           cout &amp;lt;&amp;lt; "SEMICOLON" &amp;lt;&amp;lt; endl;
    [\t\r\n\f]    ; /* ignore whitespace */

    %%
    /* Code */

    int main() {
      yylex();
    }


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

&lt;/div&gt;

&lt;p&gt;This introduces a few new concepts, so let's go over them:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;%%&lt;/code&gt; is used to separate sections of the .lex file. The first section is declarations - basically variables to make the lexer more readable. It's also where you import, surrounded by &lt;code&gt;%{&lt;/code&gt; and &lt;code&gt;%}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Second part is the rules, which we saw before. These are basically a large &lt;code&gt;if&lt;/code&gt; &lt;code&gt;else if&lt;/code&gt; block. It will execute the line with the &lt;em&gt;longest match&lt;/em&gt;. Thus, even if you change the order of the float and int, the floats will still match, as matching 3 characters of &lt;code&gt;3.2&lt;/code&gt; is more than 1 character of &lt;code&gt;3&lt;/code&gt;. Note that if none of these rules are matched, it goes to the default rule, simply printing the character to standard out. You can then use &lt;code&gt;yytext&lt;/code&gt; to refer to what it saw that matched that rule.&lt;/p&gt;

&lt;p&gt;Third part is the code, which is simply C or C++ source code that is run on execution. &lt;code&gt;yylex();&lt;/code&gt; is a function call which runs the lexer. You can also make it read input from a file, but by default it reads from standard input.&lt;/p&gt;

&lt;p&gt;Say you created these two files as &lt;code&gt;source.ect&lt;/code&gt; and &lt;code&gt;scanner.lex&lt;/code&gt;. We can create a C++ program using the &lt;code&gt;flex&lt;/code&gt; command (given you have &lt;code&gt;flex&lt;/code&gt; installed), then compile that down and input our source code to reach our awesome print statements. Let's put this into action!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash
evan:ectlang/ $ flex scanner.lex
evan:ectlang/ $ g++ lex.yy.c -lfl
evan:ectlang/ $ ./a.out &amp;lt; source.ect
INT: (3)
PLUS
FLOAT: (3.2)
SEMICOLON
FLOAT: (5.0)
DIVIDED BY
FLOAT: (1.9)
SEMICOLON
INT: (6)
TIMES
INT: (2)
SEMICOLON
evan:ectlang/ $ 


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

&lt;/div&gt;

&lt;p&gt;Hey, cool! You're just writing C++ code that matches input to rules in order to do something.&lt;/p&gt;

&lt;p&gt;Now, how do compilers use this? Generally, instead of printing something, each rule will return something - a token! These tokens can be defined in the next part of the compiler...&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax Analyzer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  AKA "Making pretty source code usable"
&lt;/h3&gt;

&lt;p&gt;It's time to have fun! Once we get here, we start to define the structure of the program. The parser is just given a stream of tokens, and it has to match elements in this stream in order to make the source code have structure that's usable. To do this, it uses &lt;a href="https://en.wikipedia.org/wiki/Formal_grammar" rel="noopener noreferrer"&gt;grammars&lt;/a&gt;, that thing you probably saw in a theory class or heard your weird friend geeking out about. They're incredibly powerful, and there's so much to go into, but I'll just give what you need to know for our sorta dumb parser.&lt;/p&gt;

&lt;p&gt;Basically, grammars match non-terminal symbols to some combination of terminal and non-terminal symbols. Terminals are leaves of the tree; non-terminals have children. Don't worry about it if that doesn't make sense, the code will probably be more understandable.&lt;/p&gt;

&lt;p&gt;We'll be using a parser generator called &lt;a href="https://www.gnu.org/software/bison/" rel="noopener noreferrer"&gt;Bison&lt;/a&gt;. This time, I'll split the file up into sections for explanation purposes. First, the declarations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cpp
    // parser.y
    %{
      #include &amp;lt;iostream&amp;gt;
      using namespace std;
      extern "C" void yyerror(char *s);
      extern "C" int yyparse();
    %}

    %union{
      int intVal;
      float floatVal;
    }

    %start program

    %token &amp;lt;intVal&amp;gt; INTEGER_LITERAL
    %token &amp;lt;floatVal&amp;gt; FLOAT_LITERAL
    %token SEMI
    %type &amp;lt;floatVal&amp;gt; exp
    %type &amp;lt;floatVal&amp;gt; statement
    %left PLUS MINUS
    %left MULT DIV


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

&lt;/div&gt;

&lt;p&gt;The first part should look familiar: we're importing stuff that we want to use. After that it gets a little more tricky.&lt;/p&gt;

&lt;p&gt;The union is a mapping of a "real" C++ type to what we're going to call it throughout this program. So, when we see &lt;code&gt;intVal&lt;/code&gt;, you can replace that in your head with &lt;code&gt;int&lt;/code&gt;, and when we see &lt;code&gt;floatVal&lt;/code&gt;, you can replace that in your head with &lt;code&gt;float&lt;/code&gt;. You'll see why later.&lt;/p&gt;

&lt;p&gt;Next we get to the symbols. You can divide these in your head as terminals and non-terminals, like with the grammars we talked about before. Capital letters means terminals, so they don't continue to expand. Lowercase means non-terminals, so they continue to expand. That's just convention.&lt;/p&gt;

&lt;p&gt;Each declaration (starting with &lt;code&gt;%&lt;/code&gt;) declares some symbol. First, we see that we start with a non-terminal &lt;code&gt;program&lt;/code&gt;. Then, we define some tokens. The &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; brackets define the return type: so the &lt;code&gt;INTEGER_LITERAL&lt;/code&gt; terminal returns an &lt;code&gt;intVal&lt;/code&gt;. The &lt;code&gt;SEMI&lt;/code&gt; terminal returns nothing. A similar thing can be done with non-terminals using &lt;code&gt;type&lt;/code&gt;, as can be seen when defining &lt;code&gt;exp&lt;/code&gt; as a non-terminal that returns a &lt;code&gt;floatVal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally we get into precedence. We know PEMDAS, or whatever other acronym you may have learned, which tells you some simple precedence rules: multiplication comes before addition, etc. Now, we declare that here in a weird way. First, lower in the list means higher precedence. Second, you may wonder what the &lt;code&gt;left&lt;/code&gt; means. That's associativity: pretty much, if we have &lt;code&gt;a op b op c&lt;/code&gt;, do &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; go together, or maybe &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;? Most of our operators do the former, where &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; go together first: that's called left associativity. Some operators, like exponentiation, do the opposite: &lt;code&gt;a^b^c&lt;/code&gt; expects that you raise &lt;code&gt;b^c&lt;/code&gt; then &lt;code&gt;a^(b^c)&lt;/code&gt;. However, we won't deal with that. Look at the Bison page if you want more detail.&lt;/p&gt;

&lt;p&gt;Okay I probably bored you enough with declarations, here's the grammar rules:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cpp
    // parser.y
    %%
    program: /* empty */
        | program statement { cout &amp;lt;&amp;lt; "Result: " &amp;lt;&amp;lt; $2 &amp;lt;&amp;lt; endl; }
        ;

    statement: exp SEMI

    exp:
        INTEGER_LITERAL { $$ = $1; }
        | FLOAT_LITERAL { $$ = $1; }
        | exp PLUS exp  { $$ = $1 + $3; }
        | exp MINUS exp { $$ = $1 - $3; }
        | exp MULT exp  { $$ = $1 * $3; }
        | exp DIV exp   { $$ = $1 / $3; }
        ;


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

&lt;/div&gt;

&lt;p&gt;This is the grammar we were talking about before. If you're not familiar with grammars, it's pretty simple: the left hand side can turn into any of the things on the right hand side, separated with &lt;code&gt;|&lt;/code&gt; (logical &lt;code&gt;or&lt;/code&gt;). If it can go down multiple paths, that's a no-no, we call that an &lt;em&gt;ambiguous grammar&lt;/em&gt;. This isn't ambiguous because of our precedence declarations - if we change it so that plus is no longer left associative but instead is declared as a &lt;code&gt;token&lt;/code&gt; like &lt;code&gt;SEMI&lt;/code&gt;, we see that we get a shift/reduce conflict. Want to know more? Look up how Bison works, hint, it uses an LR parsing algorithm.&lt;/p&gt;

&lt;p&gt;Okay, so &lt;code&gt;exp&lt;/code&gt; can become one of those cases: an &lt;code&gt;INTEGER_LITERAL&lt;/code&gt;, a &lt;code&gt;FLOAT_LITERAL&lt;/code&gt;, etc. Note it's also recursive, so &lt;code&gt;exp&lt;/code&gt; can turn into two &lt;code&gt;exp&lt;/code&gt;. This allows us to use complex expressions, like &lt;code&gt;1 + 2 / 3 * 5&lt;/code&gt;. Each &lt;code&gt;exp&lt;/code&gt;, remember, returns a float type.&lt;/p&gt;

&lt;p&gt;What is inside of the brackets is the same as we saw with the lexer: arbitrary C++ code, but with more weird syntactic sugar. In this case, we have special variables prepended with &lt;code&gt;$&lt;/code&gt;. The variable &lt;code&gt;$$&lt;/code&gt; is basically what is returned. &lt;code&gt;$1&lt;/code&gt; is what is returned by the first argument, &lt;code&gt;$2&lt;/code&gt; what is returned by the second, etc. By "argument" I mean parts of the grammar rule: so the rule &lt;code&gt;exp PLUS exp&lt;/code&gt; has argument 1 &lt;code&gt;exp&lt;/code&gt;, argument 2 &lt;code&gt;PLUS&lt;/code&gt;, and argument 3 &lt;code&gt;exp&lt;/code&gt;. So, in our code execution, we add the first expression's result to the third.&lt;/p&gt;

&lt;p&gt;Finally, once it gets back up to the &lt;code&gt;program&lt;/code&gt; non-terminal, it will print the result of the statement. A program, in this case, is a bunch of statements, where statements are an expression followed by a semicolon.&lt;/p&gt;

&lt;p&gt;Now let's write the code part. This is what will actually be run when we go through the parser:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cpp
    // parser.y
    %%
    int main(int argc, char **argv) {
      if (argc &amp;lt; 2) {
        cout &amp;lt;&amp;lt; "Provide a filename to parse!" &amp;lt;&amp;lt; endl;
        exit(1);
      }
      FILE *sourceFile = fopen(argv[1], "r");

      if (!sourceFile) {
        cout &amp;lt;&amp;lt; "Could not open source file " &amp;lt;&amp;lt; argv[1] &amp;lt;&amp;lt; endl;
        exit(1);
      }

      // Sets input for flex to the file instead of standard in
      yyin = sourceFile;
      // Now let's parse it!
      yyparse();
    }

    // Called on error with message s
    void yyerror(char *s) {
      cerr &amp;lt;&amp;lt; s &amp;lt;&amp;lt; endl;
    }


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

&lt;/div&gt;

&lt;p&gt;Okay, this is starting to get interesting. Our main function now reads from a file provided by the first argument instead of from standard in, and we added some error code. It's pretty self explanatory, and comments do a good job of explaining what's going on, so I'll leave it as an exercise to the reader to figure this out. All you need to know is now we're back to the lexer to provide the tokens to the parser! Here is our new lexer:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cpp
    // scanner.lex
    %{
      extern "C" int yylex();
      #include "parser.tab.c"  // Defines the tokens
    %}

    %%
    [0-9]+        { yylval.intVal = atoi(yytext); return INTEGER_LITERAL; }
    [0-9]+.[0-9]+ { yylval.floatVal = atof(yytext); return FLOAT_LITERAL; }
    "+"           { return PLUS; }
    "-"           { return MINUS; }
    "*"           { return MULT; }
    "/"           { return DIV; }
    ";"           { return SEMI; }
    [ \t\r\n\f]   ; /* ignore whitespace */


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

&lt;/div&gt;

&lt;p&gt;Hey, that's actually smaller now! What we see is that instead of printing, we're returning terminal symbols. Some of these, like ints and floats, we're first setting the value before moving on (&lt;code&gt;yylval&lt;/code&gt; is the return value of the terminal symbol). Other than that, it's just giving the parser a stream of terminal tokens to use at its discretion.&lt;/p&gt;

&lt;p&gt;Cool, lets run it then!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash
evan:ectlang/ $ bison parser.y
evan:ectlang/ $ flex scanner.lex
evan:ectlang/ $ g++ lex.yy.c -lfl
evan:ectlang/ $ ./a.out source.ect
Result: 6.2
Result: 2.63158
Result: 12


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

&lt;/div&gt;

&lt;p&gt;There we go - our parser prints the correct values! But this isn't really a compiler, it just runs C++ code that executes what we want. To make a compiler, we want to turn this into machine code. To do that, we need to add a little bit more...&lt;/p&gt;

&lt;h2&gt;
  
  
  Until Next Time...
&lt;/h2&gt;

&lt;p&gt;I'm realizing now that this post will be a lot longer than I imagined, so I figured I'd end this one here. We basically have a working lexer and parser, so it's a good stopping point.&lt;/p&gt;

&lt;p&gt;I've put the source code on &lt;a href="https://github.com/evantypanski/ectlang" rel="noopener noreferrer"&gt;my Github&lt;/a&gt;, if you're curious about seeing the final product. As more posts are released, that repo will see more activity.&lt;/p&gt;

&lt;p&gt;Given our lexer and parser, we can now generate an intermediate representation of our code that can be finally converted into real machine code, and I'll show you exactly how to do it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/evantypanski/writing-a-simple-programming-language-from-scratch-part-2-2522"&gt;Part 2 is up!&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional Resources
&lt;/h3&gt;

&lt;p&gt;If you happen to want more info on anything covered here, I've linked some stuff to get started. I went right over a lot, so this is my chance to show you how to dive into those topics.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flex codebase: &lt;a href="https://github.com/westes/flex" rel="noopener noreferrer"&gt;https://github.com/westes/flex&lt;/a&gt; - the lexical analysis tool we used.&lt;/li&gt;
&lt;li&gt;Bison documentation: &lt;a href="https://www.gnu.org/software/bison/" rel="noopener noreferrer"&gt;https://www.gnu.org/software/bison/&lt;/a&gt; - the parser generator we used. Awesome documentation here.&lt;/li&gt;
&lt;li&gt;LALR parsing: &lt;a href="https://web.cs.dal.ca/%7Esjackson/lalr1.html" rel="noopener noreferrer"&gt;https://web.cs.dal.ca/~sjackson/lalr1.html&lt;/a&gt; - a well made explanation on how LALR(1) parsers (like what Bison generates!) work.&lt;/li&gt;
&lt;li&gt;Resolving parsing conflicts: &lt;a href="http://www.cs.ecu.edu/karl/5220/spr16/Notes/Bottom-up/conflict.html" rel="noopener noreferrer"&gt;http://www.cs.ecu.edu/karl/5220/spr16/Notes/Bottom-up/conflict.html&lt;/a&gt; - how to fix shift/reduce or reduce/reduce conflicts, like the one we saw before.&lt;/li&gt;
&lt;li&gt;Chomsky hierarchy: &lt;a href="https://en.wikipedia.org/wiki/Chomsky_hierarchy" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Chomsky_hierarchy&lt;/a&gt; - didn't go much in detail about this, but we were using a context-free grammar so Bison can compile it. If you want context sensitivity, that's for later phases.&lt;/li&gt;
&lt;li&gt;Symbol table: &lt;a href="https://www.tutorialspoint.com/compiler_design/compiler_design_symbol_table.htm" rel="noopener noreferrer"&gt;https://www.tutorialspoint.com/compiler_design/compiler_design_symbol_table.htm&lt;/a&gt; - using symbol tables, how compilers deal with variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Oh, by the way, if you didn't like my phases of a compiler, here's an actual diagram. I still left off the symbol table and error handler. Also note that a lot of diagrams are different from this, but this best demonstrates what we're concerned with.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frseplbvfh4w4nw2z1jh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frseplbvfh4w4nw2z1jh9.png" alt="Real Phases of a Compiler"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>c</category>
      <category>tutorial</category>
      <category>compilers</category>
    </item>
    <item>
      <title>Lessons from My First Internship</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Mon, 13 Aug 2018 16:49:01 +0000</pubDate>
      <link>https://dev.to/evantypanski/lessons-from-my-first-internship-4bfp</link>
      <guid>https://dev.to/evantypanski/lessons-from-my-first-internship-4bfp</guid>
      <description>&lt;p&gt;I'm currently a student at the University of Virginia, having just finished my first internship.  The company has a relatively small engineering department, but one that has been around for a while.  This internship taught me a lot more than I thought it would, ranging from technical experience to what I should focus on.  These are the lessons I learned from one summer away from school.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 1: Learn how to interview
&lt;/h2&gt;

&lt;p&gt;Interviewing went both well and terribly.  The first time, I would say that I did well.  The second time, however, felt terrible.  I struggled to answer any question that had any technical capacity, from data modeling to Rails to Javascript.  After that, I felt like I had no chance.  However, about a month later, I got the offer for an internship, which I essentially accepted on the spot.&lt;/p&gt;

&lt;p&gt;During my time, I got to sit in on two technical interviews.  This was incredibly enlightening.  With no disrespect to those interviewed, I got to see people fail in &lt;em&gt;exactly&lt;/em&gt; the same way I failed.  I also got to see them do far better than I had done.  Then, I got to sit in on the discussion about the candidates, where they seemed to want to attribute as much positivity as they could to the candidates.  Both got job offers and accepted them.  Interviewing wasn't meant to be a test with right or wrong answers; interviewing was a way to see if that person would work well in the company.  A simple distinction, but this is astonishing to someone just starting out in the industry.&lt;/p&gt;

&lt;p&gt;Why is this important?  Because if you can't interview, then nobody can tell if you fit the role or not.  It's not about being able to ace it like a test, it's about being able to effectively communicate what you are and are not knowledgable of.  I failed to give any idea that I might even be okay for the job.  However, I still got an offer.  It still feels like I never deserved the job from my performance on the interview, but I can definitely say that it turned out pretty okay.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 2: Ask questions
&lt;/h2&gt;

&lt;p&gt;I know, everyone says this.  No matter where you go, whether it's a job or internship, people say that you should ask questions.  At the start of my internship, I was suffering pretty severe imposter syndrome, like I was only asked to join them because I knew what Rails was.  I didn't want people to realize that I didn't know very much about Rails, or Javascript for that matter.  My first bit of actual work on the job was secluded; I feared people realizing that I Googled things like how to check if an element is in an array in Javascript.&lt;/p&gt;

&lt;p&gt;This got completely flipped on its head.  I have no idea when or how, but suddenly I decided that I will ask &lt;em&gt;every&lt;/em&gt; question I had.  Now, obviously I consulted Google first and tried to figure it out on my own.  But, if that wasn't helping in a reasonable amount of time, I would ask.  This also goes for pull requests, where I would put comments in my code that asked if there were other, better ways of doing what I wanted to do.  From then on out, I became a &lt;em&gt;far&lt;/em&gt; better developer.  Just like anyone, I got worried that I was asking too many questions.  That shouldn't matter.  I focused on my learning and development, so long as the other engineers were willing to help.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parallel: Don't spend too long on a problem
&lt;/h3&gt;

&lt;p&gt;One small story at about my second week at the internship, I was writing tests for essentially the first time ever.  Large testing frameworks and setups were new to me, but Rails tests were especially new to me.  I was told to write a simple controller test to determine whether or not an element was showing up on a page, so I went through with writing the test and came to a dilemma: I needed to test what an instance variable was assigned to.&lt;/p&gt;

&lt;p&gt;Now, this seems pretty simple to most: spend a few minutes Googling, if you can't figure it out, ask.  But, for some reason I &lt;em&gt;insisted&lt;/em&gt; on figuring it out myself, stressing out and continuing to Google from home.  This made my imposter syndrome feel incredibly real; I couldn't figure out such a simple answer.  After a time too embarassing to tell spent researching, I asked someone.  Turns out there's something that can be used to get any assigned variables.  It's a pretty simple method called &lt;code&gt;assigns&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 3: Everyone is human
&lt;/h2&gt;

&lt;p&gt;It's pretty well known that software developers are (mostly) human.  But, for some reason, I got into the habbit of seeing these people like they're constantly annoyed at the intern.  They just want to sit there and code.  Any time they said "we can pair on that after lunch" just meant "why can't this guy do that?"  The longer I went through my internship the more I realized that &lt;em&gt;nobody&lt;/em&gt; would act like that.  I put myself in their shoes and realized that if an intern came into my company, I would want to help them as much as possible, make them feel as comfortable as possible.  It's not just a nicety to treat others with respect, it's a natural human reaction.&lt;/p&gt;

&lt;p&gt;This realization largely happened when my engineering manager would have one on ones with me, continuously asking for feedback.  Furthermore, he genuinely seemed to care about what I had been doing, how I was feeling, and what I wanted from the internship.  I wasn't just a slave to do the small tickets that nobody wanted to do anymore; I was part of the team, someone to have lunch with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 4: You're important
&lt;/h2&gt;

&lt;p&gt;It's pretty weird to think that some intern is important to the company.  It felt more like a ploy at first to trick people into doing annoying, boring tasks.  What it turned out to be was a lot more complicated.  There's obvious reasons that a company would want to hire an intern, none of which surprise me, such as cheaper labor than a normal employee and fewer strings attached.  However, that's all I thought I was at first.  In reality, you're important to the company even if you are an intern, especially at a smaller tech company.  You work alongside real developers in order to deliver real products.  You're a part of the team.&lt;/p&gt;

&lt;p&gt;This extends beyond an internship as well.  Any company that doesn't treat you like you're important is not worth your time.  Much like how everybody has a story to tell, every person has leverage based on the simple fact that they're human and important.  But, then again, maybe I'm just a young idealist.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;I went into my first internship as a conflicted twenty year old, both with a large ego and massive imposter syndrome.  There was no doubt in my mind that I would work the summer, make some decent money, then move on to the next school year.  I thought that I had no more to learn about the real world.  In retrospect, that seems &lt;em&gt;so stupid&lt;/em&gt;.  The summer of 2018 held a lot for me, from meeting some of the best people to working on cool projects.  I also learned a lot about Rails, Javascript, testing, you name it.  But, what I got most from my internship was the lessons I never thought I needed.  As cliché as it sounds, this cocky 20 year old learned a lot about life from one summer internship.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>development</category>
      <category>internships</category>
      <category>career</category>
    </item>
    <item>
      <title>The Art of a Personal Website</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Sat, 14 Jul 2018 20:01:54 +0000</pubDate>
      <link>https://dev.to/evantypanski/the-art-of-a-personal-website-15h1</link>
      <guid>https://dev.to/evantypanski/the-art-of-a-personal-website-15h1</guid>
      <description>&lt;p&gt;When I first started studying computer science, I felt as if a personal website was for web developers.  That wasn't me, I wasn't about to devote my life to Ruby on Rails, so I left it at that.  About a year later, I started college and realized it's more than that.  Your personal website is far more than a showcase that you can program a website: in fact, my website is made using Jekyll, so all it's showing is html, css, and a tiny bit of Javascript.  Nobody &lt;em&gt;really&lt;/em&gt; cares that I can write some mean html.&lt;/p&gt;

&lt;p&gt;My website uses a very simple system: about once or twice a year, I'll remake it.  This may be making it using a different framework, like switching from Rails to Jekyll, or it may just be a visual overhaul.  Each iteration I simply change the repository name to &lt;code&gt;personal_site_x&lt;/code&gt;, where &lt;code&gt;x&lt;/code&gt; is just the number of my current iteration.  I didn't expect that to go on for more than 2 or 3 times, but I'm now at 4, and definitely looking to keep the trend going.&lt;/p&gt;

&lt;p&gt;The variations are not simply because I stop liking the website.  The variations in my website come about by the understanding that my website isn't just a branding tool, nor a display of my development prowess.  In fact, those are some of the last things I want to show off with my website - I find portfolio sites to be bland, entirely free from personality.  As a developer we have the freedom to push far beyond this, making a website that creates a timeline.  The website is not the important part; the timeline is what's important.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fcepqzispamomgokm6lf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fcepqzispamomgokm6lf4.png" title="Personal Site 1" alt="Personal Site 1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Homepage of iteration 1 of my personal website: &lt;code&gt;personal_site_1&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The development timeline in a personal website is a simple display of what you've become as a developer.  For example, the first iteration of my website was in Rails, when I was very much trying to learn as many skills as possible.  Now, my website uses Jekyll, showing a maturity where I don't need to use an overkill solution in order to fulfill my desires to learn new skills.  My first website in Jekyll used bootstrap, had pretty bad UX, and was bloated just in the html.  My current version uses css grid, better programming practices, and a lot more useful technologies in order to achieve a similar goal.  Each step shows an increase in maturity - it may not affect the end user very much, like the switch from Rails to Jekyll, but it's a way to see myself in the past.&lt;/p&gt;

&lt;p&gt;A timeline then becomes a sandbox with each iteration.  Your personal website is where you have the most freedom as a developer; the greatest ability to do whatever you want.  I didn't get this at first.  My first iteration was a shot at being noticed by employers - what first years had decent personal websites at UVA, much less using a powerful web framework like Rails?  Well, not many, and it may have helped land an internship, but it really didn't get me much more from there.  What the website became was a way to see a new website and say, "Wow, I really want to do something like that!"  So, I did.  That keeps happening, and it should never be stopped.  My website is the sandbox that I can create something just because I thought it'd be cool to build.  It's one of few places I can create that content and push it out for the public eye.&lt;/p&gt;

&lt;p&gt;But, isn't a personal website great for employers and for branding?  It is, I won't deny it.  Especially if you're in web development or UX design, having a personal website is a must.  This branding, however, tends to comes with the territory.  I remade my website recently, largely because I wanted to make the code better, but also because I wanted to make the website more me.  This is also why a personal website works wonders - it can show off who you really are.  My old website was very safe and bland, mostly because I considered myself a safe and bland person.  I'm stepping a little bit out of the box with the most recent iteration, and the next iteration will likely be a lot further out of the box, but we'll have to wait and see for that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F08mj38edhxb4ok1cu3qg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F08mj38edhxb4ok1cu3qg.png" title="Personal Site 3" alt="Personal Site 3"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Homepage of iteration 3 of my personal website: &lt;code&gt;personal_site_3&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I'm no expert, but your personal website shouldn't just be for an employer.  You shouldn't just care about what an employer thinks if they see your website.  I wouldn't say go all out with an obnoxious pink background and unicorns everywhere, even if that is you.  Just put less thought into how an employer would think and more thought into how your website helps you improve as a developer.  Your growth and development through time is far more valuable than a safe site with your resume on it, and that's worth keeping in mind.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>jekyll</category>
      <category>rails</category>
      <category>design</category>
    </item>
    <item>
      <title>A Look At My Notes</title>
      <dc:creator>Evan Typanski</dc:creator>
      <pubDate>Thu, 26 Apr 2018 14:46:24 +0000</pubDate>
      <link>https://dev.to/evantypanski/a-look-at-my-notes-1kg7</link>
      <guid>https://dev.to/evantypanski/a-look-at-my-notes-1kg7</guid>
      <description>&lt;p&gt;Notetaking is a hotly debated topic, with &lt;a href="https://www.npr.org/2016/04/17/474525392/attention-students-put-your-laptops-away" rel="noopener noreferrer"&gt;many saying&lt;/a&gt; that taking notes by hand is simply better than digitally.  This seems to be because the actual process of transcription tends to be more important than rereading the notes.  I don't find that all that surprising - I hardly read back my notes simply because it doesn't seem to benefit me.  I believe many (but not all) feel the same.&lt;/p&gt;

&lt;p&gt;The actual process of writing is far slower than typing.  I'll use myself as an example - I tested myself using &lt;a href="https://10fastfingers.com/text/30765-Simple-Paragraph" rel="noopener noreferrer"&gt;this paragraph&lt;/a&gt; from a random typing speed site and made entirely sure not to try more than once.  When I wrote the paragraph by hand, it took 2 minutes 20 seconds.  Typing it with a fair number of corrections (21 according to the site) took 55 seconds, clocking at 79 words per minute, which is slower than normal.  The takeaway?  My typing speed is over twice as fast as my writing speed.&lt;/p&gt;

&lt;p&gt;That doesn't even account for the messiness of my handwriting.  I write like a five year old - my base line is all over the place and I need to look at the page.  I clench the pencil too hard so it hurts.  Typing, on the other hand, is always neat and comfortable.  Since switching to Dvorak keyboard layout (&lt;a href="https://www.evantypanski.com/posts/on-alternative-keyboard-layouts" rel="noopener noreferrer"&gt;see my other blog post&lt;/a&gt;), I have found that I am much more comfortable typing than with QWERTY.  Basically, I get incredibly tired writing, but not typing.&lt;/p&gt;

&lt;p&gt;We can keep going on how much better typing is because you can relay more information in a neater format.  But, what's the benefit of writing?  I said previously that I don't write my notes to reread them.  I write notes to retain information the second I learn it.  &lt;a href="http://drawingchildrenintoreading.com/assets/the_pen_is_mightier_than_the_keyboard-libre.pdf" rel="noopener noreferrer"&gt;Studies show&lt;/a&gt; that writing notes simply lets you retain more information.  I would likely agree with that finding for most individuals.  The primary reasons seem not to be a product of the laptop, but instead a product of how the individuals use the laptop.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I Take Notes
&lt;/h2&gt;

&lt;p&gt;A short history, I learned LaTeX in Fall 2016 in order to take notes and do homeworks more efficiently in my discrete math class.  This was the beginning of my first year at college.  Before I learned LaTeX, I took notes using my laptop, but generally in simple Microsoft products.  This was incredibly cumbersome for equations and prettiness.  I downloaded &lt;a href="https://miktex.org/" rel="noopener noreferrer"&gt;MikTex&lt;/a&gt; for Windows and struggled through the first month of not knowing how to do practically anything.  By the end of my first year, most of my notes were typed with LaTeX and generally were pretty ok.&lt;/p&gt;

&lt;p&gt;Here's an example of some discrete math that I took my first year.  These notes were often rewritten to make more clear as well (part of how I study):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.evantypanski.com%2Fassets%2Fimg%2Fposts%2Fa-look-at-my-notes%2Fdiscrete.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.evantypanski.com%2Fassets%2Fimg%2Fposts%2Fa-look-at-my-notes%2Fdiscrete.png" title="Discrete Math" alt="Discrete Math"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm definitely not going to claim these are perfect notes, but it certainly provided a good reference as well as forcing me to write down what each law was.  I likely did this after the class that we learned it, but this was very common for my notes: to have every piece of information.&lt;/p&gt;

&lt;p&gt;My more recent notes are different based on the class.  For example, in algorithms, pictures are often very useful, but I'm not about to draw a flow graph and scan it into my notes.  Thus, I just describe it.  The key is, however, I ensure that I learn the content and then write my translation of the lecture.  It's similar to what you have to do when hand writing notes, but I do it voluntarily while typing.  The following is an example of the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.evantypanski.com%2Fassets%2Fimg%2Fposts%2Fa-look-at-my-notes%2Falgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.evantypanski.com%2Fassets%2Fimg%2Fposts%2Fa-look-at-my-notes%2Falgo.png" title="Algorithms" alt="Algo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These notes seem obviously less curated than my discrete math notes - I take that as a sign of maturity.  I've learned that I can't just write everything down.  I could make a table with definitions and have a huge professional notebook that can essentially also be a textbook.  But, I'm not going to look back at these notes that much, so why would I?  I might ensure that my intuition now matches my intuition when I first learned the concepts, but other than that these notes will likely just have their use from my writing them in the first place.  I don't need to make it awesome and pretty - I need to make it easy to do what I need to do.&lt;/p&gt;

&lt;p&gt;The main increase in benefit occured when I switched from using MikTex to using Vim on a Linux environment.  The main reason is that Vim helps me write notes even more efficiently, should I need to change something.  This created a problem because I didn't know how to create a PDF efficiently other than just compiling the notes with &lt;code&gt;pdflatex algo.tex&lt;/code&gt;.  This was fixed when I found &lt;a href="https://github.com/xuhdev/vim-latex-live-preview" rel="noopener noreferrer"&gt;Vim Latex Live Preview&lt;/a&gt;, a Vim plugin that recompiles the PDF document as you type (after a certain amount of inactivity at least).  It doesn't use a ton of computing power and it feels very natural.  This has been what's changed my notetaking from being pretty to being efficient as well.  Combine this with a tiling window manager and this setup is perfect for ensuring your notes serve their purpose, compile correctly, and look how you want them to look.&lt;/p&gt;

&lt;p&gt;I won't go too much into how I organize sections and lecture dates, mostly because that's unimportant.  It's loosely correlated with the textbook in most cases.  But, I will say that taking my notes in LaTeX has made it more likely for me to look back at my notes, as I feel more comfortable grabbing an equation from my notes rather than lecture slides.  This gives me the best of both worlds: so long as I do not get distracted during class and I make an effort to write the notes in my own words, I've found this works for me.  It might not work for everyone, but it definitely works for me.&lt;/p&gt;

</description>
      <category>latex</category>
      <category>productivity</category>
      <category>learning</category>
      <category>notes</category>
    </item>
  </channel>
</rss>
