prism/examples/prism-lua.html

80 lines
1.5 KiB
HTML

<h2>Comments</h2>
<pre><code>#!/usr/local/bin/lua
--
-- Single line comment
--[[ Multi line
comment ]]
--[====[ Multi line
comment ]====]</code></pre>
<h2>Strings</h2>
<pre><code>""
"Foo\"bar"
"Foo\
bar \z
baz"
''
'Foo\'bar'
'Foo\
bar \z
baz'
[[Multi "line"
string]]
[==[Multi [["line"]]
string]==]</code></pre>
<h2>Numbers</h2>
<pre><code>3
345
0xff
0xBEBADA
3, 3., 3.1, .3,
3e12, 3.e-41, 3.1E+1, .3e1
0x0.1E
0xA23p-4
0X1.921FB54442D18P+1</code></pre>
<h2>Full example</h2>
<pre><code>function To_Functable(t, fn)
return setmetatable(t,
{
__index = function(t, k) return fn(k) end,
__call = function(t, k) return t[k] end
})
end
-- Functable bottles of beer implementation
spell_out = {
"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
[0] = "No more",
[-1] = "Lots more"
}
spell_out = To_Functable(spell_out, function(i) return i end)
bottles = To_Functable({"Just one bottle of beer"},
function(i)
return spell_out(i) .. " bottles of beer"
end)
function line1(i)
return bottles(i) .. " on the wall, " .. bottles(i) .. "\n"
end
line2 = To_Functable({[0] = "Go to the store, Buy some more,\n"},
function(i)
return "Take one down and pass it around,\n"
end)
function line3(i)
return bottles(i) .. " on the wall.\n"
end
function song(n)
for i = n, 0, -1 do
io.write(line1(i), line2(i), line3(i - 1), "\n")
end
end</code></pre>