Преглед на файлове

spearl: it's may working

intirain преди 4 години
родител
ревизия
a832b8e68e
променени са 8 файла, в които са добавени 148 реда и са изтрити 12 реда
  1. 2 2
      Makefile
  2. 66 0
      builtin.c
  3. 6 5
      error.c
  4. 2 1
      include/spearl/error.h
  5. 41 0
      include/spearl/spearl.h
  6. 2 0
      libc-requirements.m4
  7. 1 1
      samples/1.c
  8. 28 3
      spearl.c

+ 2 - 2
Makefile

@@ -1,8 +1,8 @@
 # the autoconf utils can not run well on ms-windows, and it's 
 # not easy to write a configure.ac for libraries. 
 
-objs 		= spearl.o error.o klist.o 
-srcs 		= spearl.c error.c klist.c 
+objs 		= spearl.o error.o builtin.o klist.o 
+srcs 		= spearl.c error.c builtin.c klist.c 
 target 		= libspearl.a
 CFLAGS 		= -Wall -Wextra -Werror -Iinclude -g 
 MAKEFLAGS 	= --no-print-directory

+ 66 - 0
builtin.c

@@ -0,0 +1,66 @@
+/* spearl/builtin.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 <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <spearl/spearl.h>
+#include <spearl/error.h>
+#include <spearl/sys.h>
+
+static struct spearl_list *do_builtin_macro(struct spearl_mcall *mcall) {
+	(void) mcall;
+
+	printf("hello, world");
+
+	return root_object.list;
+}
+
+int spl_builtin_init(void) {
+	spearl_define_builtin("macro", do_builtin_macro);
+
+	return 0;
+}
+
+int expand_builtin_macro(struct spearl_macro *macro, struct spearl_mcall *mcall, 
+		struct spl_klist_node *node) {
+	struct spearl_list *list;
+
+	if (!(list = macro->builtin.do_builtin(mcall))) 
+		return -1;
+
+	spl_klist_add(node->next, &container_of(list, struct spearl_object, list)->node);
+	return 0;
+}
+
+struct spearl_macro __visible *spearl_define_builtin(char *name, 
+				spearl_builtin_fn_t fn) {
+	struct spearl_macro *macro;
+
+	macro = spl_macro_alloc();
+	macro->type = BUILTIN;
+	macro->name = name;
+	macro->builtin.name = name;
+	macro->builtin.do_builtin = fn;
+
+	spl_klist_init(&macro->node);
+	spl_klist_add(&spl_macros, &macro->node);
+
+	return macro;
+}
+

+ 6 - 5
error.c

@@ -1,19 +1,19 @@
-/* readconf/error.c 
+/* spearl/error.c 
  * 
- * This file is part of readconf. 
+ * This file is part of spearl. 
  * 
- * readconf is free software: you can redistribute it and/or modify 
+ * 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. 
  * 
- * readconf is distributed in the hope that it will be useful, 
+ * 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 readconf. If not, see <https://www.gnu.org/licenses/>
+ * along with spearl. If not, see <https://www.gnu.org/licenses/>
  */ 
 
 
@@ -34,6 +34,7 @@ char __visible *spearl_strerrors[] = {
 	[-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_MACRO_NEXISTS] = "No such macro. ", 
 	[-SPL_MAX_ERROR] = "Unknown error. ", 
 };
 

+ 2 - 1
include/spearl/error.h

@@ -32,7 +32,8 @@
 #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)
+#define SPL_MACRO_NEXISTS 	(SPL_ERRNO - 7)
+#define SPL_MAX_ERROR 		(SPL_ERRNO - 8)
 
 extern char __visible *spearl_strerrors[];
 /* it's not a negative number */

+ 41 - 0
include/spearl/spearl.h

@@ -42,6 +42,8 @@
 extern char __locally *spl_input;
 extern int __locally spl_input_size;
 
+extern struct spearl_object __visible root_object;
+
 extern char __visible lbracket, rbracket;
 static inline __visible void spearl_setbracket_chars(char l, char r) {
 	lbracket = l, rbracket = r;
@@ -142,10 +144,49 @@ struct spearl_mcall {
 
 spl_make_alloc(struct spearl_mcall, mcall);
 
+enum spl_macro_type {
+	USER, 
+	BUILTIN, 
+};
+
+typedef struct spearl_list *spearl_builtin_fn_t (struct spearl_mcall *);
+
+struct spearl_builtin {
+	char 				*name;
+	spearl_builtin_fn_t 		*do_builtin;
+};
+
+struct spearl_macro {
+	enum spl_macro_type 		type;
+	char 				*name;
+
+	union {
+		struct {
+			struct spearl_object 
+					*eval,  
+					*ins;
+		};
+
+		struct spearl_builtin 	builtin;
+	};
+
+	struct spl_klist_node 		node;
+};
+
+#define macro_of_node(n) 		struct_of_node(n, struct spearl_macro)
+
+spl_make_alloc(struct spearl_macro, macro);
+extern struct spl_klist_node __visible spl_macros;
+
 /* initialize the library, or reset */
 int __visible spearl_init(void);
 /* join a string to spl_input and scan, join into spearl_list */
 int __visible spearl_join(char *s);
+/* define a new builtin */
+struct spearl_macro *spearl_define_builtin(char *name, spearl_builtin_fn_t fn);
+
+int __locally spl_builtin_init(void);
+int expand_builtin_macro(struct spearl_macro *, struct spearl_mcall *, struct spl_klist_node *);
 
 #endif
 

+ 2 - 0
libc-requirements.m4

@@ -9,6 +9,8 @@ rq_libcfn(`realloc')
 rq_libcfn(`malloc')
 rq_libcfn(`strlen')
 rq_libcfn(`strcat')
+rq_libcfn(`strcmp')
+rq_libcfn(`strncmp')
 rq_libcfn(`abort')
 rq_libcfn(`perror')
 rq_libcfn(`isspace')

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

+ 28 - 3
spearl.c

@@ -29,6 +29,7 @@ char __locally *spl_input;
 char __visible lbracket = '(', rbracket = ')';
 
 struct spl_klist_node __visible wordlist = SPL_KLIST_NODE_INIT(&wordlist);
+struct spl_klist_node __visible spl_macros = SPL_KLIST_NODE_INIT(&spl_macros);
 struct spearl_object __visible root_object;
 
 #define callfn(fn) if (fn == -1) return -1
@@ -43,6 +44,8 @@ int spearl_init(void) {
 	
 	root_object.list->nobjs = 0;
 
+	spl_builtin_init();
+
 	return 0;
 }
 
@@ -295,15 +298,31 @@ struct spearl_mcall *collect_args(struct spearl_list *list) {
 
 
 static inline int mcall_valid(struct spearl_mcall *mcall) {
-	if (mcall->dest && mcall->mname && !spl_klist_empty(&mcall->args)) 
+	if (mcall->dest && mcall->mname) 
 		return 1;
 
 	return 0;
 }
 
 
-static int do_expand_macro(struct spearl_list *list) {
+static struct spearl_macro *find_macro(struct spearl_mcall *mcall) {
+	struct spl_klist_node *n;
+	struct spearl_macro *macro;
+
+	for (n = spl_macros.next; n != &spl_macros; n = n->next) {
+		macro = macro_of_node(n);
+
+		if (!strncmp(macro->name, mcall->mname->word->p + 1, mcall->mname->word->len - 1)) 
+			return macro;
+	}
+	
+	return NULL;
+}
+
+
+static int do_expand_macro(struct spearl_list *list, struct spl_klist_node *node) {
 	struct spearl_mcall *mcall;
+	struct spearl_macro *macro;
 
 	if (!(mcall = collect_args(list))) 
 		return -1;
@@ -311,6 +330,12 @@ static int do_expand_macro(struct spearl_list *list) {
 	if (!mcall_valid(mcall)) 
 		return spearl_error(SPL_INCOMPLETE_CALL);
 
+	if (!(macro = find_macro(mcall))) 
+		return spearl_error(SPL_MACRO_NEXISTS);
+
+	if (macro->type == BUILTIN) 
+		callfn(expand_builtin_macro(macro, mcall, node));
+
 	return 0;
 }
 
@@ -325,7 +350,7 @@ static int expand_calls(void) {
 		if (obj->type != LIST) 
 			return spearl_error(SPL_INVALID_LIST);
 
-		callfn(do_expand_macro(obj->list));
+		callfn(do_expand_macro(obj->list, n));
 	}
 
 	return 0;