Skip to content

Instantly share code, notes, and snippets.

@haskellcamargo
Created October 24, 2017 17:22
Show Gist options
  • Save haskellcamargo/109106824adfb73151b13f6d070cd96d to your computer and use it in GitHub Desktop.
Save haskellcamargo/109106824adfb73151b13f6d070cd96d to your computer and use it in GitHub Desktop.
Stmt = _ 'function'i _ name:Name _ '(' ')' _ EOL _ body:Stmt+ _ {
return 'var name ' + name + ' = function () {' + body + '}'
} / _ 'return' _ e:Expression _ {
return 'return ' + e
}
Expression
= head:Term tail:(_ ("+" / "-") _ Term)* {
return tail.reduce(function(result, element) {
if (element[1] === "+") { return result + element[3]; }
if (element[1] === "-") { return result - element[3]; }
}, head);
}
Term
= head:Factor tail:(_ ("*" / "/") _ Factor)* {
return tail.reduce(function(result, element) {
if (element[1] === "*") { return result * element[3]; }
if (element[1] === "/") { return result / element[3]; }
}, head);
}
Factor
= "(" _ expr:Expression _ ")" { return expr; }
/ Integer
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
Name "name"
= _ x:[a-zA-Z] xs:[a-zA-Z0-9]* { return x + xs.join('') }
_ "whitespace"
= [ \t]*
EOL "eol"
= [\r\n]+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment