| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /* spearl/spearl.c
- *
- * This file is part of spearl.
- *
- * Spearl is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Spearl is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with spearl. If not, see <https://www.gnu.org/licenses/>
- */
- #include <spearl/spearl.h>
- #include <spearl/error.h>
- #include <spearl/sys.h>
- int __locally spl_input_size;
- char __locally *spl_input;
- char __visible lbracket = '(', rbracket = ')';
- struct spl_klist_node __visible wordlist = SPL_KLIST_NODE_INIT(&wordlist);
- struct spearl_object __visible root_object;
- #define callfn(fn) if (fn == -1) return -1
- int spearl_init(void) {
- root_object.type = LIST;
- root_object.list = spl_list_alloc();
- spl_klist_init(&root_object.node);
- spl_klist_init(&root_object.list->objs);
- spl_klist_init(&root_object.list->node);
-
- root_object.list->nobjs = 0;
- return 0;
- }
- static char *join_to_spl_input(char *s, int len) {
- return strcat(check_alloc((spl_input = realloc(spl_input, (spl_input_size += (len = strlen(s)))))), s);
- }
- static struct spearl_word *new_word(char *p, char *f) {
- struct spearl_word *word;
- word = spl_word_alloc();
- spl_klist_init(&word->node);
- spl_klist_add(&wordlist, &word->node);
- word->p = p;
- word->len = f - p;
- return word;
- }
- static inline char *skip_spaces(char *p) {
- while (*p && isspace(*p))
- p++;
- if (!*p)
- return NULL;
- return p;
- }
- #define notfirstchar() (save < p)
- static int read_words(char *s) {
- char *p, *save;
- spl_make_state();
- save = p = s;
- while (*p) {
- if (*p == lbracket) {
- if (notfirstchar()) {
- new_word(save, p);
- save = p;
- }
- p++;
- new_word(save, p);
- save = p;
- spl_state_left();
- continue;
- }
-
- else if (*p == rbracket) {
- if (!spl_current_state_level())
- return spearl_error(SPL_BAD_BRACKETS);
- new_word(save, p);
- p++;
- save = p;
- spl_state_right();
- continue;
- }
- else if (isspace(*p)) {
- new_word(save, p);
- p = save = skip_spaces(p);
- }
- else
- ++p;
- }
- if (spl_current_state_level())
- return spearl_error(SPL_BAD_BRACKETS);
- if (notfirstchar())
- new_word(save, p);
- return 0;
- }
- static int join_words(char *s) {
- join_to_spl_input(s, strlen(s));
- callfn(read_words(s));
- return 0;
- }
- static struct spearl_object *read_word_object(struct spearl_word *word,
- struct spl_klist_node **nout) {
- struct spearl_object *obj;
- obj = spl_object_alloc();
- spl_klist_init(&obj->node);
- obj->type = WORD;
- obj->word = word;
- *nout = word->node.next;
- spl_klist_del(&wordlist, &word->node);
- printf("word object %s\n", strndup(word->p, word->len));
- return obj;
- }
- static struct spearl_object *read_list_object(struct spearl_word *word,
- struct spl_klist_node **nout) {
- struct spearl_list *list;
- struct spearl_object *object, *obj;
- struct spl_klist_node *n, *next;
- object = spl_object_alloc();
- list = spl_list_alloc();
- spl_klist_init(&object->node);
- spl_klist_init(&list->objs);
- spl_klist_init(&list->node);
- object->type = LIST;
- object->list = list;
- list->nobjs = 0;
- spl_klist_del(&wordlist, &word->node);
- for (n = word->node.next; n != &wordlist; n = next) {
- next = n->next;
- word = word_of_node(n);
- if (*word->p == rbracket) {
- *nout = next;
- spl_klist_del(&wordlist, n);
- return object;
- }
- else if (*word->p == lbracket)
- obj = read_list_object(word, &next);
- else
- obj = read_word_object(word, &next);
- spl_klist_add(&list->objs, &obj->node);
- }
- return object;
- }
- static int build_list(void) {
- struct spl_klist_node *n;
- struct spearl_word *word;
- struct spearl_object *obj;
- n = wordlist.next;
- while (!spl_klist_empty(&wordlist)) {
- word = word_of_node(n);
- if (*word->p != lbracket)
- return spearl_error(SPL_INVALID_LIST);
- if (!(obj = read_list_object(word, &n)))
- return -1;
-
- spl_klist_add(&root_object.list->objs, &obj->node);
- root_object.list->nobjs++;
- }
- return 0;
- }
- int spearl_join(char *s) {
- callfn(join_words(s));
- callfn(build_list());
- return 0;
- }
|