<?xml version="1.0" encoding="UTF-8"?> 
<rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        >
<channel>
  <title>asgaard</title>
  <description>Loljs</description>
  <link>https://blog.asgaard.co.uk/t/loljs</link>
  <lastBuildDate>Wed, 13 May 26 04:19:36 +0000</lastBuildDate>
  <language>en</language>
  <count>3</count>
  <offset>0</offset>
      <item>
    <title>My favourite JavaScript bug</title>
    <link>https://blog.asgaard.co.uk/2013/05/27/my-favourite-javascript-bug</link>
    <pubDate>Mon, 27 May 13 18:41:00 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2013/05/27/my-favourite-javascript-bug</guid>
    <description><![CDATA[
<p>
To be clear, this isn&#039;t a bug *in* JavaScript, it&#039;s a bug in my own code. But one which various properties of JavaScript made it both easy to write and very hard to detect later.
<p>
I am currently writing a LOLCODE interpreter in JavaScript. It&#039;s not overly complex - it just recursively evaluates an AST directly. 
<p>
LOLCODE allows for user defined functions:
<p>
<pre>HOW DUZ I ADD YR NUM1 AN NUM2
  SUM OF NUM1 AN NUM2
OIC

VISIBLE ADD 1 AN 3 MKAY  BTW =&gt; prints 4</pre>
<p>
The AST for the function definition looks something like:
<p>
<pre>{
    &quot;_name&quot;: &quot;FunctionDefinition&quot;,
    &quot;name&quot;: &quot;ADD&quot;,
    &quot;args&quot;: [
        &quot;NUM1&quot;,
        &quot;NUM2&quot;
    ],
    &quot;body&quot;: {
        &quot;_name&quot;: &quot;Body&quot;,
        &quot;lines&quot;: [
            {
                &quot;_name&quot;: &quot;FunctionCall&quot;,
                &quot;name&quot;: &quot;SUM OF&quot;,
                &quot;args&quot;: {
                    &quot;_name&quot;: &quot;A</pre>[...]]]></description>
    <content:encoded><![CDATA[
<p>
To be clear, this isn&#039;t a bug *in* JavaScript, it&#039;s a bug in my own code. But one which various properties of JavaScript made it both easy to write and very hard to detect later.
<p>
I am currently writing a LOLCODE interpreter in JavaScript. It&#039;s not overly complex - it just recursively evaluates an AST directly. 
<p>
LOLCODE allows for user defined functions:
<p>
<pre>HOW DUZ I ADD YR NUM1 AN NUM2
  SUM OF NUM1 AN NUM2
OIC

VISIBLE ADD 1 AN 3 MKAY  BTW =&gt; prints 4</pre>
<p>
The AST for the function definition looks something like:
<p>
<pre>{
    &quot;_name&quot;: &quot;FunctionDefinition&quot;,
    &quot;name&quot;: &quot;ADD&quot;,
    &quot;args&quot;: [
        &quot;NUM1&quot;,
        &quot;NUM2&quot;
    ],
    &quot;body&quot;: {
        &quot;_name&quot;: &quot;Body&quot;,
        &quot;lines&quot;: [
            {
                &quot;_name&quot;: &quot;FunctionCall&quot;,
                &quot;name&quot;: &quot;SUM OF&quot;,
                &quot;args&quot;: {
                    &quot;_name&quot;: &quot;ArgList&quot;,
                    &quot;values&quot;: [
                        {
                            &quot;_name&quot;: &quot;Identifier&quot;,
                            &quot;name&quot;: &quot;NUM1&quot;
                        },
                        {
                            &quot;_name&quot;: &quot;Identifier&quot;,
                            &quot;name&quot;: &quot;NUM2&quot;
                        }
                    ]
                }
            }
        ]
    }
}</pre>
<p>
The easiest way to evaluate this is not to do anything special to compile that function, but just to use the magic of JS to represent the evaluation of the FunctionDefinition node as an interpreter action in its own right, similarly to how we&#039;d evaluate an identifier or any other construct.
<p>
If we can create a function which represents the evaluation of &#039;ADD&#039;, then we have a nice consistency with evaluation of built-in functions, which are implemented natively, like SUM OF, e.g:
<p>
<pre>lol = function() {
    var self = this;

    this.symbols = {
      'SUM OF': function(a, b) { return a + b; }
    }


    var evalFuncDef = function(node) {
        self.symbols[node.name] = function() {
            return self.evaluate(node.body);
        }
    }

    this.evaluate(node) {
       // delegate to appropriate sub function
       if (node._name === 'FunctionDefinition') {
           return evalFuncDef(node);
       }
    }
}</pre>
<p>
I&#039;ve omitted setting up the argument list, figuring out the return value, etc, but the basic point is that both &#039;SUM OF&#039; (a native function) and &#039;ADD&#039; (a user supplied LOLCODE function) both exist in the symbol table in the form of an executable JavaScript function.
<p>
It&#039;s a fairly innocent looking piece of code.
<p>
Except for one thing.
<p>
The interpreter also has the ability to pause the program and evaluate watch-statements, like what you&#039;d find in Firebug or Chrome&#039;s debugger. For various uninteresting reasons<sup>1</sup>, the easiest way to do this is to clone the current symbol table and other scope into another interpreter and execute it there.
<p>
Something interesting happens here.
<p>
In the above code, I&#039;ve used the this/self idiom to get a reference to the current object into a nested function.
<p>
When we clone the symbol table into a different object, the self reference comes across unchanged. What that means is that the second interpreter happily executes any expression you give it correctly, until you supply it with one that tries to invoke a user written function. At this point, the <em>first</em> kicks into action, and continues executing. Imagine how difficult to debug this was - you can trace it all you want, you will see you&#039;re always in the right functions. The key is realising you&#039;ve suddenly switched to the wrong <em>object</em>, which in this case, always has very similar (if not identical) state!
<p>
The solution to this is obvious - don&#039;t rely on self inside the function we create, instead require it to be called with <code>symbols[node.name].call(this, ...)</code>. 
<p>
But the bug itself is admirable in its subtlety.
<br>
____
<br>
1. Mostly to do with keeping track of an awkward asynchronous callback. Call it an &#039;implementation issue&#039;.
<p>
]]></content:encoded>
  </item>
      <item>
    <title>I&#039;m going to do something with loljs</title>
    <link>https://blog.asgaard.co.uk/2013/04/28/i-m-going-to-do-something-with-loljs</link>
    <pubDate>Sun, 28 Apr 13 22:46:10 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2013/04/28/i-m-going-to-do-something-with-loljs</guid>
    <description><![CDATA[
<p>
<a href='http://asgaard.co.uk/misc/loljs/'>loljs</a> is a pretty rubbish LOLCODE to JavaScript translator I hacked together a few years ago before I really knew JavaScript. It really is rubbish, half the time it will crash on bad input and give absolutely no indication that it has done so. But according to traffic stats, people love it.
<p>
It hit the big time (sort of) after a (relatively) popular YouTube channel video used it for a &quot;how to learn programming video&quot;.
<p>
lolcode is not a sensible way to learn to program. But it strikes me that it <em>is</em> a sensible way to learn some things about programming languages. lolcode has the nice pair of properties that it&#039;s powerful enough to express normal programming constructs while being small enough to be able to keep the whole language&#039;s structure in your head at once. It&#039;s also amusing.
<p>
I would like to relaunch loljs as:<ol><li>A more robust compiler/runtime.</li><li>A well featured text editor.</li><li>Something to visualise the relationship between the lolcode and the way a compiler sees it.</li></ol>
<p>
I have <a href='https://github.com/markwatkinson/loljs'>started writ</a>[...]]]></description>
    <content:encoded><![CDATA[
<p>
<a href='http://asgaard.co.uk/misc/loljs/'>loljs</a> is a pretty rubbish LOLCODE to JavaScript translator I hacked together a few years ago before I really knew JavaScript. It really is rubbish, half the time it will crash on bad input and give absolutely no indication that it has done so. But according to traffic stats, people love it.
<p>
It hit the big time (sort of) after a (relatively) popular YouTube channel video used it for a &quot;how to learn programming video&quot;.
<p>
lolcode is not a sensible way to learn to program. But it strikes me that it <em>is</em> a sensible way to learn some things about programming languages. lolcode has the nice pair of properties that it&#039;s powerful enough to express normal programming constructs while being small enough to be able to keep the whole language&#039;s structure in your head at once. It&#039;s also amusing.
<p>
I would like to relaunch loljs as:<ol><li>A more robust compiler/runtime.</li><li>A well featured text editor.</li><li>Something to visualise the relationship between the lolcode and the way a compiler sees it.</li></ol>
<p>
I have <a href='https://github.com/markwatkinson/loljs'>started writing the compiler</a> using <a href='http://zaach.github.io/jison/'>Jison</a>, instead of the rubbish and buggy hand-written parser I had before. I am currently working down the route of interpreting the resulting tree instead of translating it to JS, as this gives a bit more runtime control and better error feedback. It&#039;s also easier and more fun than messing around with creating strings of JavaScript code and having to worry about how safe they are and how we figure out relaying runtime errors back to the user.
<p>
For the text editor I&#039;ll probably use CodeMirror and write a custom mode.
<p>
The third point needs more thought. I like the idea of being able to hover over blocks of code and see some representation of the syntax tree for that fragment. The first version will probably just show some nicely formatted JSON, but I wonder if something clever could be done with Canvas to display a real, explorable tree.
<p>
I am only working on this in my free time, so it might take a while.]]></content:encoded>
  </item>
      <item>
    <title>Popularity is the hallmark of mediocrity</title>
    <link>https://blog.asgaard.co.uk/2012/08/26/popularity-is-the-hallmark-of-mediocrity</link>
    <pubDate>Sun, 26 Aug 12 10:31:09 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2012/08/26/popularity-is-the-hallmark-of-mediocrity</guid>
    <description><![CDATA[
<p>
So said Dr. Niles Crane, quoting an unspecified &quot;great man&quot;.
<p>
I&#039;ve sunk a lot of hours into building various different programs. Partly because I enjoy it, partly due to necessity and partly because sometimes I genuinely believe I&#039;m creating something useful. The latter encompasses a bunch of my projects and with these I aim for painstakingly high quality because I want people to use them. I spend hours making sure code is written and formatted well, making sure unit test coverage is good, and ensuring that APIs are well documented, etc.
<p>
And then we have <a href='http://asgaard.co.uk/misc/loljs' target='_blank'>loljs</a>. loljs is my LOLCODE to JavaScript translator/interpreter. It&#039;s a proper translator with a recursive descent parse, not just a hideous regular expression thing, but it&#039;s still pretty rubbish. Probably its one saving grace in my eyes is that it correctly converts LOLCODE&#039;s prefix notation to JavaScript&#039;s infix notation for operators with arbitrary arity. I wrote it as a &quot;let&#039;s learn JavaScript project&[...]]]></description>
    <content:encoded><![CDATA[
<p>
So said Dr. Niles Crane, quoting an unspecified &quot;great man&quot;.
<p>
I&#039;ve sunk a lot of hours into building various different programs. Partly because I enjoy it, partly due to necessity and partly because sometimes I genuinely believe I&#039;m creating something useful. The latter encompasses a bunch of my projects and with these I aim for painstakingly high quality because I want people to use them. I spend hours making sure code is written and formatted well, making sure unit test coverage is good, and ensuring that APIs are well documented, etc.
<p>
And then we have <a href='http://asgaard.co.uk/misc/loljs' target='_blank'>loljs</a>. loljs is my LOLCODE to JavaScript translator/interpreter. It&#039;s a proper translator with a recursive descent parse, not just a hideous regular expression thing, but it&#039;s still pretty rubbish. Probably its one saving grace in my eyes is that it correctly converts LOLCODE&#039;s prefix notation to JavaScript&#039;s infix notation for operators with arbitrary arity. I wrote it as a &quot;let&#039;s learn JavaScript project&quot; in about two days. It&#039;s very rough, the internals are awful (it&#039;s pretty obvious I didn&#039;t know JavaScript) and it&#039;s not exactly easy to use.
<p>
And yet it&#039;s had literally thousands and thousands of visits. It&#039;s far and away the most successful project I&#039;ve done. Even taking into account professional projects, and I&#039;ve worked on some for a few big names, I think loljs is a pretty strong contendor in the popularity stakes.
<p>
It&#039;s recently been the subject of a <a href='http://www.youtube.com/watch?v=NkmJcgbXkJA&amp;feature=g-all-u' target='_blank'>&quot;learn to program&quot; video on a popular channel on YouTube</a> and according to my primitive stats, it&#039;s had six thousand views in the last 36 hours (not *all* from YouTube). Which is great. My web host is earning their monthly fee. I&#039;m very happy. I just wish you&#039;d all be so interested in the stuff I spent more time doing.]]></content:encoded>
  </item>
  </channel>
</rss>