| 1 | <h2>Comments</h2> |
| 2 | <pre><code>#!/usr/local/bin/lua |
| 3 | -- |
| 4 | -- Single line comment |
| 5 | --[[ Multi line |
| 6 | comment ]] |
| 7 | --[====[ Multi line |
| 8 | comment ]====]</code></pre> |
| 9 | |
| 10 | <h2>Strings</h2> |
| 11 | <pre><code>"" |
| 12 | "Foo\"bar" |
| 13 | "Foo\ |
| 14 | bar \z |
| 15 | baz" |
| 16 | '' |
| 17 | 'Foo\'bar' |
| 18 | 'Foo\ |
| 19 | bar \z |
| 20 | baz' |
| 21 | [[Multi "line" |
| 22 | string]] |
| 23 | [==[Multi [["line"]] |
| 24 | string]==]</code></pre> |
| 25 | |
| 26 | <h2>Numbers</h2> |
| 27 | <pre><code>3 |
| 28 | 345 |
| 29 | 0xff |
| 30 | 0xBEBADA |
| 31 | 3, 3., 3.1, .3, |
| 32 | 3e12, 3.e-41, 3.1E+1, .3e1 |
| 33 | 0x0.1E |
| 34 | 0xA23p-4 |
| 35 | 0X1.921FB54442D18P+1</code></pre> |
| 36 | |
| 37 | <h2>Full example</h2> |
| 38 | <pre><code>function To_Functable(t, fn) |
| 39 | return setmetatable(t, |
| 40 | { |
| 41 | __index = function(t, k) return fn(k) end, |
| 42 | __call = function(t, k) return t[k] end |
| 43 | }) |
| 44 | end |
| 45 | |
| 46 | -- Functable bottles of beer implementation |
| 47 | |
| 48 | spell_out = { |
| 49 | "One", "Two", "Three", "Four", "Five", |
| 50 | "Six", "Seven", "Eight", "Nine", "Ten", |
| 51 | [0] = "No more", |
| 52 | [-1] = "Lots more" |
| 53 | } |
| 54 | |
| 55 | spell_out = To_Functable(spell_out, function(i) return i end) |
| 56 | |
| 57 | bottles = To_Functable({"Just one bottle of beer"}, |
| 58 | function(i) |
| 59 | return spell_out(i) .. " bottles of beer" |
| 60 | end) |
| 61 | |
| 62 | function line1(i) |
| 63 | return bottles(i) .. " on the wall, " .. bottles(i) .. "\n" |
| 64 | end |
| 65 | |
| 66 | line2 = To_Functable({[0] = "Go to the store, Buy some more,\n"}, |
| 67 | function(i) |
| 68 | return "Take one down and pass it around,\n" |
| 69 | end) |
| 70 | |
| 71 | function line3(i) |
| 72 | return bottles(i) .. " on the wall.\n" |
| 73 | end |
| 74 | |
| 75 | function song(n) |
| 76 | for i = n, 0, -1 do |
| 77 | io.write(line1(i), line2(i), line3(i - 1), "\n") |
| 78 | end |
| 79 | end</code></pre> |