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 !.gitignore
!^.*.c
*.o !^.*.cpp
*.out !^.*.h
!^.*.md
*.obj !Makefile
*.exe

View File

@ -1,3 +1,7 @@
注意 由于使用c++的regex库所以需要编译时需要注意大于等于c++11语法 - 由于使用c++的regex库所以需要编译时需要至少c++11标准才能运行
注意 这是一个多文件编译的项目需要你结合main.c和expr.cpp使用
注意 gen-expr里面需要命令行里编译c语言所以Windows需要自行配置以保证可以使用system命令编译以及popen的路径 - 这是一个多文件编译的项目需要你结合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_BG_WHITE "\33[1;47m"
#define ANSI_NONE "\33[0m" #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 #define ANSI_FMT(str, fmt) fmt str ANSI_NONE
#else
#define ANSI_FMT(str, fmt) str
#endif
#define _Log(...) \ #define _Log(...) \
do { \ do { \

View File

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

6
main.c
View File

@ -16,6 +16,10 @@
#include "common.h" #include "common.h"
#ifndef PRId32 // dev-cpp 5.11 does not support PRId32
#define PRId32 "d"
#endif
void init_regex(void); void init_regex(void);
uint32_t expr(char *e, bool *success); uint32_t expr(char *e, bool *success);
@ -29,7 +33,7 @@ static int cmd_p(char *args) {
bool success = true; bool success = true;
uint32_t res = expr(args, &success); uint32_t res = expr(args, &success);
if (success) { if (success) {
printf("$res = % " PRIdLEAST32 "\n", res); printf("$res = % " PRId32 "\n", res);
} }
return success; return success;
} }