Jelajahi Sumber

tripl: add variable setting (c-like and math-like)

rain 4 tahun lalu
induk
melakukan
e48b592ab1
7 mengubah file dengan 185 tambahan dan 16 penghapusan
  1. 89 1
      arch/arch_templ.c
  2. 32 1
      arch/interp/interp.c
  3. 48 6
      arch/x86_64/asm.c
  4. 2 1
      arch/x86_64/x86_64.c
  5. 1 1
      include/tripl/arch.h
  6. 3 3
      sample.trp
  7. 10 3
      tripl.c

+ 89 - 1
arch/arch_templ.c

@@ -36,6 +36,8 @@
 char 	*psave;
 
 __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 *);
 
 
 
@@ -65,7 +67,9 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 		break;
 
 	default:
-		exit(generic_error("unknown keyword", psave, lno));
+		if (set_name(p, arch_fn)) 
+			if (math_set_name(p, arch_fn)) 
+				exit(generic_error("unknow keyword", psave, lno));
 
 		break;
 	}
@@ -133,3 +137,87 @@ __arch_templ static int new_var(char *p, struct arch *arch_fn) {
 }
 
 
+static int set_name(char *p, struct arch *arch_fn) {
+	char *name;
+	int type, out;
+
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_NAME) 
+		return 1;
+
+	name = strndup(p, out);
+
+	p += out;
+	_skip_spaces(p);
+
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_KWORD || out != '=') 
+		return 1;
+
+	p++;
+	_skip_spaces(p);
+
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_NR) 
+		exit(generic_error("a number required", psave, lno));
+
+
+	arch_fn->arch_set_var(name, out, 0);
+
+	free(name);
+
+	return 0;
+}
+
+
+
+
+/* 9 -> a; */
+
+static int math_set_name(char *p, struct arch *arch_fn) {
+	char *name;
+	int type, out, nr;
+
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_NR) 
+		return 1;
+
+	nr = out;
+
+	/* this requires a space */
+	_skip_word(p);
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_KWORD || out != '-') 
+		exit(generic_error("a '->' required", psave, lno));
+
+	p++;
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_KWORD || out != '>') 
+		exit(generic_error("a '->' required", psave, lno));
+
+	p++;
+	_skip_spaces(p);
+
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_NAME) 
+		exit(generic_error("a name required", psave, lno));
+
+	name = strndup(p, out);
+
+	arch_fn->arch_set_var(name, nr, 0);
+
+	free(name);
+
+	return 0;
+}
+
+
+
+

+ 32 - 1
arch/interp/interp.c

@@ -37,6 +37,10 @@
 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);
+
+
+extern char *psave;
 
 struct klist_node stack;
 
@@ -47,7 +51,7 @@ struct arch interp_arch_fn = {
 	.arch_stack_dump = interp_stack_dump,  
 	.arch_import_shared = NULL, 
 	.arch_funcall = NULL, 
-	.arch_set_var = NULL
+	.arch_set_var = interp_set_var
 };
 
 
@@ -58,12 +62,39 @@ __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);
 
 	return 0;
 }
 
+__arch int interp_set_var(char *name, long value, int attr) {
+	struct klist_node *n;
+
+	/* find the name */
+
+	if (!(n = find_name(name))) 
+		exit(generic_error("name not found", psave, lno));
+
+	if (n->xmask & 4) 
+		exit(generic_error("variable sets to be read-only", psave, lno));
+
+	n->value = value;
+
+	return 0;
+}
+
 
 __arch int interp_stack_dump(void) {
 	struct klist_node *n;

+ 48 - 6
arch/x86_64/asm.c

@@ -37,6 +37,7 @@ struct x86_64_context {
 #define current_private 	((struct x86_64_context *) current->private_data)
 
 struct klist_node globvars;
+extern char *psave;
 
 
 __ll_asm const static char asm_head[] = {
@@ -72,14 +73,14 @@ __ll_asm const static char stack_var[] = {
 };
 
 
-/*__ll_asm const static char wr_stkvar[] = {
-	"	movq 	$%d, -%d(%%rbp)\n"
-};*/
+__ll_asm const static char wr_stkvar[] = {
+	"	movq 	$%ld, -%ld(%%rbp)\n"
+};
 
 
-/*__ll_asm const static char wr_glbvar[] = {
-	"	movq 	$%d, %s(%%rip)\n"
-};*/
+__ll_asm const static char wr_glbvar[] = {
+	"	movq 	$%ld, %s(%%rip)\n"
+};
 
 
 /*__ll_asm const static char noarg_funcall[] = {
@@ -106,6 +107,28 @@ __init int x86_64_init_asm(void) {
 }
 
 
+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);
 
@@ -135,3 +158,22 @@ __arch int x86_64_new_var(char *name, long value, int attr) {
 }
 
 
+__arch int x86_64_set_var(char *name, long value, int attr) {
+	struct klist_node *n;
+
+	if (!(n = find_name(name))) 
+		exit(generic_error("name not found", psave, lno));
+
+	if (n->xmask & 4) 
+		exit(generic_error("variable sets to be 'read-only'", psave, lno));
+
+	if (n->xmask & 1) 
+		printf(wr_glbvar, value, name);
+
+	else 
+		printf(wr_stkvar, value, n->value << 3);
+
+	return 0;
+}
+
+

+ 2 - 1
arch/x86_64/x86_64.c

@@ -33,6 +33,7 @@ int x86_64_init(void);
 int x86_64_fin(void);
 
 int x86_64_new_var(char *name, long value, int attr);
+int x86_64_set_var(char *name, long value, int attr);
 
 
 struct arch x86_64_arch_fn = {
@@ -42,7 +43,7 @@ struct arch x86_64_arch_fn = {
 	.arch_stack_dump = NULL, 
 	.arch_import_shared = NULL, 
 	.arch_funcall = NULL, 
-	.arch_set_var = NULL
+	.arch_set_var = x86_64_set_var
 };
 
 

+ 1 - 1
include/tripl/arch.h

@@ -37,7 +37,7 @@ struct arch {
 	int (* arch_import_shared) (char *name);
 
 	int (* arch_funcall) (char *funcname, struct arg_entry *args);
-	int (* arch_set_var) (char *name, long value);
+	int (* arch_set_var) (char *name, long value, int attr);
 };
 
 

+ 3 - 3
sample.trp

@@ -1,6 +1,6 @@
 #! /usr/bin/tripl
 
-$ aasdf = 1234;
-$ asdfgh 123;
-$ asdfg;
+$ aasdf = 12341234;
+aasdf = 1234;
+12345 -> aasdf;
 

+ 10 - 3
tripl.c

@@ -268,6 +268,9 @@ char *getword(char *fbuffer, int *type, int *out) {
 					/* name, but not a number */
 					continue;
 
+				if (!isnr(p[1])) 
+					continue;
+
 				/* get the number, and save to *out */
 
 				*out = getnumber(p);
@@ -312,6 +315,13 @@ char *getword(char *fbuffer, int *type, int *out) {
 					return fbuffer;
 				}
 
+				if (i == 1 && (p[-1] == '-' || p[-1] == '+')) {
+					*type = WORDTYPE_KWORD;
+					*out = p[-1];
+
+					return fbuffer;
+				}
+
 				if (ischr(p[-1])) {
 					*type = WORDTYPE_NAME;
 					*out = i;
@@ -408,9 +418,6 @@ static int parse_line(void) {
 		/* empty line */
 		return 0;
 
-	if (type != WORDTYPE_KWORD && type != WORDTYPE_NAME) 
-		exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
-
 	return arch_do_keyword(p, arch_fn);
 }