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,5 @@
[package]
name = "scc_utils"
version = "0.1.0"
dependencies = [{ name = "core", path = "../scc_core" }]

View File

@@ -0,0 +1,166 @@
/**
* kllist.h is a list implement by linux kernel list
* @link https://njusecourse.feishu.cn/wiki/I8vkw2zkwiEInUkujTJc7zzOnwf
* @link https://kernelnewlbies.org/FAQ/LinkedLists
* @link https://lwn.net/Articles/887097/
* @link
* https://liuluheng.github.io/wiki/public_html/Embedded-System/kernel/list-and-hlist.html
*/
#ifndef __KLLIST_H__
#define __KLLIST_H__
#ifndef NULL
#define NULL (0)
#define __NULL_KLIST_DEFINED__
#endif
#ifndef container_of
// Magic: https://radek.io/posts/magical-container_of-macro/
// StackOverflow: https://stackoverflow.com/q/15832301/1833118
#ifdef __GNUC__
#define container_of(ptr, type, member) \
({ \
const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
#else
#define container_of(ptr, type, member) \
({ \
const void *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
#endif
#endif
/**
* used by list
*/
struct list_head {
struct list_head *next, *prev;
};
/**
* list init
* @example
* 1. struct list_head your_list = LIST_HEAD_INIT(your_list);
* 2. struct list_head your_list; INIT_LIST_HEAD(&your_list);
* 3. LIST_HEAD(your_list); => struct your_list = { &(your_list), &(your_list)
* };
*/
#define LIST_HEAD_INIT(name) {&(name), &(name)}
static inline void INIT_LIST_HEAD(struct list_head *list) {
list->next = list;
list->prev = list;
}
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
/**
* list add
*/
static inline void __list_add(struct list_head *newl, struct list_head *prev,
struct list_head *next) {
next->prev = newl;
newl->next = next;
newl->prev = prev;
prev->next = newl;
}
static inline void list_add(struct list_head *newl, struct list_head *head) {
__list_add(newl, head, head->next);
}
static inline void list_add_tail(struct list_head *newl,
struct list_head *head) {
__list_add(newl, head->prev, head);
}
/**
* list delete
*/
static inline void __list_del(struct list_head *prev, struct list_head *next) {
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}
/**
* list_is_first -- tests whether @list is the first entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_first(const struct list_head *list,
const struct list_head *head) {
return list->prev == head;
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head) {
return list->next == head;
}
/**
* list_is_head - tests whether @list is the list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_head(const struct list_head *list,
const struct list_head *head) {
return list == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head) {
return head->next == head;
}
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
/**
* list sort
* by linux kernel 6.3.1 /lib/list_sort.c
* it remain use sigle linked list to merge sort
* @link https://www.geeksforgeeks.org/merge-sort-for-linked-list/
*/
#ifdef HAVE_KLIST_SORT
typedef int (*list_cmp_func_t)(void *, const struct list_head *,
const struct list_head *);
static void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
#endif
#if defined(__NULL_KLIST_DEFINED__) && !defined(__NULL_KLIST_DEFINED_NOMOVE__)
#undef NULL
#endif
#endif

View File

@@ -0,0 +1,124 @@
/**
* @file hashtable.h
* @brief 开放寻址法哈希表实现
*
* 提供基于向量容器的哈希表实现,支持动态扩容和墓碑机制
*/
#ifndef __SCC_HASHTABLE_H__
#define __SCC_HASHTABLE_H__
#include <scc_core.h>
/**
* @enum hp_entry_state_t
* @brief 哈希表条目状态标识
*/
typedef enum scc_hashtable_entry_state {
ENTRY_EMPTY, /**< 空槽位(从未使用过) */
ENTRY_ACTIVE, /**< 有效条目(包含键值对) */
ENTRY_TOMBSTONE /**< 墓碑标记(已删除条目) */
} scc_hashtable_entry_state_t;
/**
* @struct scc_hashtable_entry_t
* @brief 哈希表条目结构
*
* @note key/value内存由调用者管理哈希表不负责其生命周期
*/
typedef struct scc_hashtable_entry {
const void *key; /**< 键指针(不可变) */
void *value; /**< 值指针 */
u32 hash; /**< 预计算的哈希值(避免重复计算) */
scc_hashtable_entry_state_t state; /**< 当前条目状态 */
} scc_hashtable_entry_t;
/**
* @struct scc_hashtable_t
* @brief 哈希表主体结构
*
* 使用开放寻址法实现,采用墓碑标记处理删除操作
*/
typedef struct scc_hashtable {
SCC_VEC(scc_hashtable_entry_t) entries; /**< 条目存储容器 */
u32 count; /**< 有效条目数量(不含墓碑) */
u32 tombstone_count; /**< 墓碑条目数量 */
/**
* @brief 哈希函数指针
* @param key 键指针
* @return 32位无符号哈希值
*/
u32 (*hash_func)(const void *key);
/**
* @brief 键比较函数指针
* @param key1 第一个键指针
* @param key2 第二个键指针
* @return 相同返回0不同返回非0
*/
int (*key_cmp)(const void *key1, const void *key2);
} scc_hashtable_t;
/**
* @brief 初始化哈希表结构
* @param ht 哈希表实例指针
*
* @warning 必须设置hash_func和key_cmp后才能使用
*/
void scc_hashtable_init(scc_hashtable_t *ht);
/**
* @brief 插入/更新键值对
* @param ht 哈希表实例指针
* @param key 键指针
* @param value 值指针
* @return 被替换的旧值指针无替换返回NULL
*/
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value);
/**
* @brief 查找键对应值
* @param ht 哈希表实例指针
* @param key 查找键指针
* @return 找到返回值指针未找到返回NULL
*/
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key);
/**
* @brief 删除键值对
* @param ht 哈希表实例指针
* @param key 要删除的键指针
* @return 被删除的值指针不存在返回NULL
*
* @note 实际采用墓碑标记方式删除
*/
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key);
/**
* @brief 销毁哈希表
* @param ht 哈希表实例指针
*
* @note 仅释放哈希表内部内存不会释放key/value内存
*/
void scc_hashtable_drop(scc_hashtable_t *ht);
/**
* @typedef scc_hashtable_iter_fn
* @brief 哈希表迭代回调函数类型
* @param key 当前键指针
* @param value 当前值指针
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*scc_hashtable_iter_fn)(const void *key, void *value,
void *context);
/**
* @brief 遍历哈希表所有有效条目
* @param ht 哈希表实例指针
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void scc_hashtable_foreach(scc_hashtable_t *ht, scc_hashtable_iter_fn iter_func,
void *context);
#endif /* __SCC_HASHTABLE_H__ */

View File

@@ -0,0 +1,69 @@
/**
* @file strpool.h
* @brief 字符串池实现
*
* 提供字符串驻留String Interning功能保证相同字符串的唯一性存储
*/
#ifndef __SCC_STRPOOL_H__
#define __SCC_STRPOOL_H__
#include "scc_hashtable.h"
#include <scc_core.h>
/**
* @struct strpool_t
* @brief 字符串池上下文
*
* 组合哈希表和专用内存分配器实现的高效字符串存储池
*/
typedef struct strpool {
scc_hashtable_t ht; /**< 哈希表用于快速查找已存储字符串 */
} scc_strpool_t;
/**
* @brief 初始化字符串池
* @param pool 字符串池实例指针
*/
void scc_strpool_init(scc_strpool_t *pool);
/**
* @brief 驻留字符串到池中
* @param pool 字符串池实例指针
* @param str 要驻留的 C 字符串
* @return 池中唯一字符串的持久指针
*
* @note 返回值生命周期与字符串池一致
* @note 重复插入相同字符串会返回已有指针
*/
const char *scc_strpool_intern(scc_strpool_t *pool, const char *str);
/**
* @brief 销毁字符串池
* @param pool 字符串池实例指针
*
* @warning 销毁后已获取的字符串指针将失效
* @note 会自动释放所有驻留字符串内存
*/
void scc_strpool_drop(scc_strpool_t *pool);
/**
* @typedef scc_hashtable_iter_fn
* @brief 哈希表迭代回调函数类型
* @param key 当前键指针
* @param value 当前值指针
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*scc_strpool_iter_fn)(const char *key, char *value, void *context);
/**
* @brief 遍历字符串表所有有效条目
* @param ht 字符串表实例指针
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void scc_strpool_foreach(scc_strpool_t *pool, scc_strpool_iter_fn iter_func,
void *context);
#endif /* __SCC_STRPOOL_H__ */

View File

@@ -0,0 +1,9 @@
#ifndef __SMCC_UTILS_H__
#define __SMCC_UTILS_H__
#include "kllist.h"
#include "scc_hashtable.h"
#include "scc_strpool.h"
#include <scc_core.h>
#endif /* __SMCC_UTILS_H__ */

View File

@@ -0,0 +1,155 @@
#include <scc_hashtable.h>
#ifndef SCC_INIT_HASHMAP_SIZE
#define SCC_INIT_HASHMAP_SIZE (32)
#endif
void scc_hashtable_init(scc_hashtable_t *ht) {
scc_vec_init(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
}
static int next_power_of_two(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n + 1;
}
static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key,
u32 hash) {
if (ht->entries.cap == 0)
return NULL;
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
u32 probe = 0;
scc_hashtable_entry_t *tombstone = NULL;
while (1) {
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, index);
if (entry->state == ENTRY_EMPTY) {
return tombstone ? tombstone : entry;
}
if (entry->state == ENTRY_TOMBSTONE) {
if (!tombstone)
tombstone = entry;
} else if (entry->hash == hash && ht->key_cmp(entry->key, key) == 0) {
return entry;
}
// Liner finding
index = (index + 1) & (ht->entries.cap - 1);
probe++;
if (probe >= ht->entries.cap)
break;
}
LOG_ERROR("hashset_find: hash table is full");
return NULL;
}
static void adjust_capacity(scc_hashtable_t *ht, usize new_cap) {
new_cap = next_power_of_two(new_cap);
Assert(new_cap >= ht->entries.cap);
SCC_VEC(scc_hashtable_entry_t) old_entries;
old_entries.data = ht->entries.data;
old_entries.cap = ht->entries.cap;
// Not used size but for gdb python extention debug
ht->entries.size = new_cap;
ht->entries.cap = new_cap;
ht->entries.data =
scc_realloc(NULL, new_cap * sizeof(scc_hashtable_entry_t));
scc_memset(ht->entries.data, 0, new_cap * sizeof(scc_hashtable_entry_t));
// rehash the all of the old data
for (usize i = 0; i < old_entries.cap; i++) {
scc_hashtable_entry_t *entry = &scc_vec_at(old_entries, i);
if (entry->state == ENTRY_ACTIVE) {
scc_hashtable_entry_t *dest =
find_entry(ht, entry->key, entry->hash);
*dest = *entry;
}
}
scc_vec_free(old_entries);
ht->tombstone_count = 0;
}
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value) {
if (ht->count + ht->tombstone_count >= ht->entries.cap * 0.75) {
int new_cap = ht->entries.cap < SCC_INIT_HASHMAP_SIZE
? SCC_INIT_HASHMAP_SIZE
: ht->entries.cap * 2;
adjust_capacity(ht, new_cap);
}
u32 hash = ht->hash_func(key);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
void *old_value = NULL;
if (entry->state == ENTRY_ACTIVE) {
old_value = entry->value;
} else {
if (entry->state == ENTRY_TOMBSTONE)
ht->tombstone_count--;
ht->count++;
}
entry->key = key;
entry->value = value;
entry->hash = hash;
entry->state = ENTRY_ACTIVE;
return old_value;
}
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key) {
if (ht->entries.cap == 0)
return NULL;
u32 hash = ht->hash_func(key);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : NULL;
}
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key) {
if (ht->entries.cap == 0)
return NULL;
u32 hash = ht->hash_func(key);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
if (entry == NULL || entry->state != ENTRY_ACTIVE)
return NULL;
void *value = entry->value;
entry->state = ENTRY_TOMBSTONE;
ht->count--;
ht->tombstone_count++;
return value;
}
void scc_hashtable_drop(scc_hashtable_t *ht) {
scc_vec_free(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
}
void scc_hashtable_foreach(scc_hashtable_t *ht, scc_hashtable_iter_fn iter_func,
void *context) {
for (usize i = 0; i < ht->entries.cap; i++) {
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, i);
if (entry->state == ENTRY_ACTIVE) {
if (iter_func(entry->key, entry->value, context)) {
break; // enable callback function terminal the iter
}
}
}
}

View File

@@ -0,0 +1,32 @@
#include <scc_strpool.h>
void scc_strpool_init(scc_strpool_t *pool) {
pool->ht.hash_func = (u32 (*)(const void *))scc_strhash32;
pool->ht.key_cmp = (int (*)(const void *, const void *))scc_strcmp;
scc_hashtable_init(&pool->ht);
}
const char *scc_strpool_intern(scc_strpool_t *pool, const char *str) {
void *existing = scc_hashtable_get(&pool->ht, str);
if (existing) {
return existing;
}
usize len = scc_strlen(str) + 1;
char *new_str = scc_malloc(len);
if (!new_str) {
LOG_ERROR("strpool: Failed to allocate memory for string");
return NULL;
}
scc_memcpy(new_str, str, len);
scc_hashtable_set(&pool->ht, new_str, new_str);
return new_str;
}
void scc_strpool_drop(scc_strpool_t *pool) { scc_hashtable_drop(&pool->ht); }
void scc_strpool_foreach(scc_strpool_t *pool, scc_strpool_iter_fn iter_func,
void *context) {
scc_hashtable_foreach(&pool->ht, (scc_hashtable_iter_fn)iter_func, context);
}