> compile NEMU

221220000 张三
Linux zzy 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
 16:50:15 up 19:15,  2 users,  load average: 0.46, 0.30, 0.20
This commit is contained in:
tracer-ics2023
2024-08-18 16:50:16 +08:00
committed by zzy
parent 32620cedd9
commit 74c333d534
3 changed files with 49 additions and 0 deletions

View File

@ -167,6 +167,7 @@ int check_parentheses(const Token* arr, size_t p, size_t q) {
word_t eval(const Token* arr, size_t p, size_t q, bool* success) {
Assert (p >= 0 && q < nr_token, "invalid range on eval, "\
"p = %"PRIuMAX", q = %"PRIuMAX"\n", p, q);
int parten = check_parentheses(arr, p, q);
if (parten == -1 || p > q) {
*success = false;

View File

@ -170,10 +170,29 @@ void sdb_set_batch_mode() {
is_batch_mode = true;
}
int test_expr() {
Log("test_expr");
uint32_t expect, res;
char buf[65536] = {0};
if (scanf("%d %s", &expect, buf));
res = expr(buf, NULL);
if (res == expect) {
printf("True :");
} else {
printf("Failed:");
}
printf("%"PRIuLEAST32" : %"PRIuLEAST32"\n", expect, res);
return 0;
}
void sdb_mainloop() {
#ifdef _TEST_SDB_MAIN_LOOP_
Log("_TEST_SDB_MAIN_LOOP_");
#endif
#ifdef _TEST_EXPR_
return test_expr();
#endif
if (is_batch_mode) {
cmd_c(NULL);

View File

@ -31,8 +31,37 @@ static char *code_format =
" return 0; "
"}";
static inline int choose(int n) {
return rand() % n;
}
static int pos;
#define _GEN(buf, pos, fmt, ...) (pos += snprintf(buf + pos, sizeof(buf) - pos, fmt, ##__VA_ARGS__))
#define GEN(fmt, ...) _GEN(buf, pos, fmt, ##__VA_ARGS__)
#define RAND_NUM_MAX 100
#define GEN_RAND() GEN("%d", choose(RAND_NUM_MAX))
#define GEN_OP() do{ \
switch (choose(4)) { \
case 0: GEN("+"); break; \
case 1: GEN("-"); break; \
case 2: GEN("*"); break; \
case 3: GEN("/"); break; \
} \
} while(0)
static void _gen_rand_expr() {
switch (choose(3)) {
case 0: GEN_RAND(); break;
case 1: GEN("("); _gen_rand_expr(); GEN(")"); break;
default: _gen_rand_expr(); GEN_OP(); _gen_rand_expr(); break;
}
}
static void gen_rand_expr() {
buf[0] = '\0';
pos = 0;
_gen_rand_expr();
}
int main(int argc, char *argv[]) {