feat(lexer, preprocessor): replace cstring conversion with copy and refactor macro expansion

- Replace `scc_cstring_from_cstr(scc_cstring_as_cstr(...))` with `scc_cstring_copy()` in lexer to fix memory leaks
- Extract macro expansion logic into separate `expand_macro.c` file
- Remove `expand_stack` parameter from `scc_pp_expand_macro()` function
- Add new parsing functions for macro replacement lists and arguments
- Add string utility functions for whitespace trimming and string joining
- Update memory stream documentation for clarity
This commit is contained in:
zzy
2025-12-15 20:24:39 +08:00
parent 73d74f5e13
commit 07f5d9331b
15 changed files with 574 additions and 346 deletions

View File

@@ -20,6 +20,7 @@ typedef struct scc_cstring {
*
* @return cstring_t 初始化后的对象
*/
// FIXME need using `init` beacuse it not malloc
static inline scc_cstring_t scc_cstring_new(void) {
return (scc_cstring_t){.data = null, .size = 0, .cap = 0};
}
@@ -48,6 +49,10 @@ static inline scc_cstring_t scc_cstring_from_cstr(const char *s) {
return (scc_cstring_t){.size = len + 1, .cap = len + 1, .data = data};
}
static inline scc_cstring_t scc_cstring_copy(const scc_cstring_t *s) {
return scc_cstring_from_cstr(s->data);
}
/**
* @brief 释放动态字符串占用的内存资源
*
@@ -182,4 +187,15 @@ static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) {
return str->data;
}
static inline char *scc_cstring_move_cstr(scc_cstring_t *str) {
if (str == null || str->data == null) {
return "";
}
char *ret = str->data;
str->data = null;
str->cap = 0;
str->size = 0;
return ret;
}
#endif /* __SCC_CORE_STR_H__ */