feat(pproc): 实现C语言预处理器功能并重构项目依赖
- 新增预处理器库(pproc),替代原有的pprocessor模块 - 实现完整的宏定义解析功能,支持对象宏和函数宏 - 添加条件编译指令处理(#if、#ifdef、#ifndef、#else、#elif、#endif) - 实现宏展开机制,包括嵌套宏和递归宏处理 - 添加宏定义测试用例,覆盖基本功能和复杂场景 - 在cbuild.toml中更新依赖配置,移除parser、ast、ast2ir、ir等未完成模块 - 新增lexer工具函数用于token流处理 - 添加宏定义表管理功能,支持宏的创建、查找、删除操作 - 实现宏参数解析和替换列表处理
This commit is contained in:
97
libs/pproc/include/pproc_macro.h
Normal file
97
libs/pproc/include/pproc_macro.h
Normal file
@@ -0,0 +1,97 @@
|
||||
#ifndef __SCC_PP_MACRO_H__
|
||||
#define __SCC_PP_MACRO_H__
|
||||
|
||||
#include <scc_core.h>
|
||||
#include <scc_lexer.h>
|
||||
#include <scc_utils.h>
|
||||
|
||||
// 宏定义类型
|
||||
typedef enum {
|
||||
SCC_PP_MACRO_NONE, // 不是宏
|
||||
SCC_PP_MACRO_OBJECT, // 对象宏
|
||||
SCC_PP_MACRO_FUNCTION, // 函数宏
|
||||
} scc_pp_macro_type_t;
|
||||
|
||||
typedef scc_lexer_tok_vec_t scc_pproc_macro_list_t;
|
||||
|
||||
// 宏定义结构
|
||||
typedef struct scc_macro {
|
||||
scc_cstring_t name; // 宏名称
|
||||
scc_pp_macro_type_t type; // 宏类型
|
||||
scc_lexer_tok_vec_t replaces; // 替换列表
|
||||
scc_pproc_macro_list_t params; // 参数列表(仅函数宏)
|
||||
} scc_pp_macro_t;
|
||||
|
||||
typedef struct scc_macro_table {
|
||||
scc_hashtable_t table; // 宏定义表
|
||||
} scc_pp_macro_table_t;
|
||||
|
||||
/**
|
||||
* @brief 创建宏对象
|
||||
* @param name 宏名称
|
||||
* @param type 宏类型
|
||||
* @return 创建的宏对象指针,失败返回NULL
|
||||
*/
|
||||
scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name,
|
||||
scc_pp_macro_type_t type);
|
||||
|
||||
/**
|
||||
* @brief 销毁宏对象
|
||||
* @param macro 要销毁的宏对象
|
||||
*/
|
||||
void scc_pp_macro_drop(scc_pp_macro_t *macro);
|
||||
|
||||
/**
|
||||
* @brief 添加对象宏
|
||||
* @param pp 预处理器实例
|
||||
* @param name 宏名称
|
||||
* @param replacement 替换文本列表
|
||||
* @return 成功返回true,失败返回false
|
||||
*/
|
||||
cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp,
|
||||
const scc_cstring_t *name,
|
||||
const scc_pproc_macro_list_t *replacement);
|
||||
|
||||
/**
|
||||
* @brief 添加函数宏
|
||||
* @param pp 预处理器实例
|
||||
* @param name 宏名称
|
||||
* @param params 参数列表
|
||||
* @param replacement 替换文本列表
|
||||
* @return 成功返回true,失败返回false
|
||||
*/
|
||||
cbool scc_pp_add_function_macro(scc_pp_macro_table_t *pp,
|
||||
const scc_cstring_t *name,
|
||||
const scc_pproc_macro_list_t *params,
|
||||
const scc_pproc_macro_list_t *replacement);
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param pp
|
||||
* @param macro
|
||||
* @return scc_pp_macro_t*
|
||||
*/
|
||||
scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp,
|
||||
scc_pp_macro_t *macro);
|
||||
|
||||
/**
|
||||
* @brief 查找宏定义
|
||||
* @param pp 预处理器实例
|
||||
* @param name 宏名称
|
||||
* @return 找到的宏对象指针,未找到返回NULL
|
||||
*/
|
||||
scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp,
|
||||
const scc_cstring_t *name);
|
||||
|
||||
/**
|
||||
* @brief 从预处理器中删除宏
|
||||
* @param pp 预处理器实例
|
||||
* @param name 宏名称
|
||||
* @return 成功删除返回true,未找到返回false
|
||||
*/
|
||||
cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp,
|
||||
const scc_cstring_t *name);
|
||||
|
||||
void scc_pp_marco_table_init(scc_pp_macro_table_t *macros);
|
||||
void scc_pp_macro_table_drop(scc_pp_macro_table_t *macros);
|
||||
#endif /* __SCC_PP_MACRO_H__ */
|
||||
60
libs/pproc/include/scc_pproc.h
Normal file
60
libs/pproc/include/scc_pproc.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @file pprocessor.h
|
||||
* @brief C语言预处理器核心数据结构与接口
|
||||
*/
|
||||
|
||||
#ifndef __SCC_PPROC_H__
|
||||
#define __SCC_PPROC_H__
|
||||
|
||||
#include "pproc_macro.h"
|
||||
#include <scc_core.h>
|
||||
#include <scc_core_ring.h>
|
||||
#include <scc_lexer.h>
|
||||
|
||||
// 预处理器状态结构
|
||||
|
||||
// 条件编译状态栈
|
||||
typedef struct {
|
||||
int active; // 当前层级是否有效(即应该输出 token)
|
||||
int skip; // 当前层级是否跳过(即不输出 token)
|
||||
// 可根据需要增加状态,如 #if 的结果、#elif 已执行等
|
||||
} scc_pproc_if_state_t;
|
||||
typedef SCC_VEC(scc_pproc_if_state_t) scc_pproc_if_stack_t;
|
||||
|
||||
// 文件包含栈
|
||||
typedef struct {
|
||||
scc_lexer_t *lexer; // 当前文件的 lexer
|
||||
scc_lexer_tok_ring_t *tok_ring; // 当前文件的 token 环(由 lexer 提供)
|
||||
// 可能还需要保存当前位置等
|
||||
} scc_pproc_file_state_t;
|
||||
typedef SCC_VEC(scc_pproc_file_state_t) scc_pproc_file_stack_t;
|
||||
|
||||
typedef SCC_VEC(scc_lexer_tok_ring_t *) scc_pproc_ring_vec_t;
|
||||
typedef struct scc_pproc {
|
||||
scc_lexer_tok_ring_t *cur_ring;
|
||||
scc_strpool_t strpool;
|
||||
|
||||
scc_pp_macro_table_t macro_table;
|
||||
scc_pproc_if_stack_t if_stack;
|
||||
scc_pproc_file_stack_t file_stack;
|
||||
|
||||
scc_lexer_tok_vec_t cache;
|
||||
int cache_pos;
|
||||
scc_lexer_tok_ring_t ring;
|
||||
int ring_ref_count;
|
||||
} scc_pproc_t;
|
||||
|
||||
void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input);
|
||||
scc_lexer_tok_ring_t *scc_pproc_to_ring(scc_pproc_t *pp, int ring_size);
|
||||
void scc_pproc_drop(scc_pproc_t *pp);
|
||||
|
||||
void scc_pproc_handle_directive(scc_pproc_t *pp);
|
||||
void scc_pproc_expand_macro(scc_pproc_t *pp, const scc_pp_macro_t *macro);
|
||||
|
||||
void scc_pproc_parse_macro_arguments(scc_pproc_t *pp,
|
||||
scc_pproc_macro_list_t *args);
|
||||
void scc_pproc_parse_function_macro(scc_pproc_t *pp,
|
||||
const scc_lexer_tok_t *ident);
|
||||
void scc_pproc_parse_object_macro(scc_pproc_t *pp,
|
||||
const scc_lexer_tok_t *ident);
|
||||
#endif /* __SCC_PPROC_H__ */
|
||||
Reference in New Issue
Block a user