NCDAst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. void NCDStatement_Free (NCDStatement *o)
  224. {
  225. switch (o->type) {
  226. case NCDSTATEMENT_REG: {
  227. NCDValue_Free(&o->reg.args);
  228. free(o->reg.cmdname);
  229. free(o->reg.objname);
  230. } break;
  231. case NCDSTATEMENT_IF: {
  232. if (o->ifc.have_else) {
  233. NCDBlock_Free(&o->ifc.else_block);
  234. }
  235. NCDIfBlock_Free(&o->ifc.ifblock);
  236. } break;
  237. default: ASSERT(0);
  238. }
  239. free(o->name);
  240. }
  241. int NCDStatement_Type (NCDStatement *o)
  242. {
  243. return o->type;
  244. }
  245. const char * NCDStatement_Name (NCDStatement *o)
  246. {
  247. return o->name;
  248. }
  249. const char * NCDStatement_RegObjName (NCDStatement *o)
  250. {
  251. ASSERT(o->type == NCDSTATEMENT_REG)
  252. return o->reg.objname;
  253. }
  254. const char * NCDStatement_RegCmdName (NCDStatement *o)
  255. {
  256. ASSERT(o->type == NCDSTATEMENT_REG)
  257. return o->reg.cmdname;
  258. }
  259. NCDValue * NCDStatement_RegArgs (NCDStatement *o)
  260. {
  261. ASSERT(o->type == NCDSTATEMENT_REG)
  262. return &o->reg.args;
  263. }
  264. NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o)
  265. {
  266. ASSERT(o->type == NCDSTATEMENT_IF)
  267. return &o->ifc.ifblock;
  268. }
  269. void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block)
  270. {
  271. ASSERT(o->type == NCDSTATEMENT_IF)
  272. ASSERT(!o->ifc.have_else)
  273. o->ifc.have_else = 1;
  274. o->ifc.else_block = else_block;
  275. }
  276. NCDBlock * NCDStatement_IfElse (NCDStatement *o)
  277. {
  278. ASSERT(o->type == NCDSTATEMENT_IF)
  279. if (!o->ifc.have_else) {
  280. return NULL;
  281. }
  282. return &o->ifc.else_block;
  283. }
  284. NCDBlock NCDStatement_IfGrabElse (NCDStatement *o)
  285. {
  286. ASSERT(o->type == NCDSTATEMENT_IF)
  287. ASSERT(o->ifc.have_else)
  288. o->ifc.have_else = 0;
  289. return o->ifc.else_block;
  290. }
  291. void NCDIfBlock_Init (NCDIfBlock *o)
  292. {
  293. LinkedList1_Init(&o->ifs_list);
  294. }
  295. void NCDIfBlock_Free (NCDIfBlock *o)
  296. {
  297. LinkedList1Node *ln;
  298. while (ln = LinkedList1_GetFirst(&o->ifs_list)) {
  299. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  300. NCDIf_Free(&e->ifc);
  301. LinkedList1_Remove(&o->ifs_list, &e->ifs_list_node);
  302. free(e);
  303. }
  304. }
  305. int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc)
  306. {
  307. struct IfBlockIf *e = malloc(sizeof(*e));
  308. if (!e) {
  309. return 0;
  310. }
  311. LinkedList1_Prepend(&o->ifs_list, &e->ifs_list_node);
  312. e->ifc = ifc;
  313. return 1;
  314. }
  315. NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o)
  316. {
  317. LinkedList1Node *ln = LinkedList1_GetFirst(&o->ifs_list);
  318. if (!ln) {
  319. return NULL;
  320. }
  321. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  322. return &e->ifc;
  323. }
  324. NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei)
  325. {
  326. ASSERT(ei)
  327. struct IfBlockIf *cur_e = UPPER_OBJECT(ei, struct IfBlockIf, ifc);
  328. LinkedList1Node *ln = LinkedList1Node_Next(&cur_e->ifs_list_node);
  329. if (!ln) {
  330. return NULL;
  331. }
  332. struct IfBlockIf *e = UPPER_OBJECT(ln, struct IfBlockIf, ifs_list_node);
  333. return &e->ifc;
  334. }
  335. NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei)
  336. {
  337. ASSERT(ei)
  338. struct IfBlockIf *e = UPPER_OBJECT(ei, struct IfBlockIf, ifc);
  339. NCDIf old_ifc = e->ifc;
  340. LinkedList1_Remove(&o->ifs_list, &e->ifs_list_node);
  341. free(e);
  342. return old_ifc;
  343. }
  344. void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block)
  345. {
  346. o->cond = cond;
  347. o->block = block;
  348. }
  349. void NCDIf_Free (NCDIf *o)
  350. {
  351. NCDValue_Free(&o->cond);
  352. NCDBlock_Free(&o->block);
  353. }
  354. void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block)
  355. {
  356. *out_cond = o->cond;
  357. *out_block = o->block;
  358. }
  359. NCDValue * NCDIf_Cond (NCDIf *o)
  360. {
  361. return &o->cond;
  362. }
  363. NCDBlock * NCDIf_Block (NCDIf *o)
  364. {
  365. return &o->block;
  366. }