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

tripl: support add/sub/mul/div (and just interpreter now)

rain преди 4 години
родител
ревизия
d1b07f44f6
променени са 14 файла, в които са добавени 373 реда и са изтрити 53 реда
  1. 5 0
      Kconfig
  2. 2 1
      Makefile
  3. 1 1
      arch/Makefile
  4. 22 7
      arch/arch_templ.c
  5. 17 3
      arch/cstyles.c
  6. 1 1
      arch/interp/Makefile
  7. 4 2
      arch/interp/interp.c
  8. 203 0
      arch/interp/math.c
  9. 2 9
      error.c
  10. 0 0
      include/config.h
  11. 3 0
      include/tripl/arch.h
  12. 1 1
      include/tripl/tripl.h
  13. 30 28
      sample.trp
  14. 82 0
      scripts/mkconfig.py

+ 5 - 0
Kconfig

@@ -0,0 +1,5 @@
+[
+	{ "CONFIG_NODISPLAY_IINI" : "close stdout of the interp-in-interp task (stdout used to put assembly code)" }
+]
+
+

+ 2 - 1
Makefile

@@ -1,7 +1,7 @@
 tripl-objs 	= main.o tripl.o error.o klist.o  
 tripl-out 	= tripl
 
-CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
+CFLAGS 		= -Wall -Werror -Iinclude -g -ldl -O2 
 
 ifeq ($(install-dir),)
 	install-dir = /usr/bin
@@ -9,6 +9,7 @@ endif
 
 
 all : $(tripl-objs) 
+	@scripts/mkconfig.py 
 	@$(MAKE) -C arch/ 
 	@$(CC) 	$(tripl-objs) -o $(tripl-out) -Larch -larch -ldl 
 	@echo "tripl is ready. "

+ 1 - 1
arch/Makefile

@@ -14,7 +14,7 @@ all : 	$(templ)
 
 %.o : %.c 
 	@$(CC) $(CFLAGS) -c $*.c 
-	@echo "\tCC\t\t\t$*.c"
+	@echo -e "\tCC\t\t\t$*.c"
 
 
 .PHONY clean : 

+ 22 - 7
arch/arch_templ.c

@@ -25,10 +25,13 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <string.h>
+#include <fcntl.h>
 
 #include <errno.h>
 
 #include <klist.h>
+#include <config.h>
+
 #include <tripl/tripl.h>
 #include <tripl/limits.h>
 #include <tripl/arch.h>
@@ -50,7 +53,7 @@ __arch_templ static int declare_object(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 union math_expr *parse_math_expr(char *p);
 
 
 __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
@@ -236,12 +239,18 @@ static int set_name(char *p, struct arch *arch_fn, long *res) {
 
 	getword(p, &type, &out);
 
-	if (type != WORDTYPE_NR) 
+	if (type == WORDTYPE_KWORD && out == '$') {
+		out = arch_fn->arch_math_expr(p, name);
+
+		goto skip;
+	}
+
+	else if (type != WORDTYPE_NR)
 		out = getrightvalue(p, arch_fn);
 
 	arch_fn->arch_set_var(name, out, 0);
 
-	*res = out;
+skip:	*res = out;
 
 	free(name);
 
@@ -500,11 +509,19 @@ static int iini_run(char *interp_path, char *code) {
 
 	/* we use the command-line '-c' */
 
-	printf("\nRun %s as interpreter-in-interpreter: \n", interp_path);
+	fprintf(stderr, "\nRun %s as interpreter-in-interpreter: \n", interp_path);
 
 	if (!(pid = fork())) {
 		/* child */
 
+#if CONFIG_NODISPLAY_IINI
+
+		int fd = open("/dev/null", O_WRONLY);
+
+		dup2(fd, 1);
+		dup2(fd, 2);
+#endif
+
 		execl(interp_path, interp_path, "-c", code, NULL);
 
 		error("execl: %s\n", strerror(errno));
@@ -514,10 +531,8 @@ static int iini_run(char *interp_path, char *code) {
 
 	waitpid(pid, &stat, 0);
 
-	printf("\n");
-
 	if (WIFEXITED(stat)) 
-		printf("%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
+		fprintf(stderr, "\n%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
 
 	else if (WIFSIGNALED(stat)) {
 		error("%s terminated by signal %d%s\n", interp_path, WTERMSIG(stat), 

+ 17 - 3
arch/cstyles.c

@@ -84,7 +84,7 @@ __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
 	 * and an important thing is, we will do some check with the c-style declares. */
 
 	struct klist_node *n;
-	int type, out, nargs = 0;
+	int type, out, nargs = 0, have_args = 0;
 	char *fname;
 
 
@@ -134,8 +134,12 @@ __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
 	while (getword(p, &type, &out)) {
 		switch (type) {
 		case WORDTYPE_NAME:
-			if (!iskeyword(p, out)) 
-				nargs++;
+/*			no, no this. think about : 
+ *
+ *			int add(int, int);
+ *
+ *			if (!iskeyword(p, out)) 
+ *				nargs++; */
 			
 			p += out;
 			_skip_spaces(p);
@@ -143,6 +147,12 @@ __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
 			break;
 
 		case WORDTYPE_KWORD:
+			if (*p == ',') {
+				have_args = 1;
+
+				nargs++;
+			}
+
 			p++;
 			_skip_spaces(p);
 
@@ -154,6 +164,10 @@ __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
 		}
 	}
 
+	if (have_args) 
+		/* ',' means two arguments */
+		nargs++;
+
 	/* join to the function-list */
 
 	n = malloc(sizeof(struct klist_node));

+ 1 - 1
arch/interp/Makefile

@@ -1,4 +1,4 @@
-objs = interp.o arch_funcall.o 
+objs = interp.o arch_funcall.o math.o 
 targ = ../libarch.a 
 CFLAGS = -Wall -Werror -I../../include -g -ldl 
 

+ 4 - 2
arch/interp/interp.c

@@ -40,6 +40,7 @@ __arch int interp_new_var(char *name, long value, int attr);
 __arch int interp_dump(int);
 __arch int interp_set_var(char *name, long value, int attr);
 __arch int interp_import_shared(char *name);
+__arch int interp_math_expr(char *p, char *var);
 
 extern struct klist_node *find_name(char *name);
 
@@ -56,7 +57,8 @@ struct arch interp_arch_fn = {
 	.arch_dump = interp_dump,  
 	.arch_import_shared = interp_import_shared, 
 	.arch_funcall = NULL, 
-	.arch_set_var = interp_set_var
+	.arch_set_var = interp_set_var, 
+	.arch_math_expr = interp_math_expr
 };
 
 
@@ -127,7 +129,7 @@ __arch static int interp_names_dump(int names) {
 				printf("\tvar %s\n", (char *) n->payload);
 
 			else 
-				printf("\tvar %s: %ld\n", (char *) n->payload, n->value);
+				printf("\tvar %s: \t\t%ld\n", (char *) n->payload, n->value);
 		}
 	}
 

+ 203 - 0
arch/interp/math.c

@@ -0,0 +1,203 @@
+/* tripl/arch/interp/math.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 <klist.h>
+#include <tripl/tripl.h>
+#include <tripl/limits.h>
+#include <tripl/arch.h>
+
+
+
+extern char *psave;
+extern struct klist_node *find_name(char *);
+
+__arch_templ static long getnum(char **p, int prio);
+
+
+#define PRIO_MAX 	9
+#define PRIO_MIN 	0
+
+
+__arch_templ long interp_math_expr(char *p, char *var) {
+	struct klist_node *n;
+
+	p++;
+	_skip_spaces(p);
+
+	n = find_name(var);
+
+	if (!n) {
+		error(var);
+
+		exit(generic_error("name not found", psave, lno));
+	}
+
+	if (n->xmask & XMASK_WEAK) 
+		exit(generic_error("trying to modify weak variable", psave, lno));
+
+	if (n->xmask & 4) 
+		exit(generic_error("variable sets to be read-only", psave, lno));
+
+
+	n->value = getnum(&p, PRIO_MAX);
+
+	return n->value;
+}
+
+
+/* algorithm copied from os in a month (write an os in 30 days), kawai */
+
+__arch_templ static long getnum(char **pp, int prio) {
+	struct klist_node *n;
+	int type, out;
+	char *p = *pp, *var;
+
+	long a = 0, b = 0;
+
+	/* a + b, first we read a by ourselves. 
+	 * and, okay, we cannot use getword here because... */
+
+	switch (*p) {
+	case '+':
+	case '-':
+
+		/* +123 */
+
+		getword(p, &type, (int *) &a);
+		_skip_word(p);
+
+		break;
+
+
+	case '(':
+		p++;
+		_skip_spaces(p);
+
+		a = getnum(&p, PRIO_MAX);
+
+		if (*p != ')') 
+			exit(generic_error("no ')' found", psave, lno));
+
+		break;
+
+	default:
+		if (isnr(*p)) 
+			getword(p, &type, (int *) &a);
+
+		else if (ischr(*p)) {
+			getword(p, &type, &out);
+
+			var = strndup(p, out);
+
+			n = find_name(var);
+
+			if (!n) {
+				error(var);
+
+				exit(generic_error("name not found", psave, lno));
+			}
+
+			if (n->xmask & XMASK_WEAK) 
+				exit(generic_error("trying to modify weak variable", psave, lno));
+
+			a = n->value;
+
+			free(var);
+		}
+
+		else 
+			exit(generic_error("invalid unary operator before number", psave, lno));
+
+		_skip_word(p);
+
+		break;
+	}
+
+	for (;;) {
+		switch (*p) {
+		case '+':
+			if (prio <= 2) 
+				goto fin;
+
+			p++;
+			_skip_spaces(p);
+
+			b = getnum(&p, 2);
+			a += b;
+
+			break;
+
+		case '-':
+			if (prio <= 2) 
+				goto fin;
+
+			p++;
+			_skip_spaces(p);
+
+			b = getnum(&p, 2);
+			a -= b;
+
+			break;
+
+		case '*':
+			if (prio <= 1) 
+				goto fin;
+
+			p++;
+			_skip_spaces(p);
+
+			b = getnum(&p, 1);
+
+			a *= b;
+
+			break;
+
+		case '/':
+			if (prio <= 1) 
+				goto fin;
+
+			p++;
+			_skip_spaces(p);
+
+			b = getnum(&p, 1);
+
+			a /= b;
+
+			break;
+
+		default:
+			goto fin;
+		}
+	}
+
+fin:	_skip_spaces(p);
+	*pp = p;
+
+	return a;
+}
+
+
+

+ 2 - 9
error.c

@@ -25,17 +25,10 @@
 #include <string.h>
 
 
+extern char *line_eof;
 
 int generic_error(char *msg, char *line, int lno) {
-	char *buffer = malloc(TRIPL_LINE_MAX);
-	char *p;
-	int l;
-
-	for (l = 0, p = line; *p != '\n' && l < TRIPL_LINE_MAX; p++, l++) 
-		;
-
-	strncpy(buffer, line, l);
-	buffer[l] = 0;
+	char *buffer = strndup(line, line_eof - line + 1);
 
 	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_WHITE, BG_BLACK);
 	fprintf(stderr, "tripl: %s:%d: ", fname, lno);

+ 0 - 0
include/config.h


+ 3 - 0
include/tripl/arch.h

@@ -38,6 +38,9 @@ struct arch {
 
 	int (* arch_funcall) (char *funcname, struct arg_entry *args);
 	int (* arch_set_var) (char *name, long value, int attr);
+
+	/* var: name of variable which stores the result */
+	int (* arch_math_expr) (char *p, char *var);
 };
 
 

+ 1 - 1
include/tripl/tripl.h

@@ -85,7 +85,7 @@
 /* oh no, isdigit() is a function :-( */
 // #define isnr(c) 	isdigit(c)
 #define isnr(c) 	((c) >= '0' && (c) <= '9')
-#define ischr(c)	isalpha(c)
+#define ischr(c)	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
 #define _skip_spaces(s) while (isspace(*(s))) (s)++; if (*(s) == '\n') lno++;
 #define skip_spaces() 	_skip_spaces(line_buffer);
 #define _skip_word(s) 	while (!isspace(*(s))) (s)++; _skip_spaces(s)

+ 30 - 28
sample.trp

@@ -2,51 +2,53 @@
 
 < "libc-2.33.so" 	@ shared;
 
+# now we're testing the c-style declares
+int main(int argc, const char **argv);
+extern int fputs(char *, int);
 
-extern int *function(int arg, const asdf);
-int function2();
-char *functio2n(void);
+# and tripl-style
+< add(a, b) 		@ fn;
 
-
-< function3 ( a, b, c ) @ fn;
-
-< function4 4 		@ fn;
-
-$ a = +10;
+$ a;
+$ b;
+$ c;
 
 -10 -> a;
 
-!;	# this is a comment
+a = b = 1000;
 
 a = ` "/usr/bin/bash" {
 	echo "greeting from bash :-)"
 
-	read -p "input a number, please: " nr
+	exit 100
+};
+
 
-	case $nr in 
-		1) 
-			echo "a"
-			;;
+c = ` "./tripl" {
+	$ a;
 
-		2)
-			echo "b"
-			;;
+	a = ` "/usr/bin/bash" {
+		echo -n 	"this is a bash and it is running in a tripl-interpreter which"
+		echo 		"are running in a tripl-interpreter"
 
-		3) 
-			echo "c"
-			;;
-		*)
-			echo "well... "
-			;;
-	esac
+		exit 10
+	};
 
-	and this will let bash returns a non-zero value. 
+	! 		var;
 };
 
 
-$ b;
+b = ` "/usr/bin/python3" {
+print("hello, this is python world and this python is running in tripl :-)")
+exit(1);
+};
+
+# @ var means, only view value of var, but not functions and libraries. 
 
-b = a = 0;
+! 			@ var;
+
+b = a = $ 1 + 1 * 100 / 12;
 
 !;
 
+

+ 82 - 0
scripts/mkconfig.py

@@ -0,0 +1,82 @@
+#!/usr/bin/python3 
+
+# this script is using to create a .config configuration file
+
+import sys
+import os
+import json
+
+
+argc = 0
+
+for name in sys.argv:
+    argc += 1
+
+def fatal(msg):
+    sys.stderr.write("%s: fatal: %s\n" % (sys.argv[0], msg))
+    exit(1)
+
+
+if argc != 1:
+    fatal("no argument this requires")
+
+if os.getcwd()[-7 : -1] == "script":
+    fatal("call this as scripts/do_mkconfig.py, not ./do_mkconfig.py")
+
+
+basedir = os.getcwd()
+config_filename = basedir + "/include/config.h"
+kconfig_filename = basedir + "/Kconfig"
+
+# unlike that in linux, Kconfig is just a json likes that: 
+# [
+#   {
+#       "name": "descr"
+#   }, 
+#   {
+#        "DISABLE_A": "will you disable A"
+#   }
+# ]
+
+try:
+    open(config_filename, "r")
+    print("%s exists. remove it if you want to config again. " % basedir + "/include/config.h")
+
+    exit(0)
+
+except FileNotFoundError:
+    pass
+
+
+try:
+    kconfig_fd = open(kconfig_filename, "r")
+
+except FileNotFoundError:
+    fatal("Kconfig not exists")
+
+out_fd = open(config_filename, "w")
+
+
+kconfig_jsonobj = json.load(kconfig_fd)
+kconfig_fd.close()
+
+if type(kconfig_jsonobj) != list:
+    fatal("Kconfig should be a json list object, but %s gave" % type(kconfig_jsonobj))
+
+
+for config in kconfig_jsonobj:
+    for key, descr in config.items():
+        print("CONFIG %s: " % key)
+        print("\t(" + descr + ")")
+
+        value = input("> (int) ")
+        
+        try:
+            value = int(value)
+
+        except ValueError:
+            fatal("an integer requires for %s" % key)
+
+        out_fd.write("#define %s \t\t\t(%d)\n" % (key, value))
+        print("\n")
+