refactor(debug): 移除ANSI转义码支持并更新注释

- 从'ANSI_FMT'宏中移除ANSI转义码,以适应不支持颜色的终端。
- 现在可以通过定义'ANSI_FMT_DISABLE'宏来禁用ANSI转义码。

refactor(gen-expr): 改进代码格式和临时文件处理

- 在gen-expr.c中进行了代码格式调整,以保持一致的风格。
- 优化了临时文件的处理方式,确保路径和命令在不同操作系统上的兼容性。

refactor(main): 调整打印格式和字符串分割逻辑

- 修正了main.c中的打印格式化字符串,以适应不同编译器的规范。
- 改进了命令帮助功能的字符串分割逻辑,以正确处理子命令查询。

update(README): 添加编译和运行说明,禁用ANSI转义码说明

- 在README.md中添加了关于项目编译和运行的说明,包括C++11标准的要求。
- 提供了关于在不支持ANSI转义码的终端上禁用它的说明。

fix(.gitignore): 忽略所有文件但特定文件除外

- 修正了'.gitignore'文件,以忽略所有文件,但'.gitignore'、'.c'、'.cpp'、'.h'和'.md'文件以及'Makefile'除外。
This commit is contained in:
zzy 2024-08-26 22:27:38 +08:00
parent 7b1e1ef1eb
commit 8fd94dad01
6 changed files with 65 additions and 54 deletions

13
.gitignore vendored
View File

@ -1,8 +1,7 @@
.*
*
!.gitignore
*.o
*.out
*.obj
*.exe
!^.*.c
!^.*.cpp
!^.*.h
!^.*.md
!Makefile

View File

@ -1,3 +1,7 @@
注意 由于使用c++的regex库所以需要编译时需要注意大于等于c++11语法
注意 这是一个多文件编译的项目需要你结合main.c和expr.cpp使用
注意 gen-expr里面需要命令行里编译c语言所以Windows需要自行配置以保证可以使用system命令编译以及popen的路径
- 由于使用c++的regex库所以需要编译时需要至少c++11标准才能运行
- 这是一个多文件编译的项目需要你结合main.c和expr.cpp使用
- gen-expr里面需要命令行里编译C语言所以Windows需要自行配置需要保证可以使用system命令编译以及保证popen能够打开C源文件和编译后可执行文件(需要修改`TMP_C_SRC_FILE_PATH` `TMP_C_EXE_FILE_PATH` `TMP_COMPILE_CMD`)
- 定义`ANSI_FMT_DISABLE`宏可以用于禁用ANSI转义码(由于有些终端并不支持)

View File

@ -40,7 +40,12 @@
#define ANSI_BG_WHITE "\33[1;47m"
#define ANSI_NONE "\33[0m"
// Maybe Some Terminal Doesn't Support Color
#ifndef ANSI_FMT_DISABLE
#define ANSI_FMT(str, fmt) fmt str ANSI_NONE
#else
#define ANSI_FMT(str, fmt) str
#endif
#define _Log(...) \
do { \

View File

@ -15,8 +15,8 @@
// Modified by: Zhiyi Zhang in 2024.08
#include "common.h"
/* We use the POSIX regex functions to process regular expressions.
* Type 'man regex' for more information about POSIX regex functions.
/* We use regex to parse the expression.
* You can use regular expressions as well.
*/
#include <regex>
@ -64,8 +64,8 @@ typedef struct token {
char str[32];
} Token;
static Token tokens[32] __attribute__((used)) = {};
static int nr_token __attribute__((used)) = 0;
static Token tokens[32] = { -1 };
static int nr_token = -1;
static bool make_token(char *e) {
int position = 0;
@ -77,7 +77,6 @@ static bool make_token(char *e) {
while (e[position] != '\0') {
/* Try all rules one by one. */
for (i = 0; i < NR_REGEX; i ++) {
// if (regexec(&re[i], e + position, 1, &pmatch, 0) == 0 && pmatch.rm_so == 0) {
if (std::regex_search(e + position, pmatch, re[i], std::regex_constants::match_continuous)) {
char *substr_start = e + position;
int substr_len = pmatch.length();

View File

@ -38,38 +38,38 @@ static char *code_format =
"}";
static void gen_rand_expr() {
buf[0] = '\0';
buf[0] = '\0';
}
int main(int argc, char *argv[]) {
int seed = time(0);
srand(seed);
int loop = 1;
if (argc > 1) {
sscanf(argv[1], "%d", &loop);
}
int i;
for (i = 0; i < loop; i ++) {
gen_rand_expr();
int seed = time(0);
srand(seed);
int loop = 1;
if (argc > 1) {
sscanf(argv[1], "%d", &loop);
}
int i;
for (i = 0; i < loop; i ++) {
gen_rand_expr();
sprintf(code_buf, code_format, buf);
sprintf(code_buf, code_format, buf);
FILE *fp = fopen(TMP_C_SRC_FILE_PATH, "w");
assert(fp != NULL);
fputs(code_buf, fp);
fclose(fp);
FILE *fp = fopen(TMP_C_SRC_FILE_PATH, "w");
assert(fp != NULL);
fputs(code_buf, fp);
fclose(fp);
int ret = system(TMP_COMPILE_CMD);
if (ret != 0) continue;
int ret = system(TMP_COMPILE_CMD);
if (ret != 0) continue;
fp = popen("." TMP_C_EXE_FILE_PATH, "r");
assert(fp != NULL);
fp = popen("." TMP_C_EXE_FILE_PATH, "r");
assert(fp != NULL);
int result;
ret = fscanf(fp, "%d", &result);
pclose(fp);
int result;
ret = fscanf(fp, "%d", &result);
pclose(fp);
printf("%u %s\n", result, buf);
}
return 0;
printf("%u %s\n", result, buf);
}
return 0;
}

32
main.c
View File

@ -16,6 +16,10 @@
#include "common.h"
#ifndef PRId32 // dev-cpp 5.11 does not support PRId32
#define PRId32 "d"
#endif
void init_regex(void);
uint32_t expr(char *e, bool *success);
@ -29,7 +33,7 @@ static int cmd_p(char *args) {
bool success = true;
uint32_t res = expr(args, &success);
if (success) {
printf("$res = % " PRIdLEAST32 "\n", res);
printf("$res = % " PRId32 "\n", res);
}
return success;
}
@ -56,23 +60,23 @@ static void print_help() {
}
static int cmd_help(char *args) {
/* extract the first argument */
char *arg = strtok(NULL, " ");
int i;
/* extract the first argument */
char *arg = strtok(NULL, " ");
int i;
if (arg == NULL) {
if (arg == NULL) {
/* no argument given */
print_help();
}
else {
for (i = 0; i < NR_CMD; i ++) {
if (strcmp(arg, cmd_table[i].name) == 0) {
printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
return 0;
}
}
printf("Unknown command '%s'\n", arg);
}
else {
for (i = 0; i < NR_CMD; i ++) {
if (strcmp(arg, cmd_table[i].name) == 0) {
printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
return 0;
}
}
printf("Unknown command '%s'\n", arg);
}
return 0;
}