Jelajahi Sumber

tripl: support declares for names and functions, shared library supports too

rain 4 tahun lalu
induk
melakukan
1930e2b472
8 mengubah file dengan 265 tambahan dan 22 penghapusan
  1. 199 2
      arch/arch_templ.c
  2. 26 4
      arch/interp/interp.c
  3. 1 2
      arch/x86_64/asm.c
  4. 0 2
      arch/x86_64/x86_64.c
  5. 8 0
      include/klist.h
  6. 7 2
      include/tripl/tripl.h
  7. 6 6
      sample.trp
  8. 18 4
      tripl.c

+ 199 - 2
arch/arch_templ.c

@@ -34,17 +34,25 @@
 
 
 char 	*psave;
+struct klist_node names_list = KLIST_NODE_INIT(&names_list);
+
+extern char *line_eof;
+
+
 
 __arch_templ static int new_var(char *p, struct arch *);
 __arch_templ static int set_name(char *p, struct arch *);
 __arch_templ static int math_set_name(char *p, struct arch *);
-__arch_templ static int check_condition(char *p, struct arch *);
+__arch_templ static int declare_object(char *p, struct arch *);
 
 
 
 __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 	psave = p;
 
+	if (p == line_eof) 
+		return 0;
+
 	switch (*p) {
 	case '$':
 		/* '$ asdf = 0;' 	--- asdf = 0
@@ -67,8 +75,13 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 
 		break;
 
+	case '<':
+		declare_object(p, arch_fn);
+
+		break;
+
 	default:
-		if (set_name(p, arch_fn) + math_set_name(p, arch_fn) + check_condition(p, arch_fn) == 0) 
+		if (set_name(p, arch_fn) + math_set_name(p, arch_fn) == 0) 
 			exit(generic_error("unknow keyword", psave, lno));
 
 		break;
@@ -219,4 +232,188 @@ static int math_set_name(char *p, struct arch *arch_fn) {
 }
 
 
+/*  
+ *  1. 	
+ *  	< "libfoo.a" 	@ dynalib;
+ *  2. 
+ *  	< fn 		@ fn;
+ *
+ *  3. 
+ *  	< fn 2 		@ fn;
+ *
+ *  4. 	
+ *  	< fn (a, b) 	@ fn;
+ *
+ *  5. 
+ *  	< age 		@ var;
+ * */
+
+static int declare_object(char *p, struct arch *arch_fn) {
+	char *name;
+	int type, out, attr;
+
+	enum { SHARED, NAMES, FUNCTION } guess;
+	struct klist_node *n;
+
+	/* skip '<' */
+
+	p++;
+	_skip_spaces(p);
+
+	
+	if (!getword(p, &type, &out)) 
+		exit(generic_error("a module name or object name required", psave, lno));
+
+
+	switch (type) {
+	case WORDTYPE_STR:
+		/* it must be a dynamic library */
+
+		name = strndup(p + 1, out);
+		guess = SHARED;
+
+		if (arch_fn->arch_import_shared) 
+			arch_fn->arch_import_shared(name);
+
+		p += out + 2;
+
+		free(name);
+
+		break;
+
+	case WORDTYPE_NAME:
+		/* functions or variables */
+
+		name = strndup(p, out);
+
+		/* join to the name list */
+		
+		n = malloc(sizeof(struct klist_node));
+		klist_init(n);
+
+		n->payload = name;
+		klist_add(&names_list, n);
+
+		guess = NAMES;
+
+		p += out;
+
+		break;
+
+	default:
+		exit(generic_error("invalid type for '<'", psave, lno));
+	}
+
+	_skip_spaces(p);
+
+	if (guess == SHARED) 
+		goto skip;
+
+	/* try to getword */
+
+	getword(p, &type, &out);
+	
+	if (type == WORDTYPE_KWORD && out == '@') 
+		goto skip;
+
+	/* can just be a function define */
+
+	guess = FUNCTION;
+
+	if (type == WORDTYPE_NR) {
+		/* only number of arguments */
+		if (!out) 
+			/* special case */
+			n->value = DECLARE_NOARG;
+
+		else 
+			n->value = out;
+
+		_skip_word(p);
+		_skip_spaces(p);
+
+		goto skip;
+	}
+
+	/* argument list */
+
+	if (type == WORDTYPE_KWORD && out == '(') {
+		n->value = 0;
+
+		p++;
+		_skip_spaces(p);
+
+		for (;;) {
+			if (!getword(p, &type, &out)) 
+				/* end but no ')' */
+				exit(generic_error("argument list terminated with no ')'", psave, lno));
+
+
+			switch (type) {
+			case WORDTYPE_KWORD:
+				if (out != ')' && out != ',') 
+					exit(generic_error("invalid operators between arguments", psave, lno));
+
+				if (out == ')') {
+					if (!n->value) 
+						n->value = DECLARE_NOARG;
+
+					p++;
+					_skip_spaces(p);
+
+					goto skip;
+				}
+
+				p++;
+				_skip_spaces(p);
+
+				break;
+
+			case WORDTYPE_NAME:
+				n->value++;
+
+				p += out;
+				_skip_spaces(p);
+
+				break;
+
+			default:
+				exit(generic_error("invalid type between arguments", psave, lno));
+			}
+		}
+	}
+
+
+	if (!n->value) 
+		n->value = DECLARE_NOARG;
+
+
+skip:	attr = getattrmask(p, declare_attrtab);
+
+	if (!attr && guess != SHARED) 
+		exit(generic_error("you must give attributes for declares", psave, lno));
+
+	if (guess != SHARED) {
+		if (attr & 2) 
+			n->value |= DECLARE_FN;
+
+		else if (attr & 4) {
+			if (guess == FUNCTION) 
+				exit(generic_error("you gave us a function declare but told us this is a variable declare? ", 
+							psave, lno));
+
+			/* variable */
+			n->value = DECLARE_VAR;
+		}
+
+		else 
+			exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
+	}
+
+
+	return 0;
+}
+
+
+
 

+ 26 - 4
arch/interp/interp.c

@@ -35,33 +35,36 @@
 
 
 int interp_init(void);
+
 __arch int interp_new_var(char *name, long value, int attr);
 __arch int interp_stack_dump(void);
 __arch int interp_set_var(char *name, long value, int attr);
+__arch int interp_import_shared(char *name);
 
 
 extern char *psave;
 
-struct klist_node stack;
+struct klist_node stack = KLIST_NODE_INIT(&stack), 
+		  libs = KLIST_NODE_INIT(&libs);
+
 
 
 struct arch interp_arch_fn = {
 	.arch_init = interp_init, 
 	.arch_new_var = interp_new_var, 
 	.arch_stack_dump = interp_stack_dump,  
-	.arch_import_shared = NULL, 
+	.arch_import_shared = interp_import_shared, 
 	.arch_funcall = NULL, 
 	.arch_set_var = interp_set_var
 };
 
 
 __arch int interp_init(void) {
-	klist_init(&stack);
-
 	return 0;
 }
 
 
+
 static struct klist_node *find_name(char *name) {
 	struct klist_node *n;
 
@@ -113,3 +116,22 @@ __arch int interp_stack_dump(void) {
 }
 
 
+__arch int interp_import_shared(char *name) {
+	struct klist_node *n;
+	void *dl;
+	
+	if (!(dl = dlopen(name, RTLD_LAZY))) 
+		exit(generic_error(dlerror(), psave, lno));
+
+	n = malloc(sizeof(struct klist_node));
+	klist_init(n);
+
+	n->payload = dl;
+
+	klist_add(&libs, n);
+
+	return 0;
+}
+
+
+

+ 1 - 2
arch/x86_64/asm.c

@@ -36,7 +36,7 @@ struct x86_64_context {
 
 #define current_private 	((struct x86_64_context *) current->private_data)
 
-struct klist_node globvars;
+struct klist_node globvars = KLIST_NODE_INIT(&globvars);
 extern char *psave;
 
 
@@ -98,7 +98,6 @@ __init int x86_64_init_asm(void) {
 	current->private_data = malloc(sizeof(struct x86_64_context));
 	current_private->stkindex = 1;
 
-	klist_init(&stack);
 	klist_init(&globvars);
 
 	printf(asm_head, fname);

+ 0 - 2
arch/x86_64/x86_64.c

@@ -52,8 +52,6 @@ extern int x86_64_asm_fin(void);
 
 
 __arch int x86_64_init(void) {
-	klist_init(&stack);
-
 	return x86_64_init_asm();
 }
 

+ 8 - 0
include/klist.h

@@ -44,6 +44,14 @@ void klist_del(struct klist_node *head, struct klist_node *will_del);
 
 #define klist_empty(list) ((list)->next == (list))
 
+#define KLIST_NODE_INIT(__x) { \
+	.value = 0, \
+	.xmask = 0, \
+	.payload = NULL, \
+	.next = __x, \
+	.prev = __x, \
+}
+
 
 #endif
 

+ 7 - 2
include/tripl/tripl.h

@@ -80,7 +80,7 @@
 
 #define isnr 	isdigit
 #define ischr 	isalpha
-#define _skip_spaces(s) while (isspace(*(s))) (s)++;
+#define _skip_spaces(s) while (isspace(*(s))) (s)++; if (*(s) == '\n') lno++;
 #define skip_spaces() 	_skip_spaces(line_buffer);
 #define _skip_word(s) 	while (!isspace(*(s))) (s)++; _skip_spaces(s)
 
@@ -120,6 +120,11 @@
 #define WORDTYPE_STR 	4
 
 
+#define DECLARE_FN 		0x80000000
+#define DECLARE_VAR 		0x40000000
+#define DECLARE_NOARG	 	0x00002000
+
+
 /* main.c */
 extern char 			*fname;
 extern int 			lno;
@@ -150,7 +155,7 @@ struct attr_table {
 
 
 extern const struct attr_table vardef_attrtab[];
-extern const struct attr_table import_attrtab[];
+extern const struct attr_table declare_attrtab[];
 
 
 int do_tripl_compile(char *, off_t);

+ 6 - 6
sample.trp

@@ -1,10 +1,10 @@
 #! /usr/bin/tripl
 
-$ aasdf = 12341234;
-aasdf = 1234;
-12345 -> aasdf;
+< "libc-2.33.so" 	@ shared;
+< "libc-2.33.so";
+
+< function 		@ fn;
+< function2 2 		@ fn;
+< function3 ( a, b, c ) @ fn;
 
-aasdf : 0 {
-	asdf;
-};
 

+ 18 - 4
tripl.c

@@ -73,6 +73,18 @@ const struct attr_table vardef_attrtab[] = {
 	{ NULL, 0 }
 };
 
+const struct attr_table declare_attrtab[] = {
+	{ "shared", 1 }, 
+	{ "fn", 2 }, 
+	{ "var", 4 }, 
+
+	{ "dynalib", 1 }, 
+	{ "function", 2 }, 
+	{ "variable", 4 }, 
+
+	{ NULL, 0 }
+};
+
 
 
 int do_tripl_compile(char *fbuffer, off_t fsize) {
@@ -124,6 +136,8 @@ static char *__skip_comment(char *fbuffer) {
 
 	_skip_spaces(fbuffer);
 
+	lno++;
+
 	if (*fbuffer == '#') 
 		return __skip_comment(fbuffer);
 
@@ -147,7 +161,7 @@ static int next_line(char *fbuffer) {
 
 		/* got line_eof */
 
-		while (*p != ';') 
+		while (*p != ';' && p < eof) 
 			p++, line_eof++;
 
 		linebuffer_save = line_buffer;
@@ -162,7 +176,7 @@ static int next_line(char *fbuffer) {
 	if (fbuffer >= eof) 
 		return 0;
 
-	while (*line_buffer != ';') 
+	while (*line_buffer != ';' && line_buffer < eof) 
 		line_buffer++;
 
 
@@ -175,7 +189,7 @@ static int next_line(char *fbuffer) {
 
 	line_eof = line_buffer;
 
-	while (*line_eof != ';') 
+	while (*line_eof != ';' && line_eof < eof) 
 		line_eof++;
 
 	linebuffer_save = line_buffer;
@@ -268,7 +282,7 @@ char *getword(char *fbuffer, int *type, int *out) {
 					/* name, but not a number */
 					continue;
 
-				if (!isnr(p[1])) 
+				if ((*p == '-' || *p == '+') && !isnr(p[1])) 
 					continue;
 
 				/* get the number, and save to *out */