1 /*
2  * Copyright 2012-2013, Jakub Zawadzki <darkjames-ws@darkjames.pl>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 
7 typedef enum {
8 	OP1_INVALID = 0,
9 	OP1_MINUS,
10 	OP1_NOT,
11 	OP1_NEG
12 } npl_op1_t;
13 
14 typedef enum {
15 	OP2_INVALID = 0,
16 
17 	OP2_ASSIGN,
18 	OP2_ASSIGN_PLUS,
19 
20 	OP2_PLUS,
21 	OP2_MINUS,
22 
23 	OP2_MULTIPLY,
24 	OP2_DIV,
25 	OP2_MOD,
26 
27 	OP2_SHL,
28 	OP2_SHR,
29 
30 	OP2_EQUAL,
31 	OP2_NOTEQUAL,
32 
33 	OP2_LESS,
34 	OP2_GREATER,
35 	OP2_LEQUAL,
36 	OP2_GEQUAL,
37 
38 	OP2_LOGIC_OR,
39 	OP2_LOGIC_AND,
40 
41 	OP2_OR,
42 	OP2_AND,
43 	OP2_XOR,
44 
45 } npl_op2_t;
46 
47 #define NPL_PARAMS_MAX 20
48 
49 typedef struct {
50 	char *args[NPL_PARAMS_MAX];
51 	int count;
52 
53 } npl_params_t;
54 
55 typedef enum {
56 	EXPRESSION_INVALID = 0,
57 
58 	EXPRESSION_ID,
59 	EXPRESSION_INT,
60 	EXPRESSION_STR,
61 
62 	EXPRESSION_INDEX,
63 	EXPRESSION_MULTI_INDEX,
64 	EXPRESSION_FIELD,
65 	EXPRESSION_CALL,
66 
67 	EXPRESSION_UNARY,
68 	EXPRESSION_BINARY,
69 	EXPRESSION_COND
70 
71 } npl_expression_type_t;
72 
73 typedef struct _npl_expression_list {
74 	struct _npl_expression_list *next;
75 
76 	struct _npl_expression *expr;
77 } npl_expression_list_t;
78 
79 typedef struct _npl_expression {
80 union {
81 	struct {
82 		npl_expression_type_t type;
83 	};
84 
85 	struct {
86 		npl_expression_type_t type;	/* EXPRESSION_ID */
87 		char *id;
88 	} id;
89 
90 	struct {
91 		npl_expression_type_t type;	/* EXPRESSION_INT */
92 		unsigned int digit;
93 	} num;
94 
95 	struct {
96 		npl_expression_type_t type;	/* EXPRESSION_STR */
97 		char *str;
98 	} str;
99 
100 	struct {
101 		npl_expression_type_t type;	/* EXPRESSION_INDEX */
102 
103 		struct _npl_expression *base;
104 		struct _npl_expression *index;
105 	} arr;
106 
107 	struct {
108 		npl_expression_type_t type;	/* EXPRESSION_MULTI_INDEX */
109 
110 		struct _npl_expression *base;
111 		npl_expression_list_t *indexes;
112 	} aarr;
113 
114 	struct {
115 		npl_expression_type_t type;	/* EXPRESSION_FIELD */
116 
117 		struct _npl_expression *base;
118 		char *field;
119 	} fld;
120 
121 	struct {
122 		npl_expression_type_t type;	/* EXPRESSION_UNARY */
123 
124 		npl_op1_t operator;
125 		struct _npl_expression *operand;
126 
127 	} u;
128 
129 	struct {
130 		npl_expression_type_t type;	/* EXPRESSION_BINARY */
131 
132 		struct _npl_expression *operand1;
133 		struct _npl_expression *operand2;
134 		npl_op2_t operator;
135 
136 	} b;
137 
138 	struct {
139 		npl_expression_type_t type;	/* EXPRESSION_CALL */
140 
141 		struct _npl_expression *fn;
142 		npl_expression_list_t *args;
143 
144 	} call;
145 
146 	struct {
147 		npl_expression_type_t type;	/* EXPRESSION_COND */
148 
149 		struct _npl_expression *test_expr;
150 		struct _npl_expression *true_expr;
151 		struct _npl_expression *false_expr;
152 	} c;
153 
154 
155 };
156 } npl_expression_t;
157 
158 struct _npl_statement;
159 
160 typedef struct {
161 	char *id;
162 	int private;
163 	npl_params_t params;
164 
165 	npl_expression_t *format;
166 	npl_expression_t *count_expr;
167 	struct _npl_statements *sts;
168 
169 	/* code generator */
170 	char *tmpid;
171 	struct ettinfo *ett;
172 	struct symbol *sym;
173 	int struct_size;
174 } npl_struct_t;
175 
176 typedef struct {
177 	char *id;
178 	npl_params_t params;
179 
180 	npl_expression_t *switch_expr;
181 
182 	struct npl_table_case {
183 		struct npl_table_case *next;
184 
185 		npl_expression_t e;
186 
187 		npl_expression_t *return_expr;
188 
189 	} *cases;
190 
191 	npl_expression_t *default_expr;
192 
193 	/* code generator */
194 	struct symbol *sym;
195 } npl_table_t;
196 
197 typedef struct {
198 	char *id;
199 	npl_expression_t expr;
200 
201 	/* code generator */
202 	struct symbol *sym;
203 } npl_const_t;
204 
205 typedef enum {
206 	STATEMENT_INVALID = 0,
207 
208 	STATEMENT_WHILE,
209 	STATEMENT_TABLE,
210 	STATEMENT_STRUCT,
211 	STATEMENT_FIELD,
212 	STATEMENT_SWITCH,
213 	STATEMENT_DYNAMIC_SWITCH,
214 
215 } npl_statement_type_t;
216 
217 typedef struct {
218 	npl_expression_t *switch_expr;
219 
220 	struct npl_switch_case {
221 		struct npl_switch_case *next;
222 
223 		npl_expression_t e;
224 
225 		struct _npl_statement *st;
226 
227 	} *cases;
228 
229 	struct _npl_statement *default_st;
230 
231 } npl_switch_t;
232 
233 typedef struct _npl_attribute_list {
234 	struct _npl_attribute_list *next;
235 	struct _npl_expression *expr;
236 
237 	/* code generator */
238 	const char *resolved;
239 	npl_expression_t *assign_expr;
240 	int flags;
241 } npl_attribute_list_t;
242 
243 typedef struct _npl_statement {
244 	union {
245 		struct {
246 			npl_statement_type_t type;
247 			npl_attribute_list_t *attr_list;
248 		};
249 
250 		struct {
251 			npl_statement_type_t type;	/* STATEMENT_WHILE */
252 			npl_attribute_list_t *attr_list;
253 
254 			char *id;
255 			npl_expression_t expr;
256 
257 			struct _npl_statements *sts;
258 		} w;
259 
260 		struct {
261 			npl_statement_type_t type;	/* STATEMENT_TABLE */
262 			npl_attribute_list_t *attr_list;
263 
264 			npl_table_t data;
265 		} t;
266 
267 		struct {
268 			npl_statement_type_t type;	/* STATEMENT_STRUCT */
269 			npl_attribute_list_t *attr_list;
270 
271 			npl_struct_t data;
272 		} s;
273 
274 		struct {
275 			npl_statement_type_t type;	/* STATEMENT_SWITCH or STATEMENT_DYNAMIC_SWITCH */
276 			npl_attribute_list_t *attr_list;
277 
278 			npl_switch_t data;
279 		} sw;
280 
281 		struct _npl_statement_field {
282 			npl_statement_type_t type;	/* STATEMENT_FIELD */
283 			npl_attribute_list_t *attr_list;
284 
285 			char *t_id;
286 			char *id;
287 
288 			unsigned int bits;
289 			npl_expression_t *arr;
290 
291 			npl_expression_t *format;
292 			struct _npl_statements *sts;
293 			npl_expression_list_t *params;
294 
295 			/* code generator */
296 			struct hfinfo *hfi;
297 			npl_expression_t *byte_order_attr;
298 			int generate_var;
299 			int field_size;
300 		} f;
301 
302 	};
303 } npl_statement_t;
304 
305 struct _npl_statements {
306 	struct _npl_statements *next;
307 
308 	npl_statement_t st;
309 };
310 
311 typedef struct {
312 	char *id;
313 	npl_params_t params;
314 
315 	npl_expression_t *format;
316 	struct _npl_statements *sts;
317 
318 	/* code generator */
319 	struct symbol *sym;
320 } npl_protocol_t;
321 
322 typedef enum {
323 	FIELD_INVALID = 0,
324 
325 	FIELD_DECIMAL,
326 	FIELD_NUMBER,
327 	FIELD_TIME,
328 	FIELD_UNSIGNED_NUMBER
329 
330 } npl_field_type_t;
331 
332 typedef struct {
333 	npl_field_type_t type;
334 
335 	char *id;
336 	npl_params_t params;
337 
338 	npl_expression_t *byte_order;
339 	npl_expression_t *display_format;
340 	npl_expression_t *size;
341 
342 	/* code generator */
343 	struct symbol *sym;
344 
345 } npl_type_t;
346 
347 typedef enum {
348 	DECL_INVALID = 0,
349 
350 	DECL_INCLUDE,
351 	DECL_STRUCT,
352 	DECL_TABLE,
353 	DECL_CONST,
354 	DECL_PROTOCOL,
355 	DECL_TYPE
356 
357 } npl_decl_type_t;
358 
359 typedef struct {
360 	union {
361 		struct {
362 			npl_decl_type_t type;
363 			npl_attribute_list_t *attr_list;
364 		};
365 
366 		struct {
367 			npl_decl_type_t type;	/* DECL_INCLUDE */
368 			npl_attribute_list_t *attr_list;
369 
370 			char *file;
371 		} i;
372 
373 		struct {
374 			npl_decl_type_t type;	/* DECL_STRUCT */
375 			npl_attribute_list_t *attr_list;
376 
377 			npl_struct_t data;
378 		} s;
379 
380 		struct {
381 			npl_decl_type_t type;	/* DECL_TABLE */
382 			npl_attribute_list_t *attr_list;
383 
384 			npl_table_t data;
385 		} t;
386 
387 		struct {
388 			npl_decl_type_t type;	/* DECL_PROTOCOL */
389 			npl_attribute_list_t *attr_list;
390 
391 			npl_protocol_t data;
392 		} p;
393 
394 		struct {
395 			npl_decl_type_t type;	/* DECL_CONST */
396 			npl_attribute_list_t *attr_list;
397 
398 			npl_const_t data;
399 		} c;
400 
401 		struct {
402 			npl_decl_type_t type;	/* DECL_TYPE */
403 			npl_attribute_list_t *attr_list;
404 
405 			npl_type_t data;
406 		} ty;
407 
408 	};
409 } npl_decl_t;
410 
411 typedef struct {
412 	struct _npl_decl_list {
413 		struct _npl_decl_list *next;
414 		npl_decl_t d;
415 
416 	} *decls;
417 
418 } npl_code_t;
419