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,14 @@
#ifndef __SCC_CORE_H__
#define __SCC_CORE_H__
#include <scc_core_log.h>
#include <scc_core_impl.h>
#include <scc_core_macro.h>
#include <scc_core_mem.h>
#include <scc_core_pos.h>
#include <scc_core_str.h>
#include <scc_core_stream.h>
#include <scc_core_vec.h>
#endif // __SCC_CORE_H__

View File

@@ -0,0 +1,47 @@
#ifndef __SCC_CORE_IMPL_H__
#define __SCC_CORE_IMPL_H__
#include "scc_core_type.h"
/* ====== 内存管理核心接口 ====== */
void *scc_malloc(usize size);
void *scc_calloc(usize count, usize size);
void *scc_realloc(void *ptr, usize new_size);
void scc_free(void *ptr);
/* ====== 文件系统核心接口 ====== */
/* 文件句柄 - 不透明指针 */
typedef struct scc_file *scc_file_t;
/* 文件打开模式 - 只保留编译器真正需要的 */
typedef enum {
SCC_FILE_READ, /* 读取源文件、头文件 */
SCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SCC_FILE_APPEND /* 日志、调试输出 */
} scc_fmode_t;
/* 核心文件操作 */
scc_file_t scc_fopen(const char *path, scc_fmode_t mode);
void scc_fclose(scc_file_t file);
usize scc_fread(scc_file_t file, void *buffer, usize size);
usize scc_fwrite(scc_file_t file, const void *buffer, usize size);
cbool scc_fexists(const char *path);
/* ====== 输入输出核心接口 ====== */
void scc_snprintf(char *buf, usize size, const char *format, ...);
/* 标准输出 - 用于编译进度、结果 */
void scc_printf(const char *format, ...);
/* 错误输出 - 用于错误信息、警告 */
void scc_eprintf(const char *format, ...);
/* ====== 系统核心接口 ====== */
/* 程序控制 */
void scc_exit(int code);
#endif /* __SCC_CORE_IMPL_H__ */

View File

@@ -0,0 +1,17 @@
#ifndef __SCC_CORE_LOG_H__
#define __SCC_CORE_LOG_H__
#ifndef log_snprintf
#define log_snprintf scc_snprintf
#endif
#ifndef log_printf
#define log_printf scc_printf
#endif
#ifndef log_exit
#define log_exit scc_exit
#endif
#include <log.h>
#endif /* __SCC_CORE_LOG_H__ */

View File

@@ -0,0 +1,11 @@
#ifndef __SCC_CORE_MACRO_H__
#define __SCC_CORE_MACRO_H__
#define _SCC_STR(str) #str
#define SCC_STR(str) _SCC_STR(str)
#define SCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define SCC_FUNC
#endif // __SCC_CORE_MACRO_H__

View File

@@ -0,0 +1,56 @@
#ifndef __SCC_CORE_MEM_H__
#define __SCC_CORE_MEM_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);
void *scc_memset(void *s, int c, usize n);
int scc_memcmp(const void *s1, const void *s2, usize n);
/**
* @brief 使用FNV-1a进行 C 字符串哈希
*
* @param s
* @return u32
*/
static inline u32 scc_strhash32(const char *s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
hash *= 16777619u;
}
return hash;
}
/**
* @brief 获取 C 字符串长度
*
* @param str
* @return usize
*/
static inline usize scc_strlen(const char *str) {
usize len = 0;
while (*str) {
len++;
str++;
}
return len;
}
/**
* @brief 比较两个 C 字符串
*
* @param s1
* @param s2
* @return int
*/
static inline int scc_strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 - *s2;
}
#endif /* __SCC_CORE_MEM_H__ */

View File

@@ -0,0 +1,28 @@
#ifndef __SCC_CORE_POS_H__
#define __SCC_CORE_POS_H__
#include "scc_core_str.h"
#include "scc_core_type.h"
typedef struct scc_pos {
scc_cstring_t name;
usize line;
usize col;
usize offset;
} scc_pos_t;
static inline scc_pos_t scc_pos_create() {
return (scc_pos_t){scc_cstring_create(), 1, 1, 0};
}
static inline void scc_pos_next(scc_pos_t *pos) {
pos->offset++;
pos->col++;
}
static inline void scc_pos_next_line(scc_pos_t *pos) {
pos->offset++;
pos->line++;
pos->col = 1;
}
#endif /* __SCC_CORE_POS_H__ */

View File

@@ -0,0 +1,208 @@
#ifndef __SCC_CORE_STR_H__
#define __SCC_CORE_STR_H__
#include "scc_core_impl.h"
#include "scc_core_log.h"
#include "scc_core_mem.h"
#include "scc_core_type.h"
/**
* @brief 动态字符串结构体
* @attention 创建的字符串对象需要使用 scc_cstring_free 释放
*/
typedef struct scc_cstring {
usize size; /**< 字符串当前大小(包括结尾的'\0'*/
usize cap; /**< 分配的容量 */
char *data; /**< 实际存储数据的指针 */
} scc_cstring_t;
/**
* @brief 创建一个新的空动态字符串对象
*
* @return cstring_t 初始化后的对象
*/
static inline scc_cstring_t scc_cstring_create(void) {
return (scc_cstring_t){.data = null, .size = 0, .cap = 0};
}
static inline void scc_cstring_init(scc_cstring_t *string) {
Assert(string != null);
string->data = null;
string->size = 0;
string->cap = 0;
}
/**
* @brief 从 C 风格字符串创建一个动态字符串副本
*
* @param s 输入的 C 风格字符串
* @return cstring_t 新建对象,包含输入字符串的副本
*/
static inline scc_cstring_t scc_cstring_from_cstr(const char *s) {
if (s == null) {
return scc_cstring_create();
}
usize len = 0;
const char *p = s;
while (*p++)
len++;
char *data = (char *)scc_malloc(len + 1);
Assert(data != null);
scc_memcpy(data, s, len);
data[len] = '\0';
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 释放动态字符串占用的内存资源
*
* @param str 要被释放的字符串指针
*/
static inline void scc_cstring_free(scc_cstring_t *str) {
if (str == null) {
return;
}
if (str->data != null) {
scc_free(str->data);
str->data = null;
}
str->size = 0;
str->cap = 0;
}
/**
* @brief 向动态字符串末尾追加一段 C 风格字符串
*
* @param str 目标动态字符串指针
* @param data 要追加的 C 字符串指针
* @param len 要追加的 C 字符串长度
*/
static inline void scc_cstring_append_cstr(scc_cstring_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
return;
}
if (str->cap == 0) {
// add '\0' to str
str->size = 1;
}
// 如果需要扩容
if (str->size + len > str->cap) {
usize new_cap = str->cap;
while (new_cap < str->size + len) {
if (new_cap == 0) {
new_cap = str->size + len;
break;
} else {
new_cap *= 2;
}
// FIXME 处理溢出情况
}
char *new_data = (char *)scc_realloc(str->data, new_cap);
Assert(new_data != null);
str->data = new_data;
str->cap = new_cap;
}
scc_memcpy(str->data + str->size - 1, data, len);
str->size += len;
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
}
/**
* @brief 向动态字符串末尾追加另一个动态字符串
*
* @param str 目标动态字符串指针
* @param other 要追加的动态字符串指针
*/
static inline void scc_cstring_append(scc_cstring_t *str,
const scc_cstring_t *other) {
scc_cstring_append_cstr(str, other->data, other->size - 1);
}
/**
* @brief 向动态字符串末尾追加一个字符
*
* @param str 目标动态字符串指针
* @param ch 要追加的字符
*/
static inline void scc_cstring_append_ch(scc_cstring_t *str, char ch) {
scc_cstring_append_cstr(str, &ch, 1);
}
/**
* @brief 获取动态字符串的实际内容长度 不包括结尾的'\0'
*
* @param str 动态字符串指针
* @return usize 字符串实际长度
*/
static inline usize scc_cstring_len(const scc_cstring_t *str) {
if (str == null) {
return 0;
}
if (str->size == 0) {
return 0;
}
return str->size - 1;
}
/**
* @brief 判断动态字符串是否为空
*
* @param str 动态字符串指针
* @return cbool
*/
static inline cbool scc_cstring_is_empty(const scc_cstring_t *str) {
return str == null || str->size == 0;
}
/**
* @brief 清空动态字符串内容但保留已分配的内存空间
*
* @param str 动态字符串指针
*/
static inline void scc_cstring_clear(scc_cstring_t *str) {
if (str) {
str->size = 1;
if (str->data) {
str->data[0] = '\0';
}
}
}
/**
* @brief 将动态字符串转换为 C 风格字符串
*
* @param str 动态字符串指针
* @return char* 返回指向内部缓冲区的 C 风格字符串指针
*/
static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) {
if (str == null || str->data == null) {
return null;
}
return str->data;
}
static inline char *scc_cstring_move_cstr(scc_cstring_t *str) {
if (str == null || str->data == null) {
return null;
}
char *ret = str->data;
str->data = null;
str->cap = 0;
str->size = 0;
return ret;
}
#endif /* __SCC_CORE_STR_H__ */

View File

@@ -0,0 +1,130 @@
#ifndef __SMCC_CORE_PROBE_STREAM_H__
#define __SMCC_CORE_PROBE_STREAM_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;
#define scc_stream_eof (-1)
/**
* @brief 带探针的流接口
*
* 这个流提供了双指针机制:当前读取位置(头指针)和探针位置(尾指针)。
* 尾指针只能向前移动,用于查看而不消费。
* 头指针可以前进或单次后退,但不能一直后退到尾指针后面。
*/
struct scc_probe_stream {
scc_cstring_t name;
/// @brief 消费头指针处的字符(移动头指针)
int (*consume)(scc_probe_stream_t *stream);
/// @brief 查看当前探针位置的字符,不移动任何指针
int (*peek)(scc_probe_stream_t *stream);
/// @brief 移动探针位置并返回字符
int (*next)(scc_probe_stream_t *stream);
/// @brief 移动头指针到探针位置
void (*sync)(scc_probe_stream_t *stream);
/// @brief 重置探针位置到头指针位置
void (*reset)(scc_probe_stream_t *stream);
/// @brief 回退一个字符(单次后退,头指针后退一步)
cbool (*back)(scc_probe_stream_t *stream);
/// @brief 读取指定数量的字符到缓冲区
usize (*read_buf)(scc_probe_stream_t *stream, char *buffer, usize count);
/// @brief 检查是否到达流末尾
cbool (*is_at_end)(scc_probe_stream_t *stream);
/// @brief 销毁流并释放资源
void (*drop)(scc_probe_stream_t *stream);
};
static inline int scc_probe_stream_consume(scc_probe_stream_t *self) {
return self->consume(self);
}
static inline int scc_probe_stream_peek(scc_probe_stream_t *self) {
return self->peek(self);
}
static inline int scc_probe_stream_next(scc_probe_stream_t *self) {
return self->next(self);
}
static inline void scc_probe_stream_sync(scc_probe_stream_t *self) {
self->sync(self);
}
static inline cbool scc_probe_stream_back(scc_probe_stream_t *self) {
return self->back(self);
}
static inline void scc_probe_stream_reset(scc_probe_stream_t *self) {
self->reset(self);
}
static inline usize scc_probe_stream_read_buf(scc_probe_stream_t *self,
char *buffer, usize count) {
return self->read_buf(self, buffer, count);
}
static inline cbool scc_probe_stream_is_at_end(scc_probe_stream_t *self) {
return self->is_at_end(self);
}
static inline cbool scc_probe_stream_has_more(scc_probe_stream_t *self) {
return !self->is_at_end(self);
}
static inline void scc_probe_stream_drop(scc_probe_stream_t *self) {
self->drop(self);
}
#ifndef __SCC_NO_MEM_PROBE_STREAM__
/**
* @brief 内存探针流结构
*/
typedef struct scc_mem_probe_stream {
scc_probe_stream_t stream;
const char *data;
usize data_length;
usize curr_pos; // 当前读取位置
usize probe_pos; // 探针位置用于peek
cbool owned; // 是否拥有数据(如果拥有将会自动释放)
} scc_mem_probe_stream_t;
/**
* @brief 初始化内存探针流(由你负责scc_mem_probe_stream_t的释放)
*
* @param stream 流结构指针
* @param data 数据指针
* @param length 数据长度
* @param owned 是否拥有数据(如果拥有将会自动释放)
* @return core_probe_stream_t* 成功返回流指针失败返回NULL
*/
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
const char *data, usize length,
cbool owned);
/**
* @brief 构造内存探针流(其中drop会自动释放内存)
*
* @param data
* @param length
* @param owned 是否拥有数据(如果拥有将会自动释放)
* @return scc_probe_stream_t*
*/
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
cbool owned);
#endif
#endif /* __SMCC_CORE_PROBE_STREAM_H__ */

View File

@@ -0,0 +1,73 @@
#ifndef __SCC_CORE_TYPE_H__
#define __SCC_CORE_TYPE_H__
#ifndef __SCC_BUILTIN_TYPE__
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* clang-format off */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef intptr_t isize;
typedef uintptr_t usize;
typedef ptrdiff_t pdiff;
typedef float f32;
typedef double f64;
typedef bool cbool;
/// void / null
#define null NULL
/* clang-format on */
static_assert(sizeof(cbool) == 1, "cbool size must 1");
#else
#define __scc_i8
#define __scc_i16
#define __scc_i32
#define __scc_i64
#define __scc_u8
#define __scc_u16
#define __scc_u32
#define __scc_u64
#define __scc_f32
#define __scc_f64
#define __scc_bool
#define __scc_char
#define __scc_void
#define __scc_null
#define __scc_isize
#define __scc_usize
#endif
typedef union scc_cvalue {
long double ld;
double d;
float f;
unsigned long long ull;
char ch;
/* number value */
uint64_t n;
/* string value */
struct {
char *data;
uintptr_t len;
} cstr;
/* 16 byte == 128 bit */
char val[16];
} scc_cvalue_t;
#endif /* __SCC_CORE_TYPE_H__ */

View File

@@ -0,0 +1,153 @@
/**
* @file vec.h
* @brief 动态数组Dynamic Array实现
*
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
*/
#ifndef __SCC_CORE_VEC_H__
#define __SCC_CORE_VEC_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 <stddef.h>
#include <stdlib.h>
typedef size_t usize;
#define __scc_vec_realloc realloc
#define __scc_vec_free free
#ifndef LOG_FATAL
#include <stdio.h>
#define LOG_FATAL(...) \
do { \
printf(__VA_ARGS__); \
exit(1); \
} while (0)
#endif
#ifndef Assert
#include <assert.h>
#define Assert(cond) assert(cond)
#endif
#endif
/** @defgroup vec_struct 数据结构定义 */
/**
* @def SCC_VEC(type)
* @brief 声明向量结构体
* @param type 存储的数据类型
*
* 生成包含size/cap/data三个字段的结构体定义
* - size: 当前元素数量
* - cap: 数组容量
* - data: 存储数组指针
* @example
* SCC_VEC(char) string; <=> char[dynamic_array] string;
* struct people { SCC_VEC(char) name; int age; SCC_VEC(struct people) children;
* };
*/
#define SCC_VEC(type) \
struct { \
usize size; \
usize cap; \
type *data; \
}
/** @defgroup vec_operations 动态数组操作宏 */
/**
* @def scc_vec_init(vec)
* @brief 初始化向量结构体
* @param vec 要初始化的向量结构体变量
*
* @note 此宏不会分配内存,仅做零初始化
*/
#define scc_vec_init(vec) \
do { \
(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 添加元素到向量末尾
* @param vec 目标向量结构体
* @param value 要添加的值(需匹配存储类型)
*
* @note 当容量不足时自动扩容为2倍初始容量为4
* @warning 内存分配失败时会触发LOG_FATAL
*/
#define scc_vec_push(vec, value) \
do { \
if ((vec).size >= (vec).cap) { \
int cap = (vec).cap ? (vec).cap * 2 : 4; \
scc_vec_realloc(vec, cap); \
} \
Assert((vec).data != null); \
(vec).data[(vec).size++] = value; \
} while (0)
/**
* @def scc_vec_pop(vec)
* @brief 弹出最后一个元素
* @param vec 目标向量结构体
* @return 最后元素的引用
* @warning 需确保size > 0时使用
*/
#define scc_vec_pop(vec) ((vec).data[--(vec).size])
/**
* @def scc_vec_at(vec, idx)
* @brief 获取指定索引元素
* @param vec 目标向量结构体
* @param idx 元素索引0 <= idx < size
* @return 对应元素的引用
*/
#define scc_vec_at(vec, idx) (((vec).data)[idx])
/**
* @def scc_vec_idx(vec, ptr)
* @brief 获取元素指针对应的索引
* @param vec 目标向量结构体
* @param ptr 元素指针需在data数组范围内
* @return 元素索引值
*/
#define scc_vec_idx(vec, ptr) ((ptr) - (vec).data)
/**
* @def scc_vec_free(vec)
* @brief 释放向量内存
* @param vec 目标向量结构体
*
* @note 释放后需重新初始化才能再次使用
*/
#define scc_vec_free(vec) \
do { \
__scc_vec_free((vec).data); \
(vec).data = NULL; \
(vec).size = (vec).cap = 0; \
} while (0)
#endif /* __SCC_CORE_VEC_H__ */

File diff suppressed because it is too large Load Diff