basic_functions.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. // Concatenation functions.
  168. static int concat_recurser (ExpString *estr, NCDValRef arg, struct NCDModuleFunction_eval_params const *params)
  169. {
  170. if (NCDVal_IsString(arg)) {
  171. if (!ExpString_AppendBinary(estr, (uint8_t const *)NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
  172. FunctionLog(params, BLOG_ERROR, "ExpString_AppendBinary failed");
  173. return 0;
  174. }
  175. } else if (NCDVal_IsList(arg)) {
  176. size_t count = NCDVal_ListCount(arg);
  177. for (size_t i = 0; i < count; i++) {
  178. if (!concat_recurser(estr, NCDVal_ListGet(arg, i), params)) {
  179. return 0;
  180. }
  181. }
  182. } else {
  183. FunctionLog(params, BLOG_ERROR, "concat: value is not a string or list");
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. static int concat_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  189. {
  190. int res = 0;
  191. ExpString estr;
  192. if (!ExpString_Init(&estr)) {
  193. FunctionLog(params, BLOG_ERROR, "ExpString_Init failed");
  194. goto fail0;
  195. }
  196. size_t count = NCDEvaluatorArgs_Count(&args);
  197. for (size_t i = 0; i < count; i++) {
  198. NCDValRef arg;
  199. if (!NCDEvaluatorArgs_EvalArg(&args, i, mem, &arg)) {
  200. goto fail1;
  201. }
  202. if (!concat_recurser(&estr, arg, params)) {
  203. goto fail1;
  204. }
  205. }
  206. *out = NCDVal_NewStringBin(mem, (uint8_t const *)ExpString_Get(&estr), ExpString_Length(&estr));
  207. res = 1;
  208. fail1:
  209. ExpString_Free(&estr);
  210. fail0:
  211. return res;
  212. }
  213. static int concatlist_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
  214. {
  215. int res = 0;
  216. NCDValRef args_list;
  217. if (!ncd_eval_func_args(args, mem, &args_list)) {
  218. goto fail0;
  219. }
  220. size_t arg_count = NCDVal_ListCount(args_list);
  221. size_t elem_count = 0;
  222. for (size_t i = 0; i < arg_count; i++) {
  223. NCDValRef arg = NCDVal_ListGet(args_list, i);
  224. if (!NCDVal_IsList(arg)) {
  225. FunctionLog(params, BLOG_ERROR, "concatlist: argument is not a list");
  226. goto fail0;
  227. }
  228. elem_count += NCDVal_ListCount(arg);
  229. }
  230. *out = NCDVal_NewList(mem, elem_count);
  231. if (NCDVal_IsInvalid(*out)) {
  232. goto fail0;
  233. }
  234. for (size_t i = 0; i < arg_count; i++) {
  235. NCDValRef arg = NCDVal_ListGet(args_list, i);
  236. size_t arg_list_count = NCDVal_ListCount(arg);
  237. for (size_t j = 0; j < arg_list_count; j++) {
  238. NCDValRef copy = NCDVal_NewCopy(mem, NCDVal_ListGet(arg, j));
  239. if (NCDVal_IsInvalid(copy)) {
  240. goto fail0;
  241. }
  242. if (!NCDVal_ListAppend(*out, copy)) {
  243. goto fail0;
  244. }
  245. }
  246. }
  247. res = 1;
  248. fail0:
  249. return res;
  250. }
  251. // Integer comparison functions.
  252. typedef int (*integer_compare_func) (uintmax_t n1, uintmax_t n2);
  253. static int integer_compare_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params, integer_compare_func func)
  254. {
  255. int res = 0;
  256. if (NCDEvaluatorArgs_Count(&args) != 2) {
  257. FunctionLog(params, BLOG_ERROR, "integer_compare: need two arguments");
  258. goto fail0;
  259. }
  260. uintmax_t ints[2];
  261. for (int i = 0; i < 2; i++) {
  262. NCDValRef arg;
  263. if (!NCDEvaluatorArgs_EvalArg(&args, i, mem, &arg)) {
  264. goto fail0;
  265. }
  266. if (!NCDVal_IsString(arg)) {
  267. FunctionLog(params, BLOG_ERROR, "integer_compare: wrong type");
  268. goto fail0;
  269. }
  270. if (!ncd_read_uintmax(arg, &ints[i])) {
  271. FunctionLog(params, BLOG_ERROR, "integer_compare: wrong value");
  272. goto fail0;
  273. }
  274. }
  275. int value = func(ints[0], ints[1]);
  276. *out = ncd_make_boolean(mem, value, params->params->iparams->string_index);
  277. res = 1;
  278. fail0:
  279. return res;
  280. }
  281. #define DEFINE_INT_COMPARE(name, expr) \
  282. static int integer_compare_##name##_func (uintmax_t n1, uintmax_t n2) \
  283. { \
  284. return expr; \
  285. } \
  286. static int integer_compare_##name##_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params) \
  287. { \
  288. return integer_compare_eval(args, mem, out, params, integer_compare_##name##_func); \
  289. }
  290. DEFINE_INT_COMPARE(lesser, (n1 < n2))
  291. DEFINE_INT_COMPARE(greater, (n1 > n2))
  292. DEFINE_INT_COMPARE(lesser_equal, (n1 <= n2))
  293. DEFINE_INT_COMPARE(greater_equal, (n1 >= n2))
  294. DEFINE_INT_COMPARE(equal, (n1 == n2))
  295. DEFINE_INT_COMPARE(different, (n1 != n2))
  296. // Integer operators.
  297. typedef int (*integer_operator_func) (uintmax_t n1, uintmax_t n2, uintmax_t *out, struct NCDModuleFunction_eval_params const *params);
  298. static int integer_operator_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params, integer_operator_func func)
  299. {
  300. int res = 0;
  301. if (NCDEvaluatorArgs_Count(&args) != 2) {
  302. FunctionLog(params, BLOG_ERROR, "integer_operator: need two arguments");
  303. goto fail0;
  304. }
  305. uintmax_t ints[2];
  306. for (int i = 0; i < 2; i++) {
  307. NCDValRef arg;
  308. if (!NCDEvaluatorArgs_EvalArg(&args, i, mem, &arg)) {
  309. goto fail0;
  310. }
  311. if (!NCDVal_IsString(arg)) {
  312. FunctionLog(params, BLOG_ERROR, "integer_operator: wrong type");
  313. goto fail0;
  314. }
  315. if (!ncd_read_uintmax(arg, &ints[i])) {
  316. FunctionLog(params, BLOG_ERROR, "integer_operator: wrong value");
  317. goto fail0;
  318. }
  319. }
  320. uintmax_t value;
  321. if (!func(ints[0], ints[1], &value, params)) {
  322. goto fail0;
  323. }
  324. *out = ncd_make_uintmax(mem, value);
  325. res = 1;
  326. fail0:
  327. return res;
  328. }
  329. #define DEFINE_INT_OPERATOR(name, expr, check_expr, check_err_str) \
  330. static int integer_operator_##name##_func (uintmax_t n1, uintmax_t n2, uintmax_t *out, struct NCDModuleFunction_eval_params const *params) \
  331. { \
  332. if (check_expr) { \
  333. FunctionLog(params, BLOG_ERROR, check_err_str); \
  334. return 0; \
  335. } \
  336. *out = expr; \
  337. return 1; \
  338. } \
  339. static int integer_operator_##name##_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params) \
  340. { \
  341. return integer_operator_eval(args, mem, out, params, integer_operator_##name##_func); \
  342. }
  343. DEFINE_INT_OPERATOR(add, (n1 + n2), (n1 > UINTMAX_MAX - n2), "addition overflow")
  344. DEFINE_INT_OPERATOR(subtract, (n1 - n2), (n1 < n2), "subtraction underflow")
  345. DEFINE_INT_OPERATOR(multiply, (n1 * n2), (n2 != 0 && n1 > UINTMAX_MAX / n2), "multiplication overflow")
  346. DEFINE_INT_OPERATOR(divide, (n1 / n2), (n2 == 0), "division quotient is zero")
  347. DEFINE_INT_OPERATOR(modulo, (n1 % n2), (n2 == 0), "modulo modulus is zero")
  348. static struct NCDModuleFunction const functions[] = {
  349. {
  350. .func_name = "__error__",
  351. .func_eval = error_eval
  352. }, {
  353. .func_name = "__identity__",
  354. .func_eval = identity_eval
  355. }, {
  356. .func_name = "__if__",
  357. .func_eval = if_eval
  358. }, {
  359. .func_name = "__bool__",
  360. .func_eval = bool_eval
  361. }, {
  362. .func_name = "__not__",
  363. .func_eval = not_eval
  364. }, {
  365. .func_name = "__and__",
  366. .func_eval = and_eval
  367. }, {
  368. .func_name = "__or__",
  369. .func_eval = or_eval
  370. }, {
  371. .func_name = "__imp__",
  372. .func_eval = imp_eval
  373. }, {
  374. .func_name = "__concat__",
  375. .func_eval = concat_eval
  376. }, {
  377. .func_name = "__concatlist__",
  378. .func_eval = concatlist_eval
  379. }, {
  380. .func_name = "__num_lesser__",
  381. .func_eval = integer_compare_lesser_eval
  382. }, {
  383. .func_name = "__num_greater__",
  384. .func_eval = integer_compare_greater_eval
  385. }, {
  386. .func_name = "__num_lesser_equal__",
  387. .func_eval = integer_compare_lesser_equal_eval
  388. }, {
  389. .func_name = "__num_greater_equal__",
  390. .func_eval = integer_compare_greater_equal_eval
  391. }, {
  392. .func_name = "__num_equal__",
  393. .func_eval = integer_compare_equal_eval
  394. }, {
  395. .func_name = "__num_different__",
  396. .func_eval = integer_compare_different_eval
  397. }, {
  398. .func_name = "__num_add__",
  399. .func_eval = integer_operator_add_eval
  400. }, {
  401. .func_name = "__num_subtract__",
  402. .func_eval = integer_operator_subtract_eval
  403. }, {
  404. .func_name = "__num_multiply__",
  405. .func_eval = integer_operator_multiply_eval
  406. }, {
  407. .func_name = "__num_divide__",
  408. .func_eval = integer_operator_divide_eval
  409. }, {
  410. .func_name = "__num_modulo__",
  411. .func_eval = integer_operator_modulo_eval
  412. }, {
  413. .func_name = NULL
  414. }
  415. };
  416. const struct NCDModuleGroup ncdmodule_basic_functions = {
  417. .functions = functions
  418. };