Skip to content

Instantly share code, notes, and snippets.

@davidm
davidm / meta-interpolate.lua
Created November 4, 2011 03:42
Lua metaprogramming using custom searcher with string interpolation example
--[[
The following example illustrates string interpolation [1]
by way of a preprocessor that runs as a customer searcher
function in package.loaders.
Tested in Lua 5.2.0-beta.
Depends on
file_slurp https://gist.github.com/1325400 (for loading files simply)
luabalanced https://github.com/davidm/lua-balanced (for Lua parsing)
@davidm
davidm / sourceunpack.lua
Created December 2, 2011 07:35
Lua module 'sourcepack' - Extract Lua files from other Lua files.
#!/usr/bin/env lua
--[[ FILE README.txt
LUA MODULE
sourceunpack v$(_VERSION) - Extract Lua files containing other Lua files.
SYNOPSIS
@davidm
davidm / README.md
Last active December 14, 2015 17:59
checkglobals patch

Detailed Description: checkglobals module + patch

The checkglobals module + patch (from LuaPowerPatches -- Download Patch for Lua 5.1.3 is a hybrid of a compile-time and run-time approach for detecting undefined variables. Consider the following trivial Lua module:

-- multiplybyx.lua
local function multiplybyx(y)
  return y * X                -- is X defined???
end
return multiplybyx
@davidm
davidm / globalsplus.lua
Created March 9, 2013 22:18
globalsplus.lua for DetectingUndefinedVariables
-- globalsplus.lua
-- Like globals.lua in Lua 5.1.4 but records fields in global tables too.
-- Probably works but not well tested. Could be extended even further.
--
-- usage: luac -p -l example.lua | lua globalsplus.lua
--
-- D.Manura, 2010-07, public domain
local function parse(line)
local idx,linenum,opname,arga,argb,extra =
@davidm
davidm / makebuild.lua
Last active December 14, 2015 17:59
Lua makefile generator in < 100 lines of code.
--[[
Simple Makefile generator in Lua 5.1.
With gcc include dependency support.
WARNING: proof of concept; not well tested. GNU style Makefile.
See premake for a more complete implementation.
(c) David Manura, 2013-03. MIT license (same as Lua).
--]]
local M = {}
@davidm
davidm / notes-fd-staircase.txt
Created July 13, 2012 19:34
notes-fd-staircase.txt
One method I've prototyped to reduce effects of finite difference (F.D.) staircase effects (though not really recommended nor used but provided for reference):
(1) Solve the partial differential equation for f in the usual way by F.D.
(2) Measure f and grad(f) at each point about one or two mesh units away from the boundary surface by using data from #1. Use this to update values at boundary condition mesh points. For example, by linear extrpolation or other extrapolation technique.
(3) Repeat #1 until convergence.
Other obvious variations for updating boundary mesh point values are possible, including expressing #1-3 as a single matrix equation to solve (which probably has some equivalence to currently used techniques), thereby avoiding the repetition needed in #3.
@davidm
davidm / hamming_weight_test.lua
Created March 17, 2012 21:05
hamming weight calculation tests
--[[
Correctness and benchmark tests of various hamming weight implementations.
This is also called "popcount".
See also
http://lua-users.org/wiki/HammingWeight
http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer
http://www.dalkescientific.com/writings/diary/archive/2008/07/03/hakmem_and_other_popcounts.html
http://perso.citi.insa-lyon.fr/claurado/ham/overview.pdf
http://chessprogramming.wikispaces.com/Population+Count
David Manura, 2012-03.
@davidm
davidm / memoize.lua
Created March 17, 2012 20:35
memoize.lua
--[[
memoize.lua
http://lua-users.org/wiki/FuncTables
http://www.lua.org/pil/17.1.html
Code is public domain.
D.Manura.
--]]