feat(core): 重构词法分析器流接口并迁移至 core 库
将 lexer_stream 抽象为 core_stream,统一运行时核心组件的输入流模型。 移除了旧的 `lexer_stream.h` 定义,并将其功能完整迁移至 `core_stream.h` 中。 更新了内存流实现以适配新的 core_stream 接口,并修复部分资源释放问题。 同时调整日志模块包含方式,增强模块间解耦能力。 此变更影响词法分析器对输入流的操作方式,所有涉及 stream 的类型与函数均已替换为 core 前缀版本。 测试用例同步更新并验证通过。
This commit is contained in:
18
runtime/libcore/include/core_log.h
Normal file
18
runtime/libcore/include/core_log.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef __SMCC_CORE_LOG_H__
|
||||
#define __SMCC_CORE_LOG_H__
|
||||
|
||||
#ifndef log_snprintf
|
||||
#define log_snprintf smcc_snprintf
|
||||
#endif
|
||||
|
||||
#ifndef log_printf
|
||||
#define log_printf smcc_printf
|
||||
#endif
|
||||
|
||||
#ifndef log_exit
|
||||
#define log_exit smcc_exit
|
||||
#endif
|
||||
#include <log.h>
|
||||
|
||||
|
||||
#endif /* __SMCC_CORE_LOG_H__ */
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "core_type.h"
|
||||
#include "core_impl.h"
|
||||
#include "log.h"
|
||||
#include "core_log.h"
|
||||
|
||||
typedef struct cstring {
|
||||
char* data;
|
||||
@@ -37,16 +37,16 @@ static inline cstring_t cstring_from_cstr(const char* s) {
|
||||
if (s == null) {
|
||||
return cstring_new();
|
||||
}
|
||||
|
||||
|
||||
usize len = 0;
|
||||
const char* p = s;
|
||||
while (*p++) len++;
|
||||
|
||||
|
||||
char* data = (char*)smcc_malloc(len + 1);
|
||||
Assert(data != null);
|
||||
smcc_memcpy(data, s, len);
|
||||
data[len] = '\0';
|
||||
|
||||
|
||||
return (cstring_t) { .data = data, .len = len, .cap = len };
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ static inline cstring_t cstring_from_cstr(const char* s) {
|
||||
* 释放字符串资源
|
||||
*/
|
||||
static inline void cstring_free(cstring_t* str) {
|
||||
if (str && str->data) {
|
||||
if (str && str->data && str->cap != 0) {
|
||||
smcc_free(str->data);
|
||||
str->data = null;
|
||||
str->len = 0;
|
||||
@@ -65,11 +65,11 @@ static inline void cstring_free(cstring_t* str) {
|
||||
/**
|
||||
* 向字符串追加内容
|
||||
*/
|
||||
static inline void cstring_push_str(cstring_t* str, const char* data, usize len) {
|
||||
static inline void cstring_push_cstr(cstring_t* str, const char* data, usize len) {
|
||||
if (str == null || data == null || len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 如果需要扩容
|
||||
if (str->len + len + 1 > str->cap) {
|
||||
// FIXME c string 兼容性问题 bad practice a lot of `+ 1`
|
||||
@@ -81,7 +81,7 @@ static inline void cstring_push_str(cstring_t* str, const char* data, usize len)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char* new_data = str->data ?
|
||||
(char*)smcc_realloc(str->data, new_cap) :
|
||||
(char*)smcc_malloc(new_cap);
|
||||
@@ -90,7 +90,7 @@ static inline void cstring_push_str(cstring_t* str, const char* data, usize len)
|
||||
str->data = new_data;
|
||||
str->cap = new_cap;
|
||||
}
|
||||
|
||||
|
||||
smcc_memcpy(str->data + str->len, data, len);
|
||||
str->len += len;
|
||||
str->data[str->len] = '\0'; // 保证 C 字符串兼容性
|
||||
@@ -100,7 +100,7 @@ static inline void cstring_push_str(cstring_t* str, const char* data, usize len)
|
||||
* 向字符串追加单个字符
|
||||
*/
|
||||
static inline void cstring_push(cstring_t* str, char ch) {
|
||||
cstring_push_str(str, &ch, 1);
|
||||
cstring_push_cstr(str, &ch, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
65
runtime/libcore/include/core_stream.h
Normal file
65
runtime/libcore/include/core_stream.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef __SMCC_CORE_STREAM_H__
|
||||
#define __SMCC_CORE_STREAM_H__
|
||||
|
||||
#include "core_impl.h"
|
||||
#include "core_mem.h"
|
||||
#include "core_str.h"
|
||||
#include "core_macro.h"
|
||||
|
||||
typedef struct core_stream core_stream_t;
|
||||
|
||||
#define core_stream_eof (-1)
|
||||
|
||||
struct core_stream {
|
||||
cstring_t name;
|
||||
|
||||
/// @brief 读取指定数量的字符到缓冲区
|
||||
usize (*read_buf)(core_stream_t* stream, char* buffer, usize count);
|
||||
|
||||
/// @brief 获取下一个字符
|
||||
int (*peek_char)(core_stream_t* stream);
|
||||
|
||||
/// @brief 重置字符流位置
|
||||
void (*reset_char) (core_stream_t* stream);
|
||||
|
||||
/// @brief 读取并消费下一个字符(移动流位置)
|
||||
int (*next_char)(core_stream_t* stream);
|
||||
|
||||
/// @brief 释放资源
|
||||
void (*free_stream) (core_stream_t* steam);
|
||||
};
|
||||
|
||||
static inline usize core_stream_read_buf(core_stream_t* self, char* buffer, usize count) {
|
||||
return self->read_buf(self, buffer, count);
|
||||
}
|
||||
|
||||
static inline int core_stream_peek_char(core_stream_t* self) {
|
||||
return self->peek_char(self);
|
||||
}
|
||||
|
||||
static inline void core_stream_reset_char(core_stream_t* self) {
|
||||
self->reset_char(self);
|
||||
}
|
||||
|
||||
static inline int core_stream_next_char(core_stream_t* self) {
|
||||
return self->next_char(self);
|
||||
}
|
||||
|
||||
static inline void core_stream_free_stream(core_stream_t* self) {
|
||||
self->free_stream(self);
|
||||
}
|
||||
|
||||
#ifndef __SMCC_CORE_NO_MEM_STREAM__
|
||||
typedef struct core_mem_stream {
|
||||
core_stream_t stream;
|
||||
const char* data;
|
||||
usize data_length;
|
||||
usize curr_pos;
|
||||
usize peek_pos;
|
||||
cbool owned;
|
||||
} core_mem_stream_t;
|
||||
core_stream_t* core_mem_stream_init(core_mem_stream_t* stream, const char* data, usize length, cbool need_copy);
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* __SMCC_CORE_STREAM_H__ */
|
||||
@@ -16,5 +16,6 @@
|
||||
|
||||
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
#include <core_str.h>
|
||||
#include <core_stream.h>
|
||||
|
||||
#endif // __SMCC_CORE_H__
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
#ifdef _MSC_VER
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include <core_impl.h>
|
||||
#define __SMCC_LOG_IMPORT_SRC__
|
||||
#define log_snprintf smcc_snprintf
|
||||
#define log_printf smcc_printf
|
||||
#define log_exit smcc_exit
|
||||
#include <log.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/* ====== 内存管理核心接口实现 ====== */
|
||||
|
||||
void* smcc_malloc(usize size) {
|
||||
|
||||
101
runtime/libcore/src/stream.c
Normal file
101
runtime/libcore/src/stream.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user