
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 21:39:11 up 20:29, 1 user, load average: 0.31, 0.25, 0.18
112 lines
2.8 KiB
C
112 lines
2.8 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.
|
|
***************************************************************************************/
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
// this should be enough
|
|
static char buf[65536] = {};
|
|
static char code_buf[65536 + 128] = {}; // a little larger than `buf`
|
|
static char *code_format =
|
|
"#include <stdio.h>\n"
|
|
"int main() { "
|
|
" unsigned result = %s; "
|
|
" printf(\"%%u\", result); "
|
|
" 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(int deepth) {
|
|
if (deepth == 0) {
|
|
GEN_RAND();
|
|
return;
|
|
}
|
|
deepth -= 1;
|
|
switch (choose(3)) {
|
|
case 0: GEN_RAND(); break;
|
|
case 1: GEN("("); _gen_rand_expr(deepth); GEN(")"); break;
|
|
case 2: _gen_rand_expr(deepth); GEN_OP(); _gen_rand_expr(deepth); break;
|
|
default: assert(0);
|
|
}
|
|
for (int i = 0; i < choose(3); i ++) {
|
|
GEN(" ");
|
|
}
|
|
}
|
|
|
|
static void gen_rand_expr() {
|
|
buf[0] = '\0';
|
|
pos = 0;
|
|
_gen_rand_expr(choose(10));
|
|
}
|
|
|
|
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();
|
|
|
|
sprintf(code_buf, code_format, buf);
|
|
|
|
FILE *fp = fopen("/tmp/.code.c", "w");
|
|
assert(fp != NULL);
|
|
fputs(code_buf, fp);
|
|
fclose(fp);
|
|
|
|
int ret = system("gcc /tmp/.code.c -o /tmp/.expr -Wall -Werror");
|
|
if (ret != 0) {
|
|
printf("failed to execute gcc\n");
|
|
i ++;
|
|
continue;
|
|
}
|
|
|
|
fp = popen("/tmp/.expr", "r");
|
|
assert(fp != NULL);
|
|
|
|
int result;
|
|
ret = fscanf(fp, "%d", &result);
|
|
pclose(fp);
|
|
|
|
printf("%u %s\n", result, buf);
|
|
}
|
|
return 0;
|
|
}
|