libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
operator.c
Go to the documentation of this file.
1
8#include "operator.h"
9#include "util/abort.h"
10
11#define OP_TKN_START PS_TOKEN_EQ
12#define OP_TKN_END PS_TOKEN_BIT_XOR
13#define OP_TKN_RANGE (OP_TKN_END - OP_TKN_START + 1)
14
15/* clang-format off */
17 [PS_TOKEN_EQ - OP_TKN_START] = {
18 .is_binary = true,
19 .is_unary = false,
20 .is_left_associative = true,
21 .binary_prec = 50
22 },
23 [PS_TOKEN_ASSIGN - OP_TKN_START] = {
24 .is_binary = true,
25 .is_unary = false,
26 .is_left_associative = false,
27 .binary_prec = 0
28 },
29 [PS_TOKEN_PLUS_ASSIGN - OP_TKN_START] = {
30 .is_binary = true,
31 .is_unary = false,
32 .is_left_associative = false,
33 .binary_prec = 0
34 },
35 [PS_TOKEN_PLUS - OP_TKN_START] = {
36 .is_binary = true,
37 .is_unary = false,
38 .is_left_associative = true,
39 .binary_prec = 100
40 },
41 [PS_TOKEN_MINUS_ASSIGN - OP_TKN_START] = {
42 .is_binary = true,
43 .is_unary = true,
44 .is_left_associative = false,
45 .unary_prec = 0,
46 .binary_prec = 0
47 },
48 [PS_TOKEN_MINUS - OP_TKN_START] = {
49 .is_binary = true,
50 .is_unary = true,
51 .is_left_associative = true,
52 .unary_prec = 200,
53 .binary_prec = 100
54 },
55 [PS_TOKEN_POW_ASSIGN - OP_TKN_START] = {
56 .is_binary = true,
57 .is_unary = false,
58 .is_left_associative = false,
59 .binary_prec = 0
60 },
61 [PS_TOKEN_POW - OP_TKN_START] = {
62 .is_binary = true,
63 .is_unary = false,
64 .is_left_associative = false,
65 .binary_prec = 300
66 },
67 [PS_TOKEN_MUL_ASSIGN - OP_TKN_START] = {
68 .is_binary = true,
69 .is_unary = false,
70 .is_left_associative = false,
71 .binary_prec = 0
72 },
73 [PS_TOKEN_MUL - OP_TKN_START] = {
74 .is_binary = true,
75 .is_unary = false,
76 .is_left_associative = true,
77 .binary_prec = 200
78 },
79 [PS_TOKEN_DIV_ASSIGN - OP_TKN_START] = {
80 .is_binary = true,
81 .is_unary = false,
82 .is_left_associative = false,
83 .binary_prec = 0
84 },
85 [PS_TOKEN_DIV - OP_TKN_START] = {
86 .is_binary = true,
87 .is_unary = false,
88 .is_left_associative = true,
89 .binary_prec = 200
90 },
91 [PS_TOKEN_MOD - OP_TKN_START] = {
92 .is_binary = true,
93 .is_unary = false,
94 .is_left_associative = true,
95 .binary_prec = 200
96 },
97 [PS_TOKEN_NEQ - OP_TKN_START] = {
98 .is_binary = true,
99 .is_unary = false,
100 .is_left_associative = true,
101 .binary_prec = 50
102 },
103 [PS_TOKEN_LESS_OR_EQ - OP_TKN_START] = {
104 .is_binary = true,
105 .is_unary = false,
106 .is_left_associative = true,
107 .binary_prec = 50
108 },
109 [PS_TOKEN_GREATER_OR_EQ - OP_TKN_START] = {
110 .is_binary = true,
111 .is_unary = false,
112 .is_left_associative = true,
113 .binary_prec = 50
114 },
115 [PS_TOKEN_BIT_SHL_ASSIGN - OP_TKN_START] = {
116 .is_binary = true,
117 .is_unary = false,
118 .is_left_associative = false,
119 .binary_prec = 0
120 },
121 [PS_TOKEN_BIT_SHL - OP_TKN_START] = {
122 .is_binary = true,
123 .is_unary = false,
124 .is_left_associative = false,
125 .binary_prec = 150
126 },
127 [PS_TOKEN_BIT_SHR_ASSIGN - OP_TKN_START] = {
128 .is_binary = true,
129 .is_unary = false,
130 .is_left_associative = false,
131 .binary_prec = 0
132 },
133 [PS_TOKEN_BIT_SHR - OP_TKN_START] = {
134 .is_binary = true,
135 .is_unary = false,
136 .is_left_associative = false,
137 .binary_prec = 150
138 },
139 [PS_TOKEN_LESS - OP_TKN_START] = {
140 .is_binary = true,
141 .is_unary = false,
142 .is_left_associative = true,
143 .binary_prec = 50
144 },
145 [PS_TOKEN_GREATER - OP_TKN_START] = {
146 .is_binary = true,
147 .is_unary = false,
148 .is_left_associative = true,
149 .binary_prec = 50
150 },
151 [PS_TOKEN_LOG_AND - OP_TKN_START] = {
152 .is_binary = true,
153 .is_unary = false,
154 .is_left_associative = true,
155 .binary_prec = 35
156 },
157 [PS_TOKEN_LOG_OR - OP_TKN_START] = {
158 .is_binary = true,
159 .is_unary = false,
160 .is_left_associative = true,
161 .binary_prec = 25
162 },
163 [PS_TOKEN_LOG_NOT - OP_TKN_START] = {
164 .is_binary = false,
165 .is_unary = true,
166 .is_left_associative = true,
167 .unary_prec = 200
168 },
169 [PS_TOKEN_BIT_AND - OP_TKN_START] = {
170 .is_binary = true,
171 .is_unary = false,
172 .is_left_associative = true,
173 .binary_prec = 75
174 },
175 [PS_TOKEN_BIT_OR_ASSIGN - OP_TKN_START] = {
176 .is_binary = true,
177 .is_unary = false,
178 .is_left_associative = false,
179 .binary_prec = 0
180 },
181 [PS_TOKEN_BIT_OR - OP_TKN_START] = {
182 .is_binary = true,
183 .is_unary = false,
184 .is_left_associative = true,
185 .binary_prec = 75
186 },
187 [PS_TOKEN_BIT_NOT - OP_TKN_START] = {
188 .is_binary = false,
189 .is_unary = true,
190 .is_left_associative = true,
191 .unary_prec = 200
192 },
193 [PS_TOKEN_BIT_XOR_ASSIGN - OP_TKN_START] = {
194 .is_binary = true,
195 .is_unary = false,
196 .is_left_associative = false,
197 .binary_prec = 0
198 },
199 [PS_TOKEN_BIT_XOR - OP_TKN_START] = {
200 .is_binary = true,
201 .is_unary = false,
202 .is_left_associative = true,
203 .binary_prec = 75
204 }
205};
206/* clang-format on */
207
208// ops go from EQ to BIT_XOR
210 return type >= PS_TOKEN_EQ && type <= PS_TOKEN_BIT_XOR;
211}
212
214 ps_assert(ps_token_type_is_op(type), "precondition");
215 return &operators[type - OP_TKN_START];
216}
Defines assertion and abortion functionality.
#define ps_assert(cond, msg)
Asserts the given condition cond, aborting via ps_abort with the given msg otherwise.
Definition abort.h:53
bool ps_token_type_is_op(enum ps_token_type type)
Whether type represents an operator token.
Definition operator.c:209
const struct ps_operator * ps_token_type_get_op(enum ps_token_type type)
Returns: the operator information associated with the given operator token.
Definition operator.c:213
#define OP_TKN_RANGE
Definition operator.c:13
struct ps_operator operators[OP_TKN_RANGE]
Definition operator.c:16
#define OP_TKN_START
Definition operator.c:11
Operator precedence and associativity info.
Operator information.
Definition operator.h:18
bool is_binary
Enables processing this operator as a binary operator.
Definition operator.h:25
ps_token_type
The type of a token.
Definition token.h:18