From b753ae09116b73a6985649fe7bd401921ab8dc07 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Thu, 8 Jan 2026 11:22:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(lex=5Fparser):=20=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8Dlibcore=E4=B8=BAscc=5Fcore=E5=B9=B6=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=A4=B4=E6=96=87=E4=BB=B6=E5=8C=85=E5=90=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将依赖项从libcore重命名为scc_core - 更新头文件包含路径从 - 保持原有功能不变 refactor(lexer): 重命名libcore为scc_core并添加词法流式解析功能 - 将依赖项从libcore重命名为scc_core - 移除不再需要的scc_lexer_token结构体定义 - 重命名struct cc_lexer为struct scc_lexer - 添加scc_lexer_stream_t流式解析器相关定义和实现 - 新增lexer_stream.c文件实现流式token缓冲功能 refactor(lexer_log): 重命名logger变量和头文件定义 - 将头文件保护宏从__SMCC_LEXER_LOG_H__改为__SCC_LEXER_LOG_H__ - 将logger变量从__smcc_lexer_log改为__scc_lexer_log - 更新头文件包含从 refactor(lexer_token): 重新组织token头文件结构 - 将头文件保护宏从__SMCC_CC_TOKEN_H__改为__SCC_LEXER_TOKEN_H__ - 更新头文件包含从 - 将scc_lexer_token结构体定义移至该文件 refactor(lexer): 简化token匹配代码格式 - 移除LCC相关的注释内容 - 优化括号符号的token匹配代码格式,使用clang-format控制 refactor(pprocessor): 更新依赖项名称和头文件包含 - 将libcore重命名为scc_core - 将libutils重命名为scc_utils - 更新头文件包含路径 refactor(runtime): 重命名libcore为scc_core并重构目录结构 - 将libcore目录重命名为scc_core - 将libutils目录重命名为scc_utils - 更新所有相关的头文件包含路径 - 修改cbuild.toml中的包名称 - 更新core_vec.h中的宏定义以支持标准库模式 --- libs/lex_parser/cbuild.toml | 2 +- libs/lex_parser/include/lex_parser.h | 2 +- libs/lexer/cbuild.toml | 2 +- libs/lexer/include/lexer.h | 67 ++++++++- libs/lexer/include/lexer_log.h | 22 +-- libs/lexer/include/lexer_token.h | 28 +++- libs/lexer/src/lexer.c | 66 ++------- libs/lexer/src/lexer_log.c | 2 +- libs/lexer/src/lexer_stream.c | 138 ++++++++++++++++++ libs/lexer/tests/test_run.c | 2 +- libs/pprocessor/cbuild.toml | 4 +- libs/pprocessor/include/pp_macro.h | 4 +- libs/pprocessor/include/pp_parse.h | 2 +- libs/pprocessor/include/pprocessor.h | 6 +- libs/pprocessor/src/expand_macro.c | 2 - runtime/libcore/include/libcore.h | 14 -- runtime/libutils/cbuild.toml | 7 - runtime/{libcore => scc_core}/cbuild.toml | 2 +- runtime/scc_core/include/scc_core.h | 14 ++ .../include/scc_core_impl.h} | 2 +- .../include/scc_core_log.h} | 0 .../include/scc_core_macro.h} | 0 .../include/scc_core_mem.h} | 2 +- .../include/scc_core_pos.h} | 4 +- .../include/scc_core_str.h} | 13 +- .../include/scc_core_stream.h} | 8 +- .../include/scc_core_type.h} | 0 .../include/scc_core_vec.h} | 51 +++++-- .../include/utest/acutest.h | 0 .../{libcore => scc_core}/src/cfg.std_impl.c | 2 +- runtime/{libcore => scc_core}/src/memory.c | 2 +- runtime/{libcore => scc_core}/src/stream.c | 4 +- .../{libcore => scc_core}/tests/test_log.c | 0 runtime/scc_utils/cbuild.toml | 5 + .../{libutils => scc_utils}/include/kllist.h | 0 .../include/scc_hashtable.h} | 2 +- .../include/scc_strpool.h} | 4 +- .../include/scc_utils.h} | 6 +- .../{libutils => scc_utils}/src/hashtable.c | 2 +- runtime/{libutils => scc_utils}/src/strpool.c | 2 +- 40 files changed, 345 insertions(+), 150 deletions(-) create mode 100644 libs/lexer/src/lexer_stream.c delete mode 100644 runtime/libcore/include/libcore.h delete mode 100644 runtime/libutils/cbuild.toml rename runtime/{libcore => scc_core}/cbuild.toml (92%) create mode 100644 runtime/scc_core/include/scc_core.h rename runtime/{libcore/include/core_impl.h => scc_core/include/scc_core_impl.h} (98%) rename runtime/{libcore/include/core_log.h => scc_core/include/scc_core_log.h} (100%) rename runtime/{libcore/include/core_macro.h => scc_core/include/scc_core_macro.h} (100%) rename runtime/{libcore/include/core_mem.h => scc_core/include/scc_core_mem.h} (97%) rename runtime/{libcore/include/core_pos.h => scc_core/include/scc_core_pos.h} (90%) rename runtime/{libcore/include/core_str.h => scc_core/include/scc_core_str.h} (96%) rename runtime/{libcore/include/core_stream.h => scc_core/include/scc_core_stream.h} (97%) rename runtime/{libcore/include/core_type.h => scc_core/include/scc_core_type.h} (100%) rename runtime/{libcore/include/core_vec.h => scc_core/include/scc_core_vec.h} (71%) rename runtime/{libcore => scc_core}/include/utest/acutest.h (100%) rename runtime/{libcore => scc_core}/src/cfg.std_impl.c (98%) rename runtime/{libcore => scc_core}/src/memory.c (99%) rename runtime/{libcore => scc_core}/src/stream.c (99%) rename runtime/{libcore => scc_core}/tests/test_log.c (100%) create mode 100644 runtime/scc_utils/cbuild.toml rename runtime/{libutils => scc_utils}/include/kllist.h (100%) rename runtime/{libutils/include/hashtable.h => scc_utils/include/scc_hashtable.h} (99%) rename runtime/{libutils/include/strpool.h => scc_utils/include/scc_strpool.h} (97%) rename runtime/{libutils/include/libutils.h => scc_utils/include/scc_utils.h} (57%) rename runtime/{libutils => scc_utils}/src/hashtable.c (99%) rename runtime/{libutils => scc_utils}/src/strpool.c (97%) diff --git a/libs/lex_parser/cbuild.toml b/libs/lex_parser/cbuild.toml index 806ec27..421e53a 100644 --- a/libs/lex_parser/cbuild.toml +++ b/libs/lex_parser/cbuild.toml @@ -2,4 +2,4 @@ name = "scc_lex_parser" version = "0.1.0" -dependencies = [{ name = "libcore", path = "../../runtime/libcore" }] +dependencies = [{ name = "scc_core", path = "../../runtime/scc_core" }] diff --git a/libs/lex_parser/include/lex_parser.h b/libs/lex_parser/include/lex_parser.h index 757e815..c73f561 100644 --- a/libs/lex_parser/include/lex_parser.h +++ b/libs/lex_parser/include/lex_parser.h @@ -1,7 +1,7 @@ #ifndef __SCC_LEX_PARSER_H__ #define __SCC_LEX_PARSER_H__ -#include +#include static inline cbool scc_lex_parse_is_endline(int ch) { return ch == '\n' || ch == '\r'; diff --git a/libs/lexer/cbuild.toml b/libs/lexer/cbuild.toml index 4b75b3f..6e546a8 100644 --- a/libs/lexer/cbuild.toml +++ b/libs/lexer/cbuild.toml @@ -3,6 +3,6 @@ name = "scc_lex" version = "0.1.0" dependencies = [ - { name = "libcore", path = "../../runtime/libcore" }, + { name = "scc_core", path = "../../runtime/scc_core" }, { name = "lex_parser", path = "../lex_parser" }, ] diff --git a/libs/lexer/include/lexer.h b/libs/lexer/include/lexer.h index b530a74..d604eb3 100644 --- a/libs/lexer/include/lexer.h +++ b/libs/lexer/include/lexer.h @@ -7,20 +7,14 @@ #define __SCC_LEXER_H__ #include "lexer_token.h" -#include - -typedef struct scc_lexer_token { - scc_tok_type_t type; - scc_cvalue_t value; - scc_pos_t loc; -} scc_lexer_tok_t; +#include /** * @brief 词法分析器核心结构体 * * 封装词法分析所需的状态信息和缓冲区管理 */ -typedef struct cc_lexer { +typedef struct scc_lexer { scc_probe_stream_t *stream; scc_pos_t pos; } scc_lexer_t; @@ -50,4 +44,61 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token); */ void scc_lexer_get_valid_token(scc_lexer_t *lexer, scc_lexer_tok_t *token); +typedef SCC_VEC(scc_lexer_tok_t) scc_lexer_tok_vec_t; + +typedef struct scc_lexer_stream scc_lexer_stream_t; +struct scc_lexer_stream { + scc_lexer_t *lexer; + scc_lexer_tok_vec_t toks; // 循环缓冲区 + usize curr_pos; // 当前读取位置(逻辑位置) + usize probe_pos; // 已填充位置(逻辑位置) + cbool need_comment; + + /// @brief 向前读取n个token + const scc_lexer_tok_t *(*peek)(scc_lexer_stream_t *stream, usize n); + + /// @brief 指针推进到offset + void (*advance)(scc_lexer_stream_t *stream, usize offset); + + /// @brief 销毁并释放资源 + void (*drop)(scc_lexer_stream_t *stream); +}; + +/** + * @brief 将词法分析器转换成流式输出(自带缓冲区) + * @param[in] lexer 已经词法分析器实例 + * @param[out] stream 输出流对象指针 + * @param[in] need_comment 输出时是否需要注释 + */ +void scc_lexer_to_stream(scc_lexer_t *lexer, scc_lexer_stream_t *stream, + cbool need_comment); + +static inline const scc_lexer_tok_t * +scc_lexer_stream_current(scc_lexer_stream_t *stream) { + Assert(stream != null); + return stream->peek(stream, 0); +} + +static inline const scc_lexer_tok_t * +scc_lexer_stream_peek(scc_lexer_stream_t *stream, usize n) { + Assert(stream != null); + return stream->peek(stream, n); +} + +static inline void scc_lexer_stream_consume(scc_lexer_stream_t *stream) { + Assert(stream != null); + return stream->advance(stream, 1); +} + +static inline void scc_lexer_stream_advance(scc_lexer_stream_t *stream, + usize n) { + Assert(stream != null); + return stream->advance(stream, n); +} + +static inline void scc_lexer_stream_drop(scc_lexer_stream_t *stream) { + Assert(stream != null); + return stream->drop(stream); +} + #endif /* __SCC_LEXER_H__ */ diff --git a/libs/lexer/include/lexer_log.h b/libs/lexer/include/lexer_log.h index c1cc4c6..1cf23a0 100644 --- a/libs/lexer/include/lexer_log.h +++ b/libs/lexer/include/lexer_log.h @@ -1,48 +1,48 @@ -#ifndef __SMCC_LEXER_LOG_H__ -#define __SMCC_LEXER_LOG_H__ +#ifndef __SCC_LEXER_LOG_H__ +#define __SCC_LEXER_LOG_H__ -#include +#include #ifndef LEX_LOG_LEVEL #define LEX_LOG_LEVEL 4 #endif #if LEX_LOG_LEVEL <= 1 -#define LEX_NOTSET(fmt, ...) MLOG_NOTSET(&__smcc_lexer_log, fmt, ##__VA_ARGS__) +#define LEX_NOTSET(fmt, ...) MLOG_NOTSET(&__scc_lexer_log, fmt, ##__VA_ARGS__) #else #define LEX_NOTSET(fmt, ...) #endif #if LEX_LOG_LEVEL <= 2 -#define LEX_DEBUG(fmt, ...) MLOG_DEBUG(&__smcc_lexer_log, fmt, ##__VA_ARGS__) +#define LEX_DEBUG(fmt, ...) MLOG_DEBUG(&__scc_lexer_log, fmt, ##__VA_ARGS__) #else #define LEX_DEBUG(fmt, ...) #endif #if LEX_LOG_LEVEL <= 3 -#define LEX_INFO(fmt, ...) MLOG_INFO(&__smcc_lexer_log, fmt, ##__VA_ARGS__) +#define LEX_INFO(fmt, ...) MLOG_INFO(&__scc_lexer_log, fmt, ##__VA_ARGS__) #else #define LEX_INFO(fmt, ...) #endif #if LEX_LOG_LEVEL <= 4 -#define LEX_WARN(fmt, ...) MLOG_WARN(&__smcc_lexer_log, fmt, ##__VA_ARGS__) +#define LEX_WARN(fmt, ...) MLOG_WARN(&__scc_lexer_log, fmt, ##__VA_ARGS__) #else #define LEX_WARN(fmt, ...) #endif #if LEX_LOG_LEVEL <= 5 -#define LEX_ERROR(fmt, ...) MLOG_ERROR(&__smcc_lexer_log, fmt, ##__VA_ARGS__) +#define LEX_ERROR(fmt, ...) MLOG_ERROR(&__scc_lexer_log, fmt, ##__VA_ARGS__) #else #define LEX_ERROR(fmt, ...) #endif #if LEX_LOG_LEVEL <= 6 -#define LEX_FATAL(fmt, ...) MLOG_FATAL(&__smcc_lexer_log, fmt, ##__VA_ARGS__) +#define LEX_FATAL(fmt, ...) MLOG_FATAL(&__scc_lexer_log, fmt, ##__VA_ARGS__) #else #define LEX_FATAL(fmt, ...) #endif -extern logger_t __smcc_lexer_log; +extern logger_t __scc_lexer_log; -#endif // __SMCC_LEXER_LOG_H__ +#endif /* __SCC_LEXER_LOG_H__ */ diff --git a/libs/lexer/include/lexer_token.h b/libs/lexer/include/lexer_token.h index 9a6b138..0a52b5d 100644 --- a/libs/lexer/include/lexer_token.h +++ b/libs/lexer/include/lexer_token.h @@ -1,7 +1,7 @@ -#ifndef __SMCC_CC_TOKEN_H__ -#define __SMCC_CC_TOKEN_H__ +#ifndef __SCC_LEXER_TOKEN_H__ +#define __SCC_LEXER_TOKEN_H__ -#include +#include typedef enum scc_cstd { SCC_CSTD_C89, @@ -137,4 +137,24 @@ typedef enum scc_tok_subtype { scc_tok_subtype_t scc_get_tok_subtype(scc_tok_type_t type); const char *scc_get_tok_name(scc_tok_type_t type); -#endif +typedef struct scc_lexer_token { + scc_tok_type_t type; + scc_cvalue_t value; + scc_pos_t loc; +} scc_lexer_tok_t; + +static inline cbool scc_lexer_tok_match(const scc_lexer_tok_t *tok, + scc_tok_type_t type) { + return tok->type == type; +} + +static inline cbool scc_lexer_tok_expect(const scc_lexer_tok_t *tok, + scc_tok_type_t type) { + if (!scc_lexer_tok_match(tok, type)) { + LOG_ERROR("expected token %d, got %d\n", type, tok->type); + return false; + } + return true; +} + +#endif /* __SCC_LEXER_TOKEN_H__ */ diff --git a/libs/lexer/src/lexer.c b/libs/lexer/src/lexer.c index 4be17e0..96f4ab7 100644 --- a/libs/lexer/src/lexer.c +++ b/libs/lexer/src/lexer.c @@ -1,31 +1,3 @@ -/** - * 仿照LCCompiler的词法分析部分 - * - * 如下为LCC的README in 2025.2 -This hierarchy is the distribution for lcc version 4.2. - -lcc version 3.x is described in the book "A Retargetable C Compiler: -Design and Implementation" (Addison-Wesley, 1995, ISBN 0-8053-1670-1). -There are significant differences between 3.x and 4.x, most notably in -the intermediate code. For details, see -https://drh.github.io/lcc/documents/interface4.pdf. - -VERSION 4.2 IS INCOMPATIBLE WITH EARLIER VERSIONS OF LCC. DO NOT -UNLOAD THIS DISTRIBUTION ON TOP OF A 3.X DISTRIBUTION. - -LCC is a C89 ("ANSI C") compiler designed to be highly retargetable. - -LOG describes the changes since the last release. - -CPYRIGHT describes the conditions under you can use, copy, modify, and -distribute lcc or works derived from lcc. - -doc/install.html is an HTML file that gives a complete description of -the distribution and installation instructions. - -Chris Fraser / cwf@aya.yale.edu -David Hanson / drh@drhanson.net - */ #include #include #include @@ -329,33 +301,17 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) { break; } break; - case '[': - type = SCC_TOK_L_BRACKET; - break; - case ']': - type = SCC_TOK_R_BRACKET; - break; - case '(': - type = SCC_TOK_L_PAREN; - break; - case ')': - type = SCC_TOK_R_PAREN; - break; - case '{': - type = SCC_TOK_L_BRACE; - break; - case '}': - type = SCC_TOK_R_BRACE; - break; - case ';': - type = SCC_TOK_SEMICOLON; - break; - case ',': - type = SCC_TOK_COMMA; - break; - case ':': - type = SCC_TOK_COLON; - break; + /* clang-format off */ + case '[': type = SCC_TOK_L_BRACKET; break; + case ']': type = SCC_TOK_R_BRACKET; break; + case '(': type = SCC_TOK_L_PAREN; break; + case ')': type = SCC_TOK_R_PAREN; break; + case '{': type = SCC_TOK_L_BRACE; break; + case '}': type = SCC_TOK_R_BRACE; break; + case ';': type = SCC_TOK_SEMICOLON; break; + case ',': type = SCC_TOK_COMMA; break; + case ':': type = SCC_TOK_COLON; break; + /* clang-format on */ case '.': if (scc_probe_stream_next(stream) == '.' && scc_probe_stream_next(stream) == '.') { diff --git a/libs/lexer/src/lexer_log.c b/libs/lexer/src/lexer_log.c index d39a8f3..c6cff2d 100644 --- a/libs/lexer/src/lexer_log.c +++ b/libs/lexer/src/lexer_log.c @@ -1,6 +1,6 @@ #include -logger_t __smcc_lexer_log = { +logger_t __scc_lexer_log = { .name = "lexer", .level = LOG_LEVEL_ALL, .handler = log_default_handler, diff --git a/libs/lexer/src/lexer_stream.c b/libs/lexer/src/lexer_stream.c new file mode 100644 index 0000000..d05d830 --- /dev/null +++ b/libs/lexer/src/lexer_stream.c @@ -0,0 +1,138 @@ +#include + +static void lexer_stream_extend(scc_lexer_stream_t *stream, usize n) { + Assert(stream != null); + // 检查是否需要扩容 + if ((stream->probe_pos - stream->curr_pos + n) >= stream->toks.cap) { + // 需要扩容 - 创建新缓冲区 + usize new_cap = stream->toks.cap * 2; + if (new_cap < stream->probe_pos - stream->curr_pos + n + 1) { + new_cap = stream->probe_pos - stream->curr_pos + n + 1; + } + + scc_lexer_tok_t *new_data = + scc_realloc(null, new_cap * sizeof(scc_lexer_tok_t)); + if (!new_data) { + LOG_FATAL("lexer_stream_extend: realloc failed\n"); + } + + // 将旧缓冲区中的数据拷贝到新缓冲区,保持顺序 + usize data_count = stream->probe_pos - stream->curr_pos; + for (usize i = 0; i < data_count; ++i) { + usize old_idx = (stream->curr_pos + i) % stream->toks.cap; + new_data[i] = stream->toks.data[old_idx]; + } + + // 释放旧缓冲区 + if (stream->toks.data) { + scc_free(stream->toks.data); + } + + // 更新结构体 + stream->toks.data = new_data; + stream->toks.cap = new_cap; + stream->curr_pos = 0; + stream->probe_pos = data_count; + } + + // 填充新token + for (usize i = 0; i < n; ++i) { + usize idx = (stream->probe_pos + i) % stream->toks.cap; + if (stream->need_comment) + scc_lexer_get_token(stream->lexer, &stream->toks.data[idx]); + else + scc_lexer_get_valid_token(stream->lexer, &stream->toks.data[idx]); + } + + stream->probe_pos += n; +} + +static const scc_lexer_tok_t *lexer_stream_peek(scc_lexer_stream_t *stream, + usize n) { + Assert(stream != null); + + // 计算需要的前看token数量 + usize available = stream->probe_pos - stream->curr_pos; + if (n >= available) { + // 需要扩展缓冲区 + usize need = n - available + 1; + lexer_stream_extend(stream, need); + } + + // 计算实际缓冲区中的位置 + usize idx = (stream->curr_pos + n) % stream->toks.cap; + return &stream->toks.data[idx]; +} + +static void lexer_stream_advance(scc_lexer_stream_t *stream, usize offset) { + Assert(stream != null); + + if (stream->curr_pos + offset > stream->probe_pos) { + // 尝试填充更多token + usize need = stream->curr_pos + offset - stream->probe_pos; + lexer_stream_extend(stream, need); + } + + stream->curr_pos += offset; + + // 可选:当已消费的token过多时,压缩缓冲区 + if (stream->curr_pos > stream->toks.cap * 3 / 4) { + // 压缩缓冲区:将有效数据移动到前面 + usize data_count = stream->probe_pos - stream->curr_pos; + scc_lexer_tok_t *temp = + scc_realloc(null, data_count * sizeof(scc_lexer_tok_t)); + if (!temp) + return; // 压缩失败也没关系 + + for (usize i = 0; i < data_count; ++i) { + usize old_idx = (stream->curr_pos + i) % stream->toks.cap; + temp[i] = stream->toks.data[old_idx]; + } + + scc_free(stream->toks.data); + stream->toks.data = temp; + stream->toks.cap = data_count; + stream->curr_pos = 0; + stream->probe_pos = data_count; + } +} + +static void lexer_stream_drop(scc_lexer_stream_t *stream) { + Assert(stream != null); + + // 清理所有token(如果有需要清理的内部资源) + for (usize i = 0; i < stream->toks.cap; ++i) { + // 这里假设scc_lexer_tok_t可能包含需要释放的资源 + // 如果有,需要调用相应的清理函数 + // 例如: if (stream->toks.data[i].needs_free) + // scc_free(stream->toks.data[i].ptr); + } + + scc_vec_free(stream->toks); + stream->lexer = null; + stream->curr_pos = 0; + stream->probe_pos = 0; + stream->need_comment = false; + + stream->peek = null; + stream->advance = null; + stream->drop = null; +} + +void scc_lexer_to_stream(scc_lexer_t *lexer, scc_lexer_stream_t *stream, + cbool need_comment) { + Assert(lexer != null && stream != null); + + stream->lexer = lexer; + stream->curr_pos = 0; + stream->probe_pos = 0; + stream->need_comment = need_comment; + + // 初始化循环缓冲区 + scc_vec_init(stream->toks); + scc_vec_realloc(stream->toks, 8); // 初始容量为8 + + stream->peek = lexer_stream_peek; + stream->advance = lexer_stream_advance; + stream->drop = lexer_stream_drop; +} diff --git a/libs/lexer/tests/test_run.c b/libs/lexer/tests/test_run.c index f8c8b08..a73b8e1 100644 --- a/libs/lexer/tests/test_run.c +++ b/libs/lexer/tests/test_run.c @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) { log_set_level(NULL, LOG_LEVEL_ALL); } else { // FIXME it is a hack lexer_logger - log_set_level(&__smcc_lexer_log, LOG_LEVEL_NOTSET); + log_set_level(&__scc_lexer_log, LOG_LEVEL_NOTSET); log_set_level(NULL, LOG_LEVEL_INFO | LOG_LEVEL_WARN | LOG_LEVEL_ERROR | LOG_LEVEL_FATAL); } diff --git a/libs/pprocessor/cbuild.toml b/libs/pprocessor/cbuild.toml index d42e612..423480d 100644 --- a/libs/pprocessor/cbuild.toml +++ b/libs/pprocessor/cbuild.toml @@ -2,7 +2,7 @@ name = "scc_pprocesser" dependencies = [ - { name = "libcore", path = "../../runtime/libcore" }, - { name = "libutils", path = "../../runtime/libutils" }, + { name = "scc_core", path = "../../runtime/scc_core" }, + { name = "scc_utils", path = "../../runtime/scc_utils" }, { name = "lex_parser", path = "../lex_parser" }, ] diff --git a/libs/pprocessor/include/pp_macro.h b/libs/pprocessor/include/pp_macro.h index c119aea..1b547ba 100644 --- a/libs/pprocessor/include/pp_macro.h +++ b/libs/pprocessor/include/pp_macro.h @@ -1,8 +1,8 @@ #ifndef __SCC_PP_MACRO_H__ #define __SCC_PP_MACRO_H__ -#include -#include +#include +#include // 宏定义类型 typedef enum { diff --git a/libs/pprocessor/include/pp_parse.h b/libs/pprocessor/include/pp_parse.h index 2661f03..e9309ee 100644 --- a/libs/pprocessor/include/pp_parse.h +++ b/libs/pprocessor/include/pp_parse.h @@ -1,8 +1,8 @@ #ifndef __SCC_PP_PARSE_H__ #define __SCC_PP_PARSE_H__ -#include #include +#include void scc_pp_parse_directive(scc_probe_stream_t *stream, scc_pos_t *pos, scc_pp_macro_table_t *macros); cbool scc_pp_parse_macro_replace_list(scc_probe_stream_t *stream, diff --git a/libs/pprocessor/include/pprocessor.h b/libs/pprocessor/include/pprocessor.h index cd4e24b..b1d5ec1 100644 --- a/libs/pprocessor/include/pprocessor.h +++ b/libs/pprocessor/include/pprocessor.h @@ -6,9 +6,9 @@ #ifndef __SCC_PP_H__ #define __SCC_PP_H__ -#include -#include #include +#include +#include // 条件编译状态 typedef enum { @@ -59,4 +59,4 @@ typedef struct pp_stream { scc_probe_stream_t *tmp_stream; } scc_pp_stream_t; -#endif /* __SMC_PP_H__ */ +#endif /* __SCC_PP_H__ */ diff --git a/libs/pprocessor/src/expand_macro.c b/libs/pprocessor/src/expand_macro.c index 682453d..f57c053 100644 --- a/libs/pprocessor/src/expand_macro.c +++ b/libs/pprocessor/src/expand_macro.c @@ -1,8 +1,6 @@ #include -#include #include #include - static inline void scc_generate_cstr(scc_cstring_t *buff) { scc_cstring_t out_buff = scc_cstring_create(); scc_cstring_append_ch(&out_buff, '\"'); diff --git a/runtime/libcore/include/libcore.h b/runtime/libcore/include/libcore.h deleted file mode 100644 index 742051b..0000000 --- a/runtime/libcore/include/libcore.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef __SCC_CORE_H__ -#define __SCC_CORE_H__ - -#include - -#include -#include -#include -#include -#include -#include -#include - -#endif // __SCC_CORE_H__ diff --git a/runtime/libutils/cbuild.toml b/runtime/libutils/cbuild.toml deleted file mode 100644 index 8af1d0e..0000000 --- a/runtime/libutils/cbuild.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "libutils" -version = "0.1.0" - -dependencies = [ - { name = "core", path = "../libcore" } -] \ No newline at end of file diff --git a/runtime/libcore/cbuild.toml b/runtime/scc_core/cbuild.toml similarity index 92% rename from runtime/libcore/cbuild.toml rename to runtime/scc_core/cbuild.toml index 361b1c4..56bebed 100644 --- a/runtime/libcore/cbuild.toml +++ b/runtime/scc_core/cbuild.toml @@ -1,5 +1,5 @@ [package] -name = "libcore" +name = "scc_core" version = "0.1.0" default_features = ["std_impl"] diff --git a/runtime/scc_core/include/scc_core.h b/runtime/scc_core/include/scc_core.h new file mode 100644 index 0000000..59834f3 --- /dev/null +++ b/runtime/scc_core/include/scc_core.h @@ -0,0 +1,14 @@ +#ifndef __SCC_CORE_H__ +#define __SCC_CORE_H__ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#endif // __SCC_CORE_H__ diff --git a/runtime/libcore/include/core_impl.h b/runtime/scc_core/include/scc_core_impl.h similarity index 98% rename from runtime/libcore/include/core_impl.h rename to runtime/scc_core/include/scc_core_impl.h index f260a09..9e8bac8 100644 --- a/runtime/libcore/include/core_impl.h +++ b/runtime/scc_core/include/scc_core_impl.h @@ -1,7 +1,7 @@ #ifndef __SCC_CORE_IMPL_H__ #define __SCC_CORE_IMPL_H__ -#include "core_type.h" +#include "scc_core_type.h" /* ====== 内存管理核心接口 ====== */ diff --git a/runtime/libcore/include/core_log.h b/runtime/scc_core/include/scc_core_log.h similarity index 100% rename from runtime/libcore/include/core_log.h rename to runtime/scc_core/include/scc_core_log.h diff --git a/runtime/libcore/include/core_macro.h b/runtime/scc_core/include/scc_core_macro.h similarity index 100% rename from runtime/libcore/include/core_macro.h rename to runtime/scc_core/include/scc_core_macro.h diff --git a/runtime/libcore/include/core_mem.h b/runtime/scc_core/include/scc_core_mem.h similarity index 97% rename from runtime/libcore/include/core_mem.h rename to runtime/scc_core/include/scc_core_mem.h index 5dd59f1..cfd788a 100644 --- a/runtime/libcore/include/core_mem.h +++ b/runtime/scc_core/include/scc_core_mem.h @@ -1,7 +1,7 @@ #ifndef __SCC_CORE_MEM_H__ #define __SCC_CORE_MEM_H__ -#include "core_type.h" +#include "scc_core_type.h" void *scc_memcpy(void *dest, const void *src, usize n); void *scc_memmove(void *dest, const void *src, usize n); diff --git a/runtime/libcore/include/core_pos.h b/runtime/scc_core/include/scc_core_pos.h similarity index 90% rename from runtime/libcore/include/core_pos.h rename to runtime/scc_core/include/scc_core_pos.h index 0be3895..a8b8b47 100644 --- a/runtime/libcore/include/core_pos.h +++ b/runtime/scc_core/include/scc_core_pos.h @@ -1,8 +1,8 @@ #ifndef __SCC_CORE_POS_H__ #define __SCC_CORE_POS_H__ -#include "core_str.h" -#include "core_type.h" +#include "scc_core_str.h" +#include "scc_core_type.h" typedef struct scc_pos { scc_cstring_t name; usize line; diff --git a/runtime/libcore/include/core_str.h b/runtime/scc_core/include/scc_core_str.h similarity index 96% rename from runtime/libcore/include/core_str.h rename to runtime/scc_core/include/scc_core_str.h index 864a3ea..62ac199 100644 --- a/runtime/libcore/include/core_str.h +++ b/runtime/scc_core/include/scc_core_str.h @@ -1,9 +1,10 @@ #ifndef __SCC_CORE_STR_H__ #define __SCC_CORE_STR_H__ -#include "core_impl.h" -#include "core_log.h" -#include "core_type.h" +#include "scc_core_impl.h" +#include "scc_core_log.h" +#include "scc_core_mem.h" +#include "scc_core_type.h" /** * @brief 动态字符串结构体 @@ -68,7 +69,7 @@ static inline void scc_cstring_free(scc_cstring_t *str) { if (str == null) { return; } - if (str->cap != 0 && str->data != null) { + if (str->data != null) { scc_free(str->data); str->data = null; } @@ -188,14 +189,14 @@ static inline void scc_cstring_clear(scc_cstring_t *str) { */ static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) { if (str == null || str->data == null) { - return ""; + return null; } return str->data; } static inline char *scc_cstring_move_cstr(scc_cstring_t *str) { if (str == null || str->data == null) { - return ""; + return null; } char *ret = str->data; str->data = null; diff --git a/runtime/libcore/include/core_stream.h b/runtime/scc_core/include/scc_core_stream.h similarity index 97% rename from runtime/libcore/include/core_stream.h rename to runtime/scc_core/include/scc_core_stream.h index 06a46b5..ccf145d 100644 --- a/runtime/libcore/include/core_stream.h +++ b/runtime/scc_core/include/scc_core_stream.h @@ -1,10 +1,10 @@ #ifndef __SMCC_CORE_PROBE_STREAM_H__ #define __SMCC_CORE_PROBE_STREAM_H__ -#include "core_impl.h" -#include "core_macro.h" -#include "core_mem.h" -#include "core_str.h" +#include "scc_core_impl.h" +#include "scc_core_macro.h" +#include "scc_core_mem.h" +#include "scc_core_str.h" struct scc_probe_stream; typedef struct scc_probe_stream scc_probe_stream_t; diff --git a/runtime/libcore/include/core_type.h b/runtime/scc_core/include/scc_core_type.h similarity index 100% rename from runtime/libcore/include/core_type.h rename to runtime/scc_core/include/scc_core_type.h diff --git a/runtime/libcore/include/core_vec.h b/runtime/scc_core/include/scc_core_vec.h similarity index 71% rename from runtime/libcore/include/core_vec.h rename to runtime/scc_core/include/scc_core_vec.h index 75ebdf8..952963a 100644 --- a/runtime/libcore/include/core_vec.h +++ b/runtime/scc_core/include/scc_core_vec.h @@ -8,11 +8,34 @@ #ifndef __SCC_CORE_VEC_H__ #define __SCC_CORE_VEC_H__ -#include "core_impl.h" -#include "core_type.h" +#ifndef __SCC_CORE_VEC_USE_STD__ +#include "scc_core_impl.h" +#include "scc_core_type.h" #define __scc_vec_realloc scc_realloc #define __scc_vec_free scc_free +#else +#include +#include + +typedef size_t usize; +#define __scc_vec_realloc realloc +#define __scc_vec_free free + +#ifndef LOG_FATAL +#include +#define LOG_FATAL(...) \ + do { \ + printf(__VA_ARGS__); \ + exit(1); \ + } while (0) +#endif + +#ifndef Assert +#include +#define Assert(cond) assert(cond) +#endif +#endif /** @defgroup vec_struct 数据结构定义 */ @@ -51,6 +74,22 @@ (vec).size = 0, (vec).cap = 0, (vec).data = 0; \ } while (0) +#define scc_vec_realloc(vec, new_cap) \ + do { \ + void *data = \ + __scc_vec_realloc((vec).data, new_cap * sizeof(*(vec).data)); \ + if (!data) { \ + LOG_FATAL("vector_push: realloc failed\n"); \ + } \ + (vec).cap = new_cap; \ + (vec).data = data; \ + } while (0) + +#define scc_vec_size(vec) ((vec).size) +#define scc_vec_cap(vec) ((vec).cap) +#define scc_vec_foreach(vec, idx) \ + for (usize idx = 0; idx < scc_vec_size(vec); ++idx) + /** * @def scc_vec_push(vec, value) * @brief 添加元素到向量末尾 @@ -64,13 +103,7 @@ do { \ if ((vec).size >= (vec).cap) { \ int cap = (vec).cap ? (vec).cap * 2 : 4; \ - void *data = \ - __scc_vec_realloc((vec).data, cap * sizeof(*(vec).data)); \ - if (!data) { \ - LOG_FATAL("vector_push: realloc failed\n"); \ - } \ - (vec).cap = cap; \ - (vec).data = data; \ + scc_vec_realloc(vec, cap); \ } \ Assert((vec).data != null); \ (vec).data[(vec).size++] = value; \ diff --git a/runtime/libcore/include/utest/acutest.h b/runtime/scc_core/include/utest/acutest.h similarity index 100% rename from runtime/libcore/include/utest/acutest.h rename to runtime/scc_core/include/utest/acutest.h diff --git a/runtime/libcore/src/cfg.std_impl.c b/runtime/scc_core/src/cfg.std_impl.c similarity index 98% rename from runtime/libcore/src/cfg.std_impl.c rename to runtime/scc_core/src/cfg.std_impl.c index b1cf660..cbdd690 100644 --- a/runtime/libcore/src/cfg.std_impl.c +++ b/runtime/scc_core/src/cfg.std_impl.c @@ -2,7 +2,7 @@ #define _CRT_SECURE_NO_WARNINGS #endif -#include +#include #define __SCC_LOG_IMPORT_SRC__ #define log_snprintf scc_snprintf #define log_printf scc_printf diff --git a/runtime/libcore/src/memory.c b/runtime/scc_core/src/memory.c similarity index 99% rename from runtime/libcore/src/memory.c rename to runtime/scc_core/src/memory.c index 0654c43..980ae9c 100644 --- a/runtime/libcore/src/memory.c +++ b/runtime/scc_core/src/memory.c @@ -1,4 +1,4 @@ -#include +#include // 判断是否支持非对齐访问(x86/x64 支持) #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \ diff --git a/runtime/libcore/src/stream.c b/runtime/scc_core/src/stream.c similarity index 99% rename from runtime/libcore/src/stream.c rename to runtime/scc_core/src/stream.c index 8159807..571895a 100644 --- a/runtime/libcore/src/stream.c +++ b/runtime/scc_core/src/stream.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #ifndef __SCC_CORE_NO_MEM_PROBE_STREAM__ diff --git a/runtime/libcore/tests/test_log.c b/runtime/scc_core/tests/test_log.c similarity index 100% rename from runtime/libcore/tests/test_log.c rename to runtime/scc_core/tests/test_log.c diff --git a/runtime/scc_utils/cbuild.toml b/runtime/scc_utils/cbuild.toml new file mode 100644 index 0000000..51a9988 --- /dev/null +++ b/runtime/scc_utils/cbuild.toml @@ -0,0 +1,5 @@ +[package] +name = "scc_utils" +version = "0.1.0" + +dependencies = [{ name = "core", path = "../scc_core" }] diff --git a/runtime/libutils/include/kllist.h b/runtime/scc_utils/include/kllist.h similarity index 100% rename from runtime/libutils/include/kllist.h rename to runtime/scc_utils/include/kllist.h diff --git a/runtime/libutils/include/hashtable.h b/runtime/scc_utils/include/scc_hashtable.h similarity index 99% rename from runtime/libutils/include/hashtable.h rename to runtime/scc_utils/include/scc_hashtable.h index 594c9ac..7de94c2 100644 --- a/runtime/libutils/include/hashtable.h +++ b/runtime/scc_utils/include/scc_hashtable.h @@ -8,7 +8,7 @@ #ifndef __SCC_HASHTABLE_H__ #define __SCC_HASHTABLE_H__ -#include +#include /** * @enum hp_entry_state_t diff --git a/runtime/libutils/include/strpool.h b/runtime/scc_utils/include/scc_strpool.h similarity index 97% rename from runtime/libutils/include/strpool.h rename to runtime/scc_utils/include/scc_strpool.h index 7f00555..e341ac0 100644 --- a/runtime/libutils/include/strpool.h +++ b/runtime/scc_utils/include/scc_strpool.h @@ -8,8 +8,8 @@ #ifndef __SCC_STRPOOL_H__ #define __SCC_STRPOOL_H__ -#include "hashtable.h" -#include +#include "scc_hashtable.h" +#include /** * @struct strpool_t diff --git a/runtime/libutils/include/libutils.h b/runtime/scc_utils/include/scc_utils.h similarity index 57% rename from runtime/libutils/include/libutils.h rename to runtime/scc_utils/include/scc_utils.h index 0ef7256..32a0a6d 100644 --- a/runtime/libutils/include/libutils.h +++ b/runtime/scc_utils/include/scc_utils.h @@ -1,9 +1,9 @@ #ifndef __SMCC_UTILS_H__ #define __SMCC_UTILS_H__ -#include "hashtable.h" #include "kllist.h" -#include "strpool.h" -#include +#include "scc_hashtable.h" +#include "scc_strpool.h" +#include #endif /* __SMCC_UTILS_H__ */ diff --git a/runtime/libutils/src/hashtable.c b/runtime/scc_utils/src/hashtable.c similarity index 99% rename from runtime/libutils/src/hashtable.c rename to runtime/scc_utils/src/hashtable.c index bb2f0ed..817334c 100644 --- a/runtime/libutils/src/hashtable.c +++ b/runtime/scc_utils/src/hashtable.c @@ -1,4 +1,4 @@ -#include +#include #ifndef SCC_INIT_HASHMAP_SIZE #define SCC_INIT_HASHMAP_SIZE (32) diff --git a/runtime/libutils/src/strpool.c b/runtime/scc_utils/src/strpool.c similarity index 97% rename from runtime/libutils/src/strpool.c rename to runtime/scc_utils/src/strpool.c index 7bcc938..b82a9b9 100644 --- a/runtime/libutils/src/strpool.c +++ b/runtime/scc_utils/src/strpool.c @@ -1,4 +1,4 @@ -#include "strpool.h" +#include void scc_strpool_init(scc_strpool_t *pool) { pool->ht.hash_func = (u32 (*)(const void *))scc_strhash32;