Parcourir la source

spearl: collect arguments of macro calls, and more errors

intirain il y a 4 ans
Parent
commit
af80b04c00
5 fichiers modifiés avec 156 ajouts et 5 suppressions
  1. 5 1
      error.c
  2. 5 1
      include/spearl/error.h
  3. 29 0
      include/spearl/spearl.h
  4. 1 1
      samples/1.c
  5. 116 2
      spearl.c

+ 5 - 1
error.c

@@ -27,9 +27,13 @@
 int __visible spearl_errno = 0;
 
 char __visible *spearl_strerrors[] = {
-	[0] = "Success. \n", 
+	[0] = "Success. ", 
 	[-SPL_BAD_BRACKETS] = "Brackets didn't match. ", 
 	[-SPL_INVALID_LIST] = "Invalid list format. ", 
+	[-SPL_LIST_NFOUND] = "Not a list after `~'. ", 
+	[-SPL_UNEXPECTED_LIST] = "Unexpected list in the macro call. ", 
+	[-SPL_INCOMPLETE_CALL] = "Incomplete macro call. ", 
+	[-SPL_UNEXPECTED_WORD] = "Unexpected word without qualifiers in the macro call. ", 
 	[-SPL_MAX_ERROR] = "Unknown error. ", 
 };
 

+ 5 - 1
include/spearl/error.h

@@ -28,7 +28,11 @@
 
 #define SPL_BAD_BRACKETS	(SPL_ERRNO - 1)
 #define SPL_INVALID_LIST 	(SPL_ERRNO - 2)
-#define SPL_MAX_ERROR 		(SPL_ERRNO - 3)
+#define SPL_LIST_NFOUND 	(SPL_ERRNO - 3)
+#define SPL_UNEXPECTED_LIST 	(SPL_ERRNO - 4)
+#define SPL_INCOMPLETE_CALL 	(SPL_ERRNO - 5)
+#define SPL_UNEXPECTED_WORD 	(SPL_ERRNO - 6)
+#define SPL_MAX_ERROR 		(SPL_ERRNO - 7)
 
 extern char __visible *spearl_strerrors[];
 /* it's not a negative number */

+ 29 - 0
include/spearl/spearl.h

@@ -51,6 +51,10 @@ static inline __visible void spearl_setbracket(char *s) {
 	spearl_setbracket_chars(s[0], s[1]);
 }
 
+#define MCALL_MNAME 			'#'
+#define MCALL_MARGS 			'~'
+#define MCALL_MDEST 			'@'
+
 struct spearl_word {
 	/* pointer to somewhere in spl_input, doesn't terminate with zero */
 	char 				*p;
@@ -113,6 +117,31 @@ enum spl_state {
 #define spl_state_left() 		spl_current_state_level()++
 #define spl_state_right() 		if (!--spl_current_state_level()) spl_set_state(OUT)
 
+struct spearl_mcall_arg {
+	enum spl_object_type 		type;
+	struct spearl_object 		*object;
+
+	union {
+		/* zero-terminated */
+
+		char 			*p;
+		struct spearl_list 	*list;
+	};
+
+	struct spl_klist_node 		node;
+};
+
+spl_make_alloc(struct spearl_mcall_arg, arg);
+
+struct spearl_mcall {
+	struct spearl_list 		*list;
+	struct spearl_object 		*dest;
+	struct spearl_object 		*mname;
+	struct spl_klist_node 		args;
+};
+
+spl_make_alloc(struct spearl_mcall, mcall);
+
 /* initialize the library, or reset */
 int __visible spearl_init(void);
 /* join a string to spl_input and scan, join into spearl_list */

+ 1 - 1
samples/1.c

@@ -7,7 +7,7 @@ int main(int argc, const char **argv) {
 	(void) argv;
 
 	spearl_init();
-	if (spearl_join("(hello, world9 asdf (inside list))") == -1) 
+	if (spearl_join("(#hello @world9 ~asdf ~(inside list) asdf)") == -1) 
 		printf("error %d: %s\n", spearl_errno, spearl_strerror(spearl_errno));
 
 	return 0;

+ 116 - 2
spearl.c

@@ -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;
 }