spearl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 spl_klist_node __visible spl_macros = SPL_KLIST_NODE_INIT(&spl_macros);
  26. struct spearl_object __visible root_object;
  27. #define callfn(fn) if (fn == -1) return -1
  28. int spearl_init(void) {
  29. root_object.type = LIST;
  30. root_object.list = spl_list_alloc();
  31. spl_klist_init(&root_object.node);
  32. spl_klist_init(&root_object.list->objs);
  33. spl_klist_init(&root_object.list->node);
  34. root_object.list->nobjs = 0;
  35. spl_builtin_init();
  36. return 0;
  37. }
  38. static char *join_to_spl_input(char *s, int len) {
  39. return strcat(check_alloc((spl_input = realloc(spl_input, (spl_input_size += (len = strlen(s)))))), s);
  40. }
  41. static struct spearl_word *new_word(char *p, char *f) {
  42. struct spearl_word *word;
  43. word = spl_word_alloc();
  44. spl_klist_init(&word->node);
  45. spl_klist_add(&wordlist, &word->node);
  46. word->p = p;
  47. word->len = f - p;
  48. return word;
  49. }
  50. static inline char *skip_spaces(char *p) {
  51. while (*p && isspace(*p))
  52. p++;
  53. if (!*p)
  54. return NULL;
  55. return p;
  56. }
  57. #define notfirstchar() (save < p)
  58. static int read_words(char *s) {
  59. char *p, *save;
  60. spl_make_state();
  61. save = p = s;
  62. while (*p) {
  63. if (*p == lbracket) {
  64. if (notfirstchar()) {
  65. new_word(save, p);
  66. save = p;
  67. }
  68. p++;
  69. new_word(save, p);
  70. save = p;
  71. spl_state_left();
  72. continue;
  73. }
  74. else if (*p == rbracket) {
  75. if (!spl_current_state_level())
  76. return spearl_error(SPL_BAD_BRACKETS);
  77. new_word(save, p);
  78. p++;
  79. save = p;
  80. spl_state_right();
  81. continue;
  82. }
  83. else if (isspace(*p)) {
  84. new_word(save, p);
  85. p = save = skip_spaces(p);
  86. }
  87. else
  88. ++p;
  89. }
  90. if (spl_current_state_level())
  91. return spearl_error(SPL_BAD_BRACKETS);
  92. if (notfirstchar())
  93. new_word(save, p);
  94. return 0;
  95. }
  96. static int join_words(char *s) {
  97. join_to_spl_input(s, strlen(s));
  98. callfn(read_words(s));
  99. return 0;
  100. }
  101. static struct spearl_object *read_word_object(struct spearl_word *word,
  102. struct spl_klist_node **nout) {
  103. struct spearl_object *obj;
  104. obj = spl_object_alloc();
  105. spl_klist_init(&obj->node);
  106. obj->type = WORD;
  107. obj->word = word;
  108. *nout = word->node.next;
  109. spl_klist_del(&wordlist, &word->node);
  110. return obj;
  111. }
  112. static struct spearl_object *read_list_object(struct spearl_word *word,
  113. struct spl_klist_node **nout) {
  114. struct spearl_list *list;
  115. struct spearl_object *object, *obj;
  116. struct spl_klist_node *n, *next;
  117. object = spl_object_alloc();
  118. list = spl_list_alloc();
  119. spl_klist_init(&object->node);
  120. spl_klist_init(&list->objs);
  121. spl_klist_init(&list->node);
  122. object->type = LIST;
  123. object->list = list;
  124. list->nobjs = 0;
  125. spl_klist_del(&wordlist, &word->node);
  126. for (n = word->node.next; n != &wordlist; n = next) {
  127. next = n->next;
  128. word = word_of_node(n);
  129. if (*word->p == rbracket) {
  130. *nout = next;
  131. spl_klist_del(&wordlist, n);
  132. return object;
  133. }
  134. else if (*word->p == lbracket)
  135. obj = read_list_object(word, &next);
  136. else
  137. obj = read_word_object(word, &next);
  138. spl_klist_add(&list->objs, &obj->node);
  139. }
  140. return object;
  141. }
  142. static int build_list(void) {
  143. struct spl_klist_node *n;
  144. struct spearl_word *word;
  145. struct spearl_object *obj;
  146. n = wordlist.next;
  147. while (!spl_klist_empty(&wordlist)) {
  148. word = word_of_node(n);
  149. if (*word->p != lbracket)
  150. return spearl_error(SPL_INVALID_LIST);
  151. if (!(obj = read_list_object(word, &n)))
  152. return -1;
  153. spl_klist_add(&root_object.list->objs, &obj->node);
  154. root_object.list->nobjs++;
  155. }
  156. return 0;
  157. }
  158. struct spearl_mcall *collect_args(struct spearl_list *list) {
  159. struct spearl_mcall *mcall;
  160. struct spearl_object *obj;
  161. struct spearl_word *word;
  162. struct spearl_mcall_arg *arg;
  163. struct spl_klist_node *n;
  164. spl_make_state();
  165. mcall = spl_mcall_alloc();
  166. mcall->list = list;
  167. mcall->dest = NULL;
  168. mcall->mname = NULL;
  169. spl_klist_init(&mcall->args);
  170. for (n = list->objs.next; n != &list->objs; n = n->next) {
  171. obj = object_of_node(n);
  172. if (obj->type == WORD) {
  173. word = obj->word;
  174. if (spl_state_in()) {
  175. (void) spearl_error(SPL_LIST_NFOUND);
  176. return NULL;
  177. }
  178. /* we won't do any check, to make sure we didn't set it. */
  179. switch (*word->p) {
  180. case '#':
  181. mcall->mname = obj;
  182. break;
  183. case '~':
  184. arg = spl_arg_alloc();
  185. spl_klist_init(&arg->node);
  186. spl_klist_add(&mcall->args, &arg->node);
  187. arg->object = obj;
  188. if (word->len == 1) {
  189. arg->type = LIST;
  190. spl_set_state(IN);
  191. }
  192. else {
  193. arg->type = WORD;
  194. arg->p = strndup(word->p + 1, word->len - 1);
  195. }
  196. break;
  197. case '@':
  198. mcall->dest = obj;
  199. break;
  200. default:
  201. (void) spearl_error(SPL_UNEXPECTED_WORD);
  202. return NULL;
  203. }
  204. }
  205. else {
  206. if (spl_state_in()) {
  207. arg->list = obj->list;
  208. spl_set_state(OUT);
  209. }
  210. else {
  211. (void) spearl_error(SPL_UNEXPECTED_LIST);
  212. return NULL;
  213. }
  214. }
  215. }
  216. return mcall;
  217. }
  218. static inline int mcall_valid(struct spearl_mcall *mcall) {
  219. if (mcall->dest && mcall->mname)
  220. return 1;
  221. return 0;
  222. }
  223. static struct spearl_macro *find_macro(struct spearl_mcall *mcall) {
  224. struct spl_klist_node *n;
  225. struct spearl_macro *macro;
  226. for (n = spl_macros.next; n != &spl_macros; n = n->next) {
  227. macro = macro_of_node(n);
  228. if (!strncmp(macro->name, mcall->mname->word->p + 1, mcall->mname->word->len - 1))
  229. return macro;
  230. }
  231. return NULL;
  232. }
  233. static int do_expand_macro(struct spearl_list *list, struct spl_klist_node *node) {
  234. struct spearl_mcall *mcall;
  235. struct spearl_macro *macro;
  236. if (!(mcall = collect_args(list)))
  237. return -1;
  238. if (!mcall_valid(mcall))
  239. return spearl_error(SPL_INCOMPLETE_CALL);
  240. if (!(macro = find_macro(mcall)))
  241. return spearl_error(SPL_MACRO_NEXISTS);
  242. if (macro->type == BUILTIN)
  243. callfn(expand_builtin_macro(macro, mcall, node));
  244. return 0;
  245. }
  246. static int expand_calls(void) {
  247. struct spl_klist_node *n;
  248. struct spearl_object *obj;
  249. for (n = root_object.list->objs.next; n != &root_object.list->objs; n = n->next) {
  250. obj = object_of_node(n);
  251. if (obj->type != LIST)
  252. return spearl_error(SPL_INVALID_LIST);
  253. callfn(do_expand_macro(obj->list, n));
  254. }
  255. return 0;
  256. }
  257. int spearl_join(char *s) {
  258. callfn(join_words(s));
  259. callfn(build_list());
  260. callfn(expand_calls());
  261. return 0;
  262. }