dsl.lua
· 1.5 KiB · Lua
Originalformat
Playground
local function eval (prog)
local env = {}
for _, fn in ipairs (prog) do
fn (env)
end
end
local function keywords_for (apply)
return function (word_list)
local keywords = {}
for _, word in ipairs (word_list) do
keywords[word] = apply
end
return setmetatable (keywords, {__call = function (t, ...) return apply(...) end})
end
end
local function let (var)
local _let = function (val)
return function (env)
if type (val) == "function" then
val = val (env)
end
env[var] = val
end
end
return keywords_for (_let) { "equal", "be" }
end
local function get (var)
return function (env)
return env[var]
end
end
local function add (x)
local _add = function (y)
return function (env)
if type (x) == "function" then
x = x (env)
end
if type (y) == "function" then
y = y (env)
end
if type (y) == "table" then
print (next(y))
end
return x + y
end
end
return keywords_for (_add) { "to", "with" }
end
local function show (val)
local _show = function (env)
if type (val) == "function" then
val = val (env)
end
print (val)
end
return _show
end
eval {
let "x" .equal (1) ;
let "y" .be (2) ;
let "z" (3) ;
show (add (get "x") .to (get "y")) ;
show (add (get "y") .with (get "z")) ;
}
| 1 | local function eval (prog) |
| 2 | local env = {} |
| 3 | for _, fn in ipairs (prog) do |
| 4 | fn (env) |
| 5 | end |
| 6 | end |
| 7 | |
| 8 | local function keywords_for (apply) |
| 9 | return function (word_list) |
| 10 | local keywords = {} |
| 11 | for _, word in ipairs (word_list) do |
| 12 | keywords[word] = apply |
| 13 | end |
| 14 | return setmetatable (keywords, {__call = function (t, ...) return apply(...) end}) |
| 15 | end |
| 16 | end |
| 17 | |
| 18 | local function let (var) |
| 19 | local _let = function (val) |
| 20 | return function (env) |
| 21 | if type (val) == "function" then |
| 22 | val = val (env) |
| 23 | end |
| 24 | env[var] = val |
| 25 | end |
| 26 | end |
| 27 | return keywords_for (_let) { "equal", "be" } |
| 28 | end |
| 29 | |
| 30 | local function get (var) |
| 31 | return function (env) |
| 32 | return env[var] |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | local function add (x) |
| 37 | local _add = function (y) |
| 38 | return function (env) |
| 39 | if type (x) == "function" then |
| 40 | x = x (env) |
| 41 | end |
| 42 | |
| 43 | if type (y) == "function" then |
| 44 | y = y (env) |
| 45 | end |
| 46 | if type (y) == "table" then |
| 47 | print (next(y)) |
| 48 | end |
| 49 | return x + y |
| 50 | end |
| 51 | end |
| 52 | return keywords_for (_add) { "to", "with" } |
| 53 | end |
| 54 | |
| 55 | local function show (val) |
| 56 | local _show = function (env) |
| 57 | if type (val) == "function" then |
| 58 | val = val (env) |
| 59 | end |
| 60 | |
| 61 | print (val) |
| 62 | end |
| 63 | |
| 64 | return _show |
| 65 | end |
| 66 | |
| 67 | eval { |
| 68 | let "x" .equal (1) ; |
| 69 | let "y" .be (2) ; |
| 70 | let "z" (3) ; |
| 71 | show (add (get "x") .to (get "y")) ; |
| 72 | show (add (get "y") .with (get "z")) ; |
| 73 | } |
| 74 |