From 0eb64ede24d5f6ad69c317f4a23083f299116d01 Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 23 Apr 2024 05:21:56 +0000 Subject: [PATCH] parsing git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@13 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/parser.c | 4 +++- Serenade/util.c | 18 ++++++++++++++++++ Serenade/util.h | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Serenade/parser.c b/Serenade/parser.c index 263b8a9..8324c5e 100644 --- a/Serenade/parser.c +++ b/Serenade/parser.c @@ -77,7 +77,7 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size){ op_stack[br - 1] = malloc(1); op_stack[br - 1][0] = 0; }else if(c == ')'){ - if(br == 1){ + if(br > 0){ gn_stack[br - 1]->tree->op->name = sn_strdup(op_stack[br - 1]); } br_stack[br - 1] = 0; @@ -104,6 +104,7 @@ 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){ } } } @@ -136,6 +137,7 @@ struct sn_generic** sn_parse(char* data, unsigned long long size){ memcpy(d, data + start, i - start + 1); struct sn_generic* gen = sn_expr_parse(d, i - start + 1); if(gen != NULL){ + sn_print_generic(gen); sn_generic_free(gen); } free(d); diff --git a/Serenade/util.c b/Serenade/util.c index fdec195..c82b778 100644 --- a/Serenade/util.c +++ b/Serenade/util.c @@ -32,6 +32,7 @@ #include #include +#include char* sn_strcat(const char* str1, const char* str2){ char* str = malloc(strlen(str1) + strlen(str2) + 1); @@ -47,3 +48,20 @@ char* sn_strdup(const char* str1){ str[strlen(str1)] = 0; return str; } + +void _sn_print_generic(struct sn_generic* gen, int n){ + int i; + for(i = 0; i < n; i++) fprintf(stderr, " "); + if(gen->type == SN_TYPE_TREE){ + fprintf(stderr, "%s\n", gen->tree->op->name); + if(gen->tree->args != NULL){ + for(i = 0; gen->tree->args[i] != NULL; i++){ + _sn_print_generic(gen->tree->args[i], n + 1); + } + } + } +} + +void sn_print_generic(struct sn_generic* gen){ + _sn_print_generic(gen, 0); +} diff --git a/Serenade/util.h b/Serenade/util.h index a658cec..d220850 100644 --- a/Serenade/util.h +++ b/Serenade/util.h @@ -31,7 +31,10 @@ #ifndef __SERENADE_UTIL_H__ #define __SERENADE_UTIL_H__ +#include "parser.h" + char* sn_strcat(const char* str1, const char* str2); char* sn_strdup(const char* str1); +void sn_print_generic(struct sn_generic* gen); #endif -- 2.43.0