Skip to content

Instantly share code, notes, and snippets.

@Fingercomp
Fingercomp / format.lua
Last active March 21, 2020 16:04
A slick formatter for the insane
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
@Fingercomp
Fingercomp / dbg.lua
Created May 4, 2020 11:38
A better debug.debug()
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)
@Fingercomp
Fingercomp / nn.lua
Last active September 8, 2022 21:01
OpenComputers Nanomachines control program [Minecraft]
local CONF = "/etc/nn.conf"
local m = require("component").modem
local event = require("event")
local ser = require("serialization")
local fs = require("filesystem")
local unicode = require("unicode")
_G.port = _G.port or 27091
_G.max = _G.max or 15
_G.effects = _G.effects or {}
@Fingercomp
Fingercomp / 00-sound-card-toc.md
Last active April 8, 2023 17:34
Guide to the Sound Card

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:

  1. "C#3" means play the note of C♯3, 1 unit long.
  2. 0 indicates a rest (no note is played).
  3. {"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
@Fingercomp
Fingercomp / pcall-load.md
Created August 7, 2018 12:44
О `pcall` и `load` в Lua

Познакомимся с load. Эта функция берёт первым аргументом строку и компилирует её.

local str = "Тест!"
print(load(str))
--> nil [string "Тест!"]:1: unexpected symbol near '<\208>'

Код выше вернёт странные идеограммы, но если присмотреться, то становится заметно, что именно там происходит.

@Fingercomp
Fingercomp / table-pack.md
Last active January 10, 2024 10:13
О функции `table.pack` и операторе `#` на примере REPL

Lua — прекрасный язык программирования. Прежде всего благодаря своей предельной простоте. Но даже в Lua есть свои нюансы.

Допустим, мы хотим создать свой Lua REPL. REPL — Read–Eval–Print Loop — также называется оболочкой (shell) или интерпретатором (interpreter). Из аббриевиатуры должно быть понятно, что эта прога будет делать:

  1. читать ввод
  2. интерпретировать его
  3. принтить выхлоп

Программа и так несложно выглядит, а в Lua ещё есть функция load, о которой