feat(frontend): 重构词法分析器
- 添加 .gitignore 文件,忽略编译器生成的二进制文件 - 重构 lexer.c 文件,改进了关键字处理和字符串处理 - 更新前端的前端、解析器和 AST 相关文件,以适应新的词法分析器 - 优化了 token 相关的定义和函数,引入了新的 token 类型
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
#include "strpool.h"
|
||||
|
||||
void init_strpool(strpool_t* pool) {
|
||||
lalloc_init(&pool->stralloc);
|
||||
|
||||
pool->ht.hash_func = (u32_t(*)(const void*))rt_strhash;
|
||||
pool->ht.key_cmp = (int(*)(const void*, const void*))rt_strcmp;
|
||||
hashtable_init(&pool->ht);
|
||||
}
|
||||
|
||||
const char* strpool_intern(strpool_t* pool, const char* str) {
|
||||
void* existing = hashtable_get(&pool->ht, str);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
rt_size_t len = rt_strlen(str) + 1;
|
||||
char* new_str = lalloc_alloc(&pool->stralloc, len);
|
||||
if (!new_str) {
|
||||
LOG_ERROR("strpool: Failed to allocate memory for string");
|
||||
return NULL;
|
||||
}
|
||||
rt_memcpy(new_str, str, len);
|
||||
|
||||
hashtable_set(&pool->ht, new_str, new_str);
|
||||
return new_str;
|
||||
}
|
||||
|
||||
void strpool_destroy(strpool_t* pool) {
|
||||
hashtable_destory(&pool->ht);
|
||||
lalloc_destroy(&pool->stralloc);
|
||||
}
|
||||
|
@ -2,11 +2,16 @@
|
||||
#define __SMCC_STRPOOL_H__
|
||||
|
||||
#include <lib/core.h>
|
||||
#include "../ds/hash.h"
|
||||
typedef struct strpool {
|
||||
long_alloc_t *long_alloc;
|
||||
} strpool_t;
|
||||
#include <lib/rt/rt_alloc.h>
|
||||
#include <lib/utils/ds/hashtable.h>
|
||||
|
||||
void new_strpool();
|
||||
typedef struct strpool {
|
||||
hash_table_t ht; // 用于快速查找字符串
|
||||
long_alloc_t stralloc; // 专门用于字符串存储的分配器
|
||||
} strpool_t;
|
||||
|
||||
void init_strpool(strpool_t* pool);
|
||||
const char* strpool_intern(strpool_t* pool, const char* str);
|
||||
void strpool_destroy(strpool_t* pool);
|
||||
|
||||
#endif // __SMCC_STRPOOL_H__
|
||||
|
Reference in New Issue
Block a user