> 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
 09:09:36 up 13:08,  2 users,  load average: 0.25, 0.20, 0.18
This commit is contained in:
tracer-ics2023
2024-08-18 09:09:36 +08:00
committed by zzy
parent f0d9f7c76f
commit bc5f98f070

View File

@ -165,25 +165,26 @@ int check_parentheses(const Token* arr, size_t p, size_t q) {
return !is_surrounded;
}
word_t eval(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 && p < q && q < nr_token, "invalid range on eval, "\
"p = %"PRIuMAX", q = %"PRIuMAX"\n", p, q);
if (p > q || p > nr_token || q > nr_token) {
/* Bad expression */
panic("bad expression");
int parten = check_parentheses(arr, p, q);
if (parten == -1) {
*success = false;
return 0;
}
else if (p == q) {
if (p == q) {
/* Single token.
* For now this token should be a number.
* Return the value of the number.
*/
return atoi(arr[p].str);
}
else if (check_parentheses(arr, p, q) == 0) {
else if (parten == 0) {
/* The expression is surrounded by a matched pair of parentheses.
* If that is the case, just throw away the parentheses.
*/
return eval(arr, p + 1, q - 1);
return eval(arr, p + 1, q - 1, success);
}
else {
/* We should do more things here. */
@ -208,8 +209,11 @@ word_t eval(const Token* arr, size_t p, size_t q) {
op = i;
}
}
word_t val1 = eval(arr, p, op - 1);
word_t val2 = eval(arr, op + 1, q);
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;
@ -218,7 +222,6 @@ word_t eval(const Token* arr, size_t p, size_t q) {
case '/': return val1 / val2;
default: assert(0);
}
return 0;
}
}
@ -231,5 +234,5 @@ word_t expr(char *e, bool *success) {
/* TODO: Insert codes to evaluate the expression. */
// TODO();
*success = true;
return eval(tokens, 0, nr_token - 1);
return eval(tokens, 0, nr_token - 1, success);
}