From 166c8eaf5687715dff9e57caa7616568fd493698 Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 23 Apr 2024 05:58:54 +0000 Subject: [PATCH] string works git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@14 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/parser.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++- Serenade/parser.h | 2 +- Serenade/util.c | 5 +++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Serenade/parser.c b/Serenade/parser.c index 8324c5e..00247c6 100644 --- a/Serenade/parser.c +++ b/Serenade/parser.c @@ -48,6 +48,30 @@ struct sn_generic { }; */ +#define PUSH_STACK(x) if(strlen(argbuf) > 0 && br_stack[x] >= 0){ \ + struct sn_generic* new_gn = malloc(sizeof(*new_gn)); \ + new_gn->type = argbufmode; \ + if(argbufmode == SN_TYPE_DOUBLE){ \ + new_gn->number = atof(argbuf); \ + }else if(argbufmode == SN_TYPE_STRING){ \ + new_gn->string = sn_strdup(argbuf); \ + new_gn->string_length = strlen(argbuf); \ + } \ + int j; \ + struct sn_generic** old_args = gn_stack[x]->tree->args; \ + for(j = 0; old_args[j] != NULL; j++); \ + gn_stack[x]->tree->args = malloc(sizeof(*gn_stack[x]->tree->args) * (j + 2)); \ + for(j = 0; old_args[j] != NULL; j++){ \ + gn_stack[x]->tree->args[j] = old_args[j]; \ + } \ + gn_stack[x]->tree->args[j] = new_gn; \ + gn_stack[x]->tree->args[j + 1] = NULL; \ + free(old_args); \ + } \ + free(argbuf); \ + argbuf = malloc(1); \ + argbuf[0] = 0; + struct sn_generic* sn_expr_parse(char* data, unsigned long long size){ int i; int br = 0; @@ -60,11 +84,24 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size){ op_stack[i] = NULL; gn_stack[i] = NULL; } + int argbufmode = 0; + char* argbuf = malloc(1); + argbuf[0] = 0; for(i = 0; i < size; i++){ char c = data[i]; if(c == '"'){ dq = !dq; + if(!dq){ + PUSH_STACK(br - 1); + } }else if(dq){ + argbufmode = SN_TYPE_STRING; + char cbuf[2]; + cbuf[0] = c; + cbuf[1] = 0; + char* tmp = argbuf; + argbuf = sn_strcat(tmp, cbuf); + free(tmp); }else if(c == '('){ br++; gn_stack[br - 1] = malloc(sizeof(*gn_stack)); @@ -92,10 +129,13 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size){ gn_stack[br - 2]->tree->args[j] = gn_stack[br - 1]; gn_stack[br - 2]->tree->args[j + 1] = NULL; free(old_args); + PUSH_STACK(br - 1); + }else if(strlen(argbuf) > 0){ } br--; }else if(br > 0){ if(c == ' '){ + PUSH_STACK(br - 1); br_stack[br - 1]++; }else if(br_stack[br - 1] == 0){ char cbuf[2]; @@ -104,10 +144,18 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size){ char* tmp = op_stack[br - 1]; op_stack[br - 1] = sn_strcat(tmp, cbuf); free(tmp); - }else if(br_stack[br - 1] > 0){ + }else{ + argbufmode = SN_TYPE_DOUBLE; + char cbuf[2]; + cbuf[0] = c; + cbuf[1] = 0; + char* tmp = argbuf; + argbuf = sn_strcat(tmp, cbuf); + free(tmp); } } } + free(argbuf); struct sn_generic* gen = gn_stack[0]; free(gn_stack); free(br_stack); diff --git a/Serenade/parser.h b/Serenade/parser.h index 08b21f1..b534e5b 100644 --- a/Serenade/parser.h +++ b/Serenade/parser.h @@ -32,7 +32,7 @@ #define __SERENADE_PARSER_H__ enum types { - SN_TYPE_DOUBLE, + SN_TYPE_DOUBLE = 0, SN_TYPE_STRING, SN_TYPE_TREE, SN_TYPE_PTR, diff --git a/Serenade/util.c b/Serenade/util.c index c82b778..db96a42 100644 --- a/Serenade/util.c +++ b/Serenade/util.c @@ -59,6 +59,11 @@ void _sn_print_generic(struct sn_generic* gen, int n){ _sn_print_generic(gen->tree->args[i], n + 1); } } + }else if(gen->type == SN_TYPE_DOUBLE){ + fprintf(stderr, "%f\n", gen->number); + }else if(gen->type == SN_TYPE_STRING){ + fwrite(gen->string, 1, gen->string_length, stderr); + fprintf(stderr, "\n"); } } -- 2.43.0