basic_functions.c 15 KB

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