BPredicate_internal.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @file BPredicate_internal.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * {@link BPredicate} expression tree definitions and functions.
  32. */
  33. #ifndef BADVPN_PREDICATE_BPREDICATE_INTERNAL_H
  34. #define BADVPN_PREDICATE_BPREDICATE_INTERNAL_H
  35. #include <misc/debug.h>
  36. #define NODE_CONSTANT 0
  37. #define NODE_NEG 2
  38. #define NODE_CONJUNCT 3
  39. #define NODE_DISJUNCT 4
  40. #define NODE_FUNCTION 5
  41. struct arguments_node;
  42. struct predicate_node {
  43. int type;
  44. union {
  45. struct {
  46. int val;
  47. } constant;
  48. struct {
  49. struct predicate_node *op;
  50. } neg;
  51. struct {
  52. struct predicate_node *op1;
  53. struct predicate_node *op2;
  54. } conjunct;
  55. struct {
  56. struct predicate_node *op1;
  57. struct predicate_node *op2;
  58. } disjunct;
  59. struct {
  60. char *name;
  61. struct arguments_node *args;
  62. } function;
  63. };
  64. int eval_value;
  65. };
  66. #define ARGUMENT_INVALID 0
  67. #define ARGUMENT_PREDICATE 1
  68. #define ARGUMENT_STRING 2
  69. struct arguments_arg {
  70. int type;
  71. union {
  72. struct predicate_node *predicate;
  73. char *string;
  74. };
  75. };
  76. struct arguments_node {
  77. struct arguments_arg arg;
  78. struct arguments_node *next;
  79. };
  80. static void free_predicate_node (struct predicate_node *root);
  81. static void free_argument (struct arguments_arg arg);
  82. static void free_arguments_node (struct arguments_node *n);
  83. void free_predicate_node (struct predicate_node *root)
  84. {
  85. ASSERT(root)
  86. switch (root->type) {
  87. case NODE_CONSTANT:
  88. break;
  89. case NODE_NEG:
  90. free_predicate_node(root->neg.op);
  91. break;
  92. case NODE_CONJUNCT:
  93. free_predicate_node(root->conjunct.op1);
  94. free_predicate_node(root->conjunct.op2);
  95. break;
  96. case NODE_DISJUNCT:
  97. free_predicate_node(root->disjunct.op1);
  98. free_predicate_node(root->disjunct.op2);
  99. break;
  100. case NODE_FUNCTION:
  101. free(root->function.name);
  102. if (root->function.args) {
  103. free_arguments_node(root->function.args);
  104. }
  105. break;
  106. default:
  107. ASSERT(0)
  108. break;
  109. }
  110. free(root);
  111. }
  112. void free_argument (struct arguments_arg arg)
  113. {
  114. switch (arg.type) {
  115. case ARGUMENT_INVALID:
  116. break;
  117. case ARGUMENT_PREDICATE:
  118. free_predicate_node(arg.predicate);
  119. break;
  120. case ARGUMENT_STRING:
  121. free(arg.string);
  122. break;
  123. default:
  124. ASSERT(0);
  125. }
  126. }
  127. void free_arguments_node (struct arguments_node *n)
  128. {
  129. ASSERT(n)
  130. free_argument(n->arg);
  131. if (n->next) {
  132. free_arguments_node(n->next);
  133. }
  134. free(n);
  135. }
  136. #endif