2020-10-07 14:11:16 +03:00
|
|
|
#include "chibicc.h"
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
// All local variable instances created during parsing are
|
|
|
|
// accumulated to this list.
|
|
|
|
Obj *locals;
|
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
static Node *compound_stmt(Token **rest, Token *tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *expr(Token **rest, Token *tok);
|
2020-09-26 02:50:44 +03:00
|
|
|
static Node *expr_stmt(Token **rest, Token *tok);
|
2020-09-26 02:59:56 +03:00
|
|
|
static Node *assign(Token **rest, Token *tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *equality(Token **rest, Token *tok);
|
|
|
|
static Node *relational(Token **rest, Token *tok);
|
|
|
|
static Node *add(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);
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
// Find a local variable by name.
|
|
|
|
static Obj *find_var(Token *tok) {
|
|
|
|
for (Obj *var = locals; var; var = var->next)
|
|
|
|
if (strlen(var->name) == tok->len && !strncmp(tok->loc, var->name, tok->len))
|
|
|
|
return var;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *new_node(NodeKind kind) {
|
|
|
|
Node *node = calloc(1, sizeof(Node));
|
|
|
|
node->kind = kind;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Node *new_binary(NodeKind kind, Node *lhs, Node *rhs) {
|
|
|
|
Node *node = new_node(kind);
|
|
|
|
node->lhs = lhs;
|
|
|
|
node->rhs = 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;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
static Node *new_var_node(Obj *var) {
|
2020-09-26 02:59:56 +03:00
|
|
|
Node *node = new_node(ND_VAR);
|
2020-10-07 14:12:19 +03:00
|
|
|
node->var = var;
|
2020-09-26 02:59:56 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
static Obj *new_lvar(char *name) {
|
|
|
|
Obj *var = calloc(1, sizeof(Obj));
|
|
|
|
var->name = name;
|
|
|
|
var->next = locals;
|
|
|
|
locals = var;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:57 +03:00
|
|
|
// stmt = "return" expr ";"
|
2020-09-04 07:38:41 +03:00
|
|
|
// | "{" compound-stmt
|
2020-10-07 14:12:57 +03:00
|
|
|
// | expr-stmt
|
2020-09-26 02:50:44 +03:00
|
|
|
static Node *stmt(Token **rest, Token *tok) {
|
2020-10-07 14:12:57 +03:00
|
|
|
if (equal(tok, "return")) {
|
|
|
|
Node *node = new_unary(ND_RETURN, expr(&tok, tok->next));
|
|
|
|
*rest = skip(tok, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
if (equal(tok, "{"))
|
|
|
|
return compound_stmt(rest, tok->next);
|
|
|
|
|
2020-09-26 02:50:44 +03:00
|
|
|
return expr_stmt(rest, tok);
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
// compound-stmt = stmt* "}"
|
|
|
|
static Node *compound_stmt(Token **rest, Token *tok) {
|
|
|
|
Node head = {};
|
|
|
|
Node *cur = &head;
|
|
|
|
while (!equal(tok, "}"))
|
|
|
|
cur = cur->next = stmt(&tok, tok);
|
|
|
|
|
|
|
|
Node *node = new_node(ND_BLOCK);
|
|
|
|
node->body = head.next;
|
|
|
|
*rest = tok->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:50:44 +03:00
|
|
|
// expr-stmt = expr ";"
|
|
|
|
static Node *expr_stmt(Token **rest, Token *tok) {
|
|
|
|
Node *node = new_unary(ND_EXPR_STMT, expr(&tok, tok));
|
|
|
|
*rest = skip(tok, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
// expr = assign
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *expr(Token **rest, Token *tok) {
|
2020-09-26 02:59:56 +03:00
|
|
|
return assign(rest, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign = equality ("=" assign)?
|
|
|
|
static Node *assign(Token **rest, Token *tok) {
|
|
|
|
Node *node = equality(&tok, tok);
|
|
|
|
if (equal(tok, "="))
|
|
|
|
node = new_binary(ND_ASSIGN, node, assign(&tok, tok->next));
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// equality = relational ("==" relational | "!=" relational)*
|
|
|
|
static Node *equality(Token **rest, Token *tok) {
|
|
|
|
Node *node = relational(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (equal(tok, "==")) {
|
|
|
|
node = new_binary(ND_EQ, node, relational(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "!=")) {
|
|
|
|
node = new_binary(ND_NE, node, relational(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
|
|
|
|
static Node *relational(Token **rest, Token *tok) {
|
|
|
|
Node *node = add(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (equal(tok, "<")) {
|
|
|
|
node = new_binary(ND_LT, node, add(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "<=")) {
|
|
|
|
node = new_binary(ND_LE, node, add(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">")) {
|
|
|
|
node = new_binary(ND_LT, add(&tok, tok->next), node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">=")) {
|
|
|
|
node = new_binary(ND_LE, add(&tok, tok->next), node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add = mul ("+" mul | "-" mul)*
|
|
|
|
static Node *add(Token **rest, Token *tok) {
|
|
|
|
Node *node = mul(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (equal(tok, "+")) {
|
|
|
|
node = new_binary(ND_ADD, node, mul(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "-")) {
|
|
|
|
node = new_binary(ND_SUB, node, mul(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mul = unary ("*" unary | "/" unary)*
|
|
|
|
static Node *mul(Token **rest, Token *tok) {
|
|
|
|
Node *node = unary(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (equal(tok, "*")) {
|
|
|
|
node = new_binary(ND_MUL, node, unary(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "/")) {
|
|
|
|
node = new_binary(ND_DIV, node, unary(&tok, tok->next));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
// primary = "(" expr ")" | ident | num
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *primary(Token **rest, Token *tok) {
|
|
|
|
if (equal(tok, "(")) {
|
|
|
|
Node *node = expr(&tok, tok->next);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
if (tok->kind == TK_IDENT) {
|
2020-10-07 14:12:19 +03:00
|
|
|
Obj *var = find_var(tok);
|
|
|
|
if (!var)
|
|
|
|
var = new_lvar(strndup(tok->loc, tok->len));
|
2020-09-26 02:59:56 +03:00
|
|
|
*rest = tok->next;
|
2020-10-07 14:12:19 +03:00
|
|
|
return new_var_node(var);
|
2020-09-26 02:59:56 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (tok->kind == TK_NUM) {
|
|
|
|
Node *node = new_num(tok->val);
|
|
|
|
*rest = tok->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(tok, "expected an expression");
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:50:44 +03:00
|
|
|
// program = stmt*
|
2020-10-07 14:12:19 +03:00
|
|
|
Function *parse(Token *tok) {
|
2020-09-04 07:38:41 +03:00
|
|
|
tok = skip(tok, "{");
|
2020-10-07 14:12:19 +03:00
|
|
|
|
|
|
|
Function *prog = calloc(1, sizeof(Function));
|
2020-09-04 07:38:41 +03:00
|
|
|
prog->body = compound_stmt(&tok, tok);
|
2020-10-07 14:12:19 +03:00
|
|
|
prog->locals = locals;
|
|
|
|
return prog;
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|