/* 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
*/
#include
#include
#include
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;
}