Created
October 24, 2017 17:22
-
-
Save haskellcamargo/109106824adfb73151b13f6d070cd96d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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