cgv
Loading...
Searching...
No Matches
operators.cxx
1#include "operators.h"
2
3namespace cgv {
4 namespace ppp {
5
6 const char* operator_characters = "=|^&!<>+-*/%.:~?";
7
9 {
10 OperatorType ot;
11 const char* word;
12 int arity;
13 int priority;
14 OperatorPrecedence precedence;
15 OperatorLocation location;
16 };
17
18 const operator_info& get_operator_info(OperatorType ot)
19 {
20 static const operator_info infos[] = {
21
22 { OT_ASSIGN, "=", 2, 1, OP_RIGHT, OL_PREFIX },
23 { OT_ASSIGN_REF, "=&", 2, 1, OP_RIGHT, OL_PREFIX },
24 { OT_ASSIGN_ADD, "+=", 2, 1, OP_RIGHT, OL_PREFIX },
25 { OT_ASSIGN_SUB, "-=", 2, 1, OP_RIGHT, OL_PREFIX },
26 { OT_ASSIGN_MUL, "*=", 2, 1, OP_RIGHT, OL_PREFIX },
27 { OT_ASSIGN_DIV, "/=", 2, 1, OP_RIGHT, OL_PREFIX },
28 { OT_ASSIGN_AND, "&=", 2, 1, OP_RIGHT, OL_PREFIX },
29 { OT_ASSIGN_OR, "|=", 2, 1, OP_RIGHT, OL_PREFIX },
30 { OT_ASSIGN_XOR, "^=", 2, 1, OP_RIGHT, OL_PREFIX },
31 { OT_ASSIGN_LSH, "<<=",2, 1, OP_RIGHT, OL_PREFIX },
32 { OT_ASSIGN_RSH, ">>=",2, 1, OP_RIGHT, OL_PREFIX },
33
34 { OT_LOG_OR, "||", 2, 2, OP_LEFT, OL_PREFIX },
35
36 { OT_LOG_AND, "&&", 2, 3, OP_LEFT, OL_PREFIX },
37
38 { OT_OR, "|", 2, 4, OP_LEFT, OL_PREFIX },
39
40 { OT_XOR, "^", 2, 5, OP_LEFT, OL_PREFIX },
41
42 { OT_AND, "&", 2, 6, OP_LEFT, OL_PREFIX },
43
44 { OT_EQUAL, "==", 2, 7, OP_LEFT, OL_PREFIX },
45 { OT_UNEQUAL, "!=", 2, 7, OP_LEFT, OL_PREFIX },
46 { OT_EQUAL_TYPE, "~~", 2, 7, OP_LEFT, OL_PREFIX },
47 { OT_UNEQUAL_TYPE, "!~", 2, 7, OP_LEFT, OL_PREFIX },
48
49 { OT_LESS, "<", 2, 8, OP_LEFT, OL_PREFIX },
50 { OT_GREATER, ">", 2, 8, OP_LEFT, OL_PREFIX },
51 { OT_LESS_OR_EQUAL, "<=", 2, 8, OP_LEFT, OL_PREFIX },
52 { OT_GREATER_OR_EQUAL,">=", 2, 8, OP_LEFT, OL_PREFIX },
53
54 { OT_LSH, "<<", 2, 9, OP_LEFT, OL_PREFIX },
55 { OT_RSH, ">>", 2, 9, OP_LEFT, OL_PREFIX },
56
57 { OT_ADD, "+", 2,10, OP_LEFT, OL_PREFIX },
58 { OT_SUB, "-", 2,10, OP_LEFT, OL_PREFIX },
59
60 { OT_MUL, "*", 2,11, OP_LEFT, OL_PREFIX },
61 { OT_DIV, "/", 2,11, OP_LEFT, OL_PREFIX },
62 { OT_MOD, "%", 2,11, OP_LEFT, OL_PREFIX },
63
64 { OT_NOT, "!", 1,12, OP_RIGHT, OL_PREFIX },
65 { OT_EXISTS, "?", 1,12, OP_RIGHT, OL_PREFIX },
66 { OT_INC, "++", 1,12, OP_RIGHT, OL_PREFIX },
67 { OT_DEC, "--", 1,12, OP_RIGHT, OL_PREFIX },
68 { OT_NEGATE, "-", 1,12, OP_RIGHT, OL_PREFIX },
69 { OT_COMPL, "~", 1,12, OP_RIGHT, OL_PREFIX },
70
71 { OT_DOT, ".", 2,13, OP_LEFT, OL_PREFIX },
72
73 { OT_MAP_UP, ":>", 1,14, OP_LEFT, OL_PREFIX },
74 { OT_MAP_DOWN, "<:", 1,14, OP_LEFT, OL_PREFIX },
75 { OT_BINARY_MAP, "::", 2,14, OP_LEFT, OL_PREFIX },
76 { OT_UNARY_MAP, "::", 1,14, OP_LEFT, OL_PREFIX },
77
78 };
79
80 return infos[ot];
81 }
82
83 OperatorPrecedence get_operator_precedence(OperatorType ot)
84 {
85 return get_operator_info(ot).precedence;
86 }
87
88 const char* get_operator_word(OperatorType ot)
89 {
90 return get_operator_info(ot).word;
91 }
92
93 int get_operator_priority(OperatorType ot)
94 {
95 return get_operator_info(ot).priority;
96 }
97
98 int get_operator_arity(OperatorType ot)
99 {
100 return get_operator_info(ot).arity;
101 }
102 OperatorLocation get_operator_location(OperatorType ot)
103 {
104 return get_operator_info(ot).location;
105 }
106 bool get_operator_at_begin(OperatorType ot)
107 {
108 return get_operator_info(ot).arity == 1 && get_operator_location(ot) == OL_PREFIX;
109 }
110
111
112
113 OperatorType get_operator_type(const std::string& s)
114 {
115 for (OperatorType ot = (OperatorType)0; ot < OT_LAST; ot = OperatorType(ot + 1))
116 if (s == get_operator_word(ot))
117 return ot;
118 return OT_LAST;
119 }
120
121 }
122}
the cgv namespace
Definition print.h:11