spearl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* spearl/spearl.c
  2. *
  3. * This file is part of spearl.
  4. *
  5. * Spearl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Spearl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with spearl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <spearl/spearl.h>
  19. #include <spearl/error.h>
  20. #include <spearl/sys.h>
  21. int __locally spl_input_size;
  22. char __locally *spl_input;
  23. char __visible lbracket = '(', rbracket = ')';
  24. struct spl_klist_node __visible wordlist = SPL_KLIST_NODE_INIT(&wordlist);
  25. struct spearl_object __visible root_object;
  26. #define callfn(fn) if (fn == -1) return -1
  27. int spearl_init(void) {
  28. root_object.type = LIST;
  29. root_object.list = spl_list_alloc();
  30. spl_klist_init(&root_object.node);
  31. spl_klist_init(&root_object.list->objs);
  32. spl_klist_init(&root_object.list->node);
  33. root_object.list->nobjs = 0;
  34. return 0;
  35. }
  36. static char *join_to_spl_input(char *s, int len) {
  37. return strcat(check_alloc((spl_input = realloc(spl_input, (spl_input_size += (len = strlen(s)))))), s);
  38. }
  39. static struct spearl_word *new_word(char *p, char *f) {
  40. struct spearl_word *word;
  41. word = spl_word_alloc();
  42. spl_klist_init(&word->node);
  43. spl_klist_add(&wordlist, &word->node);
  44. word->p = p;
  45. word->len = f - p;
  46. return word;
  47. }
  48. static inline char *skip_spaces(char *p) {
  49. while (*p && isspace(*p))
  50. p++;
  51. if (!*p)
  52. return NULL;
  53. return p;
  54. }
  55. #define notfirstchar() (save < p)
  56. static int read_words(char *s) {
  57. char *p, *save;
  58. spl_make_state();
  59. save = p = s;
  60. while (*p) {
  61. if (*p == lbracket) {
  62. if (notfirstchar()) {
  63. new_word(save, p);
  64. save = p;
  65. }
  66. p++;
  67. new_word(save, p);
  68. save = p;
  69. spl_state_left();
  70. continue;
  71. }
  72. else if (*p == rbracket) {
  73. if (!spl_current_state_level())
  74. return spearl_error(SPL_BAD_BRACKETS);
  75. new_word(save, p);
  76. p++;
  77. save = p;
  78. spl_state_right();
  79. continue;
  80. }
  81. else if (isspace(*p)) {
  82. new_word(save, p);
  83. p = save = skip_spaces(p);
  84. }
  85. else
  86. ++p;
  87. }
  88. if (spl_current_state_level())
  89. return spearl_error(SPL_BAD_BRACKETS);
  90. if (notfirstchar())
  91. new_word(save, p);
  92. return 0;
  93. }
  94. static int join_words(char *s) {
  95. join_to_spl_input(s, strlen(s));
  96. callfn(read_words(s));
  97. return 0;
  98. }
  99. static struct spearl_object *read_word_object(struct spearl_word *word,
  100. struct spl_klist_node **nout) {
  101. struct spearl_object *obj;
  102. obj = spl_object_alloc();
  103. spl_klist_init(&obj->node);
  104. obj->type = WORD;
  105. obj->word = word;
  106. *nout = word->node.next;
  107. spl_klist_del(&wordlist, &word->node);
  108. printf("word object %s\n", strndup(word->p, word->len));
  109. return obj;
  110. }
  111. static struct spearl_object *read_list_object(struct spearl_word *word,
  112. struct spl_klist_node **nout) {
  113. struct spearl_list *list;
  114. struct spearl_object *object, *obj;
  115. struct spl_klist_node *n, *next;
  116. object = spl_object_alloc();
  117. list = spl_list_alloc();
  118. spl_klist_init(&object->node);
  119. spl_klist_init(&list->objs);
  120. spl_klist_init(&list->node);
  121. object->type = LIST;
  122. object->list = list;
  123. list->nobjs = 0;
  124. spl_klist_del(&wordlist, &word->node);
  125. for (n = word->node.next; n != &wordlist; n = next) {
  126. next = n->next;
  127. word = word_of_node(n);
  128. if (*word->p == rbracket) {
  129. *nout = next;
  130. spl_klist_del(&wordlist, n);
  131. return object;
  132. }
  133. else if (*word->p == lbracket)
  134. obj = read_list_object(word, &next);
  135. else
  136. obj = read_word_object(word, &next);
  137. spl_klist_add(&list->objs, &obj->node);
  138. }
  139. return object;
  140. }
  141. static int build_list(void) {
  142. struct spl_klist_node *n;
  143. struct spearl_word *word;
  144. struct spearl_object *obj;
  145. n = wordlist.next;
  146. while (!spl_klist_empty(&wordlist)) {
  147. word = word_of_node(n);
  148. if (*word->p != lbracket)
  149. return spearl_error(SPL_INVALID_LIST);
  150. if (!(obj = read_list_object(word, &n)))
  151. return -1;
  152. spl_klist_add(&root_object.list->objs, &obj->node);
  153. root_object.list->nobjs++;
  154. }
  155. return 0;
  156. }
  157. int spearl_join(char *s) {
  158. callfn(join_words(s));
  159. callfn(build_list());
  160. return 0;
  161. }