diff --git a/.gitignore b/.gitignore index 6a11570..e6a356c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ -.* +* !.gitignore - -*.o -*.out - -*.obj -*.exe \ No newline at end of file +!^.*.c +!^.*.cpp +!^.*.h +!^.*.md +!Makefile \ No newline at end of file diff --git a/README.md b/README.md index 2397a57..f682fcd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ -注意 由于使用c++的regex库,所以需要编译时需要注意大于等于c++11语法 -注意 这是一个多文件编译的项目,需要你结合main.c和expr.cpp使用 -注意 gen-expr里面需要命令行里编译c语言,所以Windows需要自行配置以保证可以使用system命令编译,以及popen的路径 \ No newline at end of file +- 由于使用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转义码(由于有些终端并不支持) diff --git a/debug.h b/debug.h index 9a8900e..0aeed2f 100644 --- a/debug.h +++ b/debug.h @@ -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 { \ diff --git a/expr.cpp b/expr.cpp index 07e595f..877b783 100644 --- a/expr.cpp +++ b/expr.cpp @@ -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 @@ -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(); diff --git a/gen-expr/gen-expr.c b/gen-expr/gen-expr.c index 5917576..4f37559 100644 --- a/gen-expr/gen-expr.c +++ b/gen-expr/gen-expr.c @@ -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; } \ No newline at end of file diff --git a/main.c b/main.c index 5414848..0e12550 100644 --- a/main.c +++ b/main.c @@ -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; }