소스 검색

tripl: candy about '()' when function calling

rain 4 년 전
부모
커밋
dd32b0cc17
7개의 변경된 파일과 81개의 추가작업 그리고 18개의 파일을 삭제
  1. 9 2
      Makefile
  2. 21 3
      arch/interp/interp.c
  3. 33 3
      arch/x86_64/asm.c
  4. 1 0
      include/tripl/tripl.h
  5. 8 6
      man/coding_style.1
  6. 3 3
      sample.trp
  7. 6 1
      tripl.c

+ 9 - 2
Makefile

@@ -3,14 +3,21 @@ tripl-out 	= tripl
 
 CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
 
+ifeq ($(install-dir),)
+	install-dir = /usr/bin
+endif
 
-all:	$(tripl-objs) 
+
+all : $(tripl-objs) 
 	@$(MAKE) -C arch/ 
 	@$(CC) 	$(tripl-objs) -o $(tripl-out) -Larch -larch -ldl 
 	@echo "tripl is ready. "
 
+install : $(tripl-out)
+	@install $(tripl-out) $(install-dir)
+
 
-%.o: 	%.c
+%.o : %.c
 	@$(CC) 	-c $*.c $(CFLAGS)
 	@echo -e "\tCC\t\t\t$*.c"
 

+ 21 - 3
arch/interp/interp.c

@@ -149,7 +149,7 @@ int interp_do_keyword(char *p, char c) {
 			exit(generic_error("'<' requires a library name (string)", 
 						lbsave, lno));
 
-		name = strndup(p + 1, out - 1);
+		name = strndup(p + 1, out);
 
 		dlib = dlopen(name, RTLD_LAZY);
 
@@ -321,7 +321,7 @@ static int do_dl_funcall(void *f, char *p) {
 			break;
 
 		case WORDTYPE_STR:
-			name = strndup(p + 1, out - 1);
+			name = strndup(p + 2, out - 1);
 			dest = malloc(out - 1);
 
 			next_arg->arg_s = dest;
@@ -334,9 +334,27 @@ static int do_dl_funcall(void *f, char *p) {
 			break;
 
 		case WORDTYPE_KWORD:
-			if (out != ',') 
+			if (out != ',' && out != '(' && out != ')') 
 				exit(generic_error("unknow keyword after arguments", lbsave, lno));
 
+			if (out == '(') 
+				if (n) 
+					exit(generic_error("invalid '(' after arguments", lbsave, lno));
+
+			if (out == ')') {
+				p++;
+				_skip_spaces(p);;
+
+				/* ; */
+
+				getword(p, &type, &out);
+
+				if (type) 
+					exit(generic_error("arguments after ')'", lbsave, lno));
+
+				goto noarg;
+			}
+
 			break;
 
 		default:

+ 33 - 3
arch/x86_64/asm.c

@@ -279,7 +279,7 @@ static int set_name(char *p, char *name) {
 
 int do_funcall(char *p) {
 	struct klist_node *stknr;
-	int out, type;
+	int out, type, n;
 	char *funcname, *arg;
 
 	getword(p, &type, &out);
@@ -290,6 +290,19 @@ int do_funcall(char *p) {
 	funcname = strndup(p, out);
 	p += out;
 
+	_skip_spaces(p);
+
+	if (*p == '(') {
+		p++;
+		_skip_spaces(p);
+
+		if (*p == ')') {
+			printf(noarg_funcall, funcname);
+
+			return 0;
+		}
+	}
+
 	if (*p == ';') {
 		printf(noarg_funcall, funcname);
 
@@ -329,9 +342,24 @@ int do_funcall(char *p) {
 			break;
 
 		case WORDTYPE_KWORD:
-			if (out != ',') 
+			if (out != ',' && out != '(' && out != ')') 
 				exit(generic_error("unknow keyword after arguments", lbsave, lno));
 
+			if (out == '(' && n) 
+				exit(generic_error("invalid '(' after arguments", lbsave, lno));
+
+			if (out == ')') {
+				p++;
+				_skip_spaces(p);
+
+				getword(p, &type, &out);
+
+				if (type) 
+					exit(generic_error("arguments after ')'", lbsave, lno));
+
+				goto ok;
+			}
+
 			break;
 
 		default:
@@ -352,9 +380,11 @@ int do_funcall(char *p) {
 
 		free(arg);
 		arg = NULL;
+
+		n++;
 	}
 
-	free(funcname);
+ok:	free(funcname);
 
 	return 0;
 }

+ 1 - 0
include/tripl/tripl.h

@@ -135,6 +135,7 @@ struct attr_table {
 };
 
 extern const struct attr_table vardef_attrtab[];
+extern const struct attr_table import_attrtab[];
 
 
 int do_tripl_compile(char *, off_t);

+ 8 - 6
man/coding_style.1

@@ -1,4 +1,4 @@
-.TH "INTI C CODING STYLE" 1 "14 Jan 21"
+.TH "INTI C CODING STYLE" 1 "14 Jan 22
 .SH INTRODUCTION
 This is the inti C Language Coding Style standard. Everybody in the inti (inti's not interpreters) should 
 read this carefully and use it in their code which will commit to inti softwares. 
@@ -6,7 +6,7 @@ read this carefully and use it in their code which will commit to inti softwares
 Most of this standard copies from K&R (THE C PROGRAMMING LANGUAGE), but some of them are different. 
 
 .SH NAMES
-\fBALWAYS\fP use names with '_' and \fBNEVER\fPuse NamesLikeThis. We use names like both BSD and linux, but 
+\fBALWAYS\fP use names with '_' and \fBNEVER\fP use NamesLikeThis. We use names like both BSD and linux, but 
 \fBNEVER\fP like Windows! Remember that here is C, not Java or other. And here is UNIX, not Windows. :-)
 
 For some long words such as \fBattribute\fP and \fBsystem\fP, you should make them short and let them look 
@@ -29,11 +29,13 @@ I think tab is a tradition of C, is that okay?
 .SH DECLARES
 You can use tabs after types and you can also don't. These are okay: 
 
-\fBGOOD\fP: 	\fBint 		id;\fP
-		\fBchar 	*name;\fP
+\fBGOOD\fP: 	\fBint 		id;\fP\n
 
-\fBGOOD\fP:	\fBint id\fP
-		\fBchar *name\fP
+		\fBchar 	*name;\fP\n
+
+\fBGOOD\fP:	\fBint id;\fP\n
+
+		\fBchar *name\fP\n
 
 Before, I didn't use tabs after types because my first was python and it brings me a lot such as '{}' at 
 end of line and don't use tabs. But after reading the tyt code from linux 0.0.1, I relize it is more beatiful 

+ 3 - 3
sample.trp

@@ -1,4 +1,4 @@
-#!./tripl
-$a = 0;
-a = 10;
+#! /usr/bin/tripl
+
+@ main ();
 

+ 6 - 1
tripl.c

@@ -114,7 +114,9 @@ static char *__skip_comment(char *fbuffer) {
 	while (*fbuffer != '\n') 
 		fbuffer++;
 
-	return fbuffer++;
+	_skip_spaces(fbuffer);
+
+	return fbuffer;
 }
 
 
@@ -128,6 +130,9 @@ static int next_line(char *fbuffer) {
 	if (!line_buffer) {
 		p = line_eof = line_buffer = fbuffer = __skip_comment(fbuffer);
 
+		if (fbuffer >= eof) 
+			return 0;
+
 		while (*p != ';') 
 			line_eof++, p++;