Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Created August 11, 2010 22:04
Show Gist options
  • Save jakubkulhan/519863 to your computer and use it in GitHub Desktop.
Save jakubkulhan/519863 to your computer and use it in GitHub Desktop.
calc
= expression !.
// left associative
expression
= f:factor ( "+" g:factor { $f = $f + $g; }
/ "-" g:factor { $f = $f - $g; }
)*
-> $f
factor
= t:term ( "*" u:term { $t = $t * $u; }
/ "/" u:term { $t = $t / $u; }
)*
-> $t
// right associative
term
= p:primary "^" t:term -> pow($p, $t)
/ primary
// primary
primary
= __ "pi" __
-> M_PI
/ __ "e" __
-> 2.718281828459
/ __ f:function? __ '(' e:expression ')' __
{
if ($f) {
return $f($e);
}
return $e;
}
/ __ n:[0-9]+ __
-> intval($n)
function
= "abs"
/ "sin"
/ "asin"
/ "cos"
/ "acos"
/ "tan"
/ "atan"
/ "exp"
/ "ln" -> "log"
/ "log10"
/ "sqrt"
__
= [ \t\r\n]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment