Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/S-expressions/Lua/s-expressions-1.lua
Normal file
27
Task/S-expressions/Lua/s-expressions-1.lua
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
lpeg = require 'lpeg' -- see http://www.inf.puc-rio.br/~roberto/lpeg/
|
||||
|
||||
imports = 'P R S C V match'
|
||||
for w in imports:gmatch('%a+') do _G[w] = lpeg[w] end -- make e.g. 'lpeg.P' function available as 'P'
|
||||
|
||||
function tosymbol(s) return s end
|
||||
function tolist(x, ...) return {...} end -- ignore the first capture, the whole sexpr
|
||||
|
||||
ws = S' \t\n'^0 -- whitespace, 0 or more
|
||||
|
||||
digits = R'09'^1 -- digits, 1 or more
|
||||
Tnumber = C(digits * (P'.' * digits)^-1) * ws / tonumber -- ^-1 => at most 1
|
||||
|
||||
Tstring = C(P'"' * (P(1) - P'"')^0 * P'"') * ws
|
||||
|
||||
sep = S'()" \t\n'
|
||||
symstart = (P(1) - (R'09' + sep))
|
||||
symchar = (P(1) - sep)
|
||||
Tsymbol = C(symstart * symchar^0) * ws / tosymbol
|
||||
|
||||
atom = Tnumber + Tstring + Tsymbol
|
||||
lpar = P'(' * ws
|
||||
rpar = P')' * ws
|
||||
sexpr = P{ -- defines a recursive pattern
|
||||
'S';
|
||||
S = ws * lpar * C((atom + V'S')^0) * rpar / tolist
|
||||
}
|
||||
24
Task/S-expressions/Lua/s-expressions-2.lua
Normal file
24
Task/S-expressions/Lua/s-expressions-2.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
eg_input = [[
|
||||
((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
]]
|
||||
|
||||
eg_produced = match(sexpr, eg_input)
|
||||
|
||||
eg_expected = { -- expected Lua data structure of the reader (lpeg.match)
|
||||
{'data', '"quoted data"', 123, 4.5},
|
||||
{'data', {'!@#', {4.5}, '"(more"', '"data)"'}}
|
||||
}
|
||||
|
||||
function check(produced, expected)
|
||||
assert(type(produced) == type(expected))
|
||||
if type(expected) == 'table' then -- i.e. a list
|
||||
assert(#produced == #expected)
|
||||
for i = 1, #expected do check(produced[i], expected[i]) end
|
||||
else
|
||||
assert(produced == expected)
|
||||
end
|
||||
end
|
||||
|
||||
check(eg_produced, eg_expected)
|
||||
print("checks out!") -- won't get here if any <i>check()</i> assertion fails
|
||||
26
Task/S-expressions/Lua/s-expressions-3.lua
Normal file
26
Task/S-expressions/Lua/s-expressions-3.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
function pprint(expr, indent)
|
||||
local function prindent(fmt, expr)
|
||||
io.write(indent) -- no line break
|
||||
print(string.format(fmt, expr))
|
||||
end
|
||||
if type(expr) == 'table' then
|
||||
if #expr == 0 then
|
||||
prindent('()')
|
||||
else
|
||||
prindent('(')
|
||||
local indentmore = ' ' .. indent
|
||||
for i= 1,#expr do pprint(expr[i], indentmore) end
|
||||
prindent(')')
|
||||
end
|
||||
elseif type(expr) == 'string' then
|
||||
if expr:sub(1,1) == '"' then
|
||||
prindent("%q", expr:sub(2,-2)) -- print as a Lua string
|
||||
else
|
||||
prindent("%s", expr) -- print as a symbol
|
||||
end
|
||||
else
|
||||
prindent("%s", expr)
|
||||
end
|
||||
end
|
||||
|
||||
pprint(eg_expected, '')
|
||||
Loading…
Add table
Add a link
Reference in a new issue