Skip to content

Instantly share code, notes, and snippets.

View ramunas's full-sized avatar

Ramūnas Forsberg Gutkovas ramunas

View GitHub Profile
from functools import namedtuple
from pprint import pprint
class ParseError(BaseException): pass
def recursive_descent_matcher(rules, start, index, tokens, debug=False):
def iseof(tokens, idx):
try:
x = tokens[idx]
from functools import namedtuple
from pprint import pprint
class ParseError(BaseException): pass
def recursive_descent_matcher(rules, start, index, tokens, debug=False):
def iseof(tokens, idx):
try:
x = tokens[idx]
@ramunas
ramunas / cnf.py
Created December 19, 2019 21:21
CNF
bneg = lambda x: ('~', x)
band = lambda x, y: ('and', x, y)
bor = lambda x, y: ('or', x, y)
imp = lambda x, y: bor(bneg(x), y)
iff = lambda x, y: band(imp(x,y), imp(y,x))
v = lambda x: ('var', x)
b0 = lambda x: band(x, bneg(x))
#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
using namespace std;
template<typename T>
class channel
{
@ramunas
ramunas / fsm.cpp
Last active November 18, 2019 22:56
template <typename State, typename ...Trans> struct FSMRunner;
template <typename State, typename T, typename ...Ts> struct FSMRunner<State, T, Ts...> {
inline static void step(State &state) {
if (T::from == state && T::precond()) {
T::action();
state = T::to;
} else {
FSMRunner<State, Ts...>::step(state);
}
@ramunas
ramunas / bits.cpp
Last active November 9, 2019 20:17
#include <cstdint>
#include <iostream>
using namespace std;
/*
* Returns the bits in range msb to lsb from bits. The returned bits are
* shifted to the least signifcant bit. The bits are ordered from most to
* least signifcant bit; where the most signifcant bit has the index <number of
* bits> - 1 and the least has 0.
*
@ramunas
ramunas / sum.cpp
Last active February 22, 2023 13:20
Sum/Either type in C++ 11
#include <iostream>
#include <functional>
using namespace std;
template <typename A, typename B>
class Either {
private:
// In a language with lambdas, a disjoint union can be implemented using