新增基于 Python 的构建脚本 `cbuild.py`,支持包管理、依赖解析和模块化编译。 同时添加 `.gitignore` 忽略 `build` 目录,并在 `justfile` 中更新构建命令。 移除了原有的 `lib/Makefile` 和主目录下的相关 make 规则,统一使用新构建系统。
84 lines
2.0 KiB
C
84 lines
2.0 KiB
C
#include <lexer.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.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], "-nodebug") == 0) {
|
|
log_set_level(NULL, LOG_LEVEL_ALL);
|
|
}
|
|
|
|
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;
|
|
}
|
|
printf("open file success\n");
|
|
|
|
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;
|
|
}
|
|
|
|
smcc_lexer_t lexer;
|
|
lexer_mem_stream_t mem_stream = {0};
|
|
lexer_stream_t* stream = lexer_mem_stream_init(&mem_stream, buffer, fsize, false);
|
|
Assert(stream != null);
|
|
stream->name = __FILE__;
|
|
stream->name_len = strlen(__FILE__);
|
|
lexer_init(&lexer, stream);
|
|
lexer_tok_t tok;
|
|
|
|
while (1) {
|
|
lexer_get_valid_token(&lexer, &tok);
|
|
if (tok.type == TOKEN_EOF) {
|
|
break;
|
|
}
|
|
LOG_INFO("token `%s` at %s:%u:%u", get_tok_name(tok.type), tok.loc.name, tok.loc.line, tok.loc.column);
|
|
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, get_tok_name(tok.type));
|
|
}
|
|
|
|
free(buffer);
|
|
}
|