Last active
February 27, 2019 21:29
-
-
Save pbackus/6ceac8a72797a2ce86b3f5bfaecd605a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dub | |
/+ dub.sdl: | |
authors "Paul Backus" | |
license "MIT" | |
dependency "sumtype" version="~>0.8.0" | |
+/ | |
import sumtype; | |
alias Cell = SumType!(string, This[]); | |
alias Sexpr = Cell[]; | |
string eval(string s) | |
{ | |
return s; | |
} | |
string eval(Sexpr s) | |
{ | |
import std.exception: enforce; | |
enforce(s.length > 0, "Empty sexpr"); | |
string fname = eval(s[0]); | |
auto fp = fname in library; | |
enforce(fp !is null, fname ~ " not in the library"); | |
return (*fp)(s[1..$]); | |
} | |
string eval(Cell c) | |
{ | |
return c.match!eval; | |
} | |
alias Builtin = string function(Cell[]); | |
Builtin[string] library; | |
static this() | |
{ | |
library["cat"] = (args) { | |
string ret; | |
foreach (arg; args) ret ~= eval(arg); | |
return ret; | |
}; | |
} | |
void main() | |
{ | |
import std.stdio; | |
Sexpr prog = [Cell("cat"), Cell("Hello "), Cell("world!")]; | |
writeln(eval(prog)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment