Skip to content

Instantly share code, notes, and snippets.

@jagt
jagt / iter.lua
Created March 30, 2014 03:49
iterator example. notice the stateless one, which pairs()/ipairs() all do this.
function upto(x)
local k = 0
return function()
if k < x then
local ret = k
k = k+1
return ret
end
end
end
@jagt
jagt / fenv.lua
Created March 28, 2014 16:32
lua5.1 setfenv test.
-- lua51 setfenv tests
function setenv1()
-- 1 is the calling function
setfenv(1, {foo='1', print=_G.print})
print(foo) -- prints 1
end
setenv1()
@jagt
jagt / vvv.cc
Created March 9, 2014 07:23
c++11 examples again
#include <iostream>
#include <exception>
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Vector {
@jagt
jagt / Rainbow.hx
Created January 22, 2014 15:57
trailing rainbow in haxe, using Graphics.drawTriangles
package fake3d;
import fake3d.Rainbow.Segment;
import flash.display.Shape;
import flash.Vector.Vector;
import de.polygonal.ds.ArrayUtil;
/**
* Trailing Rainbow class, reimplemented in haxe.
* Source : https://github.com/grapefrukt/juicy-breakout/blob/0d907c2587b9e292d7441b40045d263e3462d878/src/com/grapefrukt/games/juicy/effects/Rainbow.as
@jagt
jagt / stackalloc.c
Created January 19, 2014 14:59
alloca() for dynamic memory allocation on stack.
// alloca() for dynamic stack memory allocation
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <alloca.h>
struct string {
int cnt;
char chars[1];
};
@jagt
jagt / jmp.c
Created January 19, 2014 13:48
setjmp longjmp minimal example
#include <stdio.h>
#include <setjmp.h>
jmp_buf buf;
void what() {
puts("in what()");
longjmp(buf, 1);
puts("won't be here");
}
@jagt
jagt / expr.py
Created January 18, 2014 18:00
simple math expr parser. pratt parser really rocks.
# simple math expression pratt parser
def lexer(s):
'''token generator'''
ix = 0
while ix < len(s):
if s[ix].isspace(): ix += 1
elif s[ix] in "+-*/()":
yield s[ix]; ix += 1
elif s[ix].isdigit():
@jagt
jagt / arr.c
Created January 13, 2014 16:52
example of mixing pointer extern and array definition.
char arr[] = "abcdefghijklmn";
@jagt
jagt / box.cc
Created January 12, 2014 12:31
simple textured box
#include <cstdio>
#include <cstdlib>
#include <gl/glew.h>
#include <gl/glfw.h>
#include <SOIL.h>
#define SMAP_X(n) ( ((n)%16)*16.0/256.0 )
#define SMAP_Y(n) ( ((n)/16)*16.0/256.0 )
struct SpriteCoord {
float s1, t1, s2, t2;
#!/usr/bin/python3
# pratt parser described below, reimplemented in python 3
# http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
# https://github.com/munificent/bantam
import abc
from io import StringIO
from sys import exit, stdout
from pprint import pprint
from collections import namedtuple