|
|
@@ -148,8 +148,6 @@ static struct spearl_object *read_word_object(struct spearl_word *word,
|
|
|
*nout = word->node.next;
|
|
|
spl_klist_del(&wordlist, &word->node);
|
|
|
|
|
|
- printf("word object %s\n", strndup(word->p, word->len));
|
|
|
-
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
@@ -217,10 +215,126 @@ static int build_list(void) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+struct spearl_mcall *collect_args(struct spearl_list *list) {
|
|
|
+ struct spearl_mcall *mcall;
|
|
|
+ struct spearl_object *obj;
|
|
|
+ struct spearl_word *word;
|
|
|
+ struct spearl_mcall_arg *arg;
|
|
|
+ struct spl_klist_node *n;
|
|
|
+
|
|
|
+ spl_make_state();
|
|
|
+
|
|
|
+ mcall = spl_mcall_alloc();
|
|
|
+ mcall->list = list;
|
|
|
+ mcall->dest = NULL;
|
|
|
+ mcall->mname = NULL;
|
|
|
+
|
|
|
+ spl_klist_init(&mcall->args);
|
|
|
+
|
|
|
+ for (n = list->objs.next; n != &list->objs; n = n->next) {
|
|
|
+ obj = object_of_node(n);
|
|
|
+
|
|
|
+ if (obj->type == WORD) {
|
|
|
+ word = obj->word;
|
|
|
+
|
|
|
+ if (spl_state_in()) {
|
|
|
+ (void) spearl_error(SPL_LIST_NFOUND);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* we won't do any check, to make sure we didn't set it. */
|
|
|
+ switch (*word->p) {
|
|
|
+ case '#':
|
|
|
+ mcall->mname = obj;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case '~':
|
|
|
+ arg = spl_arg_alloc();
|
|
|
+ spl_klist_init(&arg->node);
|
|
|
+ spl_klist_add(&mcall->args, &arg->node);
|
|
|
+
|
|
|
+ arg->object = obj;
|
|
|
+
|
|
|
+ if (word->len == 1) {
|
|
|
+ arg->type = LIST;
|
|
|
+ spl_set_state(IN);
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ arg->type = WORD;
|
|
|
+ arg->p = strndup(word->p + 1, word->len - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case '@':
|
|
|
+ mcall->dest = obj;
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ (void) spearl_error(SPL_UNEXPECTED_WORD);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ if (spl_state_in()) {
|
|
|
+ arg->list = obj->list;
|
|
|
+ spl_set_state(OUT);
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ (void) spearl_error(SPL_UNEXPECTED_LIST);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return mcall;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static inline int mcall_valid(struct spearl_mcall *mcall) {
|
|
|
+ if (mcall->dest && mcall->mname && !spl_klist_empty(&mcall->args))
|
|
|
+ return 1;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static int do_expand_macro(struct spearl_list *list) {
|
|
|
+ struct spearl_mcall *mcall;
|
|
|
+
|
|
|
+ if (!(mcall = collect_args(list)))
|
|
|
+ return -1;
|
|
|
+
|
|
|
+ if (!mcall_valid(mcall))
|
|
|
+ return spearl_error(SPL_INCOMPLETE_CALL);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static int expand_calls(void) {
|
|
|
+ struct spl_klist_node *n;
|
|
|
+ struct spearl_object *obj;
|
|
|
+
|
|
|
+ for (n = root_object.list->objs.next; n != &root_object.list->objs; n = n->next) {
|
|
|
+ obj = object_of_node(n);
|
|
|
+
|
|
|
+ if (obj->type != LIST)
|
|
|
+ return spearl_error(SPL_INVALID_LIST);
|
|
|
+
|
|
|
+ callfn(do_expand_macro(obj->list));
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
int spearl_join(char *s) {
|
|
|
callfn(join_words(s));
|
|
|
callfn(build_list());
|
|
|
+ callfn(expand_calls());
|
|
|
|
|
|
return 0;
|
|
|
}
|