From c3752dbc7023d32cf7fe5c73b0978a084c46eb97 Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 23 Apr 2024 23:59:03 +0000 Subject: [PATCH] repl works git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@24 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/interpreter.c | 19 +++++++++++++++ Serenade/interpreter.h | 1 + Serenade/main.c | 53 +++++++++++++++++++++++++++++++++--------- Serenade/run.c | 6 ++++- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index 9c042d5..855c970 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -30,6 +30,7 @@ #include "interpreter.h" +#include "run.h" #include "util.h" #include @@ -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; +} diff --git a/Serenade/interpreter.h b/Serenade/interpreter.h index 689ed25..fb3dbcf 100644 --- a/Serenade/interpreter.h +++ b/Serenade/interpreter.h @@ -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 diff --git a/Serenade/main.c b/Serenade/main.c index ff42a83..c114a05 100644 --- a/Serenade/main.c +++ b/Serenade/main.c @@ -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 diff --git a/Serenade/run.c b/Serenade/run.c index 2830510..f208f0e 100644 --- a/Serenade/run.c +++ b/Serenade/run.c @@ -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; +} -- 2.43.0