Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Expression
- = head:Term tail:(_ ("+" / "-") _ Term)* {
- var result = head, i;
- for (i = 0; i < tail.length; i++) {
- if (tail[i][1] === "+") { result += tail[i][3]; }
- if (tail[i][1] === "-") { result -= tail[i][3]; }
- }
- return result;
- }
- Term
- = head:Factor tail:(_ ("*" / "/") _ Factor)* {
- var result = head, i;
- for (i = 0; i < tail.length; i++) {
- if (tail[i][1] === "*") { result *= tail[i][3]; }
- if (tail[i][1] === "/") { result /= tail[i][3]; }
- }
- return result;
- }
- Factor
- = neg:NegId number:Factor_NUM { if (neg === "-") { return -number} return number }
- Factor_NUM
- = "(" _ expr:Expression _ ")" { return expr; }
- / Integer
- Integer "integer"
- = [0-9]+ { return parseInt(text(), 10); }
- NegId
- = [\-+]? { return text()}
- _ "whitespace"
- = [ \t\n\r]*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement