diff --git a/boot.c b/boot.c index ba8b11e..9093ce6 100644 --- a/boot.c +++ b/boot.c @@ -249,12 +249,16 @@ void next_token() { } else if (ch == '/') { int ch2 = getchar(); if (ch2 == '/') { - while ((ch = getchar()) != '\n'); + do ch = getchar(); while (ch != '\n' && ch != -1); next_token(); return; } else if (ch2 == '*') { while (1) { ch = getchar(); + if (ch == -1) { + eprintf("expecting '*/'\n"); + exit(1); + } if (ch == '*') { ch = getchar(); if (ch == '/') { @@ -773,7 +777,7 @@ int parse_postfix_expr() { store_t0(reg); printf(" addi t0, t0, %d\n", step_of(type)); store_t0(lhs); - return reg; + lhs = reg; } else if (token_type == TOKEN_DEC) { int type = local_type[lhs]; int reg = next_reg(type); @@ -781,11 +785,11 @@ int parse_postfix_expr() { store_t0(reg); printf(" addi t0, t0, -%d\n", step_of(type)); store_t0(lhs); - return reg; + lhs = reg; } else if (token_type == TOKEN_BRACKET_LEFT) { int rhs = parse_expr(); expect_token(TOKEN_BRACKET_RIGHT); - return indirection_of(asm_add(lhs, rhs)); + lhs = indirection_of(asm_add(lhs, rhs)); } else if (token_type == TOKEN_PAREN_LEFT) { int arg = 0; int args[8]; @@ -817,12 +821,13 @@ int parse_postfix_expr() { load_address(0, lhs); printf(" jalr t0\n"); printf(" mv t0, a0\n"); - return materialize_t0(local_type[lhs]); + lhs = materialize_t0(local_type[lhs]); } else { unget_token(); - return lhs; + break; } } + return lhs; } int parse_prefix_expr() { diff --git a/demo/queen.c b/demo/queen.c new file mode 100644 index 0000000..ac4a310 --- /dev/null +++ b/demo/queen.c @@ -0,0 +1,47 @@ +int printf(const char format[], ...); +int putchar(int ch); + +int a[9]; + +void output() { + for (int i = 1; i <= 8; ++i) { + for (int j = 1; j <= 8; ++j) { + if (a[i] == j) { + putchar('x'); + } else { + putchar('-'); + } + } + putchar('\n'); + } + putchar('\n'); +} + +int ok(int x, int y) { + for (int i = 1; i <= x - 1; ++i) { + if (a[i] == y || a[i] - i == y - x || a[i] + i == y + x) { + return 0; + } + } + return 1; +} + +void queen(int x) { + if (x > 8) { + output(); + a[0]++; + } + for (int y = 1; y <= 8; ++y) { + if (ok(x, y)) { + a[x] = y; + queen(x + 1); + a[x] = 0; + } + } +} + +int main() { + queen(1); + printf("solutions: %d\n", a[0]); + return 0; +} \ No newline at end of file