Procházet zdrojové kódy

tripl: rebuilding for arch/: now using objects (but also c :-))

rain před 4 roky
rodič
revize
c507520c40
11 změnil soubory, kde provedl 296 přidání a 791 odebrání
  1. 11 3
      arch/Makefile
  2. 135 0
      arch/arch_templ.c
  3. 24 437
      arch/interp/interp.c
  4. 1 1
      arch/x86_64/Makefile
  5. 36 304
      arch/x86_64/asm.c
  6. 19 12
      arch/x86_64/x86_64.c
  7. 1 1
      include/klist.h
  8. 26 5
      include/tripl/arch.h
  9. 11 0
      include/tripl/tripl.h
  10. 3 4
      sample.trp
  11. 29 24
      tripl.c

+ 11 - 3
arch/Makefile

@@ -1,17 +1,25 @@
-arch = interp x86_64 
+arch 	= interp x86_64 
+CFLAGS 	= -Wall -Werror -I../include -g 
+templ 	= arch_templ.o 
+targ 	= libarch.a 
 
 
-all : 
+all : 	$(templ)
 	@# this writes into arch.a 
 	@for n in $(arch); do \
 		$(MAKE) -C $$n; \
 	done;
+	$(AR) cr $(targ) $(templ)
+
+
+%.o : %.c 
+	$(CC) $(CFLAGS) -c $*.c 
 
 
 .PHONY clean : 
 	@for n in $(arch); do \
 		$(MAKE) -C $$n clean; \
 	done;
-	$(RM) 	libarch.a 
+	$(RM) 	libarch.a $(templ)
 
 

+ 135 - 0
arch/arch_templ.c

@@ -0,0 +1,135 @@
+/* tripl/arch/arch_templ.c 
+ * 
+ * This file is part of tripl. 
+ * 
+ * tripl 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. 
+ * 
+ * tripl 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 tripl. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <string.h>
+
+#include <errno.h>
+
+#include <klist.h>
+#include <tripl/tripl.h>
+#include <tripl/limits.h>
+#include <tripl/arch.h>
+
+
+
+char 	*psave;
+
+__arch_templ static int new_var(char *p, struct arch *);
+
+
+
+__arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
+	psave = p;
+
+	switch (*p) {
+	case '$':
+		/* '$ asdf = 0;' 	--- asdf = 0
+		 * '$ asdf 0;'		--- asdf = 0
+		 * '$ asdf;'		--- asdf = 0
+		 * */
+
+		p++;
+		_skip_spaces(p);
+
+		new_var(p, arch_fn);
+
+		break;
+
+	case '!':
+		if (!arch_fn->arch_stack_dump) 
+			exit(generic_error("unsupported 'stack-dump' in current mode", psave, lno));
+
+		arch_fn->arch_stack_dump();
+
+		break;
+
+	default:
+		exit(generic_error("unknown keyword", psave, lno));
+
+		break;
+	}
+
+	return 0;
+}
+
+
+__arch_templ static int new_var(char *p, struct arch *arch_fn) {
+	char *name;
+	int type, out, attr;
+	
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_NAME) 
+		exit(generic_error("a name for variable required", psave, lno));
+
+	name = strndup(p, out);
+
+	p += out;
+	_skip_spaces(p);
+
+	getword(p, &type, &out);
+
+	switch (type) {
+	case WORDTYPE_KWORD:
+		if (out != '=') 
+			exit(generic_error("keyword but it's not a '='", psave, lno));
+		
+		++p;
+		_skip_spaces(p);
+
+		getword(p, &type, &out);
+
+		if (type != WORDTYPE_NR) 
+			exit(generic_error("a number required", psave, lno));
+
+		_skip_word(p);
+
+		break;
+
+	case WORDTYPE_NR:
+		/* to attribute (if have) */
+
+		_skip_word(p);
+
+		break;
+
+	case 0:
+		out = 0;
+
+		break;
+
+	default:
+		exit(generic_error("bad word after variable name", psave, lno));
+	}
+
+	attr = getattrmask(p, vardef_attrtab);
+
+	arch_fn->arch_new_var(name, out, attr);
+
+	/* the name can't be free */
+
+	return 0;
+}
+
+

+ 24 - 437
arch/interp/interp.c

@@ -31,467 +31,54 @@
 #include <klist.h>
 #include <tripl/tripl.h>
 #include <tripl/limits.h>
+#include <tripl/arch.h>
 
 
+int interp_init(void);
+__arch int interp_new_var(char *name, long value, int attr);
+__arch int interp_stack_dump(void);
 
-struct klist_node libraries;
+struct klist_node stack;
 
-static char *lbsave;
 
+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_funcall = NULL, 
+	.arch_set_var = NULL
+};
 
-static int do_funcall(char *p), 
-	   do_dl_funcall(void *f, char *p), 
-	   do_check_string(char *p, char *d);
 
-static inline struct klist_node *find_name(char *s);
-
-extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
-
-
-int interp_init(void) {
-	klist_init(&libraries);
+__arch int interp_init(void) {
+	klist_init(&stack);
 
 	return 0;
 }
 
 
-
-int interp_do_keyword(char *p, char c) {
-	struct klist_node *n;
-	int type, out, i, attrs, flag = 0;
-
-	char *name = NULL, *save;
-	void *dlib;
-
-	lbsave = p;
-
-	switch (c) {
-	case '$':
-		/* push a number in (with a name) */
-		++p;
-		_skip_spaces(p);
-
-		getword(p, &type, &out);
-
-		if (type != WORDTYPE_NAME) 
-			exit(generic_error("'$' requires a name", lbsave, lno));
-
-		name = strndup(p, out);
-
-		p += out;
-		_skip_spaces(p);
-
-		getword(p, &type, &out);
-
-		if (type == WORDTYPE_KWORD) {
-			if (out != '=') 
-				exit(generic_error("invalid keyword after name", lbsave, lno));
-
-			p++;
-			_skip_spaces(p);
-
-			getword(p, &type, &out);
-		}
-
-		if (type != WORDTYPE_NR) {
-			if (type) 
-				exit(generic_error("'$' requires a number", lbsave, lno));
-
-			out = 0;
-		}
-
-		p++;
-		_skip_word(p);
-
-		attrs = getattrmask(p, vardef_attrtab);
-		push_nr(out, name, attrs);
-
-		break;
-
-	/* ABANDONED IN COMPILE MODE */
-	case '`':
-		/* pop a number */
-
-		if (klist_empty(&stack)) 
-			exit(generic_error("stack underflow", lbsave, lno));
-
-		_pop_nr();
-
-		break;
-
-	case '!':
-		ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
-
-		printf("stack dump: \n");
-
-		ansi_csi_ed(stdout);
-
-		if (klist_empty(&stack)) 
-			printf("\t(nothing)\n");
-
-		for (i = 0, n = stack.next; n != &stack; n = n->next, i++) {
-			if (n->payload) 
-				/* named */
-				printf("\t%d: \t\t%d = %x (%s)\n", i, 
-						n->value, n->value, (char *) n->payload);
-
-			else 
-				printf("\t%d: \t\t%d = %x\n", i, n->value, n->value);
-		}
-
-		break;
-
-	case '<':
-		++p;
-		_skip_spaces(p);
-
-		/* import shared library */
-		p = getword(p, &type, &out);
-
-		if (type != WORDTYPE_STR) 
-			exit(generic_error("'<' requires a library name (string)", 
-						lbsave, lno));
-
-		name = strndup(p + 1, out);
-
-		dlib = dlopen(name, RTLD_LAZY);
-
-		if (!dlib) {
-			generic_error(dlerror(), linebuffer_save, lno);
-			exit(generic_error("import library failed", lbsave, lno));
-		}
-
-		n = malloc(sizeof(struct klist_node));
-
-		klist_init(n);
-		n->payload = dlib;
-
-		/* join to library list */
-		klist_add(&libraries, n);
-
-		free(name);
-
-		break;
-
-	case '@':
-		++p;
-		_skip_spaces(p);
-
-		/* function call. the real funny thing is going on :-) */
-		do_funcall(p);
-
-		break;
-
-	default:
-		save = p;
-
-		/* setting 'i = 0', or function call */
-		getword(p, &type, &out);
-
-		if (type == WORDTYPE_KWORD && out == '&') {
-			flag = 1;
-			p++;
-
-			getword(p, &type, &out);
-		}
-
-		if (type != WORDTYPE_NAME) 
-			exit(generic_error("unknown keyword", lbsave, lno));
-
-		name = strndup(p, out);
-
-		_skip_word(p);
-		_skip_spaces(p);
-
-		if (*p == '(') {
-			/* function calling */
-			do_funcall(save);
-
-			break;
-		}
-
-		if (!(n = find_name(name))) 
-			exit(generic_error("name not found", lbsave, lno));
-
-		if (n->xmask & 4)
-			/* read only */
-			exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
-
-		if (n->xmask & 1 && !flag) 
-			exit(generic_error("no '&' found before global variable", lbsave, lno));
-
-		if (!getword(p, &type, &out)) 
-			exit(generic_error("no equal", lbsave, lno));
-
-		if (out != '=') 
-			exit(generic_error("not a '='", lbsave, lno));
-
-		p++;
-		_skip_spaces(p);
-
-		if (!getword(p, &type, &out)) 
-			exit(generic_error("no value", lbsave, lno));
-
-		n->value = out;
-
-		free(name);
-
-		break;
-	}
-
-	return 0;
-}
-
-
-static int do_funcall(char *p) {
-	struct klist_node *n;
-	char *name;
-	int type, out, res;
-
-	void *func;
-
-	/* get function name */
-
-	p = getword(p, &type, &out);
-
-	if (type != WORDTYPE_NAME) 
-		exit(generic_error("a function name required", lbsave, lno));
-
-	name = strndup(p, out);
-
-	for (n = libraries.next; n != &libraries; n = n->next) {
-		if ((func = dlsym(n->payload, name))) {
-			/* real function call */
-
-			res = do_dl_funcall(func, p + out);
-
-			printf("%s: %d\n", name, res);
-
-			free(name);
-
-			return 0;
-		}
-	}
-
-	error("%s", name);
-	exit(generic_error("function not found", lbsave, lno));
+__arch int interp_new_var(char *name, long value, int attr) {
+	push_nr(value, name, attr);
 
 	return 0;
 }
 
 
-static inline struct klist_node *find_name(char *np) {
+__arch int interp_stack_dump(void) {
 	struct klist_node *n;
+	int i;
 
-	for (n = stack.next; n != &stack; n = n->next) 
-		if (!strcmp(n->payload, np)) 
-			return n;
-
-	return NULL;
-}
-
-
-
-/* p: the argument list (string) */
-
-static int do_dl_funcall(void *f, char *p) {
-	struct arg_entry *args, *next_arg;
-	struct klist_node *node;
-	char *name, *dest;
-	int type, out, n = 0, res;
-
-	/* get argument list and call */
-
-	_skip_spaces(p);
-
-	args = malloc(sizeof(struct arg_entry));
-	next_arg = args;
-
-	if (*p == ';') 
-		goto noarg;
-
-	while (getword(p, &type, &out)) {
-		switch (type) {
-		case WORDTYPE_NAME:
-			name = strndup(p, out);
-
-			if (!(node = find_name(name))) {
-				error("%s", name);
-				generic_error("name not found", lbsave, lno);
-
-				free(name);
-
-				exit(1);
-			}
-
-			free(name);
-
-			out = node->value;
-		
-			/* fall through */
-
-		case WORDTYPE_NR:
-			arg_init(next_arg);
-
-			next_arg->arg_v = out;
-
-			next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
-
-			break;
-
-		case WORDTYPE_STR:
-			name = strndup(p + 1, out);
-			dest = malloc(out - 1);
-
-			next_arg->arg_s = dest;
-			next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
-
-			do_check_string(name, dest);
-
-			free(name);
-
-			break;
-
-		case WORDTYPE_KWORD:
-			if (out != ',' && out != '(' && out != ')') 
-				exit(generic_error("unknow keyword after arguments", lbsave, lno));
-
-			if (out == '(') 
-				if (n) 
-					exit(generic_error("invalid '(' after arguments", lbsave, lno));
-
-			if (out == ')') {
-				p++;
-				_skip_spaces(p);
-
-				/* ; */
-
-				getword(p, &type, &out);
-
-				if (type) 
-					exit(generic_error("arguments after ')'", lbsave, lno));
+	ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
 
-				goto noarg;
-			}
+	printf("stack dump: \n");
 
-			break;
+	ansi_csi_ed(stdout);
 
-		default:
-			exit(generic_error("error type passing argument", lbsave, lno));
-
-			break;
-		}
-
-		if (type == WORDTYPE_STR) 
-			p += out + 2;
-
-		else if (type == WORDTYPE_KWORD) {
-			p++;
-			_skip_spaces(p);
-		}
-
-		else 
-			_skip_word(p);
-
-		++n;
-	}
-
-noarg:	res = arch_funcall(f, args, n);
-
-	next_arg = args->next;
-
-	for (; args; args = next_arg) {
-		next_arg = args->next;
-
-		if (args->arg_s) 
-			free(args->arg_s);
-
-		free(args);
-	}
-
-	return res;
-}
-
-
-static int do_check_string(char *p, char *dest) {
-	int i, stat = 0;
-	char ascii = 0;
-	int high, low;
-	char buffer[2] = {0, 0};
-
-	for (i = 0; p[i]; ) {
-		if (!stat) {
-			if (p[i] == '\\') 
-				stat = 1;
-
-			else 
-				dest[i] = p[i];
-
-			++i;
-
-			continue;
-		}
-
-
-		switch (p[i]) {
-		case '\\':
-			dest[i - 1] = '\\';
-			
-			break;
-
-		case 'n':
-			dest[i - 1] = '\n';
-
-			break;
-
-		case 't':
-			dest[i - 1] = '\t';
-
-			break;
-
-		case 'x':
-			/* \xXX */
-			buffer[0] = p[i + 1];
-			buffer[1] = p[i + 2];
-
-			if (!buffer[0] || !buffer[1]) 
-				exit(generic_error("'x' requires a sequence looks like '\\xXX'", 
-							lbsave, lno));
-
-			if (buffer[0] >= 'A' && buffer[0] <= 'F') 
-				high = atoX(buffer[0]);
-
-			else 
-				high = atox(buffer[0]);
-
-			if (buffer[1] >= 'a' && buffer[1] <= 'f') 
-				low = atox(buffer[1]);
-
-			else 
-				low = atoX(buffer[1]);
-			
-			ascii = (high << 4) + low;
-
-			dest[i - 2] = ascii;
-
-			break;
-
-		case '0'...'7':
-			exit(generic_error("no support for octal numbers. use hexadecimal instead", 
-						lbsave, lno));
-
-			break;
-
-		default:
-			exit(generic_error("unknow sequence. \n", lbsave, lno));
-		}
-
-		stat = 0;
-		i++;
-	}
+	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);
 
 	return 0;
 }
 
 
-

+ 1 - 1
arch/x86_64/Makefile

@@ -1,4 +1,4 @@
-objs = do_keyword.o asm.o 
+objs = x86_64.o asm.o 
 targ = ../libarch.a 
 CFLAGS = -Wall -Werror -I../../include -g -ldl 
 

+ 36 - 304
arch/x86_64/asm.c

@@ -28,16 +28,18 @@
 #include <tripl/arch.h>
 
 
-static char *lbsave;
-static int stack_index;
 
-struct klist_node globvars;
+struct x86_64_context {
+	int stkindex;
+};
+
+
+#define current_private 	((struct x86_64_context *) current->private_data)
 
-static int do_funcall(char *p);
-static int set_name(char *p, char *name);
+struct klist_node globvars;
 
 
-const static char asm_head[] = {
+__ll_asm const static char asm_head[] = {
 	"	.file \"%s\"\n"
 	"	.text\n"
 
@@ -57,347 +59,77 @@ const static char asm_head[] = {
 };
 
 
-const static char global_var[] = {
+__ll_asm const static char global_var[] = {
 	"	.data\n"
 	"	.globl 	%s\n"
-	"%s: 	.long 	%d\n"
+	"%s: 	.quad 	%ld\n"
 	"	.text\n"
 };
 
 
-const static char stack_var[] = {
-	"	movq 	$%d, -%d(%%rbp)\n"
+__ll_asm const static char stack_var[] = {
+	"	movq 	$%ld, -%d(%%rbp)\n"
 };
 
 
-const static char wr_stkvar[] = {
+/*__ll_asm const static char wr_stkvar[] = {
 	"	movq 	$%d, -%d(%%rbp)\n"
-};
+};*/
 
 
-const static char wr_glbvar[] = {
+/*__ll_asm const static char wr_glbvar[] = {
 	"	movq 	$%d, %s(%%rip)\n"
-};
+};*/
 
 
-const static char noarg_funcall[] = {
+/*__ll_asm const static char noarg_funcall[] = {
 	"	call	%s\n"
-};
+};*/
 
 
-const static char funcend[] = {
+__ll_asm const static char funcend[] = {
 	"	popq 	%%rbp\n"
 	"	ret\n"
 };
 
 
-int x86_64_init_asm(void) {
-	stack_index = 0;
-	printf(asm_head, fname);
-
-	return 0;
-}
-
-
-int x86_64_asm_fin(void) {
-	printf(funcend);
-
-	return 0;
-}
-
-
-int x86_64_do_asm(char *p, char c) {
-	char *name = NULL, *save;
-	int out, type, attr;
-
-	lbsave = p;
-
-	switch (c) {
-	case '$':
-		++p;
-		_skip_spaces(p);
-
-		getword(p, &type, &out);
-
-		if (type != WORDTYPE_NAME) 
-			exit(generic_error("'$' requires a name", lbsave, lno));
-
-		name = strndup(p, out);
-		p += out;
-
-		_skip_spaces(p);
-
-		getword(p, &type, &out);
-
-		if (type == WORDTYPE_KWORD) {
-			if (out != '=') 
-				exit(generic_error("invalid keyword after name", lbsave, lno));
-
-			p++;
-			_skip_spaces(p);
+__init int x86_64_init_asm(void) {
+	current->private_data = malloc(sizeof(struct x86_64_context));
+	current_private->stkindex = 1;
 
-			getword(p, &type, &out);
-		}
+	klist_init(&stack);
+	klist_init(&globvars);
 
-		if (type != WORDTYPE_NR) {
-			if (type) 
-				exit(generic_error("'$' requires a number", lbsave, lno));
-
-			out = 0;
-		}
-
-		_skip_spaces(p);
-		_skip_word(p);
-		_skip_spaces(p);
-
-		attr = getattrmask(p, vardef_attrtab);
-
-		if (attr & 1) {
-			/* global variable */
-
-			printf(global_var, name, name, out);
-			__push_nr(&globvars, 0, name, attr);
-		}
-
-		else {
-			/* variables on stack */
-			printf(stack_var, out, stack_index << 3);
-			push_nr(stack_index, name, attr);
-
-			stack_index++;
-		}
-
-		break;
-
-	case '@':
-		p++;
-		_skip_spaces(p);
-
-		do_funcall(p);
-
-		break;
-
-	default:
-		save = p;
-		/* setting or function calling */
-
-		getword(p, &type, &out);
-
-		if (type != WORDTYPE_NAME) 
-			exit(generic_error("unknown keyword", lbsave, lno));
-
-		name = strndup(p, out);
-
-		_skip_word(p);
-		_skip_spaces(p);
-
-		if (*p == '(') {
-			do_funcall(save);
-
-			break;
-		}
-
-		set_name(p, name);
-
-		free(name);
-
-		break;
-	}
+	printf(asm_head, fname);
 
 	return 0;
 }
 
 
-static inline struct klist_node *find_name_stack(char *name) {
-	struct klist_node *n;
-
-	for (n = stack.next; n != &stack; n = n->next) 
-		if (!strcmp(n->payload, name)) 
-			return n;
-
-	return NULL;
-}
-
-
-static inline struct klist_node *find_name_global(char *name) {
-	struct klist_node *n;
-
-	for (n = globvars.next; n != &globvars; n = n->next) 
-		if (!strcmp(n->payload, name)) 
-			return n;
-
-	return NULL;
-}
-
-
-
-static int set_name(char *p, char *name) {
-	int type, out;
-	struct klist_node *n;
-
-	/* find the name, 
-	 * first find in stack */
-
-	if ((n = find_name_stack(name))) {
-		if (n->xmask & 4) 
-			exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
-
-		if (!getword(p, &type, &out)) 
-			exit(generic_error("no equal", lbsave, lno));
-
-		if (out != '=') 
-			exit(generic_error("not a '='", lbsave, lno));
-
-		p++;
-		_skip_spaces(p);
-
-		if (!getword(p, &type, &out)) 
-			exit(generic_error("no value", lbsave, lno));
-
-
-		printf(wr_stkvar, out, n->value << 3);
-
-		return 0;
-	}
-
-	/* global variables */
-
-	if (name[0] != '&') 
-		/* we need a '&' for global variables */
-		exit(generic_error("name not found (did you forget '&')", lbsave, lno));
-
-	if (!find_name_global(name + 1)) 
-		exit(generic_error("name not found", lbsave, lno));
-
-	_skip_word(p);
-	_skip_spaces(p);
-
-	if (!getword(p, &type, &out)) 
-		exit(generic_error("no equal", lbsave, lno));
-
-	if (out != '=') 
-		exit(generic_error("not a '='", lbsave, lno));
-
-	p++;
-	_skip_spaces(p);
-
-	if (!getword(p, &type, &out)) 
-		exit(generic_error("no value", lbsave, lno));
-
-	printf(wr_glbvar, out, name + 1);
+__init int x86_64_asm_fin(void) {
+	printf(funcend);
 
 	return 0;
 }
 
 
-int do_funcall(char *p) {
-	struct klist_node *stknr;
-	int out, type, n;
-	char *funcname, *arg;
-
-	getword(p, &type, &out);
-
-	if (type != WORDTYPE_NAME) 
-		exit(generic_error("a function name required", lbsave, lno));
-
-	funcname = strndup(p, out);
-	p += out;
+__arch int x86_64_new_var(char *name, long value, int attr) {
+	if (attr & 1) {
+		/* global variable */
 
-	_skip_spaces(p);
+		printf(global_var, name, name, value);
 
-	if (*p == '(') {
-		p++;
-		_skip_spaces(p);
-
-		if (*p == ')') {
-			printf(noarg_funcall, funcname);
-
-			return 0;
-		}
-	}
-
-	if (*p == ';') {
-		printf(noarg_funcall, funcname);
+		__push_nr(&globvars, value, name, attr);
 
 		return 0;
 	}
 
-	_skip_spaces(p);
-
-	/* get all arguments and call them */
-
-	while (getword(p, &type, &out)) {
-		switch (type) {
-		case WORDTYPE_STR:
-			arg = strndup(p + 1, out);
-
-			printf("string: %s\n", arg);
-
-			break;
-
-		case WORDTYPE_NAME:
-			arg = strndup(p, out);
-			stknr = find_name_stack(arg);
-
-			if (!stknr) {
-				printf("global variable\n");
-
-				break;
-			}
-
-			out = stknr->value;
-
-			/* fall through */
-
-		case WORDTYPE_NR:
-			printf("number %d\n", out);
-
-			break;
+	/* local variable */
 
-		case WORDTYPE_KWORD:
-			if (out != ',' && out != '(' && out != ')') 
-				exit(generic_error("unknow keyword after arguments", lbsave, lno));
-
-			if (out == '(' && n) 
-				exit(generic_error("invalid '(' after arguments", lbsave, lno));
-
-			if (out == ')') {
-				p++;
-				_skip_spaces(p);
-
-				getword(p, &type, &out);
-
-				if (type) 
-					exit(generic_error("arguments after ')'", lbsave, lno));
-
-				goto ok;
-			}
-
-			break;
-
-		default:
-			exit(generic_error("error type passing argument", lbsave, lno));
-		}
-
-		if (type == WORDTYPE_STR) 
-			p += out + 2;
-
-		else if (type == WORDTYPE_KWORD) {
-			p++;
-			_skip_spaces(p);
-		}
-
-		else 
-			_skip_word(p);
-
-
-		free(arg);
-		arg = NULL;
-
-		n++;
-	}
+	printf(stack_var, value, current_private->stkindex << 3);
+	push_nr(current_private->stkindex, name, attr);
 
-ok:	free(funcname);
+	current_private->stkindex++;
 
 	return 0;
 }

+ 19 - 12
arch/x86_64/do_keyword.c → arch/x86_64/x86_64.c

@@ -29,28 +29,35 @@
 #include <tripl/arch.h>
 
 
+int x86_64_init(void);
+int x86_64_fin(void);
+
+int x86_64_new_var(char *name, long value, int attr);
+
+
+struct arch x86_64_arch_fn = {
+	.arch_init = x86_64_init, 
+	.arch_fin = x86_64_fin, 
+	.arch_new_var = x86_64_new_var, 
+	.arch_stack_dump = NULL, 
+	.arch_import_shared = NULL, 
+	.arch_funcall = NULL, 
+	.arch_set_var = NULL
+};
+
+
 extern int x86_64_init_asm(void);
-extern int x86_64_do_asm(char *s, char c);
 extern int x86_64_asm_fin(void);
 
-extern struct klist_node globvars;
 
-
-int x86_64_init(void) {
-	klist_init(&globvars);
+__arch int x86_64_init(void) {
 	klist_init(&stack);
 
 	return x86_64_init_asm();
 }
 
 
-int x86_64_do_keyword(char *s, char c) {
-	x86_64_do_asm(s, c);
-
-	return 0;
-}
-
-int x86_64_fin(void) {
+__arch int x86_64_fin(void) {
 	x86_64_asm_fin();
 
 	return 0;

+ 1 - 1
include/klist.h

@@ -26,7 +26,7 @@
 
 
 struct klist_node {
-	int value;
+	long value;
 	int xmask;
 	void *payload;
 

+ 26 - 5
include/tripl/arch.h

@@ -25,12 +25,33 @@
 
 #include <tripl/tripl.h>
 
-int interp_do_keyword(char *s, char c);
-int interp_init(void);
 
-int x86_64_do_keyword(char *s, char c);
-int x86_64_init(void);
-int x86_64_fin(void);
+struct arch {
+	int (* arch_init) (void);
+	int (* arch_fin) (void);
+	int (* arch_new_var) (char *name, long value, int attr);
+
+	/* interpreter only */
+	int (* arch_stack_dump) (void);
+
+	int (* arch_import_shared) (char *name);
+
+	int (* arch_funcall) (char *funcname, struct arg_entry *args);
+	int (* arch_set_var) (char *name, long value);
+};
+
+
+struct context {
+	char 		*funcname;
+
+	/* prev context */
+	struct context 	*link;
+
+	void 		*private_data;
+};
+
+
+__arch_templ int arch_do_keyword(char *p, struct arch *arch_fn);
 
 
 #endif

+ 11 - 0
include/tripl/tripl.h

@@ -32,6 +32,15 @@
 
 #define BUG_ON(cond) 		({if (cond) panic(__FUNCTION__ ": " #cond)})
 
+/* macros to let you know, what does it do */
+
+#define __arch 
+#define __arch_templ 
+
+/* low level assembly: never read */
+#define __ll_asm 
+#define __init 
+
 
 #define FG_BLACK 		30
 #define FG_RED 			31
@@ -144,6 +153,8 @@ char *getword(char *fbuffer, int *type, int *out);
 int getattrmask(char *p, const struct attr_table *tab);
 int generic_error(char *msg, char *line, int lno);
 
+extern struct context *current;
+
 
 #endif
 

+ 3 - 4
sample.trp

@@ -1,7 +1,6 @@
 #! /usr/bin/tripl
 
-$ a;
-a = 1;
-
-abort ();
+$ aasdf = 1234;
+$ asdfgh 123;
+$ asdfg;
 

+ 29 - 24
tripl.c

@@ -37,22 +37,31 @@ int lno = 0;
 char *eof;
 char *line_eof;
 
-struct klist_node stack;
 
 static int next_line(char *), 
 	   parse_line(void);
 
-int (* do_keyword) (char *, char);
-int (* finish) (void);
+
+struct arch *arch_fn;
+extern struct arch 	interp_arch_fn, 
+       			x86_64_arch_fn;
+
+
+struct context *current;
+
+struct context main_context = {
+	.funcname = "main", 
+	.link = NULL, 
+	.private_data = NULL
+};
+
 
 const static struct {
 	char *name;
-	int (* do_keyword) (char *, char);
-	int (* init) (void);
-	int (* fin) (void);
+	struct arch *arch;
 } arch_list[] = {
-	{ "interp", interp_do_keyword, interp_init, NULL }, 
-	{ "x86_64", x86_64_do_keyword, x86_64_init, x86_64_fin }, 
+	{ "interp", &interp_arch_fn}, 
+	{ "x86_64", &x86_64_arch_fn}, 
 	{ NULL, NULL }
 };
 
@@ -67,28 +76,24 @@ const struct attr_table vardef_attrtab[] = {
 
 
 int do_tripl_compile(char *fbuffer, off_t fsize) {
-	char *arch = getenv("TRIPL_ARCH");
+	char *archname = getenv("TRIPL_ARCH");
 	int i;
 
+	current = &main_context;
 
-	if (!arch) {
-		do_keyword = interp_do_keyword;
-
-		interp_init();
-	}
+	if (!archname) 
+		arch_fn = &interp_arch_fn;
 
 	else {
 		for (i = 0; arch_list[i].name; i++) 
-			if (!strcmp(arch_list[i].name, arch)) {
-				do_keyword = arch_list[i].do_keyword;
-				arch_list[i].init();
-
-				finish = arch_list[i].fin;
+			if (!strcmp(arch_list[i].name, archname)) {
+				arch_fn = arch_list[i].arch;
 
 				goto ok;
 			}
+			
 
-		fprintf(stderr, "*** unsupported arch %s (got from $TRIPL_ARCH). \n", arch);
+		fprintf(stderr, "*** unsupported arch %s (got from $TRIPL_ARCH). \n", archname);
 
 		return 1;
 	}
@@ -97,12 +102,12 @@ ok:	klist_init(&stack);
 
 	eof = fbuffer + fsize;
 
+	arch_fn->arch_init();
+
 	while (next_line(fbuffer)) 
 		parse_line();
 
-
-	if (finish) 
-		finish();
+	arch_fn->arch_fin ? arch_fn->arch_fin() : 0;
 
 	return 0;
 }
@@ -406,7 +411,7 @@ static int parse_line(void) {
 	if (type != WORDTYPE_KWORD && type != WORDTYPE_NAME) 
 		exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
 
-	return do_keyword(p, *p);
+	return arch_do_keyword(p, arch_fn);
 }