|
@@ -29,9 +29,19 @@ char __locally *spl_input;
|
|
|
char __visible lbracket = '(', rbracket = ')';
|
|
char __visible lbracket = '(', rbracket = ')';
|
|
|
|
|
|
|
|
struct spl_klist_node __visible wordlist = SPL_KLIST_NODE_INIT(&wordlist);
|
|
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) {
|
|
int spearl_init(void) {
|
|
|
- /* dummy */
|
|
|
|
|
|
|
+ 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;
|
|
return 0;
|
|
|
}
|
|
}
|
|
@@ -64,7 +74,7 @@ static inline char *skip_spaces(char *p) {
|
|
|
return p;
|
|
return p;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#define isfirstchar() (save == p + 1)
|
|
|
|
|
|
|
+#define notfirstchar() (save < p)
|
|
|
|
|
|
|
|
static int read_words(char *s) {
|
|
static int read_words(char *s) {
|
|
|
char *p, *save;
|
|
char *p, *save;
|
|
@@ -73,7 +83,7 @@ static int read_words(char *s) {
|
|
|
save = p = s;
|
|
save = p = s;
|
|
|
while (*p) {
|
|
while (*p) {
|
|
|
if (*p == lbracket) {
|
|
if (*p == lbracket) {
|
|
|
- if (!isfirstchar()) {
|
|
|
|
|
|
|
+ if (notfirstchar()) {
|
|
|
new_word(save, p);
|
|
new_word(save, p);
|
|
|
save = p;
|
|
save = p;
|
|
|
}
|
|
}
|
|
@@ -89,11 +99,11 @@ static int read_words(char *s) {
|
|
|
|
|
|
|
|
else if (*p == rbracket) {
|
|
else if (*p == rbracket) {
|
|
|
if (!spl_current_state_level())
|
|
if (!spl_current_state_level())
|
|
|
- return spearl_error(SPL_BAD_BRACKET);
|
|
|
|
|
-
|
|
|
|
|
- p++;
|
|
|
|
|
|
|
+ return spearl_error(SPL_BAD_BRACKETS);
|
|
|
|
|
|
|
|
new_word(save, p);
|
|
new_word(save, p);
|
|
|
|
|
+
|
|
|
|
|
+ p++;
|
|
|
save = p;
|
|
save = p;
|
|
|
spl_state_right();
|
|
spl_state_right();
|
|
|
|
|
|
|
@@ -110,9 +120,9 @@ static int read_words(char *s) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (spl_current_state_level())
|
|
if (spl_current_state_level())
|
|
|
- return spearl_error(SPL_BAD_BRACKET);
|
|
|
|
|
|
|
+ return spearl_error(SPL_BAD_BRACKETS);
|
|
|
|
|
|
|
|
- if (!isfirstchar())
|
|
|
|
|
|
|
+ if (notfirstchar())
|
|
|
new_word(save, p);
|
|
new_word(save, p);
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
@@ -120,17 +130,98 @@ static int read_words(char *s) {
|
|
|
|
|
|
|
|
static int join_words(char *s) {
|
|
static int join_words(char *s) {
|
|
|
join_to_spl_input(s, strlen(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;
|
|
|
|
|
|
|
|
- if (read_words(s) == -1)
|
|
|
|
|
- return -1;
|
|
|
|
|
|
|
+ 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;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
int spearl_join(char *s) {
|
|
int spearl_join(char *s) {
|
|
|
- if (join_words(s) == -1)
|
|
|
|
|
- return -1;
|
|
|
|
|
-
|
|
|
|
|
|
|
+ callfn(join_words(s));
|
|
|
|
|
+ callfn(build_list());
|
|
|
|
|
+
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|