From afa94f305eeb39c96ee76f6f57539f0393ffaae5 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 27 Apr 2024 01:40:04 +0000 Subject: [PATCH] source directive git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@63 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/interpreter.c | 27 +++++++++++++++++++++++++++ Serenade/main.c | 10 +++++----- Serenade/run.c | 6 +++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index f5a62f8..ee9b82a 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -38,6 +38,7 @@ #include #include #include +#include struct sn_generic* math_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) { struct sn_generic* gen = malloc(sizeof(struct sn_generic)); @@ -119,6 +120,31 @@ struct sn_generic* eval_handler(struct sn_interpreter* sn, int args, struct sn_g return gen; } +struct sn_generic* source_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) { + int i; + struct sn_generic* r = malloc(sizeof(struct sn_generic)); + r->type = SN_TYPE_VOID; + for(i = 1; i < args; i++) { + if(gens[i]->type == SN_TYPE_STRING) { + char* path = malloc(gens[i]->string_length + 1); + memcpy(path, gens[i]->string, gens[i]->string_length); + path[gens[i]->string_length] = 0; + FILE* f = fopen(path, "rb"); + if(f != NULL) { + struct stat s; + if(stat(path, &s) == 0) { + char* data = malloc(s.st_size); + fread(data, 1, s.st_size, f); + sn_eval(sn, data, s.st_size); + fclose(f); + } + } + free(path); + } + } + return r; +} + void sn_stdlib_init(struct sn_interpreter* sn) { sn_set_handler(sn, "+", math_handler); sn_set_handler(sn, "-", math_handler); @@ -127,6 +153,7 @@ void sn_stdlib_init(struct sn_interpreter* sn) { sn_set_handler(sn, "print", print_handler); sn_set_handler(sn, "eval", eval_handler); sn_set_handler(sn, "define-var", defvar_handler); + sn_set_handler(sn, "source", source_handler); } void sn_ffi_init(struct sn_interpreter* sn) { diff --git a/Serenade/main.c b/Serenade/main.c index 2b4ba96..e6227af 100644 --- a/Serenade/main.c +++ b/Serenade/main.c @@ -48,10 +48,10 @@ extern bool is_repl; -int run_command(const char* cmd){ - if(strcmp(cmd, ":quit") == 0){ +int run_command(const char* cmd) { + if(strcmp(cmd, ":quit") == 0) { return 1; - }else{ + } else { fprintf(stderr, "Unknown command\n"); return 0; } @@ -143,7 +143,7 @@ int main(int argc, char** argv) { br--; } } - if(line[0] == ':'){ + if(line[0] == ':') { int st = run_command(line); free(line); if(st != 0) break; @@ -162,7 +162,7 @@ int main(int argc, char** argv) { free(line); #else if(cbuf[0] == '\n') { - if(str[0] == ':'){ + if(str[0] == ':') { int st = run_command(str); free(str); str = malloc(1); diff --git a/Serenade/run.c b/Serenade/run.c index 9a0530d..1b3e008 100644 --- a/Serenade/run.c +++ b/Serenade/run.c @@ -41,12 +41,12 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen); -struct sn_generic* run_code(struct sn_interpreter* sn, int argc, struct sn_generic** args){ +struct sn_generic* run_code(struct sn_interpreter* sn, int argc, struct sn_generic** args) { struct sn_generic* r = malloc(sizeof(struct sn_generic)); r->type = SN_TYPE_VOID; struct sn_generic** gens = (struct sn_generic**)args[0]->argvalue; int i; - for(i = 0; gens[i] != NULL; i++){ + for(i = 0; gens[i] != NULL; i++) { _sn_run(sn, gens[i]); } return r; @@ -72,7 +72,7 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) { r->string = sn_strdup(SERENADE_VERSION); r->string_length = strlen(r->string); return r; - }else if(strcmp(op->name, "define-func") == 0){ + } else if(strcmp(op->name, "define-func") == 0) { char* name = malloc(gen->tree->args[1]->string_length + 1); memcpy(name, gen->tree->args[1]->string, gen->tree->args[1]->string_length); name[gen->tree->args[1]->string_length] = 0; -- 2.43.0