aboutsummaryrefslogtreecommitdiff
path: root/src/commands/calc.pest
blob: 07eeddbdf6e2167c2d61d09129dc300cc9db5add (plain) (blame)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
num = {
    hex
    | oct
    | binary
    | float
}

float = @{ int ~ ( "." ~ ASCII_DIGIT*)? ~ (^"e" ~ int)? }
    int = { "-"? ~ ASCII_DIGIT+ }

hex = @{ "0x" ~ ASCII_HEX_DIGIT+ }
oct = @{ "0o" ~ ASCII_OCT_DIGIT+ }
binary = @{ "0b" ~ ASCII_BIN_DIGIT+ }

infix = _{ add | sub | mul | div | modulo }
    add = { "+" }
    sub = { "-" }
    modulo = { "%" | "mod" }
    mul = { "*" }
    div = { "/" }

tight_infix = _{ dice | pow }
	dice = { "d" }
	pow = { "^" }

trig = _{ sin | cos | tan | asin | acos | atan }
    sin = { "sin" }
    cos = { "cos" }
    tan = { "tan" }
    asin = { "asin" }
    acos = { "acos" }
    atan = { "atan" }

htrig = _{ sinh | cosh | tanh | asinh | acosh | atanh }
    sinh = { "sinh" }
    cosh = { "cosh" }
    tanh = { "tanh" }
    asinh = { "asinh" }
    acosh = { "acosh" }
    atanh = { "atanh" }

unary_prefix = _{ log | sqrt | sgn | htrig | trig | exp | abs | ceil | floor | round }
    log = { "log" | "ln" }
    sqrt = { "sqrt" }
    sgn = { "sgn" }
    exp = { "exp" }
    abs = { "abs" }
    ceil = { "ceil" }
    floor = { "floor" }
    round = { "round" }

binary_prefix = _{ min | max | atan2 }
    min = { "min" }
    max = { "max" }
    atan2 = { "atan2" }

suffix = _{ factorial }
    factorial = { "!" }

term = _{ num | "(" ~ expr ~ ")" }

suffix_expr = { term ~ suffix }
unary_expr = ${ unary_prefix ~ ws+ ~ outfix_expr }
binary_expr = ${ binary_prefix ~ ws+ ~ outfix_expr ~ ws+ ~ outfix_expr }

tight = _{ (suffix_expr | term) ~ (tight_infix ~ tight)* }

expr = { outfix_expr ~ (infix ~ outfix_expr)* }

outfix_expr = _{
    tight |
    binary_expr |
    unary_expr
}

calc = _{ SOI ~ expr ~ EOI }

ws = _{ " " | "\t" | "\n" }
WHITESPACE = _{ ws }