refactor(lex_parser): 重命名libcore为scc_core并重构头文件包含

- 将依赖项从libcore重命名为scc_core
- 更新头文件包含路径从<libcore.h>到<scc_core.h>
- 保持原有功能不变

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
- 更新头文件包含从<libcore.h>到<scc_core.h>

refactor(lexer_token): 重新组织token头文件结构

- 将头文件保护宏从__SMCC_CC_TOKEN_H__改为__SCC_LEXER_TOKEN_H__
- 更新头文件包含从<libcore.h>到<scc_core.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中的宏定义以支持标准库模式
This commit is contained in:
zzy
2026-01-08 11:22:27 +08:00
parent 09f4ac8de0
commit b753ae0911
40 changed files with 345 additions and 150 deletions

View File

@@ -0,0 +1,99 @@
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <scc_core_impl.h>
#define __SCC_LOG_IMPORT_SRC__
#define log_snprintf scc_snprintf
#define log_printf scc_printf
#define log_exit scc_exit
#include <log.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ====== 内存管理核心接口实现 ====== */
void *scc_malloc(usize size) { return malloc(size); }
void *scc_calloc(usize count, usize size) { return calloc(count, size); }
void *scc_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); }
void scc_free(void *ptr) { free(ptr); }
/* ====== 文件系统核心接口实现 ====== */
static const char *get_file_mode_string(scc_fmode_t mode) {
switch (mode) {
case SCC_FILE_READ:
return "rb";
case SCC_FILE_WRITE:
return "wb";
case SCC_FILE_APPEND:
return "ab";
default:
return "rb";
}
}
scc_file_t scc_fopen(const char *path, scc_fmode_t mode) {
const char *mode_str = get_file_mode_string(mode);
return (scc_file_t)fopen(path, mode_str);
}
void scc_fclose(scc_file_t file) {
if (file) {
fclose((FILE *)file);
}
}
usize scc_fread(scc_file_t file, void *buffer, usize size) {
if (!file || !buffer)
return 0;
return fread(buffer, 1, size, (FILE *)file);
}
usize scc_fwrite(scc_file_t file, const void *buffer, usize size) {
if (!file || !buffer)
return 0;
return fwrite(buffer, 1, size, (FILE *)file);
}
cbool scc_fexists(const char *path) {
scc_file_t fp = scc_fopen(path, SCC_FILE_READ);
if (!fp)
return false;
scc_fclose(fp);
return true;
}
/* ====== 输入输出核心接口实现 ====== */
void scc_snprintf(char *buf, usize size, const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(buf, size, format, args); // NOLINT
va_end(args);
}
void scc_printf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
void scc_eprintf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
/* ====== 系统核心接口实现 ====== */
void scc_exit(int code) { exit(code); }

View File

@@ -0,0 +1,353 @@
#include <scc_core_mem.h>
// 判断是否支持非对齐访问x86/x64 支持)
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
defined(_M_X64)
#define UNALIGNED_ACCESS_ALLOWED 1
#else
#define UNALIGNED_ACCESS_ALLOWED 0
#endif
void *scc_memcpy(void *dest, const void *restrict src, usize n) {
char *d = (char *)dest;
const char *s = (const char *)src;
// 快速路径:小内存拷贝
if (n <= 16) {
switch (n) {
case 16:
d[15] = s[15]; /* fall through */
case 15:
d[14] = s[14]; /* fall through */
case 14:
d[13] = s[13]; /* fall through */
case 13:
d[12] = s[12]; /* fall through */
case 12:
d[11] = s[11]; /* fall through */
case 11:
d[10] = s[10]; /* fall through */
case 10:
d[9] = s[9]; /* fall through */
case 9:
d[8] = s[8]; /* fall through */
case 8:
d[7] = s[7]; /* fall through */
case 7:
d[6] = s[6]; /* fall through */
case 6:
d[5] = s[5]; /* fall through */
case 5:
d[4] = s[4]; /* fall through */
case 4:
d[3] = s[3]; /* fall through */
case 3:
d[2] = s[2]; /* fall through */
case 2:
d[1] = s[1]; /* fall through */
case 1:
d[0] = s[0]; /* fall through */
default:
break;
}
return dest;
}
#if UNALIGNED_ACCESS_ALLOWED
// 按8字节批量复制适用于支持非对齐访问的平台
uint64_t *d64 = (uint64_t *)d;
const uint64_t *s64 = (const uint64_t *)s;
while (n >= 8) {
*d64++ = *s64++;
n -= 8;
}
d = (char *)d64;
s = (const char *)s64;
#endif
// 处理剩余字节
while (n--) {
*d++ = *s++;
}
return dest;
}
void *scc_memmove(void *dest, const void *src, usize n) {
char *d = (char *)dest;
const char *s = (const char *)src;
// 地址相同直接返回
if (d == s) {
return dest;
}
// 内存区域无重叠或前向拷贝
if (d < s || d >= s + n) {
return scc_memcpy(d, s, n);
} else {
// 后向拷贝处理重叠情况
d += n;
s += n;
while (n--) {
*(--d) = *(--s);
}
}
return dest;
}
void *scc_memset(void *s, int c, usize n) {
unsigned char *p = (unsigned char *)s;
unsigned char byte_val = (unsigned char)c;
// 快速设置小块内存
if (n <= 16) {
switch (n) {
case 16:
p[15] = byte_val; /* fall through */
case 15:
p[14] = byte_val; /* fall through */
case 14:
p[13] = byte_val; /* fall through */
case 13:
p[12] = byte_val; /* fall through */
case 12:
p[11] = byte_val; /* fall through */
case 11:
p[10] = byte_val; /* fall through */
case 10:
p[9] = byte_val; /* fall through */
case 9:
p[8] = byte_val; /* fall through */
case 8:
p[7] = byte_val; /* fall through */
case 7:
p[6] = byte_val; /* fall through */
case 6:
p[5] = byte_val; /* fall through */
case 5:
p[4] = byte_val; /* fall through */
case 4:
p[3] = byte_val; /* fall through */
case 3:
p[2] = byte_val; /* fall through */
case 2:
p[1] = byte_val; /* fall through */
case 1:
p[0] = byte_val; /* fall through */
default:
break;
}
return s;
}
#if UNALIGNED_ACCESS_ALLOWED
// 构造一个8字节值用于批量填充
uint64_t fill_val =
((uint64_t)byte_val << 56) | ((uint64_t)byte_val << 48) |
((uint64_t)byte_val << 40) | ((uint64_t)byte_val << 32) |
((uint64_t)byte_val << 24) | ((uint64_t)byte_val << 16) |
((uint64_t)byte_val << 8) | (uint64_t)byte_val;
uint64_t *p64 = (uint64_t *)p;
while (n >= 8) {
*p64++ = fill_val;
n -= 8;
}
p = (unsigned char *)p64;
#endif
// 设置剩余字节
while (n--) {
*p++ = byte_val;
}
return s;
}
int scc_memcmp(const void *s1, const void *s2, usize n) {
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;
// 快速比较小块内存
if (n <= 16) {
unsigned char diff = 0;
switch (n) {
case 16:
diff |= (p1[15] ^ p2[15]); /* fall through */
case 15:
diff |= (p1[14] ^ p2[14]); /* fall through */
case 14:
diff |= (p1[13] ^ p2[13]); /* fall through */
case 13:
diff |= (p1[12] ^ p2[12]); /* fall through */
case 12:
diff |= (p1[11] ^ p2[11]); /* fall through */
case 11:
diff |= (p1[10] ^ p2[10]); /* fall through */
case 10:
diff |= (p1[9] ^ p2[9]); /* fall through */
case 9:
diff |= (p1[8] ^ p2[8]); /* fall through */
case 8:
diff |= (p1[7] ^ p2[7]); /* fall through */
case 7:
diff |= (p1[6] ^ p2[6]); /* fall through */
case 6:
diff |= (p1[5] ^ p2[5]); /* fall through */
case 5:
diff |= (p1[4] ^ p2[4]); /* fall through */
case 4:
diff |= (p1[3] ^ p2[3]); /* fall through */
case 3:
diff |= (p1[2] ^ p2[2]); /* fall through */
case 2:
diff |= (p1[1] ^ p2[1]); /* fall through */
case 1:
diff |= (p1[0] ^ p2[0]); /* fall through */
default:
break;
}
// 只有当所有字节都相等时diff才为0
if (!diff)
return 0;
// 找到第一个不同的字节并返回差值
size_t i = 0;
switch (n) {
case 16:
if (p1[15] != p2[15]) {
i = 15;
break;
}
/* fall through */
case 15:
if (p1[14] != p2[14]) {
i = 14;
break;
}
/* fall through */
case 14:
if (p1[13] != p2[13]) {
i = 13;
break;
}
/* fall through */
case 13:
if (p1[12] != p2[12]) {
i = 12;
break;
}
/* fall through */
case 12:
if (p1[11] != p2[11]) {
i = 11;
break;
}
/* fall through */
case 11:
if (p1[10] != p2[10]) {
i = 10;
break;
}
/* fall through */
case 10:
if (p1[9] != p2[9]) {
i = 9;
break;
}
/* fall through */
case 9:
if (p1[8] != p2[8]) {
i = 8;
break;
}
/* fall through */
case 8:
if (p1[7] != p2[7]) {
i = 7;
break;
}
/* fall through */
case 7:
if (p1[6] != p2[6]) {
i = 6;
break;
}
/* fall through */
case 6:
if (p1[5] != p2[5]) {
i = 5;
break;
}
/* fall through */
case 5:
if (p1[4] != p2[4]) {
i = 4;
break;
}
/* fall through */
case 4:
if (p1[3] != p2[3]) {
i = 3;
break;
}
/* fall through */
case 3:
if (p1[2] != p2[2]) {
i = 2;
break;
}
/* fall through */
case 2:
if (p1[1] != p2[1]) {
i = 1;
break;
}
/* fall through */
case 1:
if (p1[0] != p2[0]) {
i = 0;
break;
}
/* fall through */
default:
break;
}
return p1[i] - p2[i];
}
#if UNALIGNED_ACCESS_ALLOWED
// 按8字节批量比较
const uint64_t *p1_64 = (const uint64_t *)p1;
const uint64_t *p2_64 = (const uint64_t *)p2;
while (n >= 8) {
if (*p1_64 != *p2_64) {
// 发现不同在8字节内定位具体位置
const unsigned char *b1 = (const unsigned char *)p1_64;
const unsigned char *b2 = (const unsigned char *)p2_64;
for (int j = 0; j < 8; j++) {
if (b1[j] != b2[j])
return b1[j] - b2[j];
}
}
p1_64++;
p2_64++;
n -= 8;
}
p1 = (const unsigned char *)p1_64;
p2 = (const unsigned char *)p2_64;
#endif
// 比较剩余字节
while (n--) {
if (*p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}

View File

@@ -0,0 +1,182 @@
#include <scc_core_log.h>
#include <scc_core_stream.h>
#ifndef __SCC_CORE_NO_MEM_PROBE_STREAM__
static int mem_probe_stream_consume(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->curr_pos >= stream->data_length) {
return scc_stream_eof;
}
unsigned char ch = stream->data[stream->curr_pos++];
// 如果探针位置落后于当前读取位置,则更新探针位置
if (stream->probe_pos < stream->curr_pos) {
stream->probe_pos = stream->curr_pos;
}
return (int)ch;
}
static int mem_probe_stream_peek(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return scc_stream_eof;
}
// 只查看而不移动探针位置
return (int)(unsigned char)stream->data[stream->probe_pos];
}
static int mem_probe_stream_next(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return scc_stream_eof;
}
// 返回探针位置的字符,并将探针位置向前移动
int ch = (int)(unsigned char)stream->data[stream->probe_pos];
stream->probe_pos++;
return ch;
}
static void mem_probe_stream_sync(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 移动头指针到探针位置(消费已查看的字符)
if (stream->probe_pos > stream->curr_pos) {
stream->curr_pos = stream->probe_pos;
}
}
static cbool mem_probe_stream_back(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 只能回退一个字符,且不能回退到探针位置之前
if (stream->curr_pos == 0 || stream->curr_pos <= stream->probe_pos) {
return false;
}
stream->curr_pos--;
return true;
}
static usize mem_probe_stream_read_buf(scc_probe_stream_t *_stream,
char *buffer, usize count) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (buffer == null) {
LOG_WARN("Buffer is null");
return 0;
}
usize remaining = stream->data_length - stream->curr_pos;
usize to_read = (remaining < count) ? remaining : count;
if (to_read > 0) {
scc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
stream->curr_pos += to_read;
// 更新探针位置
if (stream->probe_pos < stream->curr_pos) {
stream->probe_pos = stream->curr_pos;
}
} else {
LOG_WARN("Reading past end of stream [maybe count is too large or "
"negative?]");
}
return to_read;
}
static void mem_probe_stream_reset(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 重置探针位置到头指针位置
stream->probe_pos = stream->curr_pos;
}
static cbool mem_probe_stream_is_at_end(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
return stream->curr_pos >= stream->data_length;
}
static void mem_probe_stream_drop(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
scc_cstring_free(&stream->stream.name);
if (stream->owned) {
scc_free((void *)stream->data);
stream->data = null;
}
}
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
const char *data, usize length,
cbool owned) {
if (stream == null || data == null) {
LOG_ERROR("param error");
return null;
}
if (length == 0) {
LOG_WARN("input memory is empty");
owned = false;
}
stream->owned = owned;
stream->data = data;
stream->data_length = length;
stream->curr_pos = 0;
stream->probe_pos = 0;
stream->stream.name = scc_cstring_from_cstr("mem_probe_stream");
// 设置函数指针
stream->stream.consume = mem_probe_stream_consume;
stream->stream.peek = mem_probe_stream_peek;
stream->stream.next = mem_probe_stream_next;
stream->stream.sync = mem_probe_stream_sync;
stream->stream.back = mem_probe_stream_back;
stream->stream.read_buf = mem_probe_stream_read_buf;
stream->stream.reset = mem_probe_stream_reset;
stream->stream.is_at_end = mem_probe_stream_is_at_end;
stream->stream.drop = mem_probe_stream_drop;
return (scc_probe_stream_t *)stream;
}
static void scc_owned_mem_stream_drop(scc_probe_stream_t *_stream) {
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
mem_probe_stream_drop(_stream);
scc_free(stream);
}
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
cbool owned) {
scc_mem_probe_stream_t *stream =
(scc_mem_probe_stream_t *)scc_malloc(sizeof(scc_mem_probe_stream_t));
if (stream == null) {
return null;
}
scc_probe_stream_t *ret =
scc_mem_probe_stream_init(stream, data, length, owned);
stream->stream.drop = scc_owned_mem_stream_drop;
Assert(ret != null);
return ret;
}
#endif /* __SCC_CORE_NO_MEM_PROBE_STREAM__ */