-
Notifications
You must be signed in to change notification settings - Fork 18
/
math_term_parser.yrl
98 lines (81 loc) · 2.67 KB
/
math_term_parser.yrl
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Terminals '(' ')'
'+' '/' '*' '-' '^'
word ',' number
'!'
'.' '[' ']'
'~' '&' '|' '|^' '<<' '>>'
'and' 'or' 'not' '?' ':'
'==' '!=' '<=' '>=' '<' '>' nil true false string.
Nonterminals expr argument arguments function variable variables signed_number.
Rootsymbol expr.
Unary 1000 '!'.
Right 900 '^'.
Unary 850 'not'.
Unary 800 '~'.
Left 750 '*' '/'.
Left 700 '+' '-'.
Left 600 '<<' '>>'.
Left 590 '==' '!=' '<=' '<' '>=' '>'.
Left 580 '&'.
Left 550 '|^'.
Left 520 '|'.
Left 480 'and'.
Left 420 'or'.
Right 300 '?' ':'.
Unary 100 function.
Unary 0 variable.
% boolean stuff
expr -> nil : nil.
expr -> true : true.
expr -> false : false.
expr -> expr '==' expr : {eq, '$1', '$3'}.
expr -> expr '!=' expr : {neq, '$1', '$3'}.
expr -> expr '>=' expr : {gte, '$1', '$3'}.
expr -> expr '<=' expr : {lte, '$1', '$3'}.
expr -> expr '<' expr : {lt, '$1', '$3'}.
expr -> expr '>' expr : {gt, '$1', '$3'}.
expr -> expr 'and' expr : {logical_and, '$1', '$3'}.
expr -> expr 'or' expr : {logical_or, '$1', '$3'}.
expr -> 'not' expr : {logical_not, '$2'}.
expr -> expr '?' expr ':' expr : {ternary_if, '$1', '$3', '$5'}.
% Binary stuff
expr -> '~' expr : {'not', '$2'}.
expr -> expr '&' expr : {'and', '$1', '$3'}.
expr -> expr '|' expr : {'or', '$1', '$3'}.
expr -> expr '|^' expr : {'xor', '$1', '$3'}.
expr -> expr '<<' expr : {shift_left, '$1', '$3'}.
expr -> expr '>>' expr : {shift_right, '$1', '$3'}.
% Math stuff
expr -> expr '^' expr : {power, '$1', '$3'}.
expr -> expr '+' expr : {add, '$1', '$3'}.
expr -> expr '-' expr : {subtract, '$1', '$3'}.
expr -> expr '/' expr : {divide, '$1', '$3'}.
expr -> expr '*' expr : {multiply, '$1', '$3'}.
expr -> expr '!' : {factorial, '$1'}.
% Grouping
expr -> '(' expr ')' : '$2'.
% What is an expression
expr -> number : extract_token('$1').
expr -> function : '$1'.
expr -> variables : '$1'.
expr -> signed_number : '$1'.
% Support signed numbers
signed_number -> '+' number : extract_token('$2').
signed_number -> '-' number : -extract_token('$2').
% Access
variable -> word : {variable, extract_token('$1')}.
variables -> variable : {access, ['$1']}.
variables -> variables '.' variable : {access, concat([extract('$1'), ['$3']])}.
variables -> variables '[' expr ']' : {access, concat([extract('$1'), [{index, '$3'}]])}.
% Functions
argument -> expr : '$1'.
arguments -> argument : ['$1'].
arguments -> argument ',' arguments : ['$1' | '$3'].
function -> word '(' ')' : {function, extract_token('$1'), []}.
function -> word '(' arguments ')' : {function, extract_token('$1'), '$3'}.
% Strings
expr -> string : extract_token('$1').
Erlang code.
extract_token({_Token, _Line, Value}) -> Value.
extract({access, Value}) -> Value.
concat(List) -> lists:concat(List).