forked from soudy/mathcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.go
66 lines (57 loc) · 1.9 KB
/
operators.go
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
// Copyright 2016 Steven Oud. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package mathcat
type association int
const (
ASSOC_NONE association = iota
ASSOC_LEFT
ASSOC_RIGHT
)
type operator struct {
prec int
assoc association
unary bool
}
var operators = map[TokenType]*operator{
// Assignment operators
EQ: {0, ASSOC_RIGHT, false}, // =
ADD_EQ: {0, ASSOC_RIGHT, false}, // +=
SUB_EQ: {0, ASSOC_RIGHT, false}, // -=
DIV_EQ: {0, ASSOC_RIGHT, false}, // /=
MUL_EQ: {0, ASSOC_RIGHT, false}, // *=
POW_EQ: {0, ASSOC_RIGHT, false}, // **=
REM_EQ: {0, ASSOC_RIGHT, false}, // %=
AND_EQ: {0, ASSOC_RIGHT, false}, // &=
OR_EQ: {0, ASSOC_RIGHT, false}, // |=
XOR_EQ: {0, ASSOC_RIGHT, false}, // ^=
LSH_EQ: {0, ASSOC_RIGHT, false}, // <<=
RSH_EQ: {0, ASSOC_RIGHT, false}, // >>=
// Relational operators
EQ_EQ: {1, ASSOC_RIGHT, false}, // ==
BANG_EQ: {1, ASSOC_RIGHT, false}, // !=
GT: {1, ASSOC_RIGHT, false}, // >
GT_EQ: {1, ASSOC_RIGHT, false}, // >=
LT: {1, ASSOC_RIGHT, false}, // <
LT_EQ: {1, ASSOC_RIGHT, false}, // <=
// Bitwise operators
OR: {2, ASSOC_RIGHT, false}, // |
XOR: {3, ASSOC_RIGHT, false}, // ^
AND: {4, ASSOC_RIGHT, false}, // &
LSH: {5, ASSOC_RIGHT, false}, // <<
RSH: {5, ASSOC_RIGHT, false}, // >>
NOT: {9, ASSOC_LEFT, true}, // ~
// Mathematical operators
ADD: {6, ASSOC_LEFT, false}, // +
SUB: {6, ASSOC_LEFT, false}, // -
MUL: {7, ASSOC_LEFT, false}, // *
DIV: {7, ASSOC_LEFT, false}, // /
POW: {8, ASSOC_LEFT, false}, // **
REM: {7, ASSOC_LEFT, false}, // %
UNARY_MIN: {10, ASSOC_LEFT, true}, // -
}
// Determine if operator 1 has higher precedence than operator 2
func (o1 *operator) hasHigherPrecThan(o2 *operator) bool {
return (o2.assoc == ASSOC_LEFT && o2.prec <= o1.prec) ||
(o2.assoc == ASSOC_RIGHT && o2.prec < o1.prec)
}