feat(core): 重构词法分析器流接口并迁移至 core 库

将 lexer_stream 抽象为 core_stream,统一运行时核心组件的输入流模型。
移除了旧的 `lexer_stream.h` 定义,并将其功能完整迁移至 `core_stream.h` 中。
更新了内存流实现以适配新的 core_stream 接口,并修复部分资源释放问题。
同时调整日志模块包含方式,增强模块间解耦能力。

此变更影响词法分析器对输入流的操作方式,所有涉及 stream 的类型与函数均已替换为 core 前缀版本。
测试用例同步更新并验证通过。
This commit is contained in:
zzy
2025-11-20 14:17:03 +08:00
parent 5c24f35c87
commit 47b56d52f6
13 changed files with 161 additions and 99 deletions

View File

@@ -0,0 +1,101 @@
#include <core_log.h>
#include <core_stream.h>
// 内存流的具体实现结构
static usize read_buf(core_stream_t* _stream, char* buffer, usize count) {
Assert(buffer != null && buffer != null);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
usize remaining = stream->data_length - stream->curr_pos;
usize to_read = (remaining < count) ? remaining : count;
if (to_read > 0) {
smcc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
stream->curr_pos += to_read;
} else {
LOG_WARN("Reading past end of stream [maybe count is too large or negative?]");
}
return to_read;
}
static int peek_char(core_stream_t* _stream) {
Assert(_stream != null);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
// 如果已经到达末尾返回EOF
if (stream->peek_pos >= stream->data_length) {
return core_stream_eof; // EOF
}
return (int)(unsigned char)stream->data[stream->peek_pos++];
}
static int next_char(core_stream_t* _stream) {
Assert(_stream != NULL);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
// 如果已经到达末尾返回EOF
if (stream->curr_pos >= stream->data_length) {
return core_stream_eof; // EOF
}
unsigned char ch = stream->data[stream->curr_pos++];
if (stream->peek_pos < stream->curr_pos) {
stream->peek_pos = stream->curr_pos;
}
return (int)ch;
}
static void reset_char(core_stream_t* _stream) {
Assert(_stream != NULL);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
stream->peek_pos = stream->curr_pos;
}
static void free_stream(core_stream_t* _stream) {
Assert(_stream != null);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
// FIXME maybe double free?
cstring_free(&stream->stream.name);
if (stream->owned) {
smcc_free((void*)stream->data);
}
}
core_stream_t* core_mem_stream_init(core_mem_stream_t* stream, const char* data, usize length, cbool need_copy) {
if (stream == null || data == NULL || length == 0) {
LOG_ERROR("param error");
return null;
}
stream->owned = need_copy;
if (need_copy) {
char* buf = (char*)smcc_malloc(length);
if (buf == null) {
LOG_ERROR("malloc error");
return null;
}
smcc_memcpy(buf, data, length);
stream->data = buf;
} else {
stream->data = data;
}
stream->data_length = length;
stream->curr_pos = 0;
stream->peek_pos = 0;
stream->stream.name = cstring_from_cstr("mem_stream");
stream->stream.read_buf = read_buf;
stream->stream.peek_char = peek_char;
stream->stream.next_char = next_char;
stream->stream.reset_char = reset_char;
stream->stream.free_stream = free_stream;
return (void*)stream;
}