From 36f375b5ab9180614fe0583b62c70c359235da34 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 8 May 2024 03:12:03 +0000 Subject: [PATCH] discard op git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@132 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/interpreter.c | 1 + Serenade/parser.c | 3 ++- Serenade/parser.h | 1 + Serenade/run.c | 8 ++++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index ef8d8c6..aaf981b 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -293,6 +293,7 @@ do_again: } struct sn_interpreter_kv* sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen) { + if(gen != NULL) gen->reference = true; int i; bool replaced = false; bool global = true; diff --git a/Serenade/parser.c b/Serenade/parser.c index 61591ff..b61ed08 100644 --- a/Serenade/parser.c +++ b/Serenade/parser.c @@ -123,6 +123,7 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size) { argbufmode = SN_TYPE_STRING; } else if(c == '(') { gn_stack[br] = malloc(sizeof(struct sn_generic)); + gn_stack[br]->reference = false; gn_stack[br]->type = SN_TYPE_TREE; gn_stack[br]->tree = malloc(sizeof(struct sn_tree)); gn_stack[br]->tree->args = malloc(sizeof(struct sn_generic*)); @@ -316,7 +317,7 @@ void sn_tree_free(struct sn_interpreter* sn, struct sn_tree* t) { if(t->args != NULL) { int i; for(i = 0; t->args[i] != NULL; i++) { - sn_generic_free(sn, t->args[i]); + if(!t->args[i]->reference) sn_generic_free(sn, t->args[i]); } free(t->args); } diff --git a/Serenade/parser.h b/Serenade/parser.h index 7bd9101..069db00 100644 --- a/Serenade/parser.h +++ b/Serenade/parser.h @@ -50,6 +50,7 @@ struct sn_generic { struct sn_tree* tree; bool used; void* argvalue; + bool reference; struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**); }; diff --git a/Serenade/run.c b/Serenade/run.c index a32031e..818a053 100644 --- a/Serenade/run.c +++ b/Serenade/run.c @@ -187,6 +187,13 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) { sn->local_variables = old_kv; } return r; + } else if(strcmp(op->name, "discard") == 0) { + r->type = SN_TYPE_VOID; + int i; + for(i = 1; gen->tree->args[i] != NULL; i++){ + sn_generic_free(sn, _sn_run(sn, gen->tree->args[i])); + } + return r; } bool called = false; int j; @@ -272,5 +279,6 @@ int sn_run(struct sn_interpreter* sn, struct sn_generic* gen) { sn_print_to(stdout, rgen); printf("\n"); } + if(rgen->type == SN_TYPE_VOID) free(rgen); return 0; } -- 2.43.0