- 从'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'除外。
121 lines
3.2 KiB
C++
121 lines
3.2 KiB
C++
/***************************************************************************************
|
|
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
|
*
|
|
* NEMU is licensed under Mulan PSL v2.
|
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
* You may obtain a copy of Mulan PSL v2 at:
|
|
* http://license.coscl.org.cn/MulanPSL2
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
*
|
|
* See the Mulan PSL v2 for more details.
|
|
***************************************************************************************/
|
|
// Modified by: Zhiyi Zhang in 2024.08
|
|
|
|
#include "common.h"
|
|
/* We use regex to parse the expression.
|
|
* You can use regular expressions as well.
|
|
*/
|
|
#include <regex>
|
|
|
|
enum {
|
|
TK_NOTYPE = 256, TK_EQ,
|
|
|
|
/* TODO: Add more token types */
|
|
|
|
};
|
|
|
|
static struct rule {
|
|
const char *regex;
|
|
int token_type;
|
|
} rules[] = {
|
|
|
|
/* TODO: Add more rules.
|
|
* Pay attention to the precedence level of different rules.
|
|
*/
|
|
|
|
{" +", TK_NOTYPE}, // spaces
|
|
{"\\+", '+'}, // plus
|
|
{"==", TK_EQ}, // equal
|
|
};
|
|
|
|
#define NR_REGEX ARRLEN(rules)
|
|
|
|
static std::regex re[NR_REGEX] = {};
|
|
|
|
/* Rules are used for many times.
|
|
* Therefore we compile them only once before any usage.
|
|
*/
|
|
void init_regex() {
|
|
for (int i = 0; i < NR_REGEX; i ++) {
|
|
try {
|
|
re[i] = std::regex(rules[i].regex, std::regex_constants::ECMAScript);
|
|
}
|
|
catch(const std::regex_error & e) {
|
|
panic("regex compilation failed: %s\n%s", e.what(), rules[i].regex);
|
|
}
|
|
}
|
|
}
|
|
|
|
typedef struct token {
|
|
int type;
|
|
char str[32];
|
|
} Token;
|
|
|
|
static Token tokens[32] = { -1 };
|
|
static int nr_token = -1;
|
|
|
|
static bool make_token(char *e) {
|
|
int position = 0;
|
|
int i;
|
|
std::cmatch pmatch;
|
|
|
|
nr_token = 0;
|
|
|
|
while (e[position] != '\0') {
|
|
/* Try all rules one by one. */
|
|
for (i = 0; i < NR_REGEX; i ++) {
|
|
if (std::regex_search(e + position, pmatch, re[i], std::regex_constants::match_continuous)) {
|
|
char *substr_start = e + position;
|
|
int substr_len = pmatch.length();
|
|
|
|
Log("match rules[%d] = \"%s\" at position %d with len %d: %.*s",
|
|
i, rules[i].regex, position, substr_len, substr_len, substr_start);
|
|
|
|
position += substr_len;
|
|
|
|
/* TODO: Now a new token is recognized with rules[i]. Add codes
|
|
* to record the token in the array `tokens'. For certain types
|
|
* of tokens, some extra actions should be performed.
|
|
*/
|
|
|
|
switch (rules[i].token_type) {
|
|
default: TODO();
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (i == NR_REGEX) {
|
|
printf("no match at position %d\n%s\n%*.s^\n", position, e, position, "");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
uint32_t expr(char *e, bool *success) {
|
|
if (!make_token(e)) {
|
|
*success = false;
|
|
return 0;
|
|
}
|
|
|
|
/* TODO: Insert codes to evaluate the expression. */
|
|
TODO();
|
|
|
|
return 0;
|
|
} |