basic_functions.c 17 KB

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