From 40c134eb79f55bbe3b2d656119537f9f01cdd1f5 Mon Sep 17 00:00:00 2001 From: nishi Date: Fri, 26 Apr 2024 23:39:35 +0000 Subject: [PATCH] separate git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@55 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/interpreter.c | 19 ++++++++++++------- Serenade/interpreter.h | 2 ++ Serenade/main.c | 4 ++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index ebc1f2f..642f91e 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -119,13 +119,7 @@ struct sn_generic* eval_handler(struct sn_interpreter* sn, int args, struct sn_g return gen; } -struct sn_interpreter* sn_create_interpreter(void) { - struct sn_interpreter* sn = malloc(sizeof(struct sn_interpreter)); - sn->variables = malloc(sizeof(struct sn_interpreter_kv*)); - sn->variables[0] = NULL; - sn->generics = malloc(sizeof(struct sn_generic**)); - sn->generics[0] = NULL; - +void sn_stdlib_init(struct sn_interpreter* sn) { sn_set_handler(sn, "+", math_handler); sn_set_handler(sn, "-", math_handler); sn_set_handler(sn, "*", math_handler); @@ -133,9 +127,20 @@ struct sn_interpreter* sn_create_interpreter(void) { sn_set_handler(sn, "print", print_handler); sn_set_handler(sn, "eval", eval_handler); sn_set_handler(sn, "define-var", defvar_handler); +} + +void sn_ffi_init(struct sn_interpreter* sn) { #ifdef HAS_FFI_SUPPORT ffi_init(sn); #endif +} + +struct sn_interpreter* sn_create_interpreter(void) { + struct sn_interpreter* sn = malloc(sizeof(struct sn_interpreter)); + sn->variables = malloc(sizeof(struct sn_interpreter_kv*)); + sn->variables[0] = NULL; + sn->generics = malloc(sizeof(struct sn_generic**)); + sn->generics[0] = NULL; return sn; } diff --git a/Serenade/interpreter.h b/Serenade/interpreter.h index 686be99..c23a8f6 100644 --- a/Serenade/interpreter.h +++ b/Serenade/interpreter.h @@ -49,5 +49,7 @@ 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); +void sn_stdlib_init(struct sn_interpreter* sn); +void sn_ffi_init(struct sn_interpreter* sn); #endif diff --git a/Serenade/main.c b/Serenade/main.c index d17d61a..f3f8890 100644 --- a/Serenade/main.c +++ b/Serenade/main.c @@ -71,6 +71,8 @@ int main(int argc, char** argv) { } fread(str, 1, s.st_size, f); struct sn_interpreter* sn = sn_create_interpreter(); + sn_stdlib_init(sn); + sn_ffi_init(sn); int r = sn_eval(sn, str, s.st_size); sn_interpreter_free(sn); free(str); @@ -99,6 +101,8 @@ int main(int argc, char** argv) { printf("Support: %s\n", SUPPORT); printf("Parser stack size: %d\n", PARSER_STACK_SIZE); struct sn_interpreter* sn = sn_create_interpreter(); + sn_stdlib_init(sn); + sn_ffi_init(sn); char cbuf[2]; cbuf[0] = '\n'; cbuf[1] = 0; -- 2.43.0