basic_functions.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @file basic_functions.c
  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. #include <ncd/module_common.h>
  30. #include <generated/blog_channel_ncd_basic_functions.h>
  31. static int error_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  32. {
  33. FunctionLog(params, BLOG_ERROR, "error: failing");
  34. return 0;
  35. }
  36. static int identity_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  37. {
  38. if (NCDEvaluatorArgs_Count(&args) != 1) {
  39. FunctionLog(params, BLOG_ERROR, "identity: need one argument");
  40. return 0;
  41. }
  42. return NCDEvaluatorArgs_EvalArg(&args, 0, mem, out);
  43. }
  44. static int if_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  45. {
  46. if (NCDEvaluatorArgs_Count(&args) != 3) {
  47. FunctionLog(params, BLOG_ERROR, "if: need three arguments");
  48. return 0;
  49. }
  50. NCDValRef cond;
  51. if (!NCDEvaluatorArgs_EvalArg(&args, 0, mem, &cond)) {
  52. return 0;
  53. }
  54. int eval_arg = 2 - ncd_read_boolean(cond);
  55. return NCDEvaluatorArgs_EvalArg(&args, eval_arg, mem, out);
  56. }
  57. static int bool_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  58. {
  59. if (NCDEvaluatorArgs_Count(&args) != 1) {
  60. FunctionLog(params, BLOG_ERROR, "bool: need one argument");
  61. return 0;
  62. }
  63. NCDValRef cond;
  64. if (!NCDEvaluatorArgs_EvalArg(&args, 0, mem, &cond)) {
  65. return 0;
  66. }
  67. int res = ncd_read_boolean(cond);
  68. *out = ncd_make_boolean(mem, res, params->params->iparams->string_index);
  69. return 1;
  70. }
  71. static int not_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  72. {
  73. if (NCDEvaluatorArgs_Count(&args) != 1) {
  74. FunctionLog(params, BLOG_ERROR, "not: need one argument");
  75. return 0;
  76. }
  77. NCDValRef cond;
  78. if (!NCDEvaluatorArgs_EvalArg(&args, 0, mem, &cond)) {
  79. return 0;
  80. }
  81. int res = !ncd_read_boolean(cond);
  82. *out = ncd_make_boolean(mem, res, params->params->iparams->string_index);
  83. return 1;
  84. }
  85. static int and_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  86. {
  87. size_t count = NCDEvaluatorArgs_Count(&args);
  88. int res = 1;
  89. for (size_t i = 0; i < count; i++) {
  90. NCDValRef cond;
  91. if (!NCDEvaluatorArgs_EvalArg(&args, i, mem, &cond)) {
  92. return 0;
  93. }
  94. if (!ncd_read_boolean(cond)) {
  95. res = 0;
  96. break;
  97. }
  98. }
  99. *out = ncd_make_boolean(mem, res, params->params->iparams->string_index);
  100. return 1;
  101. }
  102. static int or_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  103. {
  104. size_t count = NCDEvaluatorArgs_Count(&args);
  105. int res = 0;
  106. for (size_t i = 0; i < count; i++) {
  107. NCDValRef cond;
  108. if (!NCDEvaluatorArgs_EvalArg(&args, i, mem, &cond)) {
  109. return 0;
  110. }
  111. if (ncd_read_boolean(cond)) {
  112. res = 1;
  113. break;
  114. }
  115. }
  116. *out = ncd_make_boolean(mem, res, params->params->iparams->string_index);
  117. return 1;
  118. }
  119. static int imp_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  120. {
  121. if (NCDEvaluatorArgs_Count(&args) != 2) {
  122. FunctionLog(params, BLOG_ERROR, "imp: need two arguments");
  123. return 0;
  124. }
  125. int res = 0;
  126. for (size_t i = 0; i < 2; i++) {
  127. NCDValRef cond;
  128. if (!NCDEvaluatorArgs_EvalArg(&args, i, mem, &cond)) {
  129. return 0;
  130. }
  131. if (ncd_read_boolean(cond) == i) {
  132. res = 1;
  133. break;
  134. }
  135. }
  136. *out = ncd_make_boolean(mem, res, params->params->iparams->string_index);
  137. return 1;
  138. }
  139. static struct NCDModuleFunction const functions[] = {
  140. {
  141. .func_name = "__error__",
  142. .func_eval = error_eval
  143. }, {
  144. .func_name = "__identity__",
  145. .func_eval = identity_eval
  146. }, {
  147. .func_name = "__if__",
  148. .func_eval = if_eval
  149. }, {
  150. .func_name = "__bool__",
  151. .func_eval = bool_eval
  152. }, {
  153. .func_name = "__not__",
  154. .func_eval = not_eval
  155. }, {
  156. .func_name = "__and__",
  157. .func_eval = and_eval
  158. }, {
  159. .func_name = "__or__",
  160. .func_eval = or_eval
  161. }, {
  162. .func_name = "__imp__",
  163. .func_eval = imp_eval
  164. }, {
  165. .func_name = NULL
  166. }
  167. };
  168. const struct NCDModuleGroup ncdmodule_basic_functions = {
  169. .functions = functions
  170. };