Skip to content

Instantly share code, notes, and snippets.

View tylerneylon's full-sized avatar
😆

Tyler Neylon tylerneylon

😆
View GitHub Profile
@tylerneylon
tylerneylon / bye_camels.py
Created September 19, 2014 23:03
Convert camel case to underscores.
#!/usr/local/bin/python3
#
# bye_camels.py from https://github.com/tylerneylon/cstructs
#
"""
Usage:
bye_camels.py <input_file> [<output_file>]
bye_camels.py -i <input_file> <output_file>
Replaces all camel-case identifiers in the input with
@tylerneylon
tylerneylon / call_graph.awk
Created October 7, 2014 01:08
Parse Lua code and draw its call graph.
#!/usr/bin/awk -f
#
# call_graph.awk
#
# Usage:
# ./call_graph.awk my_program.lua | dot -Tpng > call_graph.png
#
# This is a script that generates a visual call graph
# for a Lua file. This script only shows calls made
# to functions defined within the input Lua file; that is,
@tylerneylon
tylerneylon / all_top_sorts.lua
Created October 8, 2014 17:08
Print permutations where certain pairs must be in a given order.
-- all_top_sorts.lua
--
-- Generate all permutations - aka topological sorts - of
-- the set [1, 2, ..., n] such that the permutation has
-- certain pairs in a given order. This can also be thought
-- of as listing all linear orders consistent with a given
-- partial order.
--
-- This code is based on chapter 3 of
-- Donald Knuth's book Literate Programming.
@tylerneylon
tylerneylon / copy.lua
Last active May 18, 2024 16:41
How to deep copy Lua values.
-- copy.lua
--
-- Lua functions of varying complexity to deep copy tables.
--
-- 1. The Problem.
--
-- Here's an example to see why deep copies are useful. Let's
-- say function f receives a table parameter t, and it wants to
@tylerneylon
tylerneylon / json.lua
Last active May 15, 2024 12:57
Pure Lua json library.
--[[ json.lua
A compact pure-Lua JSON library.
The main functions are: json.stringify, json.parse.
## json.stringify:
This expects the following to be true of any tables being encoded:
* They only have string or number keys. Number keys must be represented as
strings in json; this is part of the json spec.
@tylerneylon
tylerneylon / simple_lua_profile.lua
Last active April 13, 2023 11:52
measure Lua code speed
local t_sum = 0
local t_num = 0
local t1, t2, t3
local do_time = true
local function mark1()
if not do_time then return end
t1 = os.clock()
end
@tylerneylon
tylerneylon / lgraph
Created December 17, 2014 22:12
Graph functions in your terminal.
#!/usr/local/bin/lua
usage_str = [[
Usage:
lgraph 'function of x'
Example:
lgraph 'x^2'
]]
Grapher = {xmin = -1, xmax = 1, ymin = -1, ymax = 1, ncols = 160, nrows = 50}
@tylerneylon
tylerneylon / time_lock_inj.lua
Created April 9, 2015 20:12
Tool to isolate bottleneck mutex locks in C-family code.
-- time_lock_inj.lua
--
local usage_str = [[
Usage:
lua time_lock_inj.lua <list_of_input_source_files>
Injects lock timing code for all the given files.
This assumes you're locking things with calls to
@tylerneylon
tylerneylon / keylog.c
Created July 2, 2015 21:59
log keyboard input to any shell command
// keylog.c
//
// Usage:
// ./keylog [command ...]
//
// This runs the given command, capturing all keyboard input in the file
// "outfile." This is currently written for mac os x. I'm guessing slight edits
// would allow it to work on other *nix systems as well.
//
// I learned how to do this by looking at the source of the "script" binary,
--[[ Summary: Below, "entity" and "item" may be different types.
They simply implement same-name methods.
--]]
-- A bit from entities.lua:
function entities.runloop(dt)
for _, entity in pairs(all_entities) do
entity:update_and_draw(dt)
end