This is the translation of my blog posts. You can find them here (in Russian).
Познакомимся с load
. Эта функция берёт первым аргументом строку и компилирует её.
local str = "Тест!"
print(load(str))
--> nil [string "Тест!"]:1: unexpected symbol near '<\208>'
Код выше вернёт странные идеограммы, но если присмотреться, то становится заметно, что именно там происходит.
local function insertNonNil(t, v) | |
if v then | |
v = tostring(v) | |
if #v > 0 then | |
table.insert(t, v) | |
end | |
end | |
end | |
local function traceback(c) |
Lua — прекрасный язык программирования. Прежде всего благодаря своей предельной простоте. Но даже в Lua есть свои нюансы.
Допустим, мы хотим создать свой Lua REPL. REPL — Read–Eval–Print Loop — также называется оболочкой (shell) или интерпретатором (interpreter). Из аббриевиатуры должно быть понятно, что эта прога будет делать:
- читать ввод
- интерпретировать его
- принтить выхлоп
Программа и так несложно выглядит, а в Lua ещё есть функция load, о которой
local function format(arg) | |
local function expand(v) | |
if type(v) == "string" then | |
return v | |
elseif type(v) == "table" then | |
return tostring(v[1]) | |
end | |
error("unsupported type") | |
end |
local function insertNonNil(t, v) | |
if v then | |
v = tostring(v) | |
if #v > 0 then | |
table.insert(t, v) | |
end | |
end | |
end | |
local function backtrace(levelStart, shift) |
This gist contains a program that plays the music defined in tracks
.
The tracks
table stores nested tables — one for each concurrent track.
Each such table defines some track properties like its envelope and volume as
well as the actual notes.
A note can be represented in either of the following forms:
"C#3"
means play the note of C♯3, 1 unit long.0
indicates a rest (no note is played).{"C#3", 4}
is just like"C#3"
but 4 units long instead of the default 1.
#!/usr/bin/env bash | |
set -eu | |
declare -a NOTES | |
NOTES=( | |
G2 A2 D3 | |
G2 A2 D3 | |
G2 B2 D3 |