basic_functions.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 <misc/expstring.h>
  30. #include <misc/bsize.h>
  31. #include <ncd/module_common.h>
  32. #include <generated/blog_channel_ncd_basic_functions.h>
  33. // Trivial functions.
  34. static void error_eval (NCDCall call)
  35. {
  36. FunctionLog(&call, BLOG_ERROR, "error: failing");
  37. }
  38. static void identity_eval (NCDCall call)
  39. {
  40. if (NCDCall_ArgCount(&call) != 1) {
  41. return FunctionLog(&call, BLOG_ERROR, "identity: need one argument");
  42. }
  43. NCDCall_SetResult(&call, NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)));
  44. }
  45. // Logical functions.
  46. static void if_eval (NCDCall call)
  47. {
  48. if (NCDCall_ArgCount(&call) != 3) {
  49. return FunctionLog(&call, BLOG_ERROR, "if: need three arguments");
  50. }
  51. NCDValRef cond = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call));
  52. if (NCDVal_IsInvalid(cond)) {
  53. return;
  54. }
  55. int cond_val;
  56. if (!ncd_read_boolean(cond, &cond_val)) {
  57. return FunctionLog(&call, BLOG_ERROR, "if: bad condition");
  58. }
  59. int eval_arg = 2 - cond_val;
  60. NCDCall_SetResult(&call, NCDCall_EvalArg(&call, eval_arg, NCDCall_ResMem(&call)));
  61. }
  62. static void bool_not_eval (NCDCall call, int negate, char const *name)
  63. {
  64. if (NCDCall_ArgCount(&call) != 1) {
  65. return FunctionLog(&call, BLOG_ERROR, "%s: need one argument", name);
  66. }
  67. NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call));
  68. if (NCDVal_IsInvalid(arg)) {
  69. return;
  70. }
  71. int arg_val;
  72. if (!ncd_read_boolean(arg, &arg_val)) {
  73. return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name);
  74. }
  75. int res = (arg_val != negate);
  76. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  77. }
  78. static void bool_eval (NCDCall call) { return bool_not_eval(call, 0, "bool"); }
  79. static void not_eval (NCDCall call) { return bool_not_eval(call, 1, "not"); }
  80. static void and_or_eval (NCDCall call, int is_and, char const *name)
  81. {
  82. size_t count = NCDCall_ArgCount(&call);
  83. int res = is_and;
  84. for (size_t i = 0; i < count; i++) {
  85. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  86. if (NCDVal_IsInvalid(arg)) {
  87. return;
  88. }
  89. int arg_val;
  90. if (!ncd_read_boolean(arg, &arg_val)) {
  91. return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name);
  92. }
  93. if (arg_val != is_and) {
  94. res = !is_and;
  95. break;
  96. }
  97. }
  98. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  99. }
  100. static void and_eval (NCDCall call) { return and_or_eval(call, 1, "and"); }
  101. static void or_eval (NCDCall call) { return and_or_eval(call, 0, "or"); }
  102. static void imp_eval (NCDCall call)
  103. {
  104. if (NCDCall_ArgCount(&call) != 2) {
  105. return FunctionLog(&call, BLOG_ERROR, "imp: need two arguments");
  106. }
  107. int res = 0;
  108. for (size_t i = 0; i < 2; i++) {
  109. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  110. if (NCDVal_IsInvalid(arg)) {
  111. return;
  112. }
  113. int arg_val;
  114. if (!ncd_read_boolean(arg, &arg_val)) {
  115. return FunctionLog(&call, BLOG_ERROR, "imp: bad argument");
  116. }
  117. if (arg_val == i) {
  118. res = 1;
  119. break;
  120. }
  121. }
  122. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  123. }
  124. // Value comparison functions.
  125. typedef int (*value_compare_func) (int cmp);
  126. static void value_compare_eval (NCDCall call, value_compare_func func)
  127. {
  128. if (NCDCall_ArgCount(&call) != 2) {
  129. return FunctionLog(&call, BLOG_ERROR, "value_compare: need two arguments");
  130. }
  131. NCDValRef vals[2];
  132. for (int i = 0; i < 2; i++) {
  133. vals[i] = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  134. if (NCDVal_IsInvalid(vals[i])) {
  135. return;
  136. }
  137. }
  138. int res = func(NCDVal_Compare(vals[0], vals[1]));
  139. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  140. }
  141. #define DEFINE_VALUE_COMPARE(name, expr) \
  142. static int value_compare_##name##_func (int cmp) { return expr; } \
  143. static void value_compare_##name##_eval (NCDCall call) { return value_compare_eval(call, value_compare_##name##_func); }
  144. DEFINE_VALUE_COMPARE(lesser, (cmp < 0))
  145. DEFINE_VALUE_COMPARE(greater, (cmp > 0))
  146. DEFINE_VALUE_COMPARE(lesser_equal, (cmp <= 0))
  147. DEFINE_VALUE_COMPARE(greater_equal, (cmp >= 0))
  148. DEFINE_VALUE_COMPARE(equal, (cmp == 0))
  149. DEFINE_VALUE_COMPARE(different, (cmp != 0))
  150. // Concatenation functions.
  151. static int concat_recurser (ExpString *estr, NCDValRef arg, NCDCall const *call)
  152. {
  153. if (NCDVal_IsString(arg)) {
  154. if (!ExpString_AppendBinary(estr, (uint8_t const *)NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
  155. FunctionLog(call, BLOG_ERROR, "ExpString_AppendBinary failed");
  156. return 0;
  157. }
  158. } else if (NCDVal_IsList(arg)) {
  159. size_t count = NCDVal_ListCount(arg);
  160. for (size_t i = 0; i < count; i++) {
  161. if (!concat_recurser(estr, NCDVal_ListGet(arg, i), call)) {
  162. return 0;
  163. }
  164. }
  165. } else {
  166. FunctionLog(call, BLOG_ERROR, "concat: value is not a string or list");
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. static void concat_eval (NCDCall call)
  172. {
  173. ExpString estr;
  174. if (!ExpString_Init(&estr)) {
  175. FunctionLog(&call, BLOG_ERROR, "ExpString_Init failed");
  176. goto fail0;
  177. }
  178. size_t count = NCDCall_ArgCount(&call);
  179. for (size_t i = 0; i < count; i++) {
  180. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  181. if (NCDVal_IsInvalid(arg)) {
  182. goto fail1;
  183. }
  184. if (!concat_recurser(&estr, arg, &call)) {
  185. goto fail1;
  186. }
  187. }
  188. NCDCall_SetResult(&call, NCDVal_NewStringBin(NCDCall_ResMem(&call), (uint8_t const *)ExpString_Get(&estr), ExpString_Length(&estr)));
  189. fail1:
  190. ExpString_Free(&estr);
  191. fail0:
  192. return;
  193. }
  194. static void concatlist_eval (NCDCall call)
  195. {
  196. NCDValRef args_list;
  197. if (!ncd_eval_func_args(&call, NCDCall_ResMem(&call), &args_list)) {
  198. return;
  199. }
  200. size_t arg_count = NCDVal_ListCount(args_list);
  201. bsize_t elem_count = bsize_fromsize(0);
  202. for (size_t i = 0; i < arg_count; i++) {
  203. NCDValRef arg = NCDVal_ListGet(args_list, i);
  204. if (!NCDVal_IsList(arg)) {
  205. return FunctionLog(&call, BLOG_ERROR, "concatlist: argument is not a list");
  206. }
  207. elem_count = bsize_add(elem_count, bsize_fromsize(NCDVal_ListCount(arg)));
  208. }
  209. if (elem_count.is_overflow) {
  210. return FunctionLog(&call, BLOG_ERROR, "concatlist: count overflow");
  211. }
  212. NCDValRef res = NCDVal_NewList(NCDCall_ResMem(&call), elem_count.value);
  213. if (NCDVal_IsInvalid(res)) {
  214. return;
  215. }
  216. for (size_t i = 0; i < arg_count; i++) {
  217. NCDValRef arg = NCDVal_ListGet(args_list, i);
  218. size_t arg_list_count = NCDVal_ListCount(arg);
  219. for (size_t j = 0; j < arg_list_count; j++) {
  220. NCDValRef copy = NCDVal_NewCopy(NCDCall_ResMem(&call), NCDVal_ListGet(arg, j));
  221. if (NCDVal_IsInvalid(copy)) {
  222. return;
  223. }
  224. if (!NCDVal_ListAppend(res, copy)) {
  225. return;
  226. }
  227. }
  228. }
  229. NCDCall_SetResult(&call, res);
  230. }
  231. // Integer comparison functions.
  232. typedef int (*integer_compare_func) (uintmax_t n1, uintmax_t n2);
  233. static void integer_compare_eval (NCDCall call, integer_compare_func func)
  234. {
  235. if (NCDCall_ArgCount(&call) != 2) {
  236. return FunctionLog(&call, BLOG_ERROR, "integer_compare: need two arguments");
  237. }
  238. uintmax_t ints[2];
  239. for (int i = 0; i < 2; i++) {
  240. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  241. if (NCDVal_IsInvalid(arg)) {
  242. return;
  243. }
  244. if (!ncd_read_uintmax(arg, &ints[i])) {
  245. return FunctionLog(&call, BLOG_ERROR, "integer_compare: wrong value");
  246. }
  247. }
  248. int res = func(ints[0], ints[1]);
  249. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  250. }
  251. #define DEFINE_INT_COMPARE(name, expr) \
  252. static int integer_compare_##name##_func (uintmax_t n1, uintmax_t n2) { return expr; } \
  253. static void integer_compare_##name##_eval (NCDCall call) { return integer_compare_eval(call, integer_compare_##name##_func); }
  254. DEFINE_INT_COMPARE(lesser, (n1 < n2))
  255. DEFINE_INT_COMPARE(greater, (n1 > n2))
  256. DEFINE_INT_COMPARE(lesser_equal, (n1 <= n2))
  257. DEFINE_INT_COMPARE(greater_equal, (n1 >= n2))
  258. DEFINE_INT_COMPARE(equal, (n1 == n2))
  259. DEFINE_INT_COMPARE(different, (n1 != n2))
  260. // Integer operators.
  261. typedef int (*integer_operator_func) (uintmax_t n1, uintmax_t n2, uintmax_t *out, NCDCall const *call);
  262. static void integer_operator_eval (NCDCall call, integer_operator_func func)
  263. {
  264. if (NCDCall_ArgCount(&call) != 2) {
  265. return FunctionLog(&call, BLOG_ERROR, "integer_operator: need two arguments");
  266. }
  267. uintmax_t ints[2];
  268. for (int i = 0; i < 2; i++) {
  269. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  270. if (NCDVal_IsInvalid(arg)) {
  271. return;
  272. }
  273. if (!ncd_read_uintmax(arg, &ints[i])) {
  274. return FunctionLog(&call, BLOG_ERROR, "integer_operator: wrong value");
  275. }
  276. }
  277. uintmax_t res;
  278. if (!func(ints[0], ints[1], &res, &call)) {
  279. return;
  280. }
  281. NCDCall_SetResult(&call, ncd_make_uintmax(NCDCall_ResMem(&call), res));
  282. }
  283. #define DEFINE_INT_OPERATOR(name, expr, check_expr, check_err_str) \
  284. static int integer_operator_##name##_func (uintmax_t n1, uintmax_t n2, uintmax_t *out, NCDCall const *call) \
  285. { \
  286. if (check_expr) { \
  287. FunctionLog(call, BLOG_ERROR, check_err_str); \
  288. return 0; \
  289. } \
  290. *out = expr; \
  291. return 1; \
  292. } \
  293. static void integer_operator_##name##_eval (NCDCall call) { return integer_operator_eval(call, integer_operator_##name##_func); }
  294. DEFINE_INT_OPERATOR(add, (n1 + n2), (n1 > UINTMAX_MAX - n2), "addition overflow")
  295. DEFINE_INT_OPERATOR(subtract, (n1 - n2), (n1 < n2), "subtraction underflow")
  296. DEFINE_INT_OPERATOR(multiply, (n1 * n2), (n2 != 0 && n1 > UINTMAX_MAX / n2), "multiplication overflow")
  297. DEFINE_INT_OPERATOR(divide, (n1 / n2), (n2 == 0), "division quotient is zero")
  298. DEFINE_INT_OPERATOR(modulo, (n1 % n2), (n2 == 0), "modulo modulus is zero")
  299. static struct NCDModuleFunction const functions[] = {
  300. {
  301. .func_name = "__error__",
  302. .func_eval = error_eval
  303. }, {
  304. .func_name = "__identity__",
  305. .func_eval = identity_eval
  306. }, {
  307. .func_name = "__if__",
  308. .func_eval = if_eval
  309. }, {
  310. .func_name = "__bool__",
  311. .func_eval = bool_eval
  312. }, {
  313. .func_name = "__not__",
  314. .func_eval = not_eval
  315. }, {
  316. .func_name = "__and__",
  317. .func_eval = and_eval
  318. }, {
  319. .func_name = "__or__",
  320. .func_eval = or_eval
  321. }, {
  322. .func_name = "__imp__",
  323. .func_eval = imp_eval
  324. }, {
  325. .func_name = "__val_lesser__",
  326. .func_eval = value_compare_lesser_eval
  327. }, {
  328. .func_name = "__val_greater__",
  329. .func_eval = value_compare_greater_eval
  330. }, {
  331. .func_name = "__val_lesser_equal__",
  332. .func_eval = value_compare_lesser_equal_eval
  333. }, {
  334. .func_name = "__val_greater_equal__",
  335. .func_eval = value_compare_greater_equal_eval
  336. }, {
  337. .func_name = "__val_equal__",
  338. .func_eval = value_compare_equal_eval
  339. }, {
  340. .func_name = "__val_different__",
  341. .func_eval = value_compare_different_eval
  342. }, {
  343. .func_name = "__concat__",
  344. .func_eval = concat_eval
  345. }, {
  346. .func_name = "__concatlist__",
  347. .func_eval = concatlist_eval
  348. }, {
  349. .func_name = "__num_lesser__",
  350. .func_eval = integer_compare_lesser_eval
  351. }, {
  352. .func_name = "__num_greater__",
  353. .func_eval = integer_compare_greater_eval
  354. }, {
  355. .func_name = "__num_lesser_equal__",
  356. .func_eval = integer_compare_lesser_equal_eval
  357. }, {
  358. .func_name = "__num_greater_equal__",
  359. .func_eval = integer_compare_greater_equal_eval
  360. }, {
  361. .func_name = "__num_equal__",
  362. .func_eval = integer_compare_equal_eval
  363. }, {
  364. .func_name = "__num_different__",
  365. .func_eval = integer_compare_different_eval
  366. }, {
  367. .func_name = "__num_add__",
  368. .func_eval = integer_operator_add_eval
  369. }, {
  370. .func_name = "__num_subtract__",
  371. .func_eval = integer_operator_subtract_eval
  372. }, {
  373. .func_name = "__num_multiply__",
  374. .func_eval = integer_operator_multiply_eval
  375. }, {
  376. .func_name = "__num_divide__",
  377. .func_eval = integer_operator_divide_eval
  378. }, {
  379. .func_name = "__num_modulo__",
  380. .func_eval = integer_operator_modulo_eval
  381. }, {
  382. .func_name = NULL
  383. }
  384. };
  385. const struct NCDModuleGroup ncdmodule_basic_functions = {
  386. .functions = functions
  387. };