The code you find here outlines how one might go about writing a kind of typed Lisp in Elm — an educational experiment for myself. The files are
- Parse.elm, exporting one type,
AST
, and one functionparse
. - ParserHelpers.elm, exporting one function,
many
, a clip from the package Punie/parser-extras. Themany
combinator is used in moduleParse
- Lisp.elm, exporting one function,
eval
- Lisp2.elm, a superset of Lisp.elm, also exporting
evalToString
One can evaluate Lisp expressions using elm-repl
or by using the little node repl program
described in the last section.
Examples:
$ elm repl
> import Parse exposing(parse)
> import Lisp2 exposing(eval)
> parse "(+ 1 2 3)"
Just (LIST [Str "+",Num 1,Num 2,Num 3])
: Maybe Parse.AST
> eval "(+ 1 2 3)"
Just (VNum 6) : Maybe Lisp2.Value
> eval "(+ 1 2 haha!)"
Just Undefined : Maybe Lisp2.Value
Note that eval
handles nonsense input in a somewhat graceful way.
To use the repl:
$ elm make --optimize src/Repl/Main.elm --output=src/Repl/main.js
$ node src/Repl/repl.js
Type ':help' for help
> (+ 1 2)
3
The repl program uses the code in ./src/Repl
following the outline
described in the Medium article Elm as BlackBox. The idea is to use the Platform.Worker
program in Repl.Main
to communicate with the
terminal via node.js
.