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

tripl 0.1.1-beta: beta test :-)

rain преди 4 години
родител
ревизия
bec4a36865
променени са 8 файла, в които са добавени 88 реда и са изтрити 99 реда
  1. 1 1
      Makefile
  2. 37 12
      arch/arch_templ.c
  3. 2 2
      arch/cstyles.c
  4. 15 42
      arch/interp/interp.c
  5. 5 27
      arch/x86_64/asm.c
  6. 7 3
      include/tripl/tripl.h
  7. 4 1
      sample.trp
  8. 17 11
      tripl.c

+ 1 - 1
Makefile

@@ -1,7 +1,7 @@
 tripl-objs 	= main.o tripl.o error.o klist.o  
 tripl-out 	= tripl
 
-CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
+CFLAGS 		= -Wall -Werror -Iinclude -g -ldl -O2 
 
 ifeq ($(install-dir),)
 	install-dir = /usr/bin

+ 37 - 12
arch/arch_templ.c

@@ -85,6 +85,13 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 
 		break;
 
+	case '+':
+	case '-':
+		if (!math_set_name(p, arch_fn)) 
+			exit(generic_error("unknow keyword", psave, lno));
+
+		break;
+
 	default:
 		if (unlikely(!set_name(p, arch_fn) && 
 					!math_set_name(p, arch_fn) && 
@@ -98,9 +105,21 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 }
 
 
+struct klist_node *find_name(char *name) {
+	struct klist_node *n;
+
+	for (n = names_list.next; n != &names_list; n = n->next) 
+		if (!strcmp(n->payload, name)) 
+			return n;
+
+	return NULL;
+}
+
+
 __arch_templ static int new_var(char *p, struct arch *arch_fn) {
 	char *name;
 	int type, out, attr;
+	struct klist_node *t;
 	
 	getword(p, &type, &out);
 
@@ -149,6 +168,12 @@ __arch_templ static int new_var(char *p, struct arch *arch_fn) {
 
 	attr = getattrmask(p, vardef_attrtab);
 
+	if (unlikely((t = find_name(name)) && !(t->xmask & XMASK_WEAK))) {
+		error(name);
+
+		exit(generic_error("name already defined", psave, lno));
+	}
+
 	arch_fn->arch_new_var(name, out, attr);
 
 	/* the name can't be free */
@@ -239,16 +264,6 @@ static int math_set_name(char *p, struct arch *arch_fn) {
 }
 
 
-int name_defined(char *name) {
-	struct klist_node *n;
-
-	for (n = names_list.next; n != &names_list; n = n->next) 
-		if (!strcmp(n->payload, name)) 
-			return 1;
-
-	return 0;
-}
-
 
 /*  
  *  1. 	
@@ -271,7 +286,7 @@ static int declare_object(char *p, struct arch *arch_fn) {
 	int type, out, attr;
 
 	enum { SHARED, NAMES, FUNCTION } guess;
-	struct klist_node *n;
+	struct klist_node *n, *t;
 
 	/* skip '<' */
 
@@ -423,12 +438,22 @@ skip:	attr = getattrmask(p, declare_attrtab);
 		exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
 
 
-	if (unlikely(name_defined(n->payload))) {
+	if (unlikely((t = find_name(n->payload))) && !(t->xmask & XMASK_WEAK)) {
 		error((char *) n->payload);
 
 		exit(generic_error("name already defined", psave, lno));
 	}
 
+	if (t && t->xmask == XMASK_WEAK) 
+		if (((t->value & DECLARE_FN) && !(n->value & DECLARE_FN)) || 
+				((n->value & DECLARE_FN) && !(t->value & DECLARE_FN))) {
+			error((char *) n->payload);
+
+			exit(generic_error("name defined in different type", psave, lno));
+		}
+	
+
+	n->xmask = XMASK_WEAK;
 	klist_add(&names_list, n);
 
 	return 0;

+ 2 - 2
arch/cstyles.c

@@ -37,7 +37,7 @@
 
 
 /* arch/arch_templ.c */
-int name_defined(char *name);
+int find_name(char *name);
 
 extern struct klist_node names_list;
 extern char *psave;
@@ -168,7 +168,7 @@ __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
 
 	n->payload = fname;
 
-	if (name_defined(n->payload)) {
+	if (find_name(n->payload)) {
 		error((char *) n->payload);
 
 		exit(generic_error("name already defined", psave, lno));

+ 15 - 42
arch/interp/interp.c

@@ -41,11 +41,11 @@ __arch int interp_dump(int);
 __arch int interp_set_var(char *name, long value, int attr);
 __arch int interp_import_shared(char *name);
 
+extern struct klist_node *find_name(char *name);
 
 extern char *psave;
 
-struct klist_node stack = KLIST_NODE_INIT(&stack), 
-		  libs = KLIST_NODE_INIT(&libs);
+struct klist_node libs = KLIST_NODE_INIT(&libs);
 extern struct klist_node names_list;
 
 
@@ -66,19 +66,8 @@ __arch int interp_init(void) {
 
 
 
-static struct klist_node *find_name(char *name) {
-	struct klist_node *n;
-
-	for (n = stack.next; n != &stack; n = n->next) 
-		if (!strcmp(n->payload, name)) 
-			return n;
-
-	return NULL;
-}
-
-
 __arch int interp_new_var(char *name, long value, int attr) {
-	push_nr(value, name, attr);
+	__push_nr(&names_list, value, name, attr);
 
 	return 0;
 }
@@ -100,24 +89,6 @@ __arch int interp_set_var(char *name, long value, int attr) {
 }
 
 
-__arch static int interp_stack_dump(void) {
-	struct klist_node *n;
-	int i;
-
-	ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
-
-	printf("stack dump: \n");
-
-	ansi_csi_ed(stdout);
-
-	for (i = 0, n = stack.next; n != &stack; n = n->next, i++) 
-		printf("\t%3d: 	%s \t%ld @ %08x\n", i, (char *) n->payload, n->value, n->xmask);
-
-	printf("\n");
-
-	return 0;
-}
-
 #define NAMES_VARS 	1
 #define NAMES_FNS 	2
 #define NAMES_ANY 	3
@@ -149,9 +120,14 @@ __arch static int interp_names_dump(int names) {
 		}
 
 		
-		if (!(n->value & DECLARE_FN) && (names & NAMES_VARS)) 
+		if (!(n->value & DECLARE_FN) && (names & NAMES_VARS)) {
 			/* variables */
-			printf("\t var %s\n", (char *)n->payload);
+			if (n->xmask & XMASK_WEAK)
+				printf("\t var %s\n", (char *) n->payload);
+
+			else 
+				printf("\t var %s: %ld\n", (char *) n->payload, n->value);
+		}
 	}
 
 	printf("\n");
@@ -179,9 +155,9 @@ __arch static int interp_libs_dump(void) {
 
 
 __arch int interp_dump(int attr) {
-	if (!attr || attr == 32) {
+	if (!attr || attr == 16) {
 		/* dump all */
-		interp_stack_dump();
+
 		interp_names_dump(NAMES_ANY);
 		interp_libs_dump();
 
@@ -189,18 +165,15 @@ __arch int interp_dump(int attr) {
 	}
 
 	if (attr & 1) 
-		interp_stack_dump();
-
-	if (attr & 2) 
 		interp_names_dump(NAMES_VARS);
 
-	if (attr & 4) 
+	if (attr & 2) 
 		interp_names_dump(NAMES_FNS);
 
-	if (attr & 8) 
+	if (attr & 4) 
 		interp_names_dump(NAMES_ANY);
 
-	if (attr & 16) 
+	if (attr & 8) 
 		interp_libs_dump();
 
 	return 0;

+ 5 - 27
arch/x86_64/asm.c

@@ -36,7 +36,8 @@ struct x86_64_context {
 
 #define current_private 	((struct x86_64_context *) current->private_data)
 
-struct klist_node globvars = KLIST_NODE_INIT(&globvars);
+extern struct klist_node *find_name(char *name);
+extern struct klist_node names_list;
 extern char *psave;
 
 
@@ -98,36 +99,12 @@ __init int x86_64_init_asm(void) {
 	current->private_data = malloc(sizeof(struct x86_64_context));
 	current_private->stkindex = 1;
 
-	klist_init(&globvars);
-
 	printf(asm_head, fname);
 
 	return 0;
 }
 
 
-static inline struct klist_node *__find_in(struct klist_node *head, char *name) {
-	struct klist_node *n;
-
-	for (n = head->next; n != head; n = n->next) 
-		if (!strcmp(n->payload, name)) 
-			return n;
-
-	return NULL;
-}
-
-
-static struct klist_node *find_name(char *name) {
-	struct klist_node *res;
-
-	if (!(res = __find_in(&stack, name))) 
-		if (!(res = __find_in(&globvars, name))) 
-			return NULL;
-
-	return res;
-}
-
-
 __init int x86_64_asm_fin(void) {
 	printf(funcend);
 
@@ -141,7 +118,7 @@ __arch int x86_64_new_var(char *name, long value, int attr) {
 
 		printf(global_var, name, name, value);
 
-		__push_nr(&globvars, value, name, attr);
+		__push_nr(&names_list, value, name, attr);
 
 		return 0;
 	}
@@ -149,7 +126,8 @@ __arch int x86_64_new_var(char *name, long value, int attr) {
 	/* local variable */
 
 	printf(stack_var, value, current_private->stkindex << 3);
-	push_nr(current_private->stkindex, name, attr);
+
+	__push_nr(&names_list, current_private->stkindex, name, attr);
 
 	current_private->stkindex++;
 

+ 7 - 3
include/tripl/tripl.h

@@ -80,8 +80,10 @@
 	fprintf(stderr, "\n")
 
 
-#define isnr 	isdigit
-#define ischr 	isalpha
+/* oh no, isdigit() is a function :-( */
+// #define isnr(c) 	isdigit(c)
+#define isnr(c) 	((c) >= '0' && (c) <= '9')
+#define ischr(c)	isalpha(c)
 #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)
@@ -131,12 +133,14 @@
 #define DECLARE_VAR 		0x40000000
 #define DECLARE_NOARG	 	0x00002000
 
+/* only declared, so will not raise name-already-defined */
+#define XMASK_WEAK 		0x80000000
+
 
 /* main.c */
 extern char 			*fname;
 extern int 			lno;
 extern char 			*eof;
-extern struct klist_node 	stack;
 extern char 			*linebuffer_save;
 
 #define arg_init(a) 		(a)->arg_s = NULL; (a)->arg_v = 0; \

+ 4 - 1
sample.trp

@@ -11,5 +11,8 @@ char *functio2n(void);
 
 < function3 ( a, b, c ) @ fn;
 
-!  			@ fn var;
+$ a = +10;
+
+-10 -> a;
+
 

+ 17 - 11
tripl.c

@@ -88,16 +88,15 @@ const struct attr_table declare_attrtab[] = {
 
 
 const struct attr_table dump_attrtab[] = {
-	{ "stack", 1 }, 
-	{ "var", 2 }, 
-	{ "fn", 4 }, 
-	{ "names", 8 }, 
-	{ "libs", 16 }, 
-	{ "all", 32 }, 
+	{ "var", 1 }, 
+	{ "fn", 2 }, 
+	{ "names", 4 }, 
+	{ "libs", 8 }, 
+	{ "all", 16 }, 
 
 	{ "variables", 2 }, 
-	{ "functions", 4 }, 
-	{ "libraries", 16 }, 
+	{ "functions", 2 }, 
+	{ "libraries", 8 }, 
 
 	{ NULL, 0 }
 };
@@ -127,9 +126,7 @@ int do_tripl_compile(char *fbuffer, off_t fsize) {
 		return 1;
 	}
 
-ok:	klist_init(&stack);
-
-	eof = fbuffer + fsize;
+ok:	eof = fbuffer + fsize;
 
 	arch_fn->arch_init();
 
@@ -312,6 +309,8 @@ char *getword(char *p, int *type, int *out) {
 
 	last_word_type = 0;
 
+	*type = 0;
+
 	if (p >= line_eof) 
 		return NULL;
 
@@ -336,6 +335,13 @@ char *getword(char *p, int *type, int *out) {
 			if (!curr_word_type) 
 				break;
 
+			if ((*p == '-' || *p == '+') && isnr(p[1])) {
+				*type = WORDTYPE_NR;
+				*out = getnumber(p);
+
+				return p;
+			}
+
 			if (isfirstchr() && curr_word_type == WORDTYPE_KWORD) {
 				*type = WORDTYPE_KWORD;
 				*out = *p;