> 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

@ -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[]) {