Why I wrote a book about interpreters
A fun and fairly simple project, with a surprisingly high ratio of usefullness to effort, is to write an interpreter for a concatenative language. Concatenative languages, like FORTH, can do a lot with very limited resources, making them good candidates for embedded systems.<p>If you want to play around with making your own concatenative language, it is actually surprisingly simple. Here is an overview of a step-by-step approach that can take you from a simple calculator to a full language with some optimization that would actually be quite reasonable to use in an embedded system.<p>So let&#x27;s start with the calculator. We are going to have a data stack, and all operations will operate on the stack. We make a &quot;dictionary&quot; whose entries are &quot;words&quot; (basically names of functions). For each word in the dictionary, the dictionary contains a pointer to the function implementing that word.<p>We&#x27;ll need six functions for the calculator: add, sub, mul, div, clr, and print. The words for these will be &quot;+&quot;, &quot;-&quot;, &quot;x&quot;, &quot;&#x2F;&quot;, &quot;clr&quot;, and &quot;print&quot;. So our dictionary looks like this in C:<p><pre><code>    struct DictEntry {
        char * word;
        int (*func)(void);
    } dict[6] = {
        {&quot;+&quot;, add}, {&quot;-&quot;, sub}, {&quot;x&quot;, mul},
        {&quot;&#x2F;&quot;, div}, {&quot;clr&quot;, clr}, {&quot;print&quot;, print}
    };
</code></pre>
We need a main loop, which will be something like this (pseudocode):<p><pre><code>    while true
        token = NextToken()
        if token is in dictionary
            call function from that dict entry
        else if token is a number
            push that number onto the data stack
</code></pre>
Write NextToken, making it read from your terminal and parse into whitespace separated strings, implement add, sub, mul, div, clr, and print, with print printing the top item on the data stack on your terminal, and you&#x27;ve got yourself an RPN calculator. Type &quot;2 3 + 4 5 + x print&quot; and you&#x27;ll get 45.<p>OK, that&#x27;s fine, but we want something we can program. To get to that, we first extend the dictionary a bit. We add a flag to each entry allowing us to mark the entry as either a C code entry or an interpreted entry, and we add a pointer to an array of integers, and we add a count telling the length of that array of integers. When an entry is marked as C code, it means that the function implementing it is written in C, and the &quot;func&quot; field in the dictionary points to the implementing function. When an entry is marked as interpreted, it means that the pointer to an array of integers points to a list of dictionary offsets, and the function is implemented by invoking the functions of the referenced dictionary entries, in order.<p>A dictionary entry now looks something like this:<p><pre><code>    struct DictEntry {
        char * word;
        bool c_flag;
        void (*func)(void);
        int * def;
        int deflen;
    }
</code></pre>
(continued in reply)