-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.lark
54 lines (37 loc) · 945 Bytes
/
math.lark
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// start: root ("\n" root)? "\n"*
?start: equation
| expr
// equation: expr "=" expr -> eqn
equation: VAR "=" expr -> asn
| expr "=" expr -> eqn
// this could be _expr to make it explicit
// because every use of expr only has one child
// so it is a useless wrapper to preserve
?expr: sum
| product
| exponent
| atom
?sum: sum "+" product -> add
| sum "-" product -> sub
| product
?product: product "*" exponent -> mul
| product "/" exponent -> div
| exponent
?exponent: atom "**" atom -> exp
| atom
?atom: NUMBER -> num
| VAR -> var
| "(" expr ")" -> par
| FUNC "(" expr ")" -> fun
| "-" atom -> neg
VAR: LCASE_LETTER
FUNC: "sin"
| "cos"
| "tan"
| "sqrt"
| "log"
| "ln"
%import common.NUMBER
%import common.LCASE_LETTER
%import common.WS_INLINE
%ignore WS_INLINE // tabs and spaces