Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. Expression
  2. = head:Term tail:(_ ("+" / "-") _ Term)* {
  3. var result = head, i;
  4.  
  5. for (i = 0; i < tail.length; i++) {
  6. if (tail[i][1] === "+") { result += tail[i][3]; }
  7. if (tail[i][1] === "-") { result -= tail[i][3]; }
  8. }
  9.  
  10. return result;
  11. }
  12.  
  13. Term
  14. = head:Factor tail:(_ ("*" / "/") _ Factor)* {
  15. var result = head, i;
  16.  
  17. for (i = 0; i < tail.length; i++) {
  18. if (tail[i][1] === "*") { result *= tail[i][3]; }
  19. if (tail[i][1] === "/") { result /= tail[i][3]; }
  20. }
  21.  
  22. return result;
  23. }
  24.  
  25. Factor
  26. = neg:NegId number:Factor_NUM { if (neg === "-") { return -number} return number }
  27.  
  28. Factor_NUM
  29. = "(" _ expr:Expression _ ")" { return expr; }
  30. / Integer
  31.  
  32. Integer "integer"
  33. = [0-9]+ { return parseInt(text(), 10); }
  34.  
  35. NegId
  36. = [\-+]? { return text()}
  37.  
  38. _ "whitespace"
  39. = [ \t\n\r]*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement