Skip to content

Instantly share code, notes, and snippets.

View jdrew1303's full-sized avatar
probably drinking coffee

James Drew jdrew1303

probably drinking coffee
View GitHub Profile
@jdrew1303
jdrew1303 / search.peg
Created November 26, 2017 03:59 — forked from joemfb/search.peg
PEG.js search grammar
{
function joinNested(x) {
return x.map(function(y) {
return Array.isArray(y) ? joinNested(y) : y
}).join('')
}
}
// match all rules, consuming the entire input
root = a:all EOF { return a }
@jdrew1303
jdrew1303 / gist:8b7885a6bf7bcfd7eb1d94cc95485e26
Created November 26, 2017 03:58 — forked from dmajda/gist:1926638
Semantic predicates and label visibility in PEG.js
/*
* Task: Parse even numbers in PEG.js (http://pegjs.majda.cz).
*
* Solution: Let's parse all numbers and reject odd ones using a semantic
* predicate -- an arbitrary piece of code that returns true (meaning "continue
* parsing") or false (meaning "halt parsing").
*
* This solution wouldn't work before commit a2af1fe612 because predicates
* didn't have access to labeled expressions in the grammar as variables
* (without ugly workarounds). But they have the access now and the solution
@jdrew1303
jdrew1303 / number.js
Created November 26, 2017 03:57 — forked from bekroogle/number.js
PEG.js grammar for testing with Ace annotations
/*
* PEG.js grammar for evaluating numbers
* Modified from Majda's JSON parser example
*/
number
= minus? int frac? exp? {return parseFloat(text());}
decimal_point = "."
digit1_9 = [1-9]
@jdrew1303
jdrew1303 / peg.js
Created November 26, 2017 03:56 — forked from JohnEarnest/peg.js
A compact set of PEG parser combinators in ES6.
// PEG parser combinators
const lookup = x => typeof x == 'string' ? eval(x) : x
const match = (x, y, z) => x ? { c:y, v:z } : { e: true }
const eof = s => match(s.length, 0, '')
const char = s => match(s.length, 1, s[0])
const not = g => s => match(g(s).e, 0, '')
const has = g => s => match(!g(s).e, 0, '')
const oneof = t => s => match(t.indexOf(s[0]) >= 0, 1, s[0])
const lit = t => s => match(!s.indexOf(t), t.length, t)
const option = g => choose(g, lit(''))
@jdrew1303
jdrew1303 / ast-commutative-peg.js
Created November 26, 2017 03:56 — forked from bekroogle/ast-commutative-peg.js
commutative expressions in PEG! no left-recursion needed!
// AST version: returns an Abstract Syntax Tree.
// Refactors subtraction into adding negatives.
// e.g. 1-2-3 is treated as 1+(-2)+(-3).
//
// Refactors division into multiplying by a reciprocal.
// e.g. 1024/2/2 is treated as 1024 * 1/2 * 1/2.
//
// Author: Benjamin J. Kruger <[email protected]
@jdrew1303
jdrew1303 / OrderBook.Limit.feature
Created November 26, 2017 03:50 — forked from prystupa/OrderBook.Limit.feature
OrderBook tests for decreasing top outstanding order
Scenario Outline: Decrease top outstanding order partially and then fill it completely
When the following orders are added to the "<Side>" book:
| Broker | Qty | Price |
| A | 100 | 10.5 |
| B | 100 | 10.5 |
Then the "<Side>" order book looks like:
| Broker | Qty | Price |
| A | 100 | 10.5 |
| B | 100 | 10.5 |
When the top order of the "<Side>" book is filled by "20"
@jdrew1303
jdrew1303 / LimitOrdersMatching.feature
Created November 26, 2017 03:50 — forked from prystupa/LimitOrdersMatching.feature
Partial matches for limit orders
Feature: Core matching logic for limit orders
Background: Submit initial non-crossing orders to work with
Given the following orders are submitted in this order:
| Broker | Side | Qty | Price |
| A | Buy | 100 | 10.4 |
| B | Buy | 200 | 10.3 |
| C | Sell | 100 | 10.7 |
| D | Sell | 200 | 10.8 |
Then no trades are generated
@jdrew1303
jdrew1303 / OrderBook.Market.feature
Created November 26, 2017 03:49 — forked from prystupa/OrderBook.Market.feature
Price and time priority for market orders
Feature: Core Market Order Functionality for Price and Time Priority
Scenario Outline: Price and time priority of market orders over limit orders
When the following orders are added to the "<Side>" book:
| Broker | Qty | Price |
| A | 100 | 10.5 |
Then the "<Side>" order book looks like:
| Broker | Qty | Price |
| A | 100 | 10.5 |
When the following orders are added to the "<Side>" book:
@jdrew1303
jdrew1303 / OrderBook.scala
Created November 26, 2017 03:49 — forked from prystupa/OrderBook.scala
OrderBook implementation with support for Market Orders price and time priority
class OrderBook(side: Side, orderTypes: (Order => OrderType)) {
private var marketBook: List[Order] = Nil
private var limitBook: List[(Double, List[Order])] = Nil
private val priceOrdering = if (side == Sell) Ordering[Double] else Ordering[Double].reverse
def add(order: Order) {
orderTypes(order).price match {
@jdrew1303
jdrew1303 / MarketOrdersMatching.feature
Created November 26, 2017 03:49 — forked from prystupa/MarketOrdersMatching.feature
Matching incoming market order against outstanding limit orders
Feature: Core matching logic for market orders
Scenario: Matching a large Buy market order against multiple limit orders
The order is large enough to fill the entire opposite book
The remainder of the market order is expected to rest in its book
When the following orders are submitted in this order:
| Broker | Side | Qty | Price |
| A | Buy | 100 | 10.7 |
| B | Buy | 200 | 10.6 |
| C | Buy | 300 | 10.5 |