If at first you don't succeed...

About | Archive

\(\newcommand{\th}{^\mathrm{th}}\) \(\newcommand{\false}{\mathrm{F}}\) \(\newcommand{\true}{\mathrm{T}}\)

Dictp Part 2

OK, so to continue the Dictp saga, some syntactic sugar.

References to local variables.

The parser can be enhanced by rewriting anything that isn’t a built-in command or a string with a lookup to __local__.


  • a \(\mapsto\) __local__['a']
  • 0 = 1 \(\mapsto\) __local__['0'] = __local__['1']
  • 0? \(\mapsto\) __local__['0']?
  • '0' \(\mapsto\) '0'

The factorial I gave in the previous article now looks like:

factorial = __empty__
    = '__if__ \
            [==[n]["0"]] \
            ["\"1\""] \
            ["* \
                [n] \
                [factorial \
factorial['__env__'] = __local__
factorial = __call__[factorial]
four = __empty__
four['n'] = '4'
to_print = __empty__
to_print['__toprint__'] = factorial[four]
# ^ Should print 24, given that "print" is defined

Literal dictionary syntax

Dictionaries can be defined like they are in python; using {a : b, c : d} syntax. The keys do not have to be quoted but the values do. We can also extend this so that within brackets, braces are unecessary; [{a : b, c : d}] is the same as [a : b, c : d]

Rewriting factorial:

factorial = {
    __program__ : '__if__ \
            [==[n]["0"]] \
            ["\"1\""] \
            ["* \
                [n] \
                [factorial \
    __env__ : __local__
factorial = __call__[factorial]
print[__toprint__ : factorial[n : '4']]

Literal program syntax

Normally, when you define code, you want the parent frame to be the frame in which it is defined (lexical scoping). This is accomplished by the use of literal quoting: <CODE> \(\mapsto\) __call__[{__program__ : 'CODE', __env__ : __local__}]. To enable full use of this, let’s say that __if__ can take a function rather than a program for its latter 2 values.

Rewriting again:

factorial = <
print[__toprint__ : factorial[n : '4']]

Note that the <>s basically define a lambda expression, except that the arguments are implicit and can therefore be completely dynamic.

OK, so we now have a working language in which everything is a dictionary. Next time I come back to Dictp, I’ll discuss implementation.

comments powered by Disqus