basic_functions.c 15 KB

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