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

tripl: it looks nice, now we can use instructions such as 'a = b = 0;' :-)

rain преди 4 години
родител
ревизия
d8fcc69d44
променени са 4 файла, в които са добавени 70 реда и са изтрити 23 реда
  1. 44 19
      arch/arch_templ.c
  2. 2 2
      arch/interp/interp.c
  3. 14 1
      man/tripl.1
  4. 10 1
      sample.trp

+ 44 - 19
arch/arch_templ.c

@@ -44,16 +44,18 @@ extern char *line_eof;
 
 
 __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 *);
+__arch_templ static int set_name(char *p, struct arch *, long *out);
+__arch_templ static int math_set_name(char *p, struct arch *, long *out);
 __arch_templ static int declare_object(char *p, struct arch *);
-__arch_templ static int interp_in_interp(char *p, struct arch *);
+__arch_templ static int interp_in_interp(char *p, struct arch *, long *out);
 
 __arch_templ int cstyle_declare(char *p, struct arch *);
 
 
 
 __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
+	long ignored;
+
 	psave = p;
 
 	if (unlikely(p == line_eof)) 
@@ -92,20 +94,20 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 	case '`':
 		/* running code in another interpreter */
 
-		interp_in_interp(p, arch_fn);
+		interp_in_interp(p, arch_fn, &ignored);
 
 		break;
 
 	case '+':
 	case '-':
-		if (!math_set_name(p, arch_fn)) 
+		if (!math_set_name(p, arch_fn, &ignored)) 
 			exit(generic_error("unknow keyword", psave, lno));
 
 		break;
 
 	default:
-		if (unlikely(!set_name(p, arch_fn) && 
-					!math_set_name(p, arch_fn) && 
+		if (unlikely(!set_name(p, arch_fn, &ignored) && 
+					!math_set_name(p, arch_fn, &ignored) && 
 					!cstyle_declare(p, arch_fn))) 
 			exit(generic_error("unknow keyword", psave, lno));
 
@@ -116,6 +118,7 @@ __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
 }
 
 
+
 struct klist_node *find_name(char *name) {
 	struct klist_node *n;
 
@@ -192,8 +195,24 @@ __arch_templ static int new_var(char *p, struct arch *arch_fn) {
 	return 0;
 }
 
+static int getrightvalue(char *p, struct arch *arch_fn) {
+	long res;
+
+	if (*p == '`') {
+		if (!interp_in_interp(p, arch_fn, &res)) 
+			/* actually, it never be here */
+			exit(generic_error("invalid right-value", psave, lno));
+	}
+
+	else if (!set_name(p, arch_fn, &res)  
+			&& !math_set_name(p, arch_fn, &res)) 
+		exit(generic_error("invalid right-value", psave, lno));
 
-static int set_name(char *p, struct arch *arch_fn) {
+	return res;
+}
+
+
+static int set_name(char *p, struct arch *arch_fn, long *res) {
 	char *name;
 	int type, out;
 
@@ -217,12 +236,13 @@ static int set_name(char *p, struct arch *arch_fn) {
 
 	getword(p, &type, &out);
 
-	if (unlikely(type != WORDTYPE_NR)) 
-		exit(generic_error("a number required", psave, lno));
-
+	if (type != WORDTYPE_NR) 
+		out = getrightvalue(p, arch_fn);
 
 	arch_fn->arch_set_var(name, out, 0);
 
+	*res = out;
+
 	free(name);
 
 	return 1;
@@ -231,9 +251,10 @@ static int set_name(char *p, struct arch *arch_fn) {
 
 
 
-/* 9 -> a; */
+/* 9 -> a; and warning that math_set_name doesn't support instructions 
+ * such as 'a = 0 -> b' */
 
-static int math_set_name(char *p, struct arch *arch_fn) {
+static int math_set_name(char *p, struct arch *arch_fn, long *res) {
 	char *name;
 	int type, out, nr;
 
@@ -268,6 +289,7 @@ static int math_set_name(char *p, struct arch *arch_fn) {
 	name = strndup(p, out);
 
 	arch_fn->arch_set_var(name, nr, 0);
+	*res = nr;
 
 	free(name);
 
@@ -497,7 +519,7 @@ static int iini_run(char *interp_path, char *code) {
 	if (WIFEXITED(stat)) 
 		printf("%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
 
-	else if (WIFSIGNALED(stat)) 
+	else if (WIFSIGNALED(stat)) {
 		error("%s terminated by signal %d%s\n", interp_path, WTERMSIG(stat), 
 
 #ifdef WCOREDUMP
@@ -505,15 +527,18 @@ static int iini_run(char *interp_path, char *code) {
 #else
 				""
 #endif
-	);
+		);
 
-	return 0;
+		exit(generic_error("interp-in-interp failed", psave, lno));
+	}
+
+	return WEXITSTATUS(stat);
 }
 
 
 
 
-static int interp_in_interp(char *p, struct arch *arch_fn) {
+static int interp_in_interp(char *p, struct arch *arch_fn, long *res) {
 	int type, out;
 	char *interp, *code, *end;
 
@@ -550,7 +575,7 @@ static int interp_in_interp(char *p, struct arch *arch_fn) {
 
 	/* now let's do something interesting */
 
-	iini_run(interp, code);
+	*res = iini_run(interp, code);
 
 	free(code);
 	free(interp);
@@ -560,7 +585,7 @@ static int interp_in_interp(char *p, struct arch *arch_fn) {
 	/* this, parse_line will not bark. */
 	line_buffer = end;
 
-	return 0;
+	return 1;
 }
 
 

+ 2 - 2
arch/interp/interp.c

@@ -124,10 +124,10 @@ __arch static int interp_names_dump(int names) {
 		if (!(n->value & DECLARE_FN) && (names & NAMES_VARS)) {
 			/* variables */
 			if (n->xmask & XMASK_WEAK)
-				printf("\t var %s\n", (char *) n->payload);
+				printf("\tvar %s\n", (char *) n->payload);
 
 			else 
-				printf("\t var %s: %ld\n", (char *) n->payload, n->value);
+				printf("\tvar %s: %ld\n", (char *) n->payload, n->value);
 		}
 	}
 

+ 14 - 1
man/tripl.1

@@ -15,7 +15,7 @@ to download that from https://gitee.com/pengruiyang-cpu/tripl.git before
 that. 
 
 .SH SYNTAX
-\fB$\fP: uses to push a number into stack. now we also support global variables. 
+\fBvariable define\fP: \fB$\fP uses to push a number into stack. now we also support global variables. 
 
 eg. 	\fB$ name 1048576;\fP
 
@@ -34,6 +34,17 @@ eg. 	\fB$ name 1048576;\fP
 
 \fB#\fP: line comment
 
+\fBname setting\fP: there are two ways to set names. one is C-style and one is math-style. 
+
+eg. 	\fBa = 0;\fP
+
+	\fB0 -> a;\fP
+
+	\fB-0 -> b;\fP
+
+	\fB-1000 -> c;\fP
+
+
 \fB!\fP: dump. This dumps things you want, e.g. functions, names, libraries 
 
 	\fBTHIS ONLY WORKS IN INTERPRETER MODE\fP
@@ -45,6 +56,8 @@ eg. 	\fB!		@ var;
 
 \fB<\fP: import shared library and declares. 
 
+	\fBTHE C-STYLE DECLARES SUPPORT\fP
+
 eg. 	\fB< "foobar.so"\fP
 	
 	\fB< fn 	@ fn;\fP

+ 10 - 1
sample.trp

@@ -18,7 +18,7 @@ $ a = +10;
 
 !;	# this is a comment
 
-` "/usr/bin/bash" {
+a = ` "/usr/bin/bash" {
 	echo "greeting from bash :-)"
 
 	read -p "input a number, please: " nr
@@ -39,5 +39,14 @@ $ a = +10;
 			echo "well... "
 			;;
 	esac
+
+	and this will let bash returns a non-zero value. 
 };
 
+
+$ b;
+
+b = a = 0;
+
+!;
+