mirror of
https://github.com/rui314/chibicc
synced 2024-11-22 06:11:18 +03:00
Add unary plus and minus
This commit is contained in:
parent
84cfcaf98f
commit
bf9ab52860
35
main.c
35
main.c
@ -134,6 +134,7 @@ typedef enum {
|
||||
ND_SUB, // -
|
||||
ND_MUL, // *
|
||||
ND_DIV, // /
|
||||
ND_NEG, // unary -
|
||||
ND_NUM, // Integer
|
||||
} NodeKind;
|
||||
|
||||
@ -159,6 +160,12 @@ static Node *new_binary(NodeKind kind, Node *lhs, Node *rhs) {
|
||||
return node;
|
||||
}
|
||||
|
||||
static Node *new_unary(NodeKind kind, Node *expr) {
|
||||
Node *node = new_node(kind);
|
||||
node->lhs = expr;
|
||||
return node;
|
||||
}
|
||||
|
||||
static Node *new_num(int val) {
|
||||
Node *node = new_node(ND_NUM);
|
||||
node->val = val;
|
||||
@ -167,6 +174,7 @@ static Node *new_num(int val) {
|
||||
|
||||
static Node *expr(Token **rest, Token *tok);
|
||||
static Node *mul(Token **rest, Token *tok);
|
||||
static Node *unary(Token **rest, Token *tok);
|
||||
static Node *primary(Token **rest, Token *tok);
|
||||
|
||||
// expr = mul ("+" mul | "-" mul)*
|
||||
@ -189,18 +197,18 @@ static Node *expr(Token **rest, Token *tok) {
|
||||
}
|
||||
}
|
||||
|
||||
// mul = primary ("*" primary | "/" primary)*
|
||||
// mul = unary ("*" unary | "/" unary)*
|
||||
static Node *mul(Token **rest, Token *tok) {
|
||||
Node *node = primary(&tok, tok);
|
||||
Node *node = unary(&tok, tok);
|
||||
|
||||
for (;;) {
|
||||
if (equal(tok, "*")) {
|
||||
node = new_binary(ND_MUL, node, primary(&tok, tok->next));
|
||||
node = new_binary(ND_MUL, node, unary(&tok, tok->next));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (equal(tok, "/")) {
|
||||
node = new_binary(ND_DIV, node, primary(&tok, tok->next));
|
||||
node = new_binary(ND_DIV, node, unary(&tok, tok->next));
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -209,6 +217,18 @@ static Node *mul(Token **rest, Token *tok) {
|
||||
}
|
||||
}
|
||||
|
||||
// unary = ("+" | "-") unary
|
||||
// | primary
|
||||
static Node *unary(Token **rest, Token *tok) {
|
||||
if (equal(tok, "+"))
|
||||
return unary(rest, tok->next);
|
||||
|
||||
if (equal(tok, "-"))
|
||||
return new_unary(ND_NEG, unary(rest, tok->next));
|
||||
|
||||
return primary(rest, tok);
|
||||
}
|
||||
|
||||
// primary = "(" expr ")" | num
|
||||
static Node *primary(Token **rest, Token *tok) {
|
||||
if (equal(tok, "(")) {
|
||||
@ -243,9 +263,14 @@ static void pop(char *arg) {
|
||||
}
|
||||
|
||||
static void gen_expr(Node *node) {
|
||||
if (node->kind == ND_NUM) {
|
||||
switch (node->kind) {
|
||||
case ND_NUM:
|
||||
printf(" mov $%d, %%rax\n", node->val);
|
||||
return;
|
||||
case ND_NEG:
|
||||
gen_expr(node->lhs);
|
||||
printf(" neg %%rax\n");
|
||||
return;
|
||||
}
|
||||
|
||||
gen_expr(node->rhs);
|
||||
|
Loading…
Reference in New Issue
Block a user