38 lines
1.0 KiB
Plaintext
38 lines
1.0 KiB
Plaintext
@preprocessor typescript
|
|
|
|
@{%
|
|
import { Expr } from "./parsed";
|
|
%}
|
|
|
|
expr -> term {% id %}
|
|
|
|
term -> term "+" factor
|
|
{% ([left, _, right]): Expr =>
|
|
({ exprType: "binary", binaryType: "add", left, right }) %}
|
|
| term "-" factor
|
|
{% ([left, _, right]): Expr =>
|
|
({ exprType: "binary", binaryType: "subtract", left, right }) %}
|
|
| factor {% id %}
|
|
|
|
factor -> factor "*" unary
|
|
{% ([left, _, right]): Expr =>
|
|
({ exprType: "binary", binaryType: "multiply", left, right }) %}
|
|
| factor "/" unary
|
|
{% ([left, _, right]): Expr =>
|
|
({ exprType: "binary", binaryType: "divide", left, right }) %}
|
|
| unary {% id %}
|
|
|
|
unary -> "+" unary
|
|
{% ([_, subject]): Expr =>
|
|
({ exprType: "unary", unaryType: "plus", subject }) %}
|
|
| "-" unary
|
|
{% ([_, subject]): Expr =>
|
|
({ exprType: "unary", unaryType: "negate", subject }) %}
|
|
| operand {% id %}
|
|
|
|
operand -> "(" expr ")" {% ([_, expr]): Expr => expr %}
|
|
| ("0" | [1-9][0-9]:*) {% ([token]): Expr => ({ exprType: "int", value: parseInt(token.value) }) %}
|
|
|
|
_ -> __:?
|
|
__ -> [ \t\r\n]:+
|