]> Nishi Git Mirror - serenade.git/commitdiff
repl works
authornishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 23:59:03 +0000 (23:59 +0000)
committernishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 23:59:03 +0000 (23:59 +0000)
git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@24 0f02c867-ac3d-714e-8a88-971ba1f6efcf

Serenade/interpreter.c
Serenade/interpreter.h
Serenade/main.c
Serenade/run.c

index 9c042d5a37b85158d904a4515eeab5d50e73f1da..855c9701061affe41c0c4138e99c2a61061c3078 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "interpreter.h"
 
+#include "run.h"
 #include "util.h"
 
 #include <stdbool.h>
@@ -166,3 +167,21 @@ void sn_set_handler(struct sn_interpreter* sn, const char* name, struct sn_gener
                sn->variables[i + 1] = NULL;
        }
 }
+
+int sn_eval(struct sn_interpreter* sn, char* data, unsigned long long len) {
+       struct sn_generic** gens = sn_parse(data, len);
+       int r = 0;
+       if(gens != NULL) {
+               int i;
+               for(i = 0; gens[i] != NULL; i++) {
+                       if(r == 0) {
+                               if(sn_run(sn, gens[i]) != 0) {
+                                       r = 1;
+                               }
+                               sn_generic_free(gens[i]);
+                       }
+               }
+               free(gens);
+       }
+       return r;
+}
index 689ed25f18a933d28e89f95671a13e654386b468..fb3dbcfbf3f9559dcf19f23a8f4e3ecefdf05818 100644 (file)
@@ -47,5 +47,6 @@ struct sn_interpreter* sn_create_interpreter(void);
 void sn_interpreter_free(struct sn_interpreter* sn);
 void sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen);
 void sn_set_handler(struct sn_interpreter* sn, const char* name, struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**));
+int sn_eval(struct sn_interpreter* sn, char* data, unsigned long long len);
 
 #endif
index ff42a83eca5f1b2c536f9b116106ff499d93bce7..c114a05e08d8d7b8310eaa2eb6db4ad97ac7ac38 100644 (file)
@@ -68,18 +68,11 @@ int main(int argc, char** argv) {
                                        return 1;
                                }
                                fread(str, 1, s.st_size, f);
-                               struct sn_generic** t = sn_parse(str, s.st_size);
-                               if(t != NULL) {
-                                       int j;
-                                       struct sn_interpreter* sn = sn_create_interpreter();
-                                       for(j = 0; t[j] != NULL; j++) {
-                                               sn_run(sn, t[j]);
-                                               sn_generic_free(t[j]);
-                                       }
-                                       sn_interpreter_free(sn);
-                                       free(t);
-                               }
+                               struct sn_interpreter* sn = sn_create_interpreter();
+                               int r = sn_eval(sn, str, s.st_size);
+                               sn_interpreter_free(sn);
                                free(str);
+                               return r;
                        } else {
                                fprintf(stderr, "%s: %s: stat fail\n", argv[0], argv[i]);
                                return 1;
@@ -91,6 +84,44 @@ int main(int argc, char** argv) {
                printf("Welcome to Serenade LISP %s\n", SERENADE_VERSION);
                printf("Support: %s\n", SUPPORT);
                printf("Parser stack size: %d\n", PARSER_STACK_SIZE);
+               struct sn_interpreter* sn = sn_create_interpreter();
+               char cbuf[2];
+               cbuf[0] = '\n';
+               cbuf[1] = 0;
+               char* str = malloc(1);
+               str[0] = 0;
+               int br = 0;
+               while(1) {
+                       if(cbuf[0] == '\n') {
+                               if(br == 0 && strlen(str) > 0) {
+                                       sn_eval(sn, str, strlen(str));
+                                       free(str);
+                                       str = malloc(1);
+                                       str[0] = 0;
+                                       printf("\n");
+                               } else if(br > 0) {
+                                       char* tmp = str;
+                                       str = sn_strcat(tmp, cbuf);
+                                       free(tmp);
+                               }
+                               printf("> ");
+                               fflush(stdout);
+                       }
+                       int l = fread(cbuf, 1, 1, stdin);
+                       if(l <= 0) break;
+                       if(cbuf[0] == '(') {
+                               br++;
+                       } else if(cbuf[0] == ')') {
+                               br--;
+                       }
+                       if(cbuf[0] != '\r' && cbuf[0] != '\n') {
+                               char* tmp = str;
+                               str = sn_strcat(tmp, cbuf);
+                               free(tmp);
+                       }
+               }
+               free(str);
+               sn_interpreter_free(sn);
        }
        return 0;
 #else
index 283051014cad0a117b4788f49814d85b5aab5432..f208f0ec6e5ef63db838a20942d0bd5a8f48bc63 100644 (file)
@@ -89,4 +89,8 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
        return NULL;
 }
 
-int sn_run(struct sn_interpreter* sn, struct sn_generic* gen) { _sn_run(sn, gen); }
+int sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
+       struct sn_generic* rgen = _sn_run(sn, gen);
+       if(rgen == NULL) return 1;
+       return 0;
+}