Skip to content

Instantly share code, notes, and snippets.

@galdosd
Created April 14, 2012 16:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save galdosd/2385890 to your computer and use it in GitHub Desktop.
My PEG
{
function id(x){ return x; }
function cf(x){ return function(y) { return x; }; }
}
start
= sp V:fn { return (V(null))(2); }
sum
= L:term "+" sp R:sum { return function(x){ return L(x)+R(x); }; }
/ pow / term
sp = [ \t\n]*
pow
= L:primary "^" sp R:primary
{ return Math.pow(L,R); }
term
= L:primary "*" sp R:term { return function(x) { return L(x)*R(x); } }
/ primary
primary = apply / fn / paren / var / integer
paren = "(" sp V:sum ")" sp { return V; }
callable = fn / var / paren
apply =
F:callable V:paren { return function(x) { return (F(x))(V(x)); }; }
fn
= "{" sp V:sum "}" sp
{ return function(x) { return V; } }
integer "integer"
= digits:[0-9]+ sp
{ return cf(parseInt(digits.join(""), 10)); }
var =
letter:[A-Za-z] sp { return id; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment