basic_functions.c 16 KB

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