Parcourir la source

tripl: candy about '$ i = 0' and '$ i 0', and a patch about '#'

rain il y a 4 ans
Parent
commit
1d3f6f45c3
6 fichiers modifiés avec 99 ajouts et 30 suppressions
  1. 18 8
      arch/interp/interp.c
  2. 51 8
      arch/x86_64/asm.c
  3. 3 0
      arch/x86_64/do_keyword.c
  4. 10 7
      include/tripl/tripl.h
  5. 3 5
      sample.trp
  6. 14 2
      tripl.c

+ 18 - 8
arch/interp/interp.c

@@ -33,6 +33,7 @@
 #include <tripl/limits.h>
 
 
+
 struct klist_node libraries;
 
 static char *lbsave;
@@ -73,20 +74,29 @@ int interp_do_keyword(char *p, char c) {
 
 		getword(p, &type, &out);
 
-		if (type != WORDTYPE_NR && type != WORDTYPE_NAME) 
-			exit(generic_error("'$' requires a number", lbsave, lno));
+		if (type != WORDTYPE_NAME) 
+			exit(generic_error("'$' requires a name", lbsave, lno));
 
-		if (type == WORDTYPE_NAME) {
-			name = strndup(p, out);
+		name = strndup(p, out);
 
-			p += out;
+		p += out;
+		_skip_spaces(p);
 
-			getword(p, &type, &out);
+		getword(p, &type, &out);
 
-			if (type != WORDTYPE_NR) 
-				exit(generic_error("'$' requires a number", lbsave, lno));
+		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) 
+			exit(generic_error("'$' requires a number", lbsave, lno));
+
 		p++;
 		_skip_word(p);
 

+ 51 - 8
arch/x86_64/asm.c

@@ -31,6 +31,8 @@
 static char *lbsave;
 static int stack_index;
 
+struct klist_node globvars;
+
 static int do_funcall(char *p);
 static int set_name(char *p, char *name);
 
@@ -122,8 +124,20 @@ int x86_64_do_asm(char *p, char c) {
 		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) 
 			exit(generic_error("'$' requires a number", lbsave, lno));
 
@@ -133,9 +147,12 @@ int x86_64_do_asm(char *p, char c) {
 
 		attr = getattrmask(p, vardef_attrtab);
 
-		if (attr & 1) 
+		if (attr & 1) {
 			/* global variable */
+
 			printf(global_var, name, name, out);
+			__push_nr(&globvars, 0, name, attr);
+		}
 
 		else {
 			/* variables on stack */
@@ -186,6 +203,18 @@ static inline struct klist_node *find_name_stack(char *name) {
 }
 
 
+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;
@@ -224,6 +253,9 @@ static int set_name(char *p, char *name) {
 		/* 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);
 
@@ -246,6 +278,7 @@ static int set_name(char *p, char *name) {
 
 
 int do_funcall(char *p) {
+	struct klist_node *stknr;
 	int out, type;
 	char *funcname, *arg;
 
@@ -276,15 +309,22 @@ int do_funcall(char *p) {
 
 			break;
 
-		case WORDTYPE_NR:
-			printf("number %d\n", out);
-
-			break;
-
 		case WORDTYPE_NAME:
 			arg = strndup(p, out);
+			stknr = find_name_stack(arg);
 
-			printf("name: %s\n", arg);
+			if (!stknr) {
+				printf("global variable\n");
+
+				break;
+			}
+
+			out = stknr->value;
+
+			/* fall through */
+
+		case WORDTYPE_NR:
+			printf("number %d\n", out);
 
 			break;
 
@@ -308,9 +348,12 @@ int do_funcall(char *p) {
 
 		else 
 			_skip_word(p);
+
+
+		free(arg);
+		arg = NULL;
 	}
 
-	free(arg);
 	free(funcname);
 
 	return 0;

+ 3 - 0
arch/x86_64/do_keyword.c

@@ -33,8 +33,11 @@ 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);
 	klist_init(&stack);
 
 	return x86_64_init_asm();

+ 10 - 7
include/tripl/tripl.h

@@ -80,26 +80,29 @@
 #define atox(c) 	((c) >= 'a' ? (c) - 'a' + 10 : (c) - '0')
 #define atoX(c) 	((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
 
-/* warning that np must be allocated by malloc or 0, not on stack */
-#define push_nr(nr, np, mask) ({ \
+
+#define __push_nr(__stack, nr, np, mask) ({ \
 	struct klist_node *p = malloc(sizeof(struct klist_node)); \
 	klist_init(p); \
 	p->value = nr; \
 	p->payload = np; \
 	p->xmask = mask; \
-	klist_add(&stack, p); \
+	klist_add(__stack, p); \
 })
 
-
-#define _pop_nr(nr) 	({ \
-	struct klist_node *p = stack.prev; \
+#define __pop_nr(__stack, nr) ({ \
+	struct klist_node *p = (__stack)->prev; \
 	int __nr = p->value; \
-	klist_del(&stack, p); \
+	klist_del(__stack, p); \
 	free(p); \
 	__nr; \
 })
 
 
+/* warning that np must be allocated by malloc or 0, not on stack */
+#define push_nr(nr, np, mask) 	__push_nr(&stack, nr, np, mask)
+#define _pop_nr(nr) 		__pop_nr(&stack, nr)
+
 
 #define WORDTYPE_INVAL 	0
 #define WORDTYPE_KWORD 	1

+ 3 - 5
sample.trp

@@ -1,6 +1,4 @@
-$ age 12;
-
-@ printf "hello, world\n", no;
-@ printf "my age: %d\n", age;
-
+#!./tripl
+$a = 0;
+a = 10;
 

+ 14 - 2
tripl.c

@@ -107,6 +107,17 @@ ok:	klist_init(&stack);
 }
 
 
+static char *__skip_comment(char *fbuffer) {
+	if (*fbuffer != '#') 
+		return fbuffer;
+
+	while (*fbuffer != '\n') 
+		fbuffer++;
+
+	return fbuffer++;
+}
+
+
 /* move line pointer to next line */
 
 static int next_line(char *fbuffer) {
@@ -115,7 +126,7 @@ static int next_line(char *fbuffer) {
 	++lno;
 
 	if (!line_buffer) {
-		p = line_eof = line_buffer = fbuffer;
+		p = line_eof = line_buffer = fbuffer = __skip_comment(fbuffer);
 
 		while (*p != ';') 
 			line_eof++, p++;
@@ -125,6 +136,8 @@ static int next_line(char *fbuffer) {
 		return 1;
 	}
 
+	fbuffer = __skip_comment(fbuffer);
+
 	for (; (*line_buffer != ';') && (line_buffer < eof); ++line_buffer);
 
 	if (line_buffer >= eof) 
@@ -338,7 +351,6 @@ static int parse_line(void) {
 	char *p;
 	int type, out;
 
-
 	if ((p = getword(line_buffer, &type, &out)) >= line_eof) 
 		/* empty line */
 		return 0;