basic_functions.c 17 KB

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