Skip to content

Commit

Permalink
Add && and ||
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 8644006 commit f30f781
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
2 changes: 2 additions & 0 deletions chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ typedef enum {
ND_DEREF, // unary *
ND_NOT, // !
ND_BITNOT, // ~
ND_LOGAND, // &&
ND_LOGOR, // ||
ND_RETURN, // "return"
ND_IF, // "if"
ND_FOR, // "for" or "while"
Expand Down
30 changes: 30 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ static void gen_expr(Node *node) {
gen_expr(node->lhs);
println(" not %%rax");
return;
case ND_LOGAND: {
int c = count();
gen_expr(node->lhs);
println(" cmp $0, %%rax");
println(" je .L.false.%d", c);
gen_expr(node->rhs);
println(" cmp $0, %%rax");
println(" je .L.false.%d", c);
println(" mov $1, %%rax");
println(" jmp .L.end.%d", c);
println(".L.false.%d:", c);
println(" mov $0, %%rax");
println(".L.end.%d:", c);
return;
}
case ND_LOGOR: {
int c = count();
gen_expr(node->lhs);
println(" cmp $0, %%rax");
println(" jne .L.true.%d", c);
gen_expr(node->rhs);
println(" cmp $0, %%rax");
println(" jne .L.true.%d", c);
println(" mov $0, %%rax");
println(" jmp .L.end.%d", c);
println(".L.true.%d:", c);
println(" mov $1, %%rax");
println(".L.end.%d:", c);
return;
}
case ND_FUNCALL: {
int nargs = 0;
for (Node *arg = node->args; arg; arg = arg->next) {
Expand Down
28 changes: 26 additions & 2 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static Node *stmt(Token **rest, Token *tok);
static Node *expr_stmt(Token **rest, Token *tok);
static Node *expr(Token **rest, Token *tok);
static Node *assign(Token **rest, Token *tok);
static Node *logor(Token **rest, Token *tok);
static Node *logand(Token **rest, Token *tok);
static Node *bitor(Token **rest, Token *tok);
static Node *bitxor(Token **rest, Token *tok);
static Node *bitand(Token **rest, Token *tok);
Expand Down Expand Up @@ -699,10 +701,10 @@ static Node *to_assign(Node *binary) {
return new_binary(ND_COMMA, expr1, expr2, tok);
}

// assign = bitor (assign-op assign)?
// assign = logor (assign-op assign)?
// assign-op = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^="
static Node *assign(Token **rest, Token *tok) {
Node *node = bitor(&tok, tok);
Node *node = logor(&tok, tok);

if (equal(tok, "="))
return new_binary(ND_ASSIGN, node, assign(rest, tok->next), tok);
Expand Down Expand Up @@ -735,6 +737,28 @@ static Node *assign(Token **rest, Token *tok) {
return node;
}

// logor = logand ("||" logand)*
static Node *logor(Token **rest, Token *tok) {
Node *node = logand(&tok, tok);
while (equal(tok, "||")) {
Token *start = tok;
node = new_binary(ND_LOGOR, node, logand(&tok, tok->next), start);
}
*rest = tok;
return node;
}

// logand = bitor ("&&" bitor)*
static Node *logand(Token **rest, Token *tok) {
Node *node = bitor(&tok, tok);
while (equal(tok, "&&")) {
Token *start = tok;
node = new_binary(ND_LOGAND, node, bitor(&tok, tok->next), start);
}
*rest = tok;
return node;
}

// bitor = bitxor ("|" bitxor)*
static Node *bitor(Token **rest, Token *tok) {
Node *node = bitxor(&tok, tok);
Expand Down
9 changes: 9 additions & 0 deletions test/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ int main() {
ASSERT(55, ({ int j=0; for (int i=0; i<=10; i=i+1) j=j+i; j; }));
ASSERT(3, ({ int i=3; int j=0; for (int i=0; i<=10; i=i+1) j=j+i; i; }));

ASSERT(1, 0||1);
ASSERT(1, 0||(2-2)||5);
ASSERT(0, 0||0);
ASSERT(0, 0||(2-2));

ASSERT(0, 0&&1);
ASSERT(0, (2-2)&&5);
ASSERT(1, 1&&5);

printf("OK\n");
return 0;
}
2 changes: 1 addition & 1 deletion tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static int from_hex(char c) {
static int read_punct(char *p) {
static char *kw[] = {
"==", "!=", "<=", ">=", "->", "+=", "-=", "*=", "/=", "++", "--",
"%=", "&=", "|=", "^=",
"%=", "&=", "|=", "^=", "&&", "||",
};

for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
Expand Down
2 changes: 2 additions & 0 deletions type.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ void add_type(Node *node) {
node->ty = ty_long;
return;
case ND_NOT:
case ND_LOGOR:
case ND_LOGAND:
node->ty = ty_int;
return;
case ND_BITNOT:
Expand Down

0 comments on commit f30f781

Please sign in to comment.