[uim-commit] r1002 - branches/r5rs/sigscheme

kzk at freedesktop.org kzk at freedesktop.org
Thu Jul 21 20:31:28 EST 2005


Author: kzk
Date: 2005-07-21 03:31:26 -0700 (Thu, 21 Jul 2005)
New Revision: 1002

Modified:
   branches/r5rs/sigscheme/datas.c
   branches/r5rs/sigscheme/io.c
   branches/r5rs/sigscheme/main.c
   branches/r5rs/sigscheme/operations.c
   branches/r5rs/sigscheme/read.c
   branches/r5rs/sigscheme/sigscheme.c
   branches/r5rs/sigscheme/sigscheme.h
   branches/r5rs/sigscheme/sigschemetype.h
Log:
* Now I implemented Scm_eval_c_string by introducing STRING_PORT.
  Currently, STRING_PORT is only for INPUT.

* sigscheme/read.c
  - (SCM_PORT_GETC): handle PORT_STRING
  - use Scm_NewStringCopying to simplify the code
* sigscheme/sigscheme.h
* sigscheme/datas.c
  - (Scm_NewFilePort): renamed from Scm_NewPort
  - (Scm_NewStringPort): new func
  - (Scm_eval_c_string): new func
* sigscheme/sigschemetype.h
  - introduce PORT_STRING related macros

* sigscheme/operations.c
  - use Scm_NewStringCopying to simplify the code

* sigscheme/sigscheme.c
* sigscheme/io.c
* sigscheme/main.c
  - (SigScm_Initialize): use Scm_NewFilePort instead of Scm_NewPort


Modified: branches/r5rs/sigscheme/datas.c
===================================================================
--- branches/r5rs/sigscheme/datas.c	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/datas.c	2005-07-21 10:31:26 UTC (rev 1002)
@@ -668,35 +668,40 @@
     return obj;
 }
 
-ScmObj Scm_NewPort(FILE *file, enum ScmPortDirection pdirection, enum ScmPortType ptype)
+ScmObj Scm_NewFilePort(FILE *file, enum ScmPortDirection pdirection)
 {
     ScmObj obj = SCM_NIL;
-    ScmPortInfo *pinfo = (ScmPortInfo *)malloc(sizeof(ScmPortInfo));;
+    ScmPortInfo *pinfo = (ScmPortInfo *)malloc(sizeof(ScmPortInfo));
 
     SCM_NEW_OBJ_INTERNAL(obj);
 
     SCM_SETPORT(obj);
     SCM_SETPORT_PORTDIRECTION(obj, pdirection);
-    switch (ptype) {
-	case PORT_FILE:
-	    {
+    SCM_SETPORT_PORTTYPE(obj, PORT_FILE);
+    pinfo->info.file_port.file = file;
+    pinfo->info.file_port.line = 0;
+    pinfo->ungottenchar = 0;
+    SCM_SETPORT_PORTINFO(obj, pinfo);
+    
+    return obj;
+}
 
-		pinfo->file         = file;
-		pinfo->line         = 0;
-		pinfo->ungottenchar = 0;
-	    }
-	    break;
-	case PORT_STRING:
-	    {
-		/* TODO : implemented this immediately! */
-	    }
-	    break;
-	default:
-	    SigScm_Error("Scm_NewPort : invalid port type\n");
-	    break;
-    }
+ScmObj Scm_NewStringPort(const char *str)
+{
+    ScmObj obj = SCM_NIL;
+    ScmPortInfo *pinfo = (ScmPortInfo *)malloc(sizeof(ScmPortInfo));
+
+    SCM_NEW_OBJ_INTERNAL(obj);
+
+    SCM_SETPORT(obj);
+    SCM_SETPORT_PORTDIRECTION(obj, PORT_INPUT);
+    SCM_SETPORT_PORTTYPE(obj, PORT_STRING);
+    pinfo->info.str_port.port_str = (char *)malloc(strlen(str) + 1);
+    strcpy(pinfo->info.str_port.port_str, str);
+    pinfo->info.str_port.str_current = pinfo->info.str_port.port_str;
+    pinfo->ungottenchar = 0;
     SCM_SETPORT_PORTINFO(obj, pinfo);
-
+    
     return obj;
 }
 
@@ -844,3 +849,14 @@
 
     return SCM_C_FUNCPOINTER_FUNC(c_funcptr);
 }
+
+ScmObj Scm_eval_c_string(const char *exp)
+{
+    ScmObj str_port = Scm_NewStringPort(exp);
+    ScmObj ret = SCM_NIL;
+
+    ret = SigScm_Read(str_port);
+    ret = ScmOp_eval(ret, SCM_NIL);
+
+    return ret;
+}

Modified: branches/r5rs/sigscheme/io.c
===================================================================
--- branches/r5rs/sigscheme/io.c	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/io.c	2005-07-21 10:31:26 UTC (rev 1002)
@@ -203,7 +203,7 @@
         SigScm_ErrorObj("open-input-file : cannot open file ", filepath);
 
     /* Allocate ScmPort */
-    return Scm_NewPort(f, PORT_INPUT, PORT_FILE);
+    return Scm_NewFilePort(f, PORT_INPUT);
 }
 
 ScmObj ScmOp_open_output_file(ScmObj filepath)
@@ -219,7 +219,7 @@
         SigScm_ErrorObj("open-output-file : cannot open file ", filepath);
 
     /* Return new ScmPort */
-    return Scm_NewPort(f, PORT_OUTPUT, PORT_FILE);
+    return Scm_NewFilePort(f, PORT_OUTPUT);
 }
 
 ScmObj ScmOp_close_input_port(ScmObj port)

Modified: branches/r5rs/sigscheme/main.c
===================================================================
--- branches/r5rs/sigscheme/main.c	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/main.c	2005-07-21 10:31:26 UTC (rev 1002)
@@ -55,8 +55,8 @@
 /* Very simple repl, please rewrite. */
 static void repl(void)
 {
-    ScmObj stdin_port  = Scm_NewPort(stdin,  PORT_INPUT, PORT_FILE);
-    ScmObj stdout_port = Scm_NewPort(stdout, PORT_INPUT, PORT_FILE);
+    ScmObj stdin_port  = Scm_NewFilePort(stdin,  PORT_INPUT);
+    ScmObj stdout_port = Scm_NewFilePort(stdout, PORT_INPUT);
     ScmObj s_exp, result;
 
     printf("sscm> ");

Modified: branches/r5rs/sigscheme/operations.c
===================================================================
--- branches/r5rs/sigscheme/operations.c	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/operations.c	2005-07-21 10:31:26 UTC (rev 1002)
@@ -1143,17 +1143,10 @@
 
 ScmObj ScmOp_symbol_to_string(ScmObj obj)
 {
-    int   size = 0;
-    char *name = NULL;
-
     if (!SCM_SYMBOLP(obj))
         return SCM_FALSE;
 
-    size = strlen(SCM_SYMBOL_NAME(obj));
-    name = (char*)malloc(sizeof(char) * size + 1);
-    strcpy(name, SCM_SYMBOL_NAME(obj));
-
-    return Scm_NewString(name);
+    return Scm_NewStringCopying(SCM_SYMBOL_NAME(obj));
 }
 
 ScmObj ScmOp_string_to_symbol(ScmObj str)
@@ -1547,17 +1540,10 @@
 
 ScmObj ScmOp_string_copy(ScmObj string)
 {
-    char *orig_str = NULL;
-    char *dest_str = NULL;
-
     if (!SCM_STRINGP(string))
         SigScm_ErrorObj("string-copy : string required but got ", string);
 
-    orig_str = SCM_STRING_STR(string);
-    dest_str = (char*)malloc(sizeof(char) * (strlen(orig_str) + 1));
-    strcpy(dest_str, orig_str);
-
-    return Scm_NewString(dest_str);
+    return Scm_NewStringCopying(SCM_STRING_STR(string));
 }
 
 ScmObj ScmOp_string_fill(ScmObj string, ScmObj ch)

Modified: branches/r5rs/sigscheme/read.c
===================================================================
--- branches/r5rs/sigscheme/read.c	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/read.c	2005-07-21 10:31:26 UTC (rev 1002)
@@ -51,15 +51,23 @@
 /*=======================================
   File Local Macro Declarations
 =======================================*/
-#define SCM_PORT_GETC(port, c) 				\
-    do {						\
-	if (SCM_PORTINFO_UNGOTTENCHAR(port)) {		\
-	    c = SCM_PORTINFO_UNGOTTENCHAR(port);	\
-	    SCM_PORTINFO_UNGOTTENCHAR(port) = 0;	\
-	} else {					\
-	    c = getc(SCM_PORTINFO_FILE(port));		\
-	    SCM_PORTINFO_UNGOTTENCHAR(port) = 0;	\
-	}						\
+#define SCM_PORT_GETC(port, c) 							\
+    do {									\
+	if (SCM_PORTINFO_UNGOTTENCHAR(port)) {					\
+	    c = SCM_PORTINFO_UNGOTTENCHAR(port);				\
+	    SCM_PORTINFO_UNGOTTENCHAR(port) = 0;				\
+	} else {								\
+	    switch (SCM_PORT_PORTTYPE(port)) {					\
+		case PORT_FILE:							\
+		    c = getc(SCM_PORTINFO_FILE(port));				\
+		    break;							\
+		case PORT_STRING:						\
+		    c = (*SCM_PORTINFO_STR_CURRENT(port));			\
+		    SCM_PORTINFO_STR_CURRENT(port)++;				\
+		    break;							\
+	    }									\
+	    SCM_PORTINFO_UNGOTTENCHAR(port) = 0;				\
+	}									\
     } while (0);
 
 #define SCM_PORT_UNGETC(port,c )	\
@@ -117,15 +125,19 @@
             while (1) {
 		SCM_PORT_GETC(port, c);
                 if (c == '\n') {
-	           SCM_PORT_PORTINFO(port)->line++;
-		   break;
+		    if (SCM_PORT_PORTTYPE(port) == PORT_FILE) {
+			SCM_PORTINFO_LINE(port)++;
+		    }
+		    break;
 		}
                 if (c == EOF ) return c;
             }
             continue;
         } else if(c == '\n') {
-	  SCM_PORT_PORTINFO(port)->line++;
-	  continue;
+	    if (SCM_PORT_PORTTYPE(port) == PORT_FILE) {
+		SCM_PORTINFO_LINE(port)++;
+	    }
+	    continue;
         } else if(isspace(c)) {
             continue;
         }
@@ -220,7 +232,6 @@
     ScmObj list_head = SCM_NIL;
     ScmObj list_tail = SCM_NIL;
     ScmObj item = SCM_NIL;
-    int line = SCM_PORT_PORTINFO(port)->line;
 
     int c = 0;
     while (1) {
@@ -231,7 +242,10 @@
 #endif
 
         if (c == EOF) {
- 	    SigScm_Error("EOF inside list. (starting from line %d)\n", line + 1);
+	    if (SCM_PORT_PORTTYPE(port) == PORT_FILE)
+		SigScm_Error("EOF inside list. (starting from line %d)\n", SCM_PORTINFO_LINE(port) + 1);
+	    else
+		SigScm_Error("EOF inside list.\n");
         } else if (c == closeParen) {
             return list_head;
         } else if (c == '.') {
@@ -298,7 +312,6 @@
 {
     char  stringbuf[1024];
     int   stringlen = 0;
-    char *dst = NULL;
     int   c = 0;
 
 #if DEBUG_PARSER
@@ -319,9 +332,7 @@
             case '\"':
                 {
                     stringbuf[stringlen] = '\0';
-                    dst = (char *)malloc(strlen(stringbuf) + 1);
-                    strcpy(dst, stringbuf);
-                    return Scm_NewString(dst);
+                    return Scm_NewStringCopying(stringbuf);
                 }
             case '\\':
                 {

Modified: branches/r5rs/sigscheme/sigscheme.c
===================================================================
--- branches/r5rs/sigscheme/sigscheme.c	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/sigscheme.c	2005-07-21 10:31:26 UTC (rev 1002)
@@ -260,9 +260,9 @@
     /*=======================================================================
       Current Input & Output Initialization
     =======================================================================*/
-    current_input_port  = Scm_NewPort(stdin,  PORT_INPUT,  PORT_FILE);
+    current_input_port  = Scm_NewFilePort(stdin,  PORT_INPUT);
     SigScm_gc_protect(current_input_port);
-    current_output_port = Scm_NewPort(stdout, PORT_OUTPUT, PORT_FILE);
+    current_output_port = Scm_NewFilePort(stdout, PORT_OUTPUT);
     SigScm_gc_protect(current_output_port);
 
     stack_start_pointer = NULL;

Modified: branches/r5rs/sigscheme/sigscheme.h
===================================================================
--- branches/r5rs/sigscheme/sigscheme.h	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/sigscheme.h	2005-07-21 10:31:26 UTC (rev 1002)
@@ -125,7 +125,8 @@
 ScmObj Scm_NewFunc(enum ScmFuncArgNum num_arg, ScmFuncType func);
 ScmObj Scm_NewClosure(ScmObj exp, ScmObj env);
 ScmObj Scm_NewVector(ScmObj *vec, int len);
-ScmObj Scm_NewPort(FILE *file, enum ScmPortDirection pdireciton, enum ScmPortType ptype);
+ScmObj Scm_NewFilePort(FILE *file, enum ScmPortDirection pdireciton);
+ScmObj Scm_NewStringPort(const char *str);  /* input only? */
 ScmObj Scm_NewContinuation(void);
 ScmObj Scm_NewCPointer(void *data);
 ScmObj Scm_NewCFuncPointer(C_FUNC func);
@@ -134,7 +135,9 @@
 char*  Scm_GetString(ScmObj str);
 void*  Scm_GetCPointer(ScmObj c_ptr);
 C_FUNC Scm_GetCFuncPointer(ScmObj c_funcptr);
+ScmObj Scm_eval_c_string(const char *exp);
 
+
 /* eval.c */
 ScmObj ScmOp_eval(ScmObj obj, ScmObj env);
 ScmObj ScmOp_apply(ScmObj arg, ScmObj env);

Modified: branches/r5rs/sigscheme/sigschemetype.h
===================================================================
--- branches/r5rs/sigscheme/sigschemetype.h	2005-07-21 09:45:56 UTC (rev 1001)
+++ branches/r5rs/sigscheme/sigschemetype.h	2005-07-21 10:31:26 UTC (rev 1002)
@@ -100,8 +100,18 @@
 /* ScmPort Info */
 typedef struct _ScmPortInfo ScmPortInfo;
 struct _ScmPortInfo {
-    FILE *file;
-    int line;
+    union {
+        struct {
+            FILE *file;
+            int line;
+        } file_port;
+        
+        struct {
+            char *port_str;
+            const char *str_current;
+        } str_port;
+    } info;
+    
     char ungottenchar;
 };
 
@@ -300,12 +310,16 @@
 #define SCM_PORTP(a) (SCM_GETTYPE(a) == ScmPort)
 #define SCM_PORT(a)  (sigassert(SCM_PORTP(a)), a)
 #define SCM_PORT_PORTDIRECTION(a) (SCM_PORT(a)->obj.port.port_direction)
+#define SCM_PORT_PORTTYPE(a) (SCM_PORT(a)->obj.port.port_type)
 #define SCM_PORT_PORTINFO(a) (SCM_PORT(a)->obj.port.port_info)
 #define SCM_SETPORT(a) (SCM_SETTYPE(a, ScmPort))
 #define SCM_SETPORT_PORTDIRECTION(a, pdirection) (SCM_PORT_PORTDIRECTION(a) = pdirection)
+#define SCM_SETPORT_PORTTYPE(a, ptype) (SCM_PORT_PORTTYPE(a) = ptype)
 #define SCM_SETPORT_PORTINFO(a, pinfo) (SCM_PORT_PORTINFO(a) = pinfo)
-#define SCM_PORTINFO_FILE(a) (SCM_PORT_PORTINFO(a)->file)
-#define SCM_PORTINFO_LINE(a) (SCM_PORT_PORTINFO(a)->line)
+#define SCM_PORTINFO_FILE(a) (SCM_PORT_PORTINFO(a)->info.file_port.file)
+#define SCM_PORTINFO_LINE(a) (SCM_PORT_PORTINFO(a)->info.file_port.line)
+#define SCM_PORTINFO_STR(a) (SCM_PORT_PORTINFO(a)->info.str_port.port_str)
+#define SCM_PORTINFO_STR_CURRENT(a) (SCM_PORT_PORTINFO(a)->info.str_port.str_current)
 #define SCM_PORTINFO_UNGOTTENCHAR(a) (SCM_PORT_PORTINFO(a)->ungottenchar)
 
 #define SCM_CONTINUATIONP(a) (SCM_GETTYPE(a) == ScmContinuation)



More information about the uim-commit mailing list