forked from nez-peg/nez-grammar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.nez
46 lines (40 loc) · 973 Bytes
/
math.nez
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
/**
* Basic mathematic operator
* author: Kimio Kuramitsu
*/
/* Start Point */
File = Expression .*
/* Code Layout */
_ = S*
S = [ \t]
"+" = '+' _
"-" = '-' _
"*" = '*' _
"/" = '/' _
"%" = '%' _
"(" = '(' _
")" = ')' _
/* Expression */
Expression = Sum
Sum = Product {$left ("+" #Add / "-" #Sub) $right(Product) }*
Product = Value {$left ("*" #Mul / "/" #Div / "%" #Mod) $right(Value) }*
Value = { [0-9]+ #Integer } _
/ { [A-Za-z0-9_]+ #Variable } _
/ "(" Expression ")"
example Expression ~51adead '''
1
'''
example Expression ~9877061 '''
1+A*3
'''
example Expression ~1da9fbd '''
1 * 2 + 3
'''
format #Add[*] `($[0] + $[1])`
format #Sub[*] `($[0] - $[1])`
format #Mul[*] `($[0] * $[1])`
format #Div[*] `($[0] / $[1])`
format #Mod[*] `($[0] % $[1])`
format #Int[*] `${text}`
format #Variable[*] `${text}`
// formatted by $ nez format