> 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
 08:56:51 up 4 days, 33 min,  1 user,  load average: 0.28, 0.24, 0.14
This commit is contained in:
tracer-ics2023
2024-08-27 08:56:51 +08:00
committed by zzy
parent 838f5864ec
commit cedb501fea

View File

@ -177,65 +177,55 @@ word_t eval(const Token* arr, size_t p, size_t q, bool* success) {
}
if (p == q || p == q - 1) {
if (p == q) {
/* Single token.
* For now this token should be a number.
* Return the value of the number.
*/
if (arr[q].type != TK_DEC) {
*success &= false;
return 0;
}
return atoi(arr[q].str);
}
/* We should do more things here. */
size_t op = 0;
int in_parentheses = 0;
if (p == q) {
return atoi(arr[q].str);
if (arr[p].type == '-') {
return - eval(arr, p + 1, q, success);
}
for (size_t i = p; i <= q; i ++) {
if (arr[i].type == '(') {
in_parentheses ++;
}
if (arr[p].type == '-') {
return - atoi(arr[q].str);
if (arr[i].type == ')') {
in_parentheses --;
}
*success &= false;
if (in_parentheses != 0) {
continue;
}
if (arr[i].type == '+' || arr[i].type == '-') {
op = i;
} else if (arr[i].type == '*' || arr[i].type == '/') {
op = i;
}
if (i + 1 <= q && arr[i + 1].type == '-') {
i++;
}
}
word_t val1 = eval(arr, p, op - 1, success);
word_t val2 = eval(arr, op + 1, q, success);
if (success == false) {
return 0;
}
else {
/* We should do more things here. */
size_t op = 0;
int in_parentheses = 0;
for (size_t i = p; i <= q; i ++) {
if (arr[i].type == '(') {
in_parentheses ++;
}
if (arr[i].type == ')') {
in_parentheses --;
}
if (in_parentheses != 0) {
continue;
}
if (arr[i].type == '+' || arr[i].type == '-') {
op = i;
} else if (arr[i].type == '*' || arr[i].type == '/') {
op = i;
}
if (i + 1 <= q && arr[i + 1].type == '-') {
i++;
}
}
word_t val1 = eval(arr, p, op - 1, success);
word_t val2 = eval(arr, op + 1, q, success);
if (success == false) {
return 0;
}
switch (arr[op].type) {
case '+': return val1 + val2;
case '-': return val1 - val2;
case '*': return val1 * val2;
case '/': return val1 / val2;
default: assert(0);
}
switch (arr[op].type) {
case '+': return val1 + val2;
case '-': return val1 - val2;
case '*': return val1 * val2;
case '/': return val1 / val2;
default:
*success &= false;
return 0;
}
}