You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pegjs/examples/arithmetics.peg

32 lines
930 B
Plaintext

start : _ expression { return $2; }
expression : additive
additive : multiplicative (plus / minus) additive {
if ($2 === "+") { return $1 + $3; }
if ($2 === "-") { return $1 - $3; }
}
/ multiplicative
multiplicative : primary (times / divide) multiplicative {
if ($2 === "*") { return $1 * $3; }
if ($2 === "/") { return $1 / $3; }
}
/ primary
primary : integer
/ lparen expression rparen { return $2 }
integer "integer"
: [0-9]+ _ { return parseInt($1.join("")); }
plus : "+" _ { return $1; }
minus : "-" _ { return $1; }
times : "*" _ { return $1; }
divide : "/" _ { return $1; }
lparen : "(" _
rparen : ")" _
_ "whitespace"
: [ \t\n\r]*