- 将依赖项从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中的宏定义以支持标准库模式
94 lines
2.4 KiB
C
94 lines
2.4 KiB
C
#include <lexer.h>
|
|
#include <lexer_log.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
/// gcc -g ../lexer.c ../token.c test_lexer.c -o test_lexer
|
|
/*
|
|
tok_tConstant {
|
|
int have;
|
|
union {
|
|
char ch;
|
|
int i;
|
|
float f;
|
|
double d;
|
|
long long ll;
|
|
char* str;
|
|
};
|
|
};
|
|
*/
|
|
|
|
int g_num;
|
|
int g_num_arr[3];
|
|
int main(int argc, char *argv[]) {
|
|
// int num = 0;
|
|
if (argc == 3 && strcmp(argv[2], "--debug") == 0) {
|
|
log_set_level(NULL, LOG_LEVEL_ALL);
|
|
} else {
|
|
// FIXME it is a hack lexer_logger
|
|
log_set_level(&__scc_lexer_log, LOG_LEVEL_NOTSET);
|
|
log_set_level(NULL, LOG_LEVEL_INFO | LOG_LEVEL_WARN | LOG_LEVEL_ERROR |
|
|
LOG_LEVEL_FATAL);
|
|
}
|
|
|
|
const char *file_name = __FILE__;
|
|
if (argc == 2) {
|
|
file_name = argv[1];
|
|
}
|
|
FILE *fp = fopen(file_name, "rb");
|
|
if (fp == NULL) {
|
|
perror("open file failed");
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(fp, 0, SEEK_END) != 0) {
|
|
perror("fseek failed");
|
|
return 1;
|
|
}
|
|
usize fsize = ftell(fp);
|
|
LOG_INFO("file size: %zu", fsize);
|
|
if (fseek(fp, 0, SEEK_SET)) {
|
|
perror("fseek failed");
|
|
return 1;
|
|
}
|
|
|
|
char *buffer = (char *)malloc(fsize);
|
|
|
|
usize read_ret = fread(buffer, 1, fsize, fp);
|
|
fclose(fp);
|
|
if (read_ret != fsize) {
|
|
LOG_FATAL("fread failed read_ret %u != fsize %u", read_ret, fsize);
|
|
free(buffer);
|
|
return 1;
|
|
}
|
|
|
|
scc_lexer_t lexer;
|
|
scc_mem_probe_stream_t mem_stream = {0};
|
|
scc_probe_stream_t *stream =
|
|
scc_mem_probe_stream_init(&mem_stream, buffer, fsize, false);
|
|
Assert(stream != null);
|
|
scc_cstring_clear(&stream->name);
|
|
scc_cstring_append_cstr(&stream->name, file_name, strlen(file_name));
|
|
scc_lexer_init(&lexer, stream);
|
|
scc_lexer_tok_t tok;
|
|
|
|
while (1) {
|
|
scc_lexer_get_valid_token(&lexer, &tok);
|
|
if (tok.type == SCC_TOK_EOF) {
|
|
break;
|
|
}
|
|
LOG_DEBUG("token `%s` at %s:%u:%u", scc_get_tok_name(tok.type),
|
|
scc_cstring_as_cstr(&tok.loc.name), tok.loc.line,
|
|
tok.loc.col);
|
|
Assert(tok.loc.offset <= fsize);
|
|
// LOG_DEBUG("%s", tok.val.str);
|
|
// printf("line: %d, column: %d, type: %3d, typename: %s\n",
|
|
// lexer.line, lexer.index, tok.type, scc_get_tok_name(tok.type));
|
|
}
|
|
|
|
free(buffer);
|
|
LOG_INFO("Lexer is Ok...");
|
|
return 0;
|
|
}
|