acts.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <rid.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #define STYLE_9
  7. #include <fn85.h>
  8. #include <rid_fn85.h>
  9. #include <unfy/unfy.h>
  10. #include <unfy/unfp.h>
  11. #include <rls/rls.h>
  12. #include <rls/rlsp.h>
  13. #define NIT_SHORT_NAMES
  14. #include <nit/list.h>
  15. #include <nit/set.h>
  16. #include <nit/map.h>
  17. #include <nit/err.h>
  18. #include <nit/file.h>
  19. #include <nit/buf.h>
  20. #include <nit/gap.h>
  21. #include "../utilize.h"
  22. #include "../examine.h"
  23. #include "../front.h"
  24. #include "acts.h"
  25. static Unfy_stat
  26. act_add(Rls_mode mode, Rls_srch *srch, void *extra);
  27. static Unfy_stat
  28. act_gensym(Rls_mode mode, Rls_srch *srch, void *extra);
  29. Rls_act ilk_acts[] = {
  30. [ILK_ACT_ADD] = act_add,
  31. [ILK_ACT_GENSYM] = act_gensym,
  32. };
  33. static const Rid act_ids[] = {
  34. /* add_________________ */
  35. [ILK_ACT_ADD] = { 0x1f, 0x98, 0x8a, 0xb8, 0xfb, 0xe0, 0x0e, 0x10,
  36. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 },
  37. /* gensym______________ */
  38. [ILK_ACT_GENSYM] = { 0x32, 0x4e, 0x0e, 0x93, 0x47, 0x69, 0xfd, 0xf6,
  39. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 },
  40. };
  41. static const char *act_heads[] = {
  42. [ILK_ACT_ADD] =
  43. "(const sys_________________ const add_________________"
  44. " var id__________________ var head________________"
  45. " var body________________)",
  46. [ILK_ACT_GENSYM] =
  47. "(const sys_________________ const gensym______________"
  48. " var sym_________________)",
  49. };
  50. /* id__________________ */
  51. static const Rid id_rid = { 0x38, 0x84, 0x13, 0x2b, 0xfb, 0xe0, 0x0e, 0x10,
  52. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  53. /* head________________ */
  54. static const Rid head_rid = { 0x35, 0x69, 0x1f, 0x92, 0xfb, 0xe0, 0x0e, 0x10,
  55. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  56. /* body________________ */
  57. static const Rid body_rid = { 0x23, 0x1c, 0x14, 0x52, 0xfb, 0xe0, 0x0e, 0x10,
  58. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  59. /* sym_________________ */
  60. static const Rid sym_rid = { 0x58, 0x5f, 0xa6, 0x9c, 0xfb, 0xe0, 0x0e, 0x10,
  61. 0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };
  62. /* Act functions */
  63. static Unfy_stat
  64. act_add(Rls_mode mode, Rls_srch *srch, void *extra)
  65. {
  66. Ilk_sys *sys = extra;
  67. Unfy_term *id = unfy_bind_get(srch->rbind, id_rid);
  68. Unfy_term *head = unfy_bind_get(srch->rbind, head_rid);
  69. Unfy_term *body = unfy_bind_get(srch->rbind, body_rid);
  70. Ilk_grp *grp;
  71. Unfy_list *body_list;
  72. if (mode == RLS_SRCH) {
  73. if (id && body && id->type == UNFY_CONST &&
  74. body->type == UNFY_LIST &&
  75. !ilk_rule_valid(head, body->u.list))
  76. return UNFY_YES;
  77. return UNFY_NO;
  78. }
  79. if (!(grp = ilk_grp_get_term(sys, head, NULL)))
  80. return UNFY_ERR;
  81. if (!(head = unfy_term_copy(head, &sys->lib->rec.unfy))) {
  82. err_cust(&sys->lib->err, "Failed to copy rule head");
  83. goto err_term;
  84. }
  85. if (unfy_list_copy(&body_list, body->u.list, &sys->lib->rec.unfy) < 0) {
  86. err_cust(&sys->lib->err, "Failed to copy rule body");
  87. goto err_list;
  88. }
  89. if (!ilk_rule_add(sys, id->u.id, head, body_list, sys->srch_cnt))
  90. goto err_add;
  91. return UNFY_YES;
  92. err_add:
  93. unfy_recycle_list(&sys->lib->rec.unfy, body_list);
  94. err_list:
  95. unfy_recycle_term(&sys->lib->rec.unfy, head);
  96. err_term:
  97. return UNFY_ERR;
  98. }
  99. static Unfy_stat
  100. act_gensym(Rls_mode mode, Rls_srch *srch, void *extra)
  101. {
  102. Ilk_sys *sys = extra;
  103. Unfy_term *var;
  104. if (mode == RLS_RUN)
  105. return UNFY_YES;
  106. if ((var = unfy_bind_get(srch->rbind, sym_rid))->type != UNFY_VAR)
  107. return UNFY_NO;
  108. /* there will only be one for the input */
  109. unfy_term_init(srch->fbind->term, UNFY_CONST, NULL);
  110. unfy_recycle_bind(&sys->lib->rec.unfy, srch->lbind);
  111. srch->lbind = NULL;
  112. unfy_recycle_bind(&sys->lib->rec.unfy, srch->rbind);
  113. srch->rbind = NULL;
  114. unfy_recycle_bind(&sys->lib->rec.unfy, srch->nbind);
  115. srch->nbind = NULL;
  116. return UNFY_YES;
  117. }
  118. Ilk_grp *
  119. ilk_act_add2(Ilk_sys *sys, Ilk_act_num act_num)
  120. {
  121. size_t pos = 0;
  122. const char *error;
  123. Unfy_term *head;
  124. Ilk_grp *grp;
  125. if (!(head = unfp_term_parse(act_heads[act_num], &pos,
  126. &error, &sys->lib->rec.unfy))) {
  127. err_cust(&sys->lib->err, error);
  128. return NULL;
  129. }
  130. if (!(grp = ilk_act_add(sys, act_ids[act_num], head, act_num)))
  131. unfy_recycle_term(&sys->lib->rec.unfy, head);
  132. return grp;
  133. }