Skip to content

Instantly share code, notes, and snippets.

@eliben
eliben / gist:1734923
Created February 4, 2012 03:20
AST for parsing Duff's device with pycparser
ileAST:
FuncDef:
Decl: send, [], [], []
FuncDecl:
ParamList:
Decl: to, [], [], []
TypeDecl: to, []
IdentifierType: ['int']
Decl: from, [], [], []
TypeDecl: from, []
@eliben
eliben / factor_server.py
Created May 27, 2012 02:57
factor_server
import multiprocessing
from multiprocessing.managers import SyncManager
import Queue
import time
from factorize import factorize_naive
from eblib.utils import Timer
IP = '192.168.1.102'
PORTNUM = 55444
AUTHKEY = 'shufflin'
@eliben
eliben / gist:2944722
Created June 17, 2012 14:42
Timer utility
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
@eliben
eliben / templatelexer.py
Created August 8, 2012 09:33
initial attempt implementing the go template lexer in Python
from collections import namedtuple
TOK_TEXT = 'TOK_TEXT'
TOK_LEFT_META = 'TOK_LEFT_META'
TOK_RIGHT_META = 'TOK_RIGHT_META'
TOK_DUMMY = 'TOK_DUMMY'
# A token has
@eliben
eliben / gist:3314190
Created August 10, 2012 13:23
ROLE_REGEX for my Python plugin article
# Regex for matching/capturing role text.
# E.g. :name:`text` - first capture group is "name", second group is "text"
#
ROLE_REGEX = re.compile(r':(\w+):`([^`]*)`')
struct SS {
long n1;
long n2;
long n3;
long n4;
};
int foo(struct SS s) {
====> lli
lli
AsmParser
Core
Support
Support
BitReader
Core
Support
IRReader
@eliben
eliben / gist:5797351
Created June 17, 2013 14:36
Generic regex-based lexer in Python
#-------------------------------------------------------------------------------
# lexer.py
#
# A generic regex-based Lexer/tokenizer tool.
# See the if __main__ section in the bottom for an example.
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
# Last modified: August 2010
#-------------------------------------------------------------------------------
@eliben
eliben / gist:6202493
Last active December 20, 2015 22:09
import io
from test.support import import_fresh_module
import csv
csv_other = import_fresh_module('csv', fresh=['_csv', 'csv'])
f = io.StringIO('foo\x00,bar\nbaz,42')
reader = csv.reader(f)
@eliben
eliben / Loop benchmark
Last active December 30, 2015 03:39
C benchmark demonstrating bizarre performance behavior of Intel CPUs
const unsigned N = 400 * 1000 * 1000;
volatile unsigned long long counter = 0;
// Don't inline the benchmarking code into main
void __attribute__((noinline)) tightloop();
void __attribute__((noinline)) loop_with_extra_call();
void tightloop() {
unsigned j;