Explorar el Código

tripl: 1.0.0-beta: rebuilding and add functions (calling), dynalib etc and naming variables.

rain hace 4 años
padre
commit
c0da546075
Se han modificado 10 ficheros con 759 adiciones y 155 borrados
  1. 8 5
      Makefile
  2. 17 0
      arch/Makefile
  3. BIN
      arch/arch.a
  4. 16 0
      arch/interp/Makefile
  5. 108 0
      arch/interp/arch_funcall.c
  6. 390 0
      arch/interp/interp.c
  7. 32 0
      include/tripl/arch.h
  8. 34 5
      include/tripl/tripl.h
  9. 2 6
      sample.trp
  10. 152 139
      tripl.c

+ 8 - 5
Makefile

@@ -1,19 +1,22 @@
-tripl-objs 	= main.o tripl.o error.o klist.o 
+tripl-objs 	= main.o tripl.o error.o klist.o  
 tripl-out 	= tripl
 
-CFLAGS 		= -Wall -Werror -Iinclude -g 
+CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
 
 
 all:	$(tripl-objs) 
-	@$(CC) 	$(tripl-objs) -o $(tripl-out)
+	@$(MAKE) -C arch/ 
+	@$(CC) 	$(tripl-objs) -o $(tripl-out) -Larch -larch -ldl 
 	@echo "tripl is ready. "
 
 
 %.o: 	%.c
-	@$(CC) 	$(CFLAGS) -c $*.c 
+	@$(CC) 	-c $*.c $(CFLAGS)
 	@echo -e "\tCC\t\t\t$*.c"
 
 
 .PHONY clean:
-	$(RM) 	-f $(tripl-objs) $(tripl-out)
+	@$(RM) 	-f $(tripl-objs) $(tripl-out)
+	@$(MAKE) -C arch/ clean
+
 

+ 17 - 0
arch/Makefile

@@ -0,0 +1,17 @@
+arch = interp 
+
+
+all : 
+	@# this writes into arch.a 
+	@for n in $(arch); do \
+		$(MAKE) -C $$n; \
+	done;
+
+
+.PHONY clean : 
+	@for n in $(arch); do \
+		$(MAKE) -C $$n clean; \
+	done;
+	$(RM) 	libarch.a 
+
+

BIN
arch/arch.a


+ 16 - 0
arch/interp/Makefile

@@ -0,0 +1,16 @@
+objs = interp.o arch_funcall.o 
+targ = ../libarch.a 
+CFLAGS = -Wall -Werror -I../../include -g -ldl 
+
+all : $(objs)
+	@$(AR) cr -ldl $(targ) $(objs) 
+
+
+%.o : %.c 
+	@$(CC) -c $*.c $(CFLAGS)
+	@echo -e "\tCC\t\t\t$*.c"
+
+
+.PHONY clean :
+	$(RM) $(objs)
+

+ 108 - 0
arch/interp/arch_funcall.c

@@ -0,0 +1,108 @@
+/* tripl/arch/interp/arch_funcall.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 <tripl/tripl.h>
+#include <tripl/limits.h>
+
+
+/* danger as your own risk */
+
+int arch_funcall(void *p, struct arg_entry *args, int nargs) {
+	register long result __asm__ ("ax");
+	struct arg_entry *arg = args;
+
+	int i;
+
+	unsigned long arg_int[6] = { 0, 0, 0, 0, 0, 0 };
+	unsigned long *args_linear = malloc((nargs - 6) << 3);
+
+
+	for (i = 0; i < 6; i++) {
+		if (!arg) 
+			break;
+
+		arg_int[i] = arg->arg_s ? (unsigned long) arg->arg_s : 
+			(unsigned long) arg->arg_v;
+
+		arg = arg->next;
+	}
+
+
+	for (i = 0; i < nargs - 6; i++) {
+		if (!arg) 
+			break;
+
+		args_linear[i] = arg->arg_s ? (unsigned long) arg->arg_s : 
+			(unsigned long) arg->arg_v;
+
+		arg = arg->next;
+	}
+
+	/* 
+	 * preparing arguments: 
+	 * 	rdi 	rsi 	rdx 	rcx 	r8	r9 	stack 
+	 * */
+
+/*	__asm__ volatile (
+		"cmpq 		$0, %%rdi\n\t"
+		"je 		2f\n\t"
+		"addq 		%2, %%rsp\n\t"
+		"1:"
+		"movq 		%1, %%rax\n\t"
+		"movq 		(%%rdi, %%rax, 8), %%rcx\n\t"
+		"pushq 		%%rcx\n\t"
+		"subq 		$1, %%rax\n\t"
+		"cmpq 		$0, %%rax\n\t"
+		"jb 		1b\n\t"
+
+		: "=D" (args_linear) : "r" ((long) nargs - 6), "r" ((long) (nargs - 6) << 3)
+		: "ax", "cx"
+	);*/
+
+	__asm__ volatile (
+		"2:"
+		"movq 		%1, %%rdi\n\t"
+		"movq 		%2, %%rsi\n\t"
+		"movq 		%3, %%rdx\n\t"
+		"movq 		%4, %%rcx\n\t"
+		"movq 		%5, %%r8\n\t"
+		"movq 		%6, %%r9\n\t"
+
+		/* real calling: push returning address first and then 
+		 * function address */ 
+
+		"leaq 		1f(%%rip), %%rax\n\t"
+		"pushq 		%%rax\n\t"
+		"pushq		%0\n\t"
+		"retq\n\t"
+	
+		"1:"
+
+		:: "m" (p), 
+		"m" (arg_int[0]), "m" (arg_int[1]), "m" (arg_int[2]), "m" (arg_int[3]), 
+		"m" (arg_int[4]), "m" (arg_int[5])
+		: "ax"
+	);
+
+	return result;
+}
+
+

+ 390 - 0
arch/interp/interp.c

@@ -0,0 +1,390 @@
+/* tripl/arch/interp/interp.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 <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <string.h>
+#include <dlfcn.h>
+
+#include <errno.h>
+
+#include <klist.h>
+#include <tripl/tripl.h>
+#include <tripl/limits.h>
+
+
+struct klist_node libraries;
+
+static char *lbsave;
+
+
+static int do_funcall(char *p), 
+	   do_dl_funcall(void *f, char *p), 
+	   do_check_string(char *p, char *d);
+
+extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
+
+
+int interp_init(void) {
+	klist_init(&libraries);
+
+	return 0;
+}
+
+
+
+int interp_do_keyword(char *p, char c) {
+	struct klist_node *n;
+	int type, out, i;
+
+	char *name = NULL;
+	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_NR && type != WORDTYPE_NAME) 
+			exit(generic_error("'$' requires a number", lbsave, lno));
+
+		if (type == WORDTYPE_NAME) {
+			name = strndup(p, out);
+
+			p += out;
+
+			getword(p, &type, &out);
+
+			if (type != WORDTYPE_NR) 
+				exit(generic_error("'$' requires a number", lbsave, lno));
+		}
+
+		push_nr(out, name);
+
+		break;
+
+
+	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 - 1);
+
+		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 '@':
+		/* function call. the real funny thing is going on :-) */
+		do_funcall(p);
+
+		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++;
+	_skip_spaces(p);
+
+	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;
+		}
+	}
+
+	exit(generic_error("function not found", lbsave, lno));
+
+	return 0;
+}
+
+
+static inline struct klist_node *find_name(char *np) {
+	struct klist_node *n;
+
+	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))) {
+				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 - 1);
+			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;
+
+		default:
+			exit(generic_error("error type passing argument", lbsave, lno));
+
+			break;
+		}
+
+		if (type != WORDTYPE_STR) {
+			_skip_word(p);
+		}
+
+		else 
+			p += out + 1;
+
+		++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++;
+	}
+
+	return 0;
+}
+
+
+

+ 32 - 0
include/tripl/arch.h

@@ -0,0 +1,32 @@
+/* tripl/include/tripl/arch.h 
+ * 
+ * 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/>
+ */ 
+
+
+
+
+#ifndef __TRIPL_ARCH_H__
+#define __TRIPL_ARCH_H__
+
+
+#include <tripl/tripl.h>
+
+int interp_do_keyword(char *s, char c);
+
+
+#endif
+

+ 34 - 5
include/tripl/tripl.h

@@ -52,8 +52,6 @@
 #define MODE_LINE 		4
 
 
-#define as(x) 			x
-
 #define ansi_csi_st(strm, m, fg, bg) \
 	fprintf(strm, "\033[%d;%d;%dm", m, fg, bg)
 
@@ -72,13 +70,22 @@
 
 #define isspace(c) 	(((c) == '\n') || ((c) == '\t') || ((c) == ' '))
 #define isnr(c) 	((c) >= '0' && (c) <= '9')
-#define skip_spaces() 	while (isspace(*line_buffer)) line_buffer++;
+#define ischr(c) 	(((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
+#define _skip_spaces(s) while (isspace(*(s))) (s)++;
+#define skip_spaces() 	_skip_spaces(line_buffer);
+#define _skip_word(s) 	{ while (!isspace(*(s))) (s)++; _skip_spaces(s); }
+
+/* okay, it's a wrong thing itoa, should be atoi... */
 #define itoa(c) 	((c) - '0')
+#define atox(c) 	((c) >= 'a' ? (c) - 'a' + 10 : (c) - '0')
+#define atoX(c) 	((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
 
-#define push_nr(nr) 	({ \
+/* warning that np must be allocated by malloc or 0, not on stack */
+#define push_nr(nr, np) ({ \
 	struct klist_node *p = malloc(sizeof(struct klist_node)); \
 	klist_init(p); \
 	p->value = nr; \
+	p->payload = np; \
 	klist_add(&stack, p); \
 })
 
@@ -93,12 +100,34 @@
 
 
 
+#define WORDTYPE_INVAL 	0
+#define WORDTYPE_KWORD 	1
+#define WORDTYPE_NR 	2
+#define WORDTYPE_NAME 	3
+#define WORDTYPE_STR 	4
+
+
 /* main.c */
-extern char *fname;
+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; \
+				(a)->next = (a); (a)->prev = (a);
+
+struct arg_entry {
+	char 			*arg_s;
+	long 			arg_v;
+
+	struct arg_entry 	*next, *prev;
+};
 
 
 int do_tripl_compile(char *, off_t);
 
+char *getword(char *fbuffer, int *type, int *out);
 int generic_error(char *msg, char *line, int lno);
 
 

+ 2 - 6
sample.trp

@@ -1,7 +1,3 @@
-$ 1
-$ 1
-+
-~ 1 + 1 = 
-: -1
-~ \n
+< "libc-2.33.so";
+@ printf "a: %d %d + %d\n" 10 1 1 2;
 

+ 152 - 139
tripl.c

@@ -23,29 +23,65 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <string.h>
 
 #include <klist.h>
 #include <tripl/tripl.h>
 #include <tripl/limits.h>
+#include <tripl/arch.h>
 
 
-static char *linebuffer_save = NULL;
-static char *line_buffer = NULL;
-static int lno = 0;
-static char *eof;
+char *linebuffer_save = NULL;
+char *line_buffer = NULL;
+int lno = 0;
+char *eof;
 
-static char *line_eof;
+char *line_eof;
 
 struct klist_node stack;
 
 
+extern int interp_init(void);
+
 static int next_line(char *), 
-	   parse_line(void), 
-	   getnumber(void);
+	   parse_line(void);
+
+int (* do_keyword) (char *, char);
+
+const static struct {
+	char *name;
+	int (* do_keyword) (char *, char);
+} arch_list[] = {
+	{ "interp", interp_do_keyword }, 
+	{ NULL, NULL }
+};
 
 
 
 int do_tripl_compile(char *fbuffer, off_t fsize) {
+	char *arch = getenv("TRIPL_ARCH");
+	int i;
+
+
+	if (!arch) 
+		do_keyword = interp_do_keyword;
+
+	else {
+		for (i = 0; arch_list[i].name; i++) 
+			if (!strcmp(arch_list[i].name, arch)) {
+				do_keyword = arch_list[i].do_keyword;
+
+				goto ok;
+			}
+
+		printf("*** unsupported arch %s (got from $TRIPL_ARCH). \n", arch);
+
+		exit(1);
+	}
+
+ok:	if (do_keyword == interp_do_keyword) 
+		interp_init();
+
 	klist_init(&stack);
 
 	eof = fbuffer + fsize;
@@ -69,7 +105,7 @@ static int next_line(char *fbuffer) {
 	if (!line_buffer) {
 		p = line_eof = line_buffer = fbuffer;
 
-		while (*p != '\n') 
+		while (*p != ';') 
 			line_eof++, p++;
 
 		linebuffer_save = line_buffer;
@@ -77,204 +113,181 @@ static int next_line(char *fbuffer) {
 		return 1;
 	}
 
-
-	for (; (*line_buffer != '\n') && (line_buffer < eof); ++line_buffer);
+	for (; (*line_buffer != ';') && (line_buffer < eof); ++line_buffer);
 
 	if (line_buffer >= eof) 
 		return 0;
 
 	p = ++line_buffer;
 
-	while (*p != '\n') 
+	while (*p != ';' && p < eof) 
 		p++, line_eof++;
 
+	if (p == eof) 
+		/* eof */
+		exit(1);
+
 	linebuffer_save = line_buffer;
 
 	return line_eof++, 1;
 }
 
 
+static int getnumber(char *p) {
+	int dec = 0, log = 1, neg = 0;
 
-static int parse_line(void) {
-	struct klist_node *node;
-	int a, strstat, strallocated, strindex, stacklvl;
-	char *p, *string, *putbuf;
-
-	skip_spaces();
+	if (!isnr(*p)) {
+		if (*p == '+') 
+			neg = 0;
 
-	if ((p = line_buffer) >= line_eof) 
-		/* empty line */
-		return 0;
+		else if (*p == '-') 
+			neg = 1;
 
-	switch (*p) {
-	case '$':
-		line_buffer++;
-		push_nr(getnumber());
+		else 
+			exit(generic_error("a dec number required, but: ", linebuffer_save, lno));
 
-		break;
+		p++;
+	}
 
-	case '@':
-		/* function: not support */
+	if (*p == '0' && isnr(p[1])) 
+		exit(generic_error("number starting with '0'", linebuffer_save, lno));
 
-		return generic_error("tripl doesn't support functions now. ", 
-				linebuffer_save, lno);
 
-	case '#':
-		return 0;
+	/* never use eof because this is calling from getword() */
 
-	case '+':
-		if (klist_empty(&stack)) 
-			return generic_error("stack underflow. ", 
-					linebuffer_save, lno);
+	while (isnr(*p)) p++;
 
-		a = _pop_nr();
+	--p;
 
-		if (klist_empty(&stack)) 
-			return generic_error("stack underflow. ", 
-					linebuffer_save, lno);
+	while (p >= line_buffer) {
+		dec += itoa(*p) * log;
+		log *= 10;
 
-		push_nr(a + _pop_nr());
+		p--;
 
-		break;
+		if (!isnr(*p)) 
+			/* for - and + */
+			break;
+	}
 
-	case '!':
-		/* show stack */
-		ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
-		printf("stack dump: \n");
-		ansi_csi_ed(stdout);
-		fflush(stdout);
+	return neg ? -dec : dec;
+}
 
-		for (node = stack.next; node != &stack; node = node->next)
-			printf("\t%09d\t0x%016x\n", node->value, node->value);
 
-		if (klist_empty(&stack)) 
-			printf("\t(nothing)\n");
 
-		break;
+#define isfirstchr() 	(!i)
+#define notfirstchr() 	i
 
 
-	case '%':
-		return generic_error("tripl doesn't support variables now. ", 
-				linebuffer_save, lno);
+char *getword(char *fbuffer, int *type, int *out) {
+	*type = WORDTYPE_INVAL;
+	char *p;
+	int i, in = 0;
 
-	case '~':
-		/* \ */
-		strstat = 0;
+	_skip_spaces(fbuffer);
 
-		strindex = 0;
-		strallocated = 1;
-		putbuf = malloc(1);
+	for (i = 0, p = fbuffer; p < line_eof; i++, p++) {
+		if (*p == '\"') {
+			if (in) {
+				/* end */
 
-		line_buffer++;
+				*out = i;
+				return fbuffer;
+			}
 
-		/* output */
-		skip_spaces();
+			in = 1;
+			*type = WORDTYPE_STR;
+		}
 
-		string = line_buffer;
+		if (!in) {
+			if (isnr(*p) || *p == '-' || *p == '+') {
+				if (notfirstchr()) 
+					/* name, but not a number */
+					continue;
 
-		while (*string != '\n') {
-			if (strstat) {
-				if (*string == 'n') 
-					putbuf[strindex++] = '\n';
+				/* get the number, and save to *out */
 
-				else if (*string == 't') 
-					putbuf[strindex++] = '\t';
+				*out = getnumber(p);
 
-				else 
-					return generic_error("unsupported character: "
-						"'\\t' or '\\n' are supported. ", 
-						linebuffer_save, lno);
+				/* line finished. */
+				*type = WORDTYPE_NR;
 
-				strstat = 0;
+				return p;
 			}
 
-			else if (*string == '\\') 
-				strstat = 1;
+			/* eg. "# " */
+			if (i == 1 && isspace(*p)) {
+				if (ischr(p[-1])) {
+					/* name */
+					*type = WORDTYPE_NAME;
+					*out = 1;
 
-			else 
-				putbuf[strindex++] = *string;
+					return p;
+				}
 
-			string++;
+				/* keyword */
 
-			putbuf = realloc(putbuf, ++strallocated);
-		}
+				*type = WORDTYPE_KWORD;
+				*out = *--p;
 
-		putbuf[strindex] = 0;
+				return p;
+			}
 
-		printf("%s", putbuf);
+			/* it can only be a name */
+			if (isspace(*p)) {
+				/* end */
+				*type = WORDTYPE_NAME;
+				*out = i;
 
-		break;
+				return fbuffer;
+			}
+		}
+	}
 
-	case ':':
-		/* output number */
-		line_buffer++;
+	/* there isn't a space at end of the line */
 
-		skip_spaces();
+	if (in) 
+		exit(generic_error("no '\"' found", linebuffer_save, lno));
 
-		stacklvl = getnumber() - 1;
+	if (i == 1) {
+		if (ischr(p[-1])) {
+			*type = WORDTYPE_NAME;
+			*out = 1;
 
-		if (stacklvl < 0) 
-			return generic_error("invalid stack index. ", 
-					linebuffer_save, lno);
+			return p;
+		}
 
-		for (strindex = 0, node = stack.next; node != &stack && strindex < stacklvl; 
-				node = node->next)
-			;
+		*type = WORDTYPE_KWORD;
+		*out = *--p;
 
-		if (node == &stack) 
-			return generic_error("stack overflow. ", 
-					linebuffer_save, lno);
+		return p;
+	}
 
-		printf("%d", node->value);
+	if (!i) 
+		return NULL;
 
-		break;
+	/* only can be a name */
 
-	default:
-		return generic_error("the first character must be one of $@#+%!~:. ", 
-				linebuffer_save, lno);
-	}
+	*type = WORDTYPE_NAME;
+	*out = i;
 
-	return 0;
+	return fbuffer;
 }
 
 
-
-static int getnumber(void) {
+static int parse_line(void) {
 	char *p;
-	int dec = 0, log = 1, neg = 0;
-
-	skip_spaces();
+	int type, out;
 
-	if (!isnr(*line_buffer) && *line_buffer != '-') 
-		exit(generic_error("'$' requires a dec number, but: ", linebuffer_save, lno));
 
-	if (*line_buffer == '-') {
-		neg = 1;
-
-		line_buffer++;
-	}
-
-	if (*line_buffer == '0' && isnr(line_buffer[1])) 
-		exit(generic_error("invalid dec number: starting with zero. ", linebuffer_save, lno));
-
-	p = line_eof;
-
-	while (!isnr(*p)) {
-		if (!isspace(*p)) 
-			exit(generic_error("invalid dec number: character at end of string. ", 
-						linebuffer_save, lno));
-
-		p--;
-	}
-
-	while (p >= line_buffer) {
-		dec += itoa(*p) * log;
-		log *= 10;
+	if ((p = getword(line_buffer, &type, &out)) >= line_eof) 
+		/* empty line */
+		return 0;
 
-		p--;
-	}
+	if (type != WORDTYPE_KWORD) 
+		exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
 
-	return neg ? -dec : dec;
+	return do_keyword(p, *p);
 }