Skip to content

Instantly share code, notes, and snippets.

View leegao's full-sized avatar

Lee Gao leegao

  • Google
  • Jersey City, NJ
View GitHub Profile
#include "ruby.h"
#include "Python.h"
// #include "sre.h"
#include "re.h"
#include "st.h"
#include "rubyio.h"
// #include "structmember.h" -- Clashes with Ruby
#include <string.h>
/*
@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
@leegao
leegao / main.cpp
Created July 7, 2011 20:41
quadtrees
// simple test run
#include "quadtree.h"
int main(){
quadtree<int>* tree = new quadtree(rectangle(0, 0, 100, 100)); // initialize a quadtree occupying the square with size 100
// blah blah tree->insert(integer, x, y);
// we need to get the list of all points in a particular region
std::list<qdatum<int>> list = tree->to_list(region);
@leegao
leegao / ClassReader.java
Created November 20, 2010 04:52
Java bytecode reader prototype
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package classreader;
import java.io.ByteArrayInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.FileInputStream;
@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 / Rationale.md
Created July 9, 2011 02:30
JIT for dummies: JIT compiling RPN in python

If you don't care about the explanation, scroll down to find the code, it's 50 some odd lines and written by someone who doesn't know any better. You have been warned.

What it does

This is a very simple proof of concept jitting RPN calculator implemented in python. Basically, it takes the source code, tokenizes it via whitespace, and asks itself one simple question: am I looking at a number or not?

First, let's talk about the underlying program flow. Pretend that you are a shoe connoisseur with a tiny desk. You may only have two individual shoes on that desk at any one time, but should you ever purchase a new one or get harassed by an unruly shoe salesman without realizing that you have the power to say no (or even maybe?), you can always sweep aside one of the two shoes on the desk (the one on the right, because you're a lefty and you feel that the left side is always superior) onto the messy floor, put the other shoe on the right hand side, and then place your newly acquired shoe in

@leegao
leegao / askreddit.py
Created February 16, 2012 03:35
Ask Reddit bot
__author__ = 'Lee'
import urllib
import json
import time
import re
seen = {}
info_dict = {}
@leegao
leegao / gist:1105982
Created July 26, 2011 04:33
Coffee Protocol: RPC 2324
;;; coffee - Submit a BREW request to an RFC2324-compliant coffee device
;;;
;;; Author: Eric Marsden <emarsden@laas.fr>
;;; Version: 0.2
;;; Copyright: (C) 1999 Eric Marsden
;;; Keywords: coffee, brew, kitchen-sink, can't
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
@leegao
leegao / example.lua
Created July 10, 2011 15:56
Heap based Priority Queue for Lua
-- A priority queue that sorts based on euclidean distance
pq = pqueue(function(point1, point2)
return (point1[1]-point2[1])^2 + (point1[2]-point2[2])^2
end)
for i=1, 20 do
pq:push{math.random(100), math.random(100)}
end
local function dump(list)
local str = "{"
local seen = {}
for _,v in ipairs(list) do
if type(v) ~= "table" then
str = str .. tostring(v) .. ", "
else
str = str .. dump(v) .. ", "
end
seen[_] = true