NCDAst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 <misc/offset.h>
  32. #include "NCDAst.h"
  33. void NCDProgram_Init (NCDProgram *o)
  34. {
  35. LinkedList1_Init(&o->processes_list);
  36. o->num_processes = 0;
  37. }
  38. void NCDProgram_Free (NCDProgram *o)
  39. {
  40. LinkedList1Node *ln;
  41. while (ln = LinkedList1_GetFirst(&o->processes_list)) {
  42. struct ProgramProcess *e = UPPER_OBJECT(ln, struct ProgramProcess, processes_list_node);
  43. NCDProcess_Free(&e->p);
  44. LinkedList1_Remove(&o->processes_list, &e->processes_list_node);
  45. free(e);
  46. }
  47. }
  48. NCDProcess * NCDProgram_PrependProcess (NCDProgram *o, NCDProcess p)
  49. {
  50. if (o->num_processes == SIZE_MAX) {
  51. return NULL;
  52. }
  53. struct ProgramProcess *e = malloc(sizeof(*e));
  54. if (!e) {
  55. return NULL;
  56. }
  57. LinkedList1_Prepend(&o->processes_list, &e->processes_list_node);
  58. e->p = p;
  59. o->num_processes++;
  60. return &e->p;
  61. }
  62. NCDProcess * NCDProgram_FirstProcess (NCDProgram *o)
  63. {
  64. LinkedList1Node *ln = LinkedList1_GetFirst(&o->processes_list);
  65. if (!ln) {
  66. return NULL;
  67. }
  68. struct ProgramProcess *e = UPPER_OBJECT(ln, struct ProgramProcess, processes_list_node);
  69. return &e->p;
  70. }
  71. NCDProcess * NCDProgram_NextProcess (NCDProgram *o, NCDProcess *ep)
  72. {
  73. ASSERT(ep)
  74. struct ProgramProcess *cur_e = UPPER_OBJECT(ep, struct ProgramProcess, p);
  75. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->processes_list_node);
  76. if (!ln) {
  77. return NULL;
  78. }
  79. struct ProgramProcess *e = UPPER_OBJECT(ln, struct ProgramProcess, processes_list_node);
  80. return &e->p;
  81. }
  82. size_t NCDProgram_NumProcesses (NCDProgram *o)
  83. {
  84. return o->num_processes;
  85. }
  86. int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block)
  87. {
  88. ASSERT(is_template == !!is_template)
  89. ASSERT(name)
  90. if (!(o->name = strdup(name))) {
  91. return 0;
  92. }
  93. o->is_template = is_template;
  94. o->block = block;
  95. return 1;
  96. }
  97. void NCDProcess_Free (NCDProcess *o)
  98. {
  99. NCDBlock_Free(&o->block);
  100. free(o->name);
  101. }
  102. int NCDProcess_IsTemplate (NCDProcess *o)
  103. {
  104. return o->is_template;
  105. }
  106. const char * NCDProcess_Name (NCDProcess *o)
  107. {
  108. return o->name;
  109. }
  110. NCDBlock * NCDProcess_Block (NCDProcess *o)
  111. {
  112. return &o->block;
  113. }
  114. void NCDBlock_Init (NCDBlock *o)
  115. {
  116. LinkedList1_Init(&o->statements_list);
  117. o->count = 0;
  118. }
  119. void NCDBlock_Free (NCDBlock *o)
  120. {
  121. LinkedList1Node *ln;
  122. while (ln = LinkedList1_GetFirst(&o->statements_list)) {
  123. struct BlockStatement *e = UPPER_OBJECT(ln, struct BlockStatement, statements_list_node);
  124. NCDStatement_Free(&e->s);
  125. LinkedList1_Remove(&o->statements_list, &e->statements_list_node);
  126. free(e);
  127. }
  128. }
  129. int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s)
  130. {
  131. return NCDBlock_InsertStatementAfter(o, NULL, s);
  132. }
  133. int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s)
  134. {
  135. struct BlockStatement *after_e = NULL;
  136. if (after) {
  137. after_e = UPPER_OBJECT(after, struct BlockStatement, s);
  138. }
  139. if (o->count == SIZE_MAX) {
  140. return 0;
  141. }
  142. struct BlockStatement *e = malloc(sizeof(*e));
  143. if (!e) {
  144. return 0;
  145. }
  146. if (after_e) {
  147. LinkedList1_InsertAfter(&o->statements_list, &e->statements_list_node, &after_e->statements_list_node);
  148. } else {
  149. LinkedList1_Prepend(&o->statements_list, &e->statements_list_node);
  150. }
  151. e->s = s;
  152. o->count++;
  153. return 1;
  154. }
  155. NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s)
  156. {
  157. ASSERT(es)
  158. struct BlockStatement *e = UPPER_OBJECT(es, struct BlockStatement, s);
  159. NCDStatement_Free(&e->s);
  160. e->s = s;
  161. return &e->s;
  162. }
  163. NCDStatement * NCDBlock_FirstStatement (NCDBlock *o)
  164. {
  165. LinkedList1Node *ln = LinkedList1_GetFirst(&o->statements_list);
  166. if (!ln) {
  167. return NULL;
  168. }
  169. struct BlockStatement *e = UPPER_OBJECT(ln, struct BlockStatement, statements_list_node);
  170. return &e->s;
  171. }
  172. NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es)
  173. {
  174. ASSERT(es)
  175. struct BlockStatement *cur_e = UPPER_OBJECT(es, struct BlockStatement, s);
  176. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->statements_list_node);
  177. if (!ln) {
  178. return NULL;
  179. }
  180. struct BlockStatement *e = UPPER_OBJECT(ln, struct BlockStatement, statements_list_node);
  181. return &e->s;
  182. }
  183. size_t NCDBlock_NumStatements (NCDBlock *o)
  184. {
  185. return o->count;
  186. }
  187. int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args)
  188. {
  189. ASSERT(cmdname)
  190. ASSERT(NCDValue_IsList(&args))
  191. o->name = NULL;
  192. o->reg.objname = NULL;
  193. o->reg.cmdname = NULL;
  194. if (name && !(o->name = strdup(name))) {
  195. goto fail;
  196. }
  197. if (objname && !(o->reg.objname = strdup(objname))) {
  198. goto fail;
  199. }
  200. if (!(o->reg.cmdname = strdup(cmdname))) {
  201. goto fail;
  202. }
  203. o->type = NCDSTATEMENT_REG;
  204. o->reg.args = args;
  205. return 1;
  206. fail:
  207. free(o->name);
  208. free(o->reg.objname);
  209. free(o->reg.cmdname);
  210. return 0;
  211. }
  212. int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock)
  213. {
  214. o->name = NULL;
  215. if (name && !(o->name = strdup(name))) {
  216. return 0;
  217. }
  218. o->type = NCDSTATEMENT_IF;
  219. o->ifc.ifblock = ifblock;
  220. o->ifc.have_else = 0;
  221. return 1;
  222. }
  223. int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block)
  224. {
  225. ASSERT(name1)
  226. o->name = NULL;
  227. o->foreach.name1 = NULL;
  228. o->foreach.name2 = NULL;
  229. if (name && !(o->name = strdup(name))) {
  230. goto fail;
  231. }
  232. if (!(o->foreach.name1 = strdup(name1))) {
  233. goto fail;
  234. }
  235. if (name2 && !(o->foreach.name2 = strdup(name2))) {
  236. goto fail;
  237. }
  238. o->type = NCDSTATEMENT_FOREACH;
  239. o->foreach.collection = collection;
  240. o->foreach.block = block;
  241. o->foreach.is_grabbed = 0;
  242. return 1;
  243. fail:
  244. free(o->name);
  245. free(o->foreach.name1);
  246. free(o->foreach.name2);
  247. return 0;
  248. }
  249. void NCDStatement_Free (NCDStatement *o)
  250. {
  251. switch (o->type) {
  252. case NCDSTATEMENT_REG: {
  253. NCDValue_Free(&o->reg.args);
  254. free(o->reg.cmdname);
  255. free(o->reg.objname);
  256. } break;
  257. case NCDSTATEMENT_IF: {
  258. if (o->ifc.have_else) {
  259. NCDBlock_Free(&o->ifc.else_block);
  260. }
  261. NCDIfBlock_Free(&o->ifc.ifblock);
  262. } break;
  263. case NCDSTATEMENT_FOREACH: {
  264. if (!o->foreach.is_grabbed) {
  265. NCDBlock_Free(&o->foreach.block);
  266. NCDValue_Free(&o->foreach.collection);
  267. }
  268. free(o->foreach.name2);
  269. free(o->foreach.name1);
  270. } break;
  271. default: ASSERT(0);
  272. }
  273. free(o->name);
  274. }
  275. int NCDStatement_Type (NCDStatement *o)
  276. {
  277. return o->type;
  278. }
  279. const char * NCDStatement_Name (NCDStatement *o)
  280. {
  281. return o->name;
  282. }
  283. const char * NCDStatement_RegObjName (NCDStatement *o)
  284. {
  285. ASSERT(o->type == NCDSTATEMENT_REG)
  286. return o->reg.objname;
  287. }
  288. const char * NCDStatement_RegCmdName (NCDStatement *o)
  289. {
  290. ASSERT(o->type == NCDSTATEMENT_REG)
  291. return o->reg.cmdname;
  292. }
  293. NCDValue * NCDStatement_RegArgs (NCDStatement *o)
  294. {
  295. ASSERT(o->type == NCDSTATEMENT_REG)
  296. return &o->reg.args;
  297. }
  298. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o)
  299. {
  300. ASSERT(o->type == NCDSTATEMENT_IF)
  301. return &o->ifc.ifblock;
  302. }
  303. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block)
  304. {
  305. ASSERT(o->type == NCDSTATEMENT_IF)
  306. ASSERT(!o->ifc.have_else)
  307. o->ifc.have_else = 1;
  308. o->ifc.else_block = else_block;
  309. }
  310. NCDBlock * NCDStatement_IfElse (NCDStatement *o)
  311. {
  312. ASSERT(o->type == NCDSTATEMENT_IF)
  313. if (!o->ifc.have_else) {
  314. return NULL;
  315. }
  316. return &o->ifc.else_block;
  317. }
  318. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o)
  319. {
  320. ASSERT(o->type == NCDSTATEMENT_IF)
  321. ASSERT(o->ifc.have_else)
  322. o->ifc.have_else = 0;
  323. return o->ifc.else_block;
  324. }
  325. NCDValue * NCDStatement_ForeachCollection (NCDStatement *o)
  326. {
  327. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  328. ASSERT(!o->foreach.is_grabbed)
  329. return &o->foreach.collection;
  330. }
  331. const char * NCDStatement_ForeachName1 (NCDStatement *o)
  332. {
  333. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  334. return o->foreach.name1;
  335. }
  336. const char * NCDStatement_ForeachName2 (NCDStatement *o)
  337. {
  338. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  339. return o->foreach.name2;
  340. }
  341. NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o)
  342. {
  343. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  344. ASSERT(!o->foreach.is_grabbed)
  345. return &o->foreach.block;
  346. }
  347. void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block)
  348. {
  349. ASSERT(o->type == NCDSTATEMENT_FOREACH)
  350. ASSERT(!o->foreach.is_grabbed)
  351. *out_collection = o->foreach.collection;
  352. *out_block = o->foreach.block;
  353. o->foreach.is_grabbed = 1;
  354. }
  355. void NCDIfBlock_Init (NCDIfBlock *o)
  356. {
  357. LinkedList1_Init(&o->ifs_list);
  358. }
  359. void NCDIfBlock_Free (NCDIfBlock *o)
  360. {
  361. LinkedList1Node *ln;
  362. while (ln = LinkedList1_GetFirst(&o->ifs_list)) {
  363. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  364. NCDIf_Free(&e->ifc);
  365. LinkedList1_Remove(&o->ifs_list, &e->ifs_list_node);
  366. free(e);
  367. }
  368. }
  369. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc)
  370. {
  371. struct IfBlockIf *e = malloc(sizeof(*e));
  372. if (!e) {
  373. return 0;
  374. }
  375. LinkedList1_Prepend(&o->ifs_list, &e->ifs_list_node);
  376. e->ifc = ifc;
  377. return 1;
  378. }
  379. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o)
  380. {
  381. LinkedList1Node *ln = LinkedList1_GetFirst(&o->ifs_list);
  382. if (!ln) {
  383. return NULL;
  384. }
  385. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  386. return &e->ifc;
  387. }
  388. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei)
  389. {
  390. ASSERT(ei)
  391. struct IfBlockIf *cur_e = UPPER_OBJECT(ei, struct IfBlockIf, ifc);
  392. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->ifs_list_node);
  393. if (!ln) {
  394. return NULL;
  395. }
  396. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  397. return &e->ifc;
  398. }
  399. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei)
  400. {
  401. ASSERT(ei)
  402. struct IfBlockIf *e = UPPER_OBJECT(ei, struct IfBlockIf, ifc);
  403. NCDIf old_ifc = e->ifc;
  404. LinkedList1_Remove(&o->ifs_list, &e->ifs_list_node);
  405. free(e);
  406. return old_ifc;
  407. }
  408. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block)
  409. {
  410. o->cond = cond;
  411. o->block = block;
  412. }
  413. void NCDIf_Free (NCDIf *o)
  414. {
  415. NCDValue_Free(&o->cond);
  416. NCDBlock_Free(&o->block);
  417. }
  418. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block)
  419. {
  420. *out_cond = o->cond;
  421. *out_block = o->block;
  422. }
  423. NCDValue * NCDIf_Cond (NCDIf *o)
  424. {
  425. return &o->cond;
  426. }
  427. NCDBlock * NCDIf_Block (NCDIf *o)
  428. {
  429. return &o->block;
  430. }