mirror of https://github.com/rui314/chibicc
Add ! operator
This commit is contained in:
parent
01a451653c
commit
3bd2f40b82
|
@ -103,6 +103,7 @@ typedef enum {
|
|||
ND_MEMBER, // . (struct member access)
|
||||
ND_ADDR, // unary &
|
||||
ND_DEREF, // unary *
|
||||
ND_NOT, // !
|
||||
ND_RETURN, // "return"
|
||||
ND_IF, // "if"
|
||||
ND_WHILE, // "while"
|
||||
|
|
|
@ -222,6 +222,14 @@ void gen(Node *node) {
|
|||
if (node->ty->kind != TY_ARRAY)
|
||||
load(node->ty);
|
||||
return;
|
||||
case ND_NOT:
|
||||
gen(node->lhs);
|
||||
printf(" pop rax\n");
|
||||
printf(" cmp rax, 0\n");
|
||||
printf(" sete al\n");
|
||||
printf(" movzb rax, al\n");
|
||||
printf(" push rax\n");
|
||||
return;
|
||||
case ND_IF: {
|
||||
int seq = labelseq++;
|
||||
if (node->els) {
|
||||
|
|
|
@ -38,8 +38,7 @@ int solve(int (*board)[10], int row) {
|
|||
return 0;
|
||||
}
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (conflict(board, row, i)) {
|
||||
} else {
|
||||
if (!conflict(board, row, i)) {
|
||||
board[row][i] = 1;
|
||||
solve(board, row + 1);
|
||||
board[row][i] = 0;
|
||||
|
|
4
parse.c
4
parse.c
|
@ -781,7 +781,7 @@ Node *cast() {
|
|||
return unary();
|
||||
}
|
||||
|
||||
// unary = ("+" | "-" | "*" | "&")? cast
|
||||
// unary = ("+" | "-" | "*" | "&" | "!")? cast
|
||||
// | ("++" | "--") unary
|
||||
// | postfix
|
||||
Node *unary() {
|
||||
|
@ -794,6 +794,8 @@ Node *unary() {
|
|||
return new_unary(ND_ADDR, cast(), tok);
|
||||
if (tok = consume("*"))
|
||||
return new_unary(ND_DEREF, cast(), tok);
|
||||
if (tok = consume("!"))
|
||||
return new_unary(ND_NOT, cast(), tok);
|
||||
if (tok = consume("++"))
|
||||
return new_unary(ND_PRE_INC, unary(), tok);
|
||||
if (tok = consume("--"))
|
||||
|
|
4
tests
4
tests
|
@ -377,6 +377,10 @@ int main() {
|
|||
assert(3, ({ int i=6; i/=2; i; }), "int i=6; i/=2; i;");
|
||||
assert(3, ({ int i=6; i/=2; }), "int i=6; i/=2;");
|
||||
|
||||
assert(0, !1, "!1");
|
||||
assert(0, !2, "!2");
|
||||
assert(1, !0, "!0");
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -279,7 +279,7 @@ Token *tokenize() {
|
|||
}
|
||||
|
||||
// Single-letter punctuator
|
||||
if (strchr("+-*/()<>;={},&[].,", *p)) {
|
||||
if (strchr("+-*/()<>;={},&[].,!", *p)) {
|
||||
cur = new_token(TK_RESERVED, cur, p++, 1);
|
||||
continue;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue