dsl.lua
· 1.5 KiB · Lua
Eredeti
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 |
dsl2.lua
· 2.8 KiB · Lua
Eredeti
Playground
local function eval (prog, env)
local env = env or {}
local last
for _, fn in ipairs (prog) do
last = fn (env)
end
return last
end
local function force (env)
return function (x)
if type (x) == "function" then
x = x (env)
end
return x
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 default (apply)
return function (operations)
return setmetatable (operations, {__call = function (t, ...)
return apply (...)
end})
end
end
local function let (var)
local _let = function (val)
return function (env)
env[force (env) (var)] = force (env) (val)
end
end
return keywords_for (_let) { "equal", "be" }
end
local function get (var)
return function (env)
return env[force (env) (var)]
end
end
local function add (x)
local _add = function (y)
return function (env)
return force (env) (x) + force (env) (y)
end
end
return keywords_for (_add) { "to", "with" }
end
local function show (val)
local _show = function (env)
print (force (env) (val))
end
return _show
end
local function is (a)
local _equals = function (b)
return function (env)
return force (env) (a) == force (env) (b)
end
end
return default (_equals) {
greater_than = function (b)
return function (env)
return force (env) (a) > force (env) (b)
end
end,
less_than = function (b)
return function (env)
return force (env) (a) < force (env) (b)
end
end,
}
end
local function when (cond)
local _when = function (when_true)
local _when_false = function (when_false)
return function (env)
if force (env) (cond) then
return eval (when_true, env)
else
return eval (when_false, env)
end
end
end
return keywords_for (_when_false) { "otherwise" }
end
return _when
end
local function rand ()
return math.random ()
end
local function return_with (value)
return function (env)
return value
end
end
math.randomseed (os.time ())
eval {
show (
when (is (rand) .greater_than (0.5)) {
show "Wow! The value was..." ;
return_with "High :>" ;
} .otherwise {
show "Damn, the value was..." ;
return_with "Low :<" ;
}
)
}
| 1 | local function eval (prog, env) |
| 2 | local env = env or {} |
| 3 | local last |
| 4 | for _, fn in ipairs (prog) do |
| 5 | last = fn (env) |
| 6 | end |
| 7 | return last |
| 8 | end |
| 9 | |
| 10 | local function force (env) |
| 11 | return function (x) |
| 12 | if type (x) == "function" then |
| 13 | x = x (env) |
| 14 | end |
| 15 | return x |
| 16 | end |
| 17 | end |
| 18 | |
| 19 | local function keywords_for (apply) |
| 20 | return function (word_list) |
| 21 | local keywords = {} |
| 22 | for _, word in ipairs (word_list) do |
| 23 | keywords[word] = apply |
| 24 | end |
| 25 | return setmetatable (keywords, {__call = function (t, ...) |
| 26 | return apply(...) |
| 27 | end}) |
| 28 | end |
| 29 | end |
| 30 | |
| 31 | local function default (apply) |
| 32 | return function (operations) |
| 33 | return setmetatable (operations, {__call = function (t, ...) |
| 34 | return apply (...) |
| 35 | end}) |
| 36 | end |
| 37 | end |
| 38 | |
| 39 | local function let (var) |
| 40 | local _let = function (val) |
| 41 | return function (env) |
| 42 | env[force (env) (var)] = force (env) (val) |
| 43 | end |
| 44 | end |
| 45 | return keywords_for (_let) { "equal", "be" } |
| 46 | end |
| 47 | |
| 48 | local function get (var) |
| 49 | return function (env) |
| 50 | return env[force (env) (var)] |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | local function add (x) |
| 55 | local _add = function (y) |
| 56 | return function (env) |
| 57 | return force (env) (x) + force (env) (y) |
| 58 | end |
| 59 | end |
| 60 | return keywords_for (_add) { "to", "with" } |
| 61 | end |
| 62 | |
| 63 | local function show (val) |
| 64 | local _show = function (env) |
| 65 | print (force (env) (val)) |
| 66 | end |
| 67 | |
| 68 | return _show |
| 69 | end |
| 70 | |
| 71 | local function is (a) |
| 72 | local _equals = function (b) |
| 73 | return function (env) |
| 74 | return force (env) (a) == force (env) (b) |
| 75 | end |
| 76 | end |
| 77 | |
| 78 | return default (_equals) { |
| 79 | greater_than = function (b) |
| 80 | return function (env) |
| 81 | return force (env) (a) > force (env) (b) |
| 82 | end |
| 83 | end, |
| 84 | less_than = function (b) |
| 85 | return function (env) |
| 86 | return force (env) (a) < force (env) (b) |
| 87 | end |
| 88 | end, |
| 89 | } |
| 90 | end |
| 91 | |
| 92 | local function when (cond) |
| 93 | local _when = function (when_true) |
| 94 | local _when_false = function (when_false) |
| 95 | return function (env) |
| 96 | if force (env) (cond) then |
| 97 | return eval (when_true, env) |
| 98 | else |
| 99 | return eval (when_false, env) |
| 100 | end |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | return keywords_for (_when_false) { "otherwise" } |
| 105 | end |
| 106 | return _when |
| 107 | end |
| 108 | |
| 109 | local function rand () |
| 110 | return math.random () |
| 111 | end |
| 112 | |
| 113 | local function return_with (value) |
| 114 | return function (env) |
| 115 | return value |
| 116 | end |
| 117 | end |
| 118 | |
| 119 | math.randomseed (os.time ()) |
| 120 | eval { |
| 121 | show ( |
| 122 | when (is (rand) .greater_than (0.5)) { |
| 123 | show "Wow! The value was..." ; |
| 124 | return_with "High :>" ; |
| 125 | } .otherwise { |
| 126 | show "Damn, the value was..." ; |
| 127 | return_with "Low :<" ; |
| 128 | } |
| 129 | ) |
| 130 | } |
| 131 |