Sfoglia il codice sorgente

fix a bug that spaces at the start of the file

rain 4 anni fa
parent
commit
51c5b3438e
5 ha cambiato i file con 46 aggiunte e 22 eliminazioni
  1. 1 1
      Makefile
  2. 21 12
      error.c
  3. 18 2
      main.c
  4. 3 7
      sample.trp
  5. 3 0
      tripl.c

+ 1 - 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 -O2 
+CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
 
 ifeq ($(install-dir),)
 	install-dir = /usr/bin

+ 21 - 12
error.c

@@ -26,25 +26,24 @@
 
 
 extern char *line_eof;
+static int ge_dump_line(int ln, char *buffer);
+
+/* yeah, you know, the color_printk() in that book :-) */
+#define color_error(mode, fg, msg, ...) 			\
+	ansi_csi_st(stderr, mode, fg, BG_BLACK); 		\
+	fprintf(stderr, msg, ##__VA_ARGS__); 			\
+	ansi_csi_ed(stderr); 					\
+
 
 int generic_error(char *msg, char *line, int lno) {
 	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);
-	ansi_csi_ed(stderr);
-
-	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_RED, BG_BLACK);
-	fprintf(stderr, "error: ");
-	ansi_csi_ed(stderr);
+	color_error(MODE_HIGHLIGHT, FG_WHITE, "tripl: %s:%d: ", fname, lno);
+	color_error(MODE_HIGHLIGHT, FG_RED, "error: ");
 
 	fprintf(stderr, "%s\n", msg);
 
-	fprintf(stderr, "%5d | ", lno);
-
-	ansi_csi_st(stderr, MODE_LINE, FG_WHITE, BG_BLACK);
-	fprintf(stderr, "%s\n", buffer);
-	ansi_csi_ed(stderr);
+	ge_dump_line(lno, buffer);
 
 	free(buffer);
 
@@ -52,3 +51,13 @@ int generic_error(char *msg, char *line, int lno) {
 }
 
 
+static int ge_dump_line(int ln, char *buffer) {
+	while (*buffer && *buffer++ == '\n') 
+		;
+
+	fprintf(stderr, "%5d | %s\n", ln, buffer);
+	
+	return 0;
+}
+
+

+ 18 - 2
main.c

@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <string.h>
 
 #include <sys/mman.h>
 #include <sys/types.h>
@@ -41,13 +42,24 @@ int main(int argc, const char **argv) {
 
 	basename = (char *) argv[0];
 
-	if (argc != 2) 
+	if (argc < 2) 
 		return usage();
 
 
 	if (unlikely(!(fname = (char *) argv[1]))) 
 		return usage();
 
+	/* i don't like getopt()... */
+	if (!strcmp(argv[1], "-c")) {
+		fbuffer = (char *) argv[2];
+
+		fsize = strlen(fbuffer);
+		fd = 0;
+		fbuffer[fsize] = 0;
+
+		goto compile;
+	}
+
 	if (unlikely((fd = open(fname, O_RDONLY)) < 0)) {
 		perror(basename);
 
@@ -65,12 +77,16 @@ int main(int argc, const char **argv) {
 	}
 
 
+compile:
 	retstat = do_tripl_compile(fbuffer, fsize);
 
+	if (fbuffer == argv[2]) 
+		goto skip;
+
 	munmap(fbuffer, fsize);
 	close(fd);
 
-	if (retstat) {
+skip:	if (retstat) {
 		error("compliation failed");
 
 		return retstat;

+ 3 - 7
sample.trp

@@ -1,3 +1,4 @@
+
 #! /usr/bin/tripl
 
 < "libc-2.33.so" 	@ shared;
@@ -25,7 +26,7 @@ a = ` "/usr/bin/bash" {
 
 
 c = ` "./tripl" {
-	$ a;
+	$ a = 0;
 
 	a = ` "/usr/bin/bash" {
 		echo -n 	"this is a bash and it is running in a tripl-interpreter which"
@@ -34,15 +35,10 @@ c = ` "./tripl" {
 		exit 10
 	};
 
-	! 		var;
+	! 		@ var;
 };
 
 
-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. 
 
 ! 			@ var;

+ 3 - 0
tripl.c

@@ -130,6 +130,9 @@ ok:	eof = fbuffer + fsize;
 
 	arch_fn->arch_init();
 
+	/* you can try to remove this and see what will happen :-) */
+	_skip_spaces(fbuffer);
+
 	while (next_line(fbuffer)) 
 		parse_line();