aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/tree.c
blob: 695a8a76fd38bb23f1d9ea1cf2f8e44762d2a651 (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/**
 * mds — A micro-display server
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "tree.h"

#include <libmdsserver/macros.h>
#undef xfree

#include <stdlib.h>
#include <string.h>
#include <errno.h>



/* Helper `typedef`:s */
typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_nesting_t;
typedef struct mds_kbdc_tree_callable mds_kbdc_tree_callable_t;
typedef struct mds_kbdc_tree_information_data mds_kbdc_tree_information_data_t;



/**
 * Tree type constant shortener
 */
#define C(t)  MDS_KBDC_TREE_TYPE_##t


/**
 * Initialise a tree node
 * 
 * @param  this  The memory slot for the tree node
 * @param  type  The type of the node
 */
void mds_kbdc_tree_initialise(mds_kbdc_tree_t* restrict this, int type)
{
  memset(this, 0, sizeof(mds_kbdc_tree_t));
  this->type = type;
}


/**
 * Create a tree node
 * 
 * @param   type  The type of the node
 * @return        The tree node, `NULL` on error
 */
mds_kbdc_tree_t* mds_kbdc_tree_create(int type)
{
  mds_kbdc_tree_t* this = malloc(sizeof(mds_kbdc_tree_t));
  if (this == NULL)
    return NULL;
  mds_kbdc_tree_initialise(this, type);
  return this;
}


/**
 * Common procedure for `mds_kbdc_tree_destroy` and `mds_kbdc_tree_destroy_nonrecursive`
 * 
 * @param  this       The tree node
 * @param  recursive  Whether subtree should be destroyed and freed
 */
static void mds_kbdc_tree_destroy_(mds_kbdc_tree_t* restrict this, int recursive)
{
#define V(type, var)    (((type)this)->var)
#define xfree(t, v)     (free(V(t, v)), V(t, v) = NULL)
#define xdestroy(t, v)  (recursive ? (mds_kbdc_tree_destroy_(V(t, v), 1), xfree(t, v)) : (V(t, v) = NULL))
  
  mds_kbdc_tree_t* prev = NULL;
  mds_kbdc_tree_t* first = this;
  
 again:
  if (this == NULL)
    return;
  
  switch (this->type)
    {
    case C(INFORMATION):
    case C(ASSUMPTION):
    case C(ALTERNATION):
    case C(UNORDERED):
    case C(ORDERED):
      xdestroy(mds_kbdc_tree_nesting_t*, inner);
      break;
      
    case C(INFORMATION_LANGUAGE):
    case C(INFORMATION_COUNTRY):
    case C(INFORMATION_VARIANT):
      xfree(mds_kbdc_tree_information_data_t*, data);
      break;
      
    case C(FUNCTION):
    case C(MACRO):
      xfree(mds_kbdc_tree_callable_t*, name);
      xdestroy(mds_kbdc_tree_callable_t*, inner);
      break;
      
    case C(INCLUDE):
      xfree(mds_kbdc_tree_include_t*, filename);
      xdestroy(mds_kbdc_tree_include_t*, inner);
      break;
      
    case C(ASSUMPTION_HAVE):
      xdestroy(mds_kbdc_tree_assumption_have_t*, data);
      break;
      
    case C(ASSUMPTION_HAVE_CHARS):
      xfree(mds_kbdc_tree_assumption_have_chars_t*, chars);
      break;
      
    case C(ASSUMPTION_HAVE_RANGE):
      xfree(mds_kbdc_tree_assumption_have_range_t*, first);
      xfree(mds_kbdc_tree_assumption_have_range_t*, last);
      break;
      
    case C(FOR):
      xfree(mds_kbdc_tree_for_t*, first);
      xfree(mds_kbdc_tree_for_t*, last);
      xfree(mds_kbdc_tree_for_t*, variable);
      xdestroy(mds_kbdc_tree_for_t*, inner);
      break;
      
    case C(IF):
      xfree(mds_kbdc_tree_if_t*, condition);
      xdestroy(mds_kbdc_tree_if_t*, inner);
      xdestroy(mds_kbdc_tree_if_t*, otherwise);
      break;
      
    case C(LET):
      xfree(mds_kbdc_tree_let_t*, variable);
      xdestroy(mds_kbdc_tree_let_t*, value);
      break;
      
    case C(MAP):
      xdestroy(mds_kbdc_tree_map_t*, sequence);
      xdestroy(mds_kbdc_tree_map_t*, result);
      break;
      
    case C(ARRAY):
      xdestroy(mds_kbdc_tree_array_t*, elements);
      break;
      
    case C(KEYS):
      xfree(mds_kbdc_tree_keys_t*, keys);
      break;
      
    case C(STRING):
      xfree(mds_kbdc_tree_string_t*, string);
      break;
      
    case C(MACRO_CALL):
      xfree(mds_kbdc_tree_macro_call_t*, name);
      xdestroy(mds_kbdc_tree_macro_call_t*, arguments);
      break;
      
    default:
      break;
    }
  
  prev = this;
  this = this->next;
  if (prev != first)
    free(prev);
  goto again;
  
#undef xdestroy
#undef xfree
#undef V
}


/**
 * Release all resources stored in a tree node,
 * without freeing the node itself or freeing
 * or destroying inner `mds_kbdc_tree_t*`:s
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_destroy_nonrecursive(mds_kbdc_tree_t* restrict this)
{
  mds_kbdc_tree_destroy_(this, 0);
}


/**
 * Release all resources stored in a tree node,
 * without freeing or destroying inner
 * `mds_kbdc_tree_t*`:s, but free this node's
 * allocation
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_free_nonrecursive(mds_kbdc_tree_t* restrict this)
{
  mds_kbdc_tree_destroy_nonrecursive(this);
  free(this);
}


/**
 * Release all resources stored in a tree node
 * recursively, but do not free the allocation
 * of the tree node
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_destroy(mds_kbdc_tree_t* restrict this)
{
  mds_kbdc_tree_destroy_(this, 1);
}


/**
 * Release all resources stored in a tree node
 * recursively, and free the allocation
 * of the tree node
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this)
{
  mds_kbdc_tree_destroy(this);
  free(this);
}



/**
 * Duplicate a subtree and goto `fail` on failure
 * 
 * @param  member:identifer  The member in the tree to duplicate
 */
#define T(member)									\
  fail_if (t->member && (n->member = mds_kbdc_tree_dup(t->member), n->member == NULL))


/**
 * Duplicate a string and goto `fail` on failure
 * 
 * @param  member:identifer  The member in the tree to duplicate
 */
#define S(member)								\
  fail_if (t->member && (n->member = strdup(t->member), n->member == NULL))


/**
 * Cast the trees to a specialised subtype
 * 
 * @param  LOWERCASE:identifer   The name of subtype
 */
#define NODE(LOWERCASE)								\
  mds_kbdc_tree_##LOWERCASE##_t* n = (mds_kbdc_tree_##LOWERCASE##_t*)node;	\
  mds_kbdc_tree_##LOWERCASE##_t* t = (mds_kbdc_tree_##LOWERCASE##_t*)this


/**
 * Create a duplicate of a tree node and its children
 * 
 * @param   this  The tree node
 * @return        A duplicate of `this`, `NULL` on error
 */
mds_kbdc_tree_t* mds_kbdc_tree_dup(mds_kbdc_tree_t* restrict this)
{
  mds_kbdc_tree_t* node = NULL;
  int saved_errno;
  
  fail_if ((this == NULL) || xcalloc(node, 1, mds_kbdc_tree_t));
  
  node->type = this->type;
  node->loc_line = this->loc_line;
  node->loc_start = this->loc_start;
  node->loc_end = this->loc_end;
  node->processed = this->processed;
  node->next = mds_kbdc_tree_dup(this->next);
  fail_if (this->next && (node->next == NULL));
  
  switch (this->type)
    {
    case C(INFORMATION):
    case C(ASSUMPTION):
    case C(ALTERNATION):
    case C(UNORDERED):
    case C(ORDERED):                { NODE(nesting); T(inner);                          }  break;
    case C(FUNCTION):
    case C(MACRO):                  { NODE(callable); S(name);T(inner);                 }  break;
    case C(ASSUMPTION_HAVE):        { NODE(assumption_have); T(data);                   }  break;
    case C(ARRAY):                  { NODE(array); T(elements);                         }  break;
    case C(LET):                    { NODE(let); S(variable);T(value);                  }  break;
    case C(MACRO_CALL):             { NODE(macro_call); S(name);T(arguments);           }  break;
    case C(INFORMATION_LANGUAGE):
    case C(INFORMATION_COUNTRY):
    case C(INFORMATION_VARIANT):    { NODE(information_data); S(data);                  }  break;
    case C(INCLUDE):                { NODE(include); S(filename);T(inner);              }  break;
    case C(ASSUMPTION_HAVE_CHARS):  { NODE(assumption_have_chars); S(chars);            }  break;
    case C(KEYS):                   { NODE(keys); S(keys);                              }  break;
    case C(STRING):                 { NODE(string); S(string);                          }  break;
    case C(ASSUMPTION_HAVE_RANGE):  { NODE(assumption_have_range); S(first);S(last);    }  break;
    case C(FOR):                    { NODE(for); S(first);S(last);S(variable);T(inner); }  break;
    case C(IF):                     { NODE(if); S(condition);T(inner);T(otherwise);     }  break;
    case C(MAP):                    { NODE(map); T(sequence);T(result);                 }  break;
    default:
      break;
    }
  
  return node;
 pfail:
  saved_errno = errno;
  mds_kbdc_tree_free(node);
  return errno = saved_errno, NULL;
}


#undef NODE
#undef S
#undef T



/**
 * Convert the tree to a specialised subtype and
 * prints its type and code location
 * 
 * @param  LOWERCASE:identifer   The name of subtype
 * @param  NOTATION:const char*  The notation for the subtype
 */
#define NODE(LOWERCASE, NOTATION)					\
  mds_kbdc_tree_##LOWERCASE##_t* node;					\
  node = (mds_kbdc_tree_##LOWERCASE##_t*)this;				\
  fprintf(output, "%*.s(\033[01m%s\033[00m", indent, "", NOTATION);	\
  fprintf(output, " \033[36m(@ %zu %zu-%zu)\033[00m",			\
	  node->loc_line + 1, node->loc_start, node->loc_end)


/**
 * Print a member for `node` which is a subtree
 * 
 * @param  MEMBER:identifier  The tree structure's member
 */
#define BRANCH(MEMBER)								\
  if (node->MEMBER)								\
    {										\
      fprintf(output, "\n%*.s(.%s\n", indent + 2, "", #MEMBER);			\
      mds_kbdc_tree_print_indented(node->MEMBER, output, indent + 4);		\
      fprintf(output, "%*.s)", indent + 2, "");					\
    }										\
  else										\
    fprintf(output, "\n%*.s(.%s \033[35mnil\033[00m)", indent + 2, "", #MEMBER)


/**
 * End a tree which has at least one member that is a subtree
 */
#define COMPLEX					\
  fprintf(output, "\n%*.s)\n", indent, "")


/**
 * Print a member for `node` which is a string
 * 
 * @param  MEMBER:identifier  The tree structure's member
 */
#define STRING(MEMBER)						\
  if (node->MEMBER)						\
    fprintf(output, " ‘\033[32m%s\033[00m’", node->MEMBER);	\
  else								\
    fprintf(output, " \033[35mnil\033[00m")


/**
 * Print a member for `node` which is a string,
 * and end the tree
 * 
 * @param  MEMBER:identifier  The tree structure's member
 */
#define SIMPLE(MEMBER)				\
  STRING(MEMBER);				\
  fprintf(output, ")\n", node->MEMBER)


/**
 * Print a tree which has only one member,
 * and whose member is a string
 * 
 * @param  LOWERCASE:identifier  See `NODE`
 * @param  NOTATION:const char*  See `NODE`
 * @param  MEMBER:identifier     See `STRING`
 */
#define SIMPLEX(LOWERCASE, NOTATION, MEMBER)	\
  {						\
    NODE(LOWERCASE, NOTATION);			\
    SIMPLE(MEMBER);				\
  }						\
  break


/**
 * Print a tree which has exactly two members,
 * and whose members is are strings
 * 
 * @param  LOWERCASE:identifier  See `NODE`
 * @param  NOTATION:const char*  See `NODE`
 * @param  FIRST:identifier      See `STRING`, the first member
 * @param  LAST:identifier       See `STRING`, the second member
 */
#define DUPLEX(LOWERCASE, NOTATION, FIRST, LAST)	\
  {							\
    NODE(LOWERCASE, NOTATION);				\
    STRING(FIRST);					\
    SIMPLE(LAST);					\
  }							\
  break


/**
 * Print a tree which has exactly one member,
 * and whose members is a subtree
 * 
 * @param  LOWERCASE:identifier  See `NODE`
 * @param  NOTATION:const char*  See `NODE`
 * @param  MEMBER:identifier     See `BRANCH`
 */
#define NESTING(LOWERCASE, NOTATION, MEMBER)	\
  {						\
    NODE(LOWERCASE, NOTATION);			\
    BRANCH(MEMBER);				\
    COMPLEX;					\
  }						\
  break


/**
 * Print a tree which has exactly two members,
 * and whose first member is a string and second
 * member is a subtree
 * 
 * @param  LOWERCASE:identifier  See `NODE`
 * @param  NOTATION:const char*  See `NODE`
 * @param  NAMER:identifier      See `STRING`
 * @param  MEMBER:identifier     See `BRANCH`
 */
#define NAMED_NESTING(LOWERCASE, NOTATION, NAMER, MEMBER)	\
  {								\
    NODE(LOWERCASE, NOTATION);					\
    STRING(NAMER);						\
    BRANCH(MEMBER);						\
    COMPLEX;							\
  }								\
  break


/**
 * Print a tree which has no members
 * 
 * @param  NOTATION:const char*  See `NODE`
 */
#define NOTHING(NOTATION)						\
  fprintf(output, "%*.s(\033[01m%s\033[00m", indent, "", NOTATION);	\
  fprintf(output, " \033[36m(@ %zu %zu-%zu)\033[00m",			\
	  this->loc_line + 1, this->loc_start, this->loc_end);		\
  fprintf(output, ")\n");						\
  break



/**
 * Print a tree into a file
 * 
 * @param  this    The tree node
 * @param  output  The output file
 * @param  indent  The indent
 */
static void mds_kbdc_tree_print_indented(mds_kbdc_tree_t* restrict this, FILE* output, int indent)
{
 again:
  if (this == NULL)
    return;
  
  switch (this->type)
    {
      /* These have their break built into their macro. */
    case C(INFORMATION):            NESTING(information, "information", inner);
    case C(INFORMATION_LANGUAGE):   SIMPLEX(information_language, "language", data);
    case C(INFORMATION_COUNTRY):    SIMPLEX(information_country, "country", data);
    case C(INFORMATION_VARIANT):    SIMPLEX(information_variant, "variant", data);
    case C(INCLUDE):                NAMED_NESTING(include, "include", filename, inner);
    case C(FUNCTION):               NAMED_NESTING(function, "function", name, inner);
    case C(MACRO):                  NAMED_NESTING(macro, "macro", name, inner);
    case C(ASSUMPTION):             NESTING(assumption, "assumption", inner);
    case C(ASSUMPTION_HAVE):        NESTING(assumption_have, "have", data);
    case C(ASSUMPTION_HAVE_CHARS):  SIMPLEX(assumption_have_chars, "have_chars", chars);
    case C(ASSUMPTION_HAVE_RANGE):  DUPLEX(assumption_have_range, "have_range", first, last);
    case C(LET):                    NAMED_NESTING(let, "let", variable, value);
    case C(ARRAY):                  NESTING(array, "array", elements);
    case C(KEYS):                   SIMPLEX(keys, "keys", keys);
    case C(STRING):                 SIMPLEX(string, "string", string);
    case C(NOTHING):                NOTHING("nothing");
    case C(ALTERNATION):            NESTING(alternation, "alternation", inner);
    case C(UNORDERED):              NESTING(unordered, "unordered", inner);
    case C(ORDERED):                NESTING(ordered, "ordered", inner);
    case C(MACRO_CALL):             NAMED_NESTING(macro_call, "macro_call", name, arguments);
    case C(RETURN):                 NOTHING("return");
    case C(BREAK):                  NOTHING("break");
    case C(CONTINUE):               NOTHING("continue");
      
    case C(FOR):
      {
	NODE(for, "for");
	STRING(first);
	STRING(last);
	fprintf(output, " (.variable");
	STRING(variable);
	fprintf(output, ")");
	BRANCH(inner);
	COMPLEX;
      }
      break;
      
    case C(IF):
      {
	NODE(if, "if");
	STRING(condition);
	BRANCH(inner);
	BRANCH(otherwise);
	COMPLEX;
      }
      break;
      
    case C(MAP):
      {
	NODE(map, "map");
	BRANCH(sequence);
	BRANCH(result);
	COMPLEX;
      }
      break;
      
    default:
      abort();
      break;
    }
  
  this = this->next;
  goto again;
}


/**
 * Print a tree into a file
 * 
 * @param  this    The tree node
 * @param  output  The output file
 */
void mds_kbdc_tree_print(mds_kbdc_tree_t* restrict this, FILE* output)
{
  mds_kbdc_tree_print_indented(this, output, 0);
}


#undef NOTHING
#undef NAMED_NESTING
#undef NESTING
#undef DUPLEX
#undef SIMPLEX
#undef SIMPLE
#undef STRING
#undef COMPLEX
#undef BRANCH
#undef NODE

#undef C