Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jagt
jagt / gist:5473794
Last active December 16, 2015 17:58
Simple LL(1) lexer and parser for a random data structure
# Simple LL(1) lexer and parser for a random data structure
# Based on description on book Language Implementation Patterns
class Lexer(object):
WS, NAME, STRING, NUM, LBRACK, RBRACK, LBRACE, RBRACE, COMMA, EQUAL, COMMENT,END = range(12);
EOF = '' # StringIO gives '' when nothing to read
def __init__(self, inio):
self.inio = inio
self.consume()
def consume(self):
@jagt
jagt / gist:5559570
Last active December 17, 2015 05:39
LR Parser using PLY
# LR Parser using PLY
from sys import exit
import ply.lex as lex
import ply.yacc as yacc
from pprint import pprint
tokens = (
'NAME',
'STRING',
'NUM',
@jagt
jagt / gist:5563131
Last active April 3, 2020 02:31
PEG parser using parsimonious
# PEG parser using parsimonious
from parsimonious.grammar import Grammar
from pprint import pprint
# MUST USE " - double quote in regex literal or gave very strange error messagas
# comment handling seems very difficult...
grammar = Grammar(
'''
record = name "=" value
name = _ ~"\w+" _
@jagt
jagt / mockapi.py
Created May 21, 2013 17:28
simple mocking rest api with file based storage, using bottle
# simple mocking rest api with file based storage
import json
from datetime import datetime, timedelta
from time import mktime
from bottle import *
tabs = ('users', 'biddings', 'auctions')
for tab in tabs:
globals()[tab] = []

The scenario:

  • We are writing a digital textbook-reading app.
  • Most of the time you have a "basic" license for your textbook, but one (and only one) of your computers can request an "enhanced" license.
  • You can only print from the computer with the enhanced license.

The problem statement:

@jagt
jagt / roguelike-dossier.mkd
Last active July 10, 2020 03:51
roguelike-dossier

Roguelike 到底是啥

讲讲 Roguelike 相关知识

最后更新 2013.5.28

引言

如果你是一位资深游戏玩家,那么最近你肯定在哪里看到过 Roguelike 这个词。Roguelike 这个古老的游戏类型现在又变得很潮流,其中各种要素都在不断的被新游戏借鉴。作为一名就是比你有更多空余时间的高玩,我想在这里介绍一下我了解的 Roguelike 相关的东西。文章很长,但应该会比较有意思。

@jagt
jagt / covering.py
Created June 15, 2013 06:19
covering holes
# http://www.reddit.com/r/dailyprogrammer/comments/1g7gyi/061213_challenge_128_intermediate_covering/
# attemp to use scipy.optimize to do this
import numpy as np
from StringIO import StringIO
from scipy.optimize import minimize
def solve(s):
f = StringIO(s)
data = np.loadtxt(f, dtype=np.uint8, skiprows=1)
n = data.shape[0]
@jagt
jagt / hashmap.cc
Last active December 18, 2015 13:39
stupid c++ hash map implementation
#include <cstddef>
#include <iostream>
#include <string>
#include <functional>
#include <cassert>
using namespace std;
template<class T>
class HashMap
{
@jagt
jagt / cache.cc
Created June 17, 2013 15:47
c++ simple lru cache using map and double linked list
#include <cassert>
#include <iostream>
#include <map>
#include <string>
using namespace std;
template<class K, class V>
class cache
{
private:
@jagt
jagt / smart.cc
Created June 18, 2013 07:09
simple smart ptr
#include <iostream>
using namespace std;
template<class T>
class Smart
{
private:
struct Body
{
T *ptr;