BPredicate_internal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file BPredicate_internal.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * {@link BPredicate} expression tree definitions and functions.
  25. */
  26. #ifndef BADVPN_PREDICATE_BPREDICATE_INTERNAL_H
  27. #define BADVPN_PREDICATE_BPREDICATE_INTERNAL_H
  28. #include <misc/debug.h>
  29. #define NODE_CONSTANT 0
  30. #define NODE_NEG 2
  31. #define NODE_CONJUNCT 3
  32. #define NODE_DISJUNCT 4
  33. #define NODE_FUNCTION 5
  34. struct arguments_node;
  35. struct predicate_node {
  36. int type;
  37. union {
  38. struct {
  39. int val;
  40. } constant;
  41. struct {
  42. struct predicate_node *op;
  43. } neg;
  44. struct {
  45. struct predicate_node *op1;
  46. struct predicate_node *op2;
  47. } conjunct;
  48. struct {
  49. struct predicate_node *op1;
  50. struct predicate_node *op2;
  51. } disjunct;
  52. struct {
  53. char *name;
  54. struct arguments_node *args;
  55. } function;
  56. };
  57. int eval_value;
  58. };
  59. #define ARGUMENT_INVALID 0
  60. #define ARGUMENT_PREDICATE 1
  61. #define ARGUMENT_STRING 2
  62. struct arguments_arg {
  63. int type;
  64. union {
  65. struct predicate_node *predicate;
  66. char *string;
  67. };
  68. };
  69. struct arguments_node {
  70. struct arguments_arg arg;
  71. struct arguments_node *next;
  72. };
  73. static void free_predicate_node (struct predicate_node *root);
  74. static void free_argument (struct arguments_arg arg);
  75. static void free_arguments_node (struct arguments_node *n);
  76. void free_predicate_node (struct predicate_node *root)
  77. {
  78. ASSERT(root)
  79. switch (root->type) {
  80. case NODE_CONSTANT:
  81. break;
  82. case NODE_NEG:
  83. free_predicate_node(root->neg.op);
  84. break;
  85. case NODE_CONJUNCT:
  86. free_predicate_node(root->conjunct.op1);
  87. free_predicate_node(root->conjunct.op2);
  88. break;
  89. case NODE_DISJUNCT:
  90. free_predicate_node(root->disjunct.op1);
  91. free_predicate_node(root->disjunct.op2);
  92. break;
  93. case NODE_FUNCTION:
  94. free(root->function.name);
  95. if (root->function.args) {
  96. free_arguments_node(root->function.args);
  97. }
  98. break;
  99. default:
  100. ASSERT(0)
  101. break;
  102. }
  103. free(root);
  104. }
  105. void free_argument (struct arguments_arg arg)
  106. {
  107. switch (arg.type) {
  108. case ARGUMENT_INVALID:
  109. break;
  110. case ARGUMENT_PREDICATE:
  111. free_predicate_node(arg.predicate);
  112. break;
  113. case ARGUMENT_STRING:
  114. free(arg.string);
  115. break;
  116. default:
  117. ASSERT(0);
  118. }
  119. }
  120. void free_arguments_node (struct arguments_node *n)
  121. {
  122. ASSERT(n)
  123. free_argument(n->arg);
  124. if (n->next) {
  125. free_arguments_node(n->next);
  126. }
  127. free(n);
  128. }
  129. #endif