Skip to content

Instantly share code, notes, and snippets.

View leegao's full-sized avatar
💭
Backpacking the 🌍

Lee Gao leegao

💭
Backpacking the 🌍
View GitHub Profile
@leegao
leegao / check.ml
Created November 25, 2012 00:45
Simple Constraint/Unification Type Inference engine for STLC
open Ast
exception TypeError
exception UnificationError
(* ---------- EQUALITY ---------- *)
(* [typ_eq t1 t2] returns [true] iff [t1] and [t2] are equal *)
let rec typ_eq (t1:typ) (t2:typ) : bool =
match t1,t2 with
def pthing(title,ext,n):
overlay = '''
<GroundOverlay>
<name>res</name>
<TimeSpan>
<begin>%s</begin>
<end>%s</end>
</TimeSpan>
<Icon>
@leegao
leegao / swinger.m
Created October 25, 2012 01:37
Swinger
function swinger(x0,y0,m1,m2,l1,l2)
% SWINGER Classic double pendulum.
% SWINGER(x,y) starts the pendulum at the given initial position.
% The initial position can reset with the mouse.
%
% SWINGER with no arguments starts at (x,y) = (0.862,-0.994).
% What is interesting about the resulting orbit?
%
% SWINGER(0,2) starts in an unstable vertical position.
% How long does it stay there and what causes it to move?
@leegao
leegao / hotpatch.c
Created October 22, 2012 22:10
Context-aware Hot Patching
#include <windows.h>
#include <stdio.h>
//68 BE BA FE CA
const unsigned char OP_PUSH = 0x68;
const unsigned char OP_JMP = 0xE9;
static DWORD rets[10000];
static PDWORD r = NULL;
struct patchwork{
@leegao
leegao / vcgen.ml
Created September 28, 2012 16:14
Verification Conditions generator for a simple imperative language using Z3 proover
open Ast
(* lexp_of_aexp: convert an aexp into an lexp *)
let rec lexp_of_aexp (a:aexp) : lexp =
match a with
| Int n ->
LInt n
| Var x ->
LPVar x
| Plus(a1,a2) ->
@leegao
leegao / scrolling.html
Created September 15, 2012 23:59
FANCY AS FUCK
<html>
<head>
<title>Scrolling Cards</title>
<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("bottom", "1px");// Math.max(0, (($(window).height() - this.outerHeight()) / 2) +
// $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) +
import json, urllib2
def perma(o):
return "http://www.reddit.com/r/%s/comments/%s/lol/%s?context=3"%(o['subreddit'],o['link_id'].replace('t3_',''),o['id'])
def q():
req = urllib.Request("http://reddit.com/r/askreddit/comments/.json", headers={"User-Agent":"Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"})
return json.loads(urllib2.urlopen(req).read())
def find(listing, func):
@leegao
leegao / encode-decode.ml
Created July 9, 2012 15:45
Huffman Tree
(* This contains the meat of the algorithm *)
(* Returns a list of all encodings stored in hm *)
let get_codes (hm : huffmantree) : encoding list =
let rec fold_tree tree enc acc =
match tree with
| _, Leaf(c) -> (c, List.rev(enc)) :: acc
| _, Node(l, r) ->
let a = (fold_tree r (1::enc) acc) in
(fold_tree l (0::enc) a) in
@leegao
leegao / overload.lua
Created June 18, 2012 20:08
Lua global function overload
-- figure out the number of arguments of a lua function
function num_args(f)
local ok, t = pcall(string.dump,f)
if not ok then return nil end
local o = t:byte(13)+t:byte(14)*0x100+t:byte(15)*0x10000+t:byte(16)*0x1000000
return t:sub(26+o):byte()
end
local __Gmt = getmetatable(_G) or {}
local __o_index = __Gmt.__index
@leegao
leegao / lop.h
Created June 12, 2012 17:10
Lua Bytecode
// from lopcodes.h
/*
** R(x) - register
** Kst(x) - constant (in constant table)
** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
*/
// Here, x can be three things
// x can be a variable denoting a register (its 8th bit is 0), in which case R(x) returns the value of the x-th register