DEV Community

James Carlson
James Carlson

Posted on

Implementing MicroScheme in Elm

I'd like to report on a little weekend project, namely to implement an interpreter for a small subset of Scheme using Elm. Here is a screenshot of the command-line interface:

Scheme Interpreter

The code (open source, of course!) is on Github with some
documentation there also. The code base, as outlined below, is quite small. I intend to keep it that way, since the goal is educational (I am learning a lot). I'll concentrate on a few core features, e.g., a proper implementation of environments and let-bindings.

--------------------------------------------------------
File                                               code
--------------------------------------------------------
src/MicroScheme/Eval.elm                            92
src/MicroScheme/Frame.elm                           64
src/MicroScheme/Parser.elm                          56
src/MicroScheme/Interpreter.elm                     43
src/Main.elm                                        41
src/MicroScheme/Environment.elm                     19
src/repl.js                                         19
src/MicroScheme/Expr.elm                            13
src/MicroScheme/Init.elm                            10
-------------------------------------------------------
SUM:                                               357
-------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

There is more detail in the docs linked above, but perhaps just a few words more. The current state of the data type for Scheme expressions is

type Expr
    = Z Int
    | F Float
    | Str String
    | Sym String
    | L (List Expr)
    | SF SpecialForm


type SpecialForm
    = Define
    | Lambda
    | Display
    | If
Enter fullscreen mode Exit fullscreen mode

I haven't done anything yet with Display and If.

The module Main is used to mediate via ports between the interpreter and the Javascript code that runs the command-line interface you saw above in the screenshot. This set-up is very small: just 43 lines of code for Main and 19 for repl.js.

Plans

When the project has matured sufficiently, I will publish a more fully-featured version of the code as an Elm library. I'm also planning to integrate the interpreter into Scripta.io, a web publishing platform for technical docs. Scripta offers three markup languages: MicroLaTeX, XMarkdown, and L0. The first two are cousins of their official counterparts, while L0 is an experimental effort with syntax inspired by Lisp.

Top comments (0)