Browse Source

spearl: built the list

intirain 4 năm trước cách đây
mục cha
commit
6cf2ce96f3
8 tập tin đã thay đổi với 140 bổ sung và 25 xóa
  1. 3 2
      error.c
  2. 4 3
      include/spearl/error.h
  3. 25 5
      include/spearl/spearl.h
  4. 1 0
      include/spearl/sys.h
  5. 1 0
      libc-requirements.m4
  6. 1 1
      samples/1.c
  7. 1 1
      samples/Makefile
  8. 104 13
      spearl.c

+ 3 - 2
error.c

@@ -28,7 +28,8 @@ int __visible spearl_errno = 0;
 
 char __visible *spearl_strerrors[] = {
 	[0] = "Success. \n", 
-	[-SPL_BAD_BRACKET] = "Brackets didn't match. ", 
-	[-SPL_MAX_ERROR] = "Unknown error. \n", 
+	[-SPL_BAD_BRACKETS] = "Brackets didn't match. ", 
+	[-SPL_INVALID_LIST] = "Invalid list format. ", 
+	[-SPL_MAX_ERROR] = "Unknown error. ", 
 };
 

+ 4 - 3
include/spearl/error.h

@@ -26,14 +26,15 @@
 #include <spearl/compiler.h>
 #include <spearl/klist.h>
 
-#define SPL_BAD_BRACKET		(SPL_ERRNO - 1)
-#define SPL_MAX_ERROR 		(SPL_ERRNO - 2)
+#define SPL_BAD_BRACKETS	(SPL_ERRNO - 1)
+#define SPL_INVALID_LIST 	(SPL_ERRNO - 2)
+#define SPL_MAX_ERROR 		(SPL_ERRNO - 3)
 
 extern char __visible *spearl_strerrors[];
 /* it's not a negative number */
 extern int __visible spearl_errno;
 
-#define spearl_error(err) 	spearl_errno = -err, -1;
+#define spearl_error(err) 	(spearl_errno = -err, -1)
 #define spearl_check_errno(err)	((err) > -SPL_ERRNO && (err) < -SPL_MAX_ERROR)
 static inline char *spearl_strerror(int err) {
 	if (spearl_check_errno(err)) 

+ 25 - 5
include/spearl/spearl.h

@@ -53,11 +53,11 @@ static inline __visible void spearl_setbracket(char *s) {
 
 struct spearl_word {
 	/* pointer to somewhere in spl_input, doesn't terminate with zero */
-	char 			*p;
+	char 				*p;
 	/* length of string, by strlen without + 1 */
-	int 			len;
+	int 				len;
 	/* the head is in spearl_list */
-	struct spl_klist_node 	node;
+	struct spl_klist_node 		node;
 };
 
 extern struct spl_klist_node __visible wordlist;
@@ -67,12 +67,32 @@ extern struct spl_klist_node __visible wordlist;
 spl_make_alloc(struct spearl_word, word);
 
 struct spearl_list {
-	struct spl_klist_node 	objs;
-	int 			nobjs;
+	struct spl_klist_node 		node;
+	struct spl_klist_node 		objs;
+	int 				nobjs;
 };
 
+#define list_of_node(n) 		struct_of_node(n, struct spearl_list)
 spl_make_alloc(struct spearl_list, list);
 
+enum spl_object_type {
+	WORD, 
+	LIST, 
+};
+
+struct spearl_object {
+	enum spl_object_type 		type;
+	struct spl_klist_node 		node;
+
+	union {
+		struct spearl_word 	*word;
+		struct spearl_list 	*list;
+	};
+};
+
+#define object_of_node(n) 		struct_of_node(n, struct spearl_object)
+spl_make_alloc(struct spearl_object, object);
+
 /* status of scanner, describes the current status. IN if is in 
  * a quote and OUT if it's OUT of a quote. */
 

+ 1 - 0
include/spearl/sys.h

@@ -33,6 +33,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <stddef.h>
 
 /* it's meaningless, just a big negative number */
 #define SPL_ERRNO -1024

+ 1 - 0
libc-requirements.m4

@@ -12,3 +12,4 @@ rq_libcfn(`strcat')
 rq_libcfn(`abort')
 rq_libcfn(`perror')
 rq_libcfn(`isspace')
+rq_libcfn(`offsetof')

+ 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)") == -1) 
+	if (spearl_join("(hello, world9 asdf (inside list))") == -1) 
 		printf("error %d: %s\n", spearl_errno, spearl_strerror(spearl_errno));
 
 	return 0;

+ 1 - 1
samples/Makefile

@@ -1,7 +1,7 @@
 objs		= 1.o
 srcs 		= 1.c
 targets 	= 1
-CFLAGS 		= -Wall -Wextra -Werror -I../include -L.. -lspearl 
+CFLAGS 		= -Wall -Wextra -Werror -I../include -L.. -lspearl -g 
 
 all : $(targets)
 %: %.c 

+ 104 - 13
spearl.c

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