NCDAst.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /**
  2. * @file NCDAst.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 <stdlib.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #include <misc/offset.h>
  33. #include <misc/strdup.h>
  34. #include "NCDAst.h"
  35. struct NCDValue__list_element {
  36. LinkedList1Node list_node;
  37. NCDValue v;
  38. };
  39. struct NCDValue__map_element {
  40. LinkedList1Node list_node;
  41. NCDValue key;
  42. NCDValue val;
  43. };
  44. struct ProgramElem {
  45. LinkedList1Node elems_list_node;
  46. NCDProgramElem elem;
  47. };
  48. struct BlockStatement {
  49. LinkedList1Node statements_list_node;
  50. NCDStatement s;
  51. };
  52. struct IfBlockIf {
  53. LinkedList1Node ifs_list_node;
  54. NCDIf ifc;
  55. };
  56. static void value_assert (NCDValue *o)
  57. {
  58. switch (o->type) {
  59. case NCDVALUE_STRING:
  60. case NCDVALUE_LIST:
  61. case NCDVALUE_MAP:
  62. case NCDVALUE_VAR:
  63. case NCDVALUE_INVOC:
  64. return;
  65. default:
  66. ASSERT(0);
  67. }
  68. }
  69. void NCDValue_Free (NCDValue *o)
  70. {
  71. switch (o->type) {
  72. case NCDVALUE_STRING: {
  73. free(o->string);
  74. } break;
  75. case NCDVALUE_LIST: {
  76. LinkedList1Node *n;
  77. while (n = LinkedList1_GetFirst(&o->list)) {
  78. struct NCDValue__list_element *e = UPPER_OBJECT(n, struct NCDValue__list_element, list_node);
  79. NCDValue_Free(&e->v);
  80. LinkedList1_Remove(&o->list, &e->list_node);
  81. free(e);
  82. }
  83. } break;
  84. case NCDVALUE_MAP: {
  85. LinkedList1Node *n;
  86. while (n = LinkedList1_GetFirst(&o->map_list)) {
  87. struct NCDValue__map_element *e = UPPER_OBJECT(n, struct NCDValue__map_element, list_node);
  88. LinkedList1_Remove(&o->map_list, &e->list_node);
  89. NCDValue_Free(&e->key);
  90. NCDValue_Free(&e->val);
  91. free(e);
  92. }
  93. } break;
  94. case NCDVALUE_VAR: {
  95. free(o->var_name);
  96. } break;
  97. case NCDVALUE_INVOC: {
  98. NCDValue_Free(o->invoc_arg);
  99. NCDValue_Free(o->invoc_func);
  100. free(o->invoc_arg);
  101. free(o->invoc_func);
  102. } break;
  103. default:
  104. ASSERT(0);
  105. }
  106. }
  107. int NCDValue_Type (NCDValue *o)
  108. {
  109. value_assert(o);
  110. return o->type;
  111. }
  112. int NCDValue_InitString (NCDValue *o, const char *str)
  113. {
  114. return NCDValue_InitStringBin(o, (const uint8_t *)str, strlen(str));
  115. }
  116. int NCDValue_InitStringBin (NCDValue *o, const uint8_t *str, size_t len)
  117. {
  118. if (len == SIZE_MAX) {
  119. return 0;
  120. }
  121. if (!(o->string = malloc(len + 1))) {
  122. return 0;
  123. }
  124. memcpy(o->string, str, len);
  125. o->string[len] = '\0';
  126. o->string_len = len;
  127. o->type = NCDVALUE_STRING;
  128. return 1;
  129. }
  130. const char * NCDValue_StringValue (NCDValue *o)
  131. {
  132. ASSERT(o->type == NCDVALUE_STRING)
  133. return (char *)o->string;
  134. }
  135. size_t NCDValue_StringLength (NCDValue *o)
  136. {
  137. ASSERT(o->type == NCDVALUE_STRING)
  138. return o->string_len;
  139. }
  140. void NCDValue_InitList (NCDValue *o)
  141. {
  142. o->type = NCDVALUE_LIST;
  143. LinkedList1_Init(&o->list);
  144. o->list_count = 0;
  145. }
  146. size_t NCDValue_ListCount (NCDValue *o)
  147. {
  148. value_assert(o);
  149. ASSERT(o->type == NCDVALUE_LIST)
  150. return o->list_count;
  151. }
  152. int NCDValue_ListAppend (NCDValue *o, NCDValue v)
  153. {
  154. value_assert(o);
  155. ASSERT(o->type == NCDVALUE_LIST)
  156. value_assert(&v);
  157. if (o->list_count == SIZE_MAX) {
  158. return 0;
  159. }
  160. struct NCDValue__list_element *e = malloc(sizeof(*e));
  161. if (!e) {
  162. return 0;
  163. }
  164. e->v = v;
  165. LinkedList1_Append(&o->list, &e->list_node);
  166. o->list_count++;
  167. return 1;
  168. }
  169. int NCDValue_ListPrepend (NCDValue *o, NCDValue v)
  170. {
  171. value_assert(o);
  172. ASSERT(o->type == NCDVALUE_LIST)
  173. value_assert(&v);
  174. if (o->list_count == SIZE_MAX) {
  175. return 0;
  176. }
  177. struct NCDValue__list_element *e = malloc(sizeof(*e));
  178. if (!e) {
  179. return 0;
  180. }
  181. e->v = v;
  182. LinkedList1_Prepend(&o->list, &e->list_node);
  183. o->list_count++;
  184. return 1;
  185. }
  186. NCDValue * NCDValue_ListFirst (NCDValue *o)
  187. {
  188. value_assert(o);
  189. ASSERT(o->type == NCDVALUE_LIST)
  190. LinkedList1Node *ln = LinkedList1_GetFirst(&o->list);
  191. if (!ln) {
  192. return NULL;
  193. }
  194. struct NCDValue__list_element *e = UPPER_OBJECT(ln, struct NCDValue__list_element, list_node);
  195. return &e->v;
  196. }
  197. NCDValue * NCDValue_ListNext (NCDValue *o, NCDValue *ev)
  198. {
  199. value_assert(o);
  200. ASSERT(o->type == NCDVALUE_LIST)
  201. struct NCDValue__list_element *cur_e = UPPER_OBJECT(ev, struct NCDValue__list_element, v);
  202. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->list_node);
  203. if (!ln) {
  204. return NULL;
  205. }
  206. struct NCDValue__list_element *e = UPPER_OBJECT(ln, struct NCDValue__list_element, list_node);
  207. return &e->v;
  208. }
  209. void NCDValue_InitMap (NCDValue *o)
  210. {
  211. o->type = NCDVALUE_MAP;
  212. LinkedList1_Init(&o->map_list);
  213. o->map_count = 0;
  214. }
  215. size_t NCDValue_MapCount (NCDValue *o)
  216. {
  217. value_assert(o);
  218. ASSERT(o->type == NCDVALUE_MAP)
  219. return o->map_count;
  220. }
  221. int NCDValue_MapPrepend (NCDValue *o, NCDValue key, NCDValue val)
  222. {
  223. value_assert(o);
  224. ASSERT(o->type == NCDVALUE_MAP)
  225. value_assert(&key);
  226. value_assert(&val);
  227. if (o->map_count == SIZE_MAX) {
  228. return 0;
  229. }
  230. struct NCDValue__map_element *e = malloc(sizeof(*e));
  231. if (!e) {
  232. return 0;
  233. }
  234. e->key = key;
  235. e->val = val;
  236. LinkedList1_Prepend(&o->map_list, &e->list_node);
  237. o->map_count++;
  238. return 1;
  239. }
  240. NCDValue * NCDValue_MapFirstKey (NCDValue *o)
  241. {
  242. value_assert(o);
  243. ASSERT(o->type == NCDVALUE_MAP)
  244. LinkedList1Node *ln = LinkedList1_GetFirst(&o->map_list);
  245. if (!ln) {
  246. return NULL;
  247. }
  248. struct NCDValue__map_element *e = UPPER_OBJECT(ln, struct NCDValue__map_element, list_node);
  249. value_assert(&e->key);
  250. value_assert(&e->val);
  251. return &e->key;
  252. }
  253. NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey)
  254. {
  255. value_assert(o);
  256. ASSERT(o->type == NCDVALUE_MAP)
  257. struct NCDValue__map_element *e0 = UPPER_OBJECT(ekey, struct NCDValue__map_element, key);
  258. value_assert(&e0->key);
  259. value_assert(&e0->val);
  260. LinkedList1Node *ln = LinkedList1Node_Next(&e0->list_node);
  261. if (!ln) {
  262. return NULL;
  263. }
  264. struct NCDValue__map_element *e = UPPER_OBJECT(ln, struct NCDValue__map_element, list_node);
  265. value_assert(&e->key);
  266. value_assert(&e->val);
  267. return &e->key;
  268. }
  269. NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey)
  270. {
  271. value_assert(o);
  272. ASSERT(o->type == NCDVALUE_MAP)
  273. struct NCDValue__map_element *e = UPPER_OBJECT(ekey, struct NCDValue__map_element, key);
  274. value_assert(&e->key);
  275. value_assert(&e->val);
  276. return &e->val;
  277. }
  278. int NCDValue_InitVar (NCDValue *o, const char *var_name)
  279. {
  280. ASSERT(var_name)
  281. if (!(o->var_name = strdup(var_name))) {
  282. return 0;
  283. }
  284. o->type = NCDVALUE_VAR;
  285. return 1;
  286. }
  287. const char * NCDValue_VarName (NCDValue *o)
  288. {
  289. value_assert(o);
  290. ASSERT(o->type == NCDVALUE_VAR)
  291. return o->var_name;
  292. }
  293. int NCDValue_InitInvoc (NCDValue *o, NCDValue func, NCDValue arg)
  294. {
  295. value_assert(&func);
  296. value_assert(&arg);
  297. if (!(o->invoc_func = malloc(sizeof(*o->invoc_func)))) {
  298. goto fail0;
  299. }
  300. if (!(o->invoc_arg = malloc(sizeof(*o->invoc_arg)))) {
  301. goto fail1;
  302. }
  303. *o->invoc_func = func;
  304. *o->invoc_arg = arg;
  305. o->type = NCDVALUE_INVOC;
  306. return 1;
  307. fail1:
  308. free(o->invoc_func);
  309. fail0:
  310. return 0;
  311. }
  312. NCDValue * NCDValue_InvocFunc (NCDValue *o)
  313. {
  314. value_assert(o);
  315. ASSERT(o->type == NCDVALUE_INVOC)
  316. return o->invoc_func;
  317. }
  318. NCDValue * NCDValue_InvocArg (NCDValue *o)
  319. {
  320. value_assert(o);
  321. ASSERT(o->type == NCDVALUE_INVOC)
  322. return o->invoc_arg;
  323. }
  324. void NCDProgram_Init (NCDProgram *o)
  325. {
  326. LinkedList1_Init(&o->elems_list);
  327. o->num_elems = 0;
  328. }
  329. void NCDProgram_Free (NCDProgram *o)
  330. {
  331. LinkedList1Node *ln;
  332. while (ln = LinkedList1_GetFirst(&o->elems_list)) {
  333. struct ProgramElem *e = UPPER_OBJECT(ln, struct ProgramElem, elems_list_node);
  334. NCDProgramElem_Free(&e->elem);
  335. LinkedList1_Remove(&o->elems_list, &e->elems_list_node);
  336. free(e);
  337. }
  338. }
  339. NCDProgramElem * NCDProgram_PrependElem (NCDProgram *o, NCDProgramElem elem)
  340. {
  341. if (o->num_elems == SIZE_MAX) {
  342. return NULL;
  343. }
  344. struct ProgramElem *e = malloc(sizeof(*e));
  345. if (!e) {
  346. return NULL;
  347. }
  348. LinkedList1_Prepend(&o->elems_list, &e->elems_list_node);
  349. e->elem = elem;
  350. o->num_elems++;
  351. return &e->elem;
  352. }
  353. NCDProgramElem * NCDProgram_FirstElem (NCDProgram *o)
  354. {
  355. LinkedList1Node *ln = LinkedList1_GetFirst(&o->elems_list);
  356. if (!ln) {
  357. return NULL;
  358. }
  359. struct ProgramElem *e = UPPER_OBJECT(ln, struct ProgramElem, elems_list_node);
  360. return &e->elem;
  361. }
  362. NCDProgramElem * NCDProgram_NextElem (NCDProgram *o, NCDProgramElem *ee)
  363. {
  364. ASSERT(ee)
  365. struct ProgramElem *cur_e = UPPER_OBJECT(ee, struct ProgramElem, elem);
  366. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->elems_list_node);
  367. if (!ln) {
  368. return NULL;
  369. }
  370. struct ProgramElem *e = UPPER_OBJECT(ln, struct ProgramElem, elems_list_node);
  371. return &e->elem;
  372. }
  373. size_t NCDProgram_NumElems (NCDProgram *o)
  374. {
  375. return o->num_elems;
  376. }
  377. int NCDProgram_ContainsElemType (NCDProgram *o, int elem_type)
  378. {
  379. for (NCDProgramElem *elem = NCDProgram_FirstElem(o); elem; elem = NCDProgram_NextElem(o, elem)) {
  380. if (NCDProgramElem_Type(elem) == elem_type) {
  381. return 1;
  382. }
  383. }
  384. return 0;
  385. }
  386. void NCDProgram_RemoveElem (NCDProgram *o, NCDProgramElem *ee)
  387. {
  388. ASSERT(ee)
  389. struct ProgramElem *e = UPPER_OBJECT(ee, struct ProgramElem, elem);
  390. NCDProgramElem_Free(&e->elem);
  391. LinkedList1_Remove(&o->elems_list, &e->elems_list_node);
  392. free(e);
  393. ASSERT(o->num_elems > 0)
  394. o->num_elems--;
  395. }
  396. int NCDProgram_ReplaceElemWithProgram (NCDProgram *o, NCDProgramElem *ee, NCDProgram replace_prog)
  397. {
  398. ASSERT(ee)
  399. if (replace_prog.num_elems > SIZE_MAX - o->num_elems) {
  400. return 0;
  401. }
  402. struct ProgramElem *e = UPPER_OBJECT(ee, struct ProgramElem, elem);
  403. LinkedList1_InsertListAfter(&o->elems_list, replace_prog.elems_list, &e->elems_list_node);
  404. o->num_elems += replace_prog.num_elems;
  405. NCDProgram_RemoveElem(o, ee);
  406. return 1;
  407. }
  408. void NCDProgramElem_InitProcess (NCDProgramElem *o, NCDProcess process)
  409. {
  410. o->type = NCDPROGRAMELEM_PROCESS;
  411. o->process = process;
  412. }
  413. int NCDProgramElem_InitInclude (NCDProgramElem *o, const char *path_data, size_t path_length)
  414. {
  415. if (!(o->include.path_data = b_strdup_bin(path_data, path_length))) {
  416. return 0;
  417. }
  418. o->type = NCDPROGRAMELEM_INCLUDE;
  419. o->include.path_length = path_length;
  420. return 1;
  421. }
  422. int NCDProgramElem_InitIncludeGuard (NCDProgramElem *o, const char *id_data, size_t id_length)
  423. {
  424. if (!(o->include_guard.id_data = b_strdup_bin(id_data, id_length))) {
  425. return 0;
  426. }
  427. o->type = NCDPROGRAMELEM_INCLUDE_GUARD;
  428. o->include_guard.id_length = id_length;
  429. return 1;
  430. }
  431. void NCDProgramElem_Free (NCDProgramElem *o)
  432. {
  433. switch (o->type) {
  434. case NCDPROGRAMELEM_PROCESS: {
  435. NCDProcess_Free(&o->process);
  436. } break;
  437. case NCDPROGRAMELEM_INCLUDE: {
  438. free(o->include.path_data);
  439. } break;
  440. case NCDPROGRAMELEM_INCLUDE_GUARD: {
  441. free(o->include_guard.id_data);
  442. } break;
  443. default: ASSERT(0);
  444. }
  445. }
  446. int NCDProgramElem_Type (NCDProgramElem *o)
  447. {
  448. return o->type;
  449. }
  450. NCDProcess * NCDProgramElem_Process (NCDProgramElem *o)
  451. {
  452. ASSERT(o->type == NCDPROGRAMELEM_PROCESS)
  453. return &o->process;
  454. }
  455. const char * NCDProgramElem_IncludePathData (NCDProgramElem *o)
  456. {
  457. ASSERT(o->type == NCDPROGRAMELEM_INCLUDE)
  458. return o->include.path_data;
  459. }
  460. size_t NCDProgramElem_IncludePathLength (NCDProgramElem *o)
  461. {
  462. ASSERT(o->type == NCDPROGRAMELEM_INCLUDE)
  463. return o->include.path_length;
  464. }
  465. const char * NCDProgramElem_IncludeGuardIdData (NCDProgramElem *o)
  466. {
  467. ASSERT(o->type == NCDPROGRAMELEM_INCLUDE_GUARD)
  468. return o->include_guard.id_data;
  469. }
  470. size_t NCDProgramElem_IncludeGuardIdLength (NCDProgramElem *o)
  471. {
  472. ASSERT(o->type == NCDPROGRAMELEM_INCLUDE_GUARD)
  473. return o->include_guard.id_length;
  474. }
  475. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block)
  476. {
  477. ASSERT(is_template == !!is_template)
  478. ASSERT(name)
  479. if (!(o->name = strdup(name))) {
  480. return 0;
  481. }
  482. o->is_template = is_template;
  483. o->block = block;
  484. return 1;
  485. }
  486. void NCDProcess_Free (NCDProcess *o)
  487. {
  488. NCDBlock_Free(&o->block);
  489. free(o->name);
  490. }
  491. int NCDProcess_IsTemplate (NCDProcess *o)
  492. {
  493. return o->is_template;
  494. }
  495. const char * NCDProcess_Name (NCDProcess *o)
  496. {
  497. return o->name;
  498. }
  499. NCDBlock * NCDProcess_Block (NCDProcess *o)
  500. {
  501. return &o->block;
  502. }
  503. void NCDBlock_Init (NCDBlock *o)
  504. {
  505. LinkedList1_Init(&o->statements_list);
  506. o->count = 0;
  507. }
  508. void NCDBlock_Free (NCDBlock *o)
  509. {
  510. LinkedList1Node *ln;
  511. while (ln = LinkedList1_GetFirst(&o->statements_list)) {
  512. struct BlockStatement *e = UPPER_OBJECT(ln, struct BlockStatement, statements_list_node);
  513. NCDStatement_Free(&e->s);
  514. LinkedList1_Remove(&o->statements_list, &e->statements_list_node);
  515. free(e);
  516. }
  517. }
  518. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s)
  519. {
  520. return NCDBlock_InsertStatementAfter(o, NULL, s);
  521. }
  522. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s)
  523. {
  524. struct BlockStatement *after_e = NULL;
  525. if (after) {
  526. after_e = UPPER_OBJECT(after, struct BlockStatement, s);
  527. }
  528. if (o->count == SIZE_MAX) {
  529. return 0;
  530. }
  531. struct BlockStatement *e = malloc(sizeof(*e));
  532. if (!e) {
  533. return 0;
  534. }
  535. if (after_e) {
  536. LinkedList1_InsertAfter(&o->statements_list, &e->statements_list_node, &after_e->statements_list_node);
  537. } else {
  538. LinkedList1_Prepend(&o->statements_list, &e->statements_list_node);
  539. }
  540. e->s = s;
  541. o->count++;
  542. return 1;
  543. }
  544. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s)
  545. {
  546. ASSERT(es)
  547. struct BlockStatement *e = UPPER_OBJECT(es, struct BlockStatement, s);
  548. NCDStatement_Free(&e->s);
  549. e->s = s;
  550. return &e->s;
  551. }
  552. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o)
  553. {
  554. LinkedList1Node *ln = LinkedList1_GetFirst(&o->statements_list);
  555. if (!ln) {
  556. return NULL;
  557. }
  558. struct BlockStatement *e = UPPER_OBJECT(ln, struct BlockStatement, statements_list_node);
  559. return &e->s;
  560. }
  561. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es)
  562. {
  563. ASSERT(es)
  564. struct BlockStatement *cur_e = UPPER_OBJECT(es, struct BlockStatement, s);
  565. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->statements_list_node);
  566. if (!ln) {
  567. return NULL;
  568. }
  569. struct BlockStatement *e = UPPER_OBJECT(ln, struct BlockStatement, statements_list_node);
  570. return &e->s;
  571. }
  572. size_t NCDBlock_NumStatements (NCDBlock *o)
  573. {
  574. return o->count;
  575. }
  576. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args)
  577. {
  578. ASSERT(cmdname)
  579. ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
  580. o->name = NULL;
  581. o->reg.objname = NULL;
  582. o->reg.cmdname = NULL;
  583. if (name && !(o->name = strdup(name))) {
  584. goto fail;
  585. }
  586. if (objname && !(o->reg.objname = strdup(objname))) {
  587. goto fail;
  588. }
  589. if (!(o->reg.cmdname = strdup(cmdname))) {
  590. goto fail;
  591. }
  592. o->type = NCDSTATEMENT_REG;
  593. o->reg.args = args;
  594. return 1;
  595. fail:
  596. free(o->name);
  597. free(o->reg.objname);
  598. free(o->reg.cmdname);
  599. return 0;
  600. }
  601. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock)
  602. {
  603. o->name = NULL;
  604. if (name && !(o->name = strdup(name))) {
  605. return 0;
  606. }
  607. o->type = NCDSTATEMENT_IF;
  608. o->ifc.ifblock = ifblock;
  609. o->ifc.have_else = 0;
  610. return 1;
  611. }
  612. int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block)
  613. {
  614. ASSERT(name1)
  615. o->name = NULL;
  616. o->foreach.name1 = NULL;
  617. o->foreach.name2 = NULL;
  618. if (name && !(o->name = strdup(name))) {
  619. goto fail;
  620. }
  621. if (!(o->foreach.name1 = strdup(name1))) {
  622. goto fail;
  623. }
  624. if (name2 && !(o->foreach.name2 = strdup(name2))) {
  625. goto fail;
  626. }
  627. o->type = NCDSTATEMENT_FOREACH;
  628. o->foreach.collection = collection;
  629. o->foreach.block = block;
  630. o->foreach.is_grabbed = 0;
  631. return 1;
  632. fail:
  633. free(o->name);
  634. free(o->foreach.name1);
  635. free(o->foreach.name2);
  636. return 0;
  637. }
  638. void NCDStatement_Free (NCDStatement *o)
  639. {
  640. switch (o->type) {
  641. case NCDSTATEMENT_REG: {
  642. NCDValue_Free(&o->reg.args);
  643. free(o->reg.cmdname);
  644. free(o->reg.objname);
  645. } break;
  646. case NCDSTATEMENT_IF: {
  647. if (o->ifc.have_else) {
  648. NCDBlock_Free(&o->ifc.else_block);
  649. }
  650. NCDIfBlock_Free(&o->ifc.ifblock);
  651. } break;
  652. case NCDSTATEMENT_FOREACH: {
  653. if (!o->foreach.is_grabbed) {
  654. NCDBlock_Free(&o->foreach.block);
  655. NCDValue_Free(&o->foreach.collection);
  656. }
  657. free(o->foreach.name2);
  658. free(o->foreach.name1);
  659. } break;
  660. default: ASSERT(0);
  661. }
  662. free(o->name);
  663. }
  664. int NCDStatement_Type (NCDStatement *o)
  665. {
  666. return o->type;
  667. }
  668. const char * NCDStatement_Name (NCDStatement *o)
  669. {
  670. return o->name;
  671. }
  672. const char * NCDStatement_RegObjName (NCDStatement *o)
  673. {
  674. ASSERT(o->type == NCDSTATEMENT_REG)
  675. return o->reg.objname;
  676. }
  677. const char * NCDStatement_RegCmdName (NCDStatement *o)
  678. {
  679. ASSERT(o->type == NCDSTATEMENT_REG)
  680. return o->reg.cmdname;
  681. }
  682. NCDValue * NCDStatement_RegArgs (NCDStatement *o)
  683. {
  684. ASSERT(o->type == NCDSTATEMENT_REG)
  685. return &o->reg.args;
  686. }
  687. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o)
  688. {
  689. ASSERT(o->type == NCDSTATEMENT_IF)
  690. return &o->ifc.ifblock;
  691. }
  692. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block)
  693. {
  694. ASSERT(o->type == NCDSTATEMENT_IF)
  695. ASSERT(!o->ifc.have_else)
  696. o->ifc.have_else = 1;
  697. o->ifc.else_block = else_block;
  698. }
  699. NCDBlock * NCDStatement_IfElse (NCDStatement *o)
  700. {
  701. ASSERT(o->type == NCDSTATEMENT_IF)
  702. if (!o->ifc.have_else) {
  703. return NULL;
  704. }
  705. return &o->ifc.else_block;
  706. }
  707. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o)
  708. {
  709. ASSERT(o->type == NCDSTATEMENT_IF)
  710. ASSERT(o->ifc.have_else)
  711. o->ifc.have_else = 0;
  712. return o->ifc.else_block;
  713. }
  714. NCDValue * NCDStatement_ForeachCollection (NCDStatement *o)
  715. {
  716. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  717. ASSERT(!o->foreach.is_grabbed)
  718. return &o->foreach.collection;
  719. }
  720. const char * NCDStatement_ForeachName1 (NCDStatement *o)
  721. {
  722. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  723. return o->foreach.name1;
  724. }
  725. const char * NCDStatement_ForeachName2 (NCDStatement *o)
  726. {
  727. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  728. return o->foreach.name2;
  729. }
  730. NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o)
  731. {
  732. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  733. ASSERT(!o->foreach.is_grabbed)
  734. return &o->foreach.block;
  735. }
  736. void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block)
  737. {
  738. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  739. ASSERT(!o->foreach.is_grabbed)
  740. *out_collection = o->foreach.collection;
  741. *out_block = o->foreach.block;
  742. o->foreach.is_grabbed = 1;
  743. }
  744. void NCDIfBlock_Init (NCDIfBlock *o)
  745. {
  746. LinkedList1_Init(&o->ifs_list);
  747. }
  748. void NCDIfBlock_Free (NCDIfBlock *o)
  749. {
  750. LinkedList1Node *ln;
  751. while (ln = LinkedList1_GetFirst(&o->ifs_list)) {
  752. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  753. NCDIf_Free(&e->ifc);
  754. LinkedList1_Remove(&o->ifs_list, &e->ifs_list_node);
  755. free(e);
  756. }
  757. }
  758. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc)
  759. {
  760. struct IfBlockIf *e = malloc(sizeof(*e));
  761. if (!e) {
  762. return 0;
  763. }
  764. LinkedList1_Prepend(&o->ifs_list, &e->ifs_list_node);
  765. e->ifc = ifc;
  766. return 1;
  767. }
  768. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o)
  769. {
  770. LinkedList1Node *ln = LinkedList1_GetFirst(&o->ifs_list);
  771. if (!ln) {
  772. return NULL;
  773. }
  774. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  775. return &e->ifc;
  776. }
  777. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei)
  778. {
  779. ASSERT(ei)
  780. struct IfBlockIf *cur_e = UPPER_OBJECT(ei, struct IfBlockIf, ifc);
  781. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->ifs_list_node);
  782. if (!ln) {
  783. return NULL;
  784. }
  785. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  786. return &e->ifc;
  787. }
  788. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei)
  789. {
  790. ASSERT(ei)
  791. struct IfBlockIf *e = UPPER_OBJECT(ei, struct IfBlockIf, ifc);
  792. NCDIf old_ifc = e->ifc;
  793. LinkedList1_Remove(&o->ifs_list, &e->ifs_list_node);
  794. free(e);
  795. return old_ifc;
  796. }
  797. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block)
  798. {
  799. o->cond = cond;
  800. o->block = block;
  801. }
  802. void NCDIf_Free (NCDIf *o)
  803. {
  804. NCDValue_Free(&o->cond);
  805. NCDBlock_Free(&o->block);
  806. }
  807. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block)
  808. {
  809. *out_cond = o->cond;
  810. *out_block = o->block;
  811. }
  812. NCDValue * NCDIf_Cond (NCDIf *o)
  813. {
  814. return &o->cond;
  815. }
  816. NCDBlock * NCDIf_Block (NCDIf *o)
  817. {
  818. return &o->block;
  819. }