Skip to content

Instantly share code, notes, and snippets.

(def lang1-parser
(instaparse.core/parser
"prog = (spaces? expr spaces? <';'> spaces?)*
<expr> = assig | add-sub
assig = varname spaces? <'='> spaces? expr
<add-sub> = mult-div | add | sub
add = add-sub spaces? <'+'> spaces? mult-div
sub = add-sub spaces? <'-'> spaces? mult-div
<mult-div> = factor | mult |div
mult = mult-div spaces? <'*'> spaces? factor
(def lang0-parser
(instaparse.core/parser
"prog = (spaces? expr spaces? <';'> spaces?)*
<expr> = assig | add-sub
assig = varname spaces? <'='> spaces? expr
<add-sub> = mult-div | add | sub
add = add-sub spaces? <'+'> spaces? mult-div
sub = add-sub spaces? <'-'> spaces? mult-div
<mult-div> = factor | mult |div
mult = mult-div spaces? <'*'> spaces? factor
(def arith-parser
(instaparse.core/parser
"prog = spaces? add-sub spaces?
<add-sub> = mult-div | add | sub
add = add-sub spaces? <'+'> spaces? mult-div
sub = add-sub spaces? <'-'> spaces? mult-div
<mult-div> = term | mult | div
mult = mult-div spaces? <'*'> spaces? term
div = mult-div spaces? <'/'> spaces? term
<term> = number | <'('> add-sub <')'>
(def addmult-parser
(instaparse.core/parser
"prog= spaces? add-sub spaces?
<add-sub> = mult-div | add | sub
add= add-sub spaces? <'+'> spaces? mult-div
sub= add-sub spaces? <'-'> spaces? mult-div
<mult-div> = number | mult | div
mult= mult-div spaces? <'*'> spaces? number
div= mult-div spaces? <'/'> spaces? number
number= #'-?[0-9]+'
(def addsub-parser
(instaparse.core/parser
"prog= spaces? add-sub spaces?
<add-sub> = number | add | sub
add= add-sub spaces? <'+'> spaces? number
sub= add-sub spaces? <'-'> spaces? number
number= #'-?[0-9]+'
<spaces> = <#'\\s+'>"))
(def const-parser
(instaparse.core/parser
"prog= spaces? number spaces?
number=#'-?[0-9]+'
<spaces> = <#'[\\s]'+>"))
(const-parser " -123456 ")
public static void swap(int[] data, int i, int j){
int tmp= data[i];
data[i]= data[j];
data[j]= tmp;
}
public static int partition(int[] data, int begin, int end, int pivotIdx){
swap(data, pivotIdx, --end);
pivotIdx= end;
int pivot= data[pivotIdx];
@scientific-coder
scientific-coder / core.clj
Last active October 7, 2018 15:27
Parsing some scala code to svg for Kingdomino deck updated for formatting, thx!
(ns kingdomino.core
(:require [instaparse.core :as insta])
(:gen-class))
(def size 100)
(def delta-sides 5)
(def ncols 2)
(def delta-dominos 10)
(def colors {:clay "#cc6600" :field "#ffff66" :water "#33ccff" :pasture "#00cc00" :forest "#669900" :mine "#5c5c3d"})
@scientific-coder
scientific-coder / chaos-polygons.cxx
Created May 4, 2016 00:34
generate pics of generalized chaos sierpinski-like figures on polygons
#include <iostream>
#include <tuple>
#include <cmath>
#include <cstdlib>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>
// STEPS=100;for i in $(seq $((3 * $STEPS)) $((8 * $STEPS))); do ./chaos-polygons $(bc -l <<< "scale=2;$i / $STEPS") > generated-cmax-100-$i.pgm; done
// for i in generated-cmax-100-*.pgm; do convert $i ${i%.pgm}.png; done
@scientific-coder
scientific-coder / core.clj
Created February 2, 2015 18:00
Toy bytecode compiler for integer arithmetic DSL
(ns clojr.core
(:require [instaparse.core :as insta])
(:import (clojure.lang DynamicClassLoader)))
(import '[clojure.asm ClassWriter Type Opcodes]
'[clojure.asm.commons Method GeneratorAdapter])
(def parser
(insta/parser
"prog = expr (<#'[\\n]+'> expr?)*