chibicc/parse.c

395 lines
9.6 KiB
C
Raw Normal View History

2019-08-05 15:12:44 +03:00
// This file contains a recursive descent parser for C.
//
// Most functions in this file are named after the symbols they are
// supposed to read from an input token list. For example, stmt() is
// responsible for reading a statement from a token list. The function
// then construct an AST node representing a statement.
//
// Each function conceptually returns two values, an AST node and
// remaining part of the input tokens. Since C doesn't support
// multiple return values, the remaining tokens are returned to the
// caller via a pointer argument.
//
// Input tokens are represented by a linked list. Unlike many recursive
// descent parsers, we don't have the notion of the "input token stream".
// Most parsing functions don't change the global state of the parser.
// So it is very easy to lookahead arbitrary number of tokens in this
// parser.
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-09-04 07:39:06 +03:00
static Node *stmt(Token **rest, Token *tok);
static Node *expr_stmt(Token **rest, Token *tok);
static Node *expr(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;
}
static Node *new_node(NodeKind kind, Token *tok) {
2020-10-07 14:11:16 +03:00
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->tok = tok;
2020-10-07 14:11:16 +03:00
return node;
}
static Node *new_binary(NodeKind kind, Node *lhs, Node *rhs, Token *tok) {
Node *node = new_node(kind, tok);
2020-10-07 14:11:16 +03:00
node->lhs = lhs;
node->rhs = rhs;
return node;
}
static Node *new_unary(NodeKind kind, Node *expr, Token *tok) {
Node *node = new_node(kind, tok);
2020-10-07 14:11:16 +03:00
node->lhs = expr;
return node;
}
static Node *new_num(int val, Token *tok) {
Node *node = new_node(ND_NUM, tok);
2020-10-07 14:11:16 +03:00
node->val = val;
return node;
}
static Node *new_var_node(Obj *var, Token *tok) {
Node *node = new_node(ND_VAR, tok);
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-10-07 06:47:09 +03:00
// | "if" "(" expr ")" stmt ("else" stmt)?
2019-08-04 11:35:53 +03:00
// | "for" "(" expr-stmt expr? ";" expr? ")" stmt
2019-08-04 11:24:03 +03:00
// | "while" "(" expr ")" stmt
2020-09-04 07:38:41 +03:00
// | "{" compound-stmt
2020-10-07 14:12:57 +03:00
// | expr-stmt
static Node *stmt(Token **rest, Token *tok) {
2020-10-07 14:12:57 +03:00
if (equal(tok, "return")) {
Node *node = new_node(ND_RETURN, tok);
node->lhs = expr(&tok, tok->next);
2020-10-07 14:12:57 +03:00
*rest = skip(tok, ";");
return node;
}
2020-10-07 06:47:09 +03:00
if (equal(tok, "if")) {
Node *node = new_node(ND_IF, tok);
2020-10-07 06:47:09 +03:00
tok = skip(tok->next, "(");
node->cond = expr(&tok, tok);
tok = skip(tok, ")");
node->then = stmt(&tok, tok);
if (equal(tok, "else"))
node->els = stmt(&tok, tok->next);
*rest = tok;
return node;
}
2019-08-04 11:35:53 +03:00
if (equal(tok, "for")) {
Node *node = new_node(ND_FOR, tok);
2019-08-04 11:35:53 +03:00
tok = skip(tok->next, "(");
node->init = expr_stmt(&tok, tok);
if (!equal(tok, ";"))
node->cond = expr(&tok, tok);
tok = skip(tok, ";");
if (!equal(tok, ")"))
node->inc = expr(&tok, tok);
tok = skip(tok, ")");
node->then = stmt(rest, tok);
return node;
}
2019-08-04 11:24:03 +03:00
if (equal(tok, "while")) {
Node *node = new_node(ND_FOR, tok);
2019-08-04 11:24:03 +03:00
tok = skip(tok->next, "(");
node->cond = expr(&tok, tok);
tok = skip(tok, ")");
node->then = stmt(rest, tok);
return node;
}
2020-09-04 07:38:41 +03:00
if (equal(tok, "{"))
return compound_stmt(rest, tok->next);
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 *node = new_node(ND_BLOCK, tok);
2020-09-04 07:38:41 +03:00
Node head = {};
Node *cur = &head;
2020-09-04 07:39:06 +03:00
while (!equal(tok, "}")) {
2020-09-04 07:38:41 +03:00
cur = cur->next = stmt(&tok, tok);
2020-09-04 07:39:06 +03:00
add_type(cur);
}
2020-09-04 07:38:41 +03:00
node->body = head.next;
*rest = tok->next;
return node;
}
2020-05-14 05:05:01 +03:00
// expr-stmt = expr? ";"
static Node *expr_stmt(Token **rest, Token *tok) {
2020-05-14 05:05:01 +03:00
if (equal(tok, ";")) {
*rest = tok->next;
return new_node(ND_BLOCK, tok);
2020-05-14 05:05:01 +03:00
}
Node *node = new_node(ND_EXPR_STMT, tok);
node->lhs = 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);
2020-09-26 02:59:56 +03:00
if (equal(tok, "="))
return new_binary(ND_ASSIGN, node, assign(rest, tok->next), tok);
2020-09-26 02:59:56 +03:00
*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 (;;) {
Token *start = tok;
2020-10-07 14:11:16 +03:00
if (equal(tok, "==")) {
node = new_binary(ND_EQ, node, relational(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
if (equal(tok, "!=")) {
node = new_binary(ND_NE, node, relational(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
*rest = tok;
return node;
}
}
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
static Node *relational(Token **rest, Token *tok) {
Node *node = add(&tok, tok);
for (;;) {
Token *start = tok;
2020-10-07 14:11:16 +03:00
if (equal(tok, "<")) {
node = new_binary(ND_LT, node, add(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
if (equal(tok, "<=")) {
node = new_binary(ND_LE, node, add(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
if (equal(tok, ">")) {
node = new_binary(ND_LT, add(&tok, tok->next), node, start);
2020-10-07 14:11:16 +03:00
continue;
}
if (equal(tok, ">=")) {
node = new_binary(ND_LE, add(&tok, tok->next), node, start);
2020-10-07 14:11:16 +03:00
continue;
}
*rest = tok;
return node;
}
}
2020-09-04 07:39:06 +03:00
// In C, `+` operator is overloaded to perform the pointer arithmetic.
// If p is a pointer, p+n adds not n but sizeof(*p)*n to the value of p,
// so that p+n points to the location n elements (not bytes) ahead of p.
// In other words, we need to scale an integer value before adding to a
// pointer value. This function takes care of the scaling.
static Node *new_add(Node *lhs, Node *rhs, Token *tok) {
add_type(lhs);
add_type(rhs);
// num + num
if (is_integer(lhs->ty) && is_integer(rhs->ty))
return new_binary(ND_ADD, lhs, rhs, tok);
if (lhs->ty->base && rhs->ty->base)
error_tok(tok, "invalid operands");
// Canonicalize `num + ptr` to `ptr + num`.
if (!lhs->ty->base && rhs->ty->base) {
Node *tmp = lhs;
lhs = rhs;
rhs = tmp;
}
// ptr + num
rhs = new_binary(ND_MUL, rhs, new_num(8, tok), tok);
return new_binary(ND_ADD, lhs, rhs, tok);
}
// Like `+`, `-` is overloaded for the pointer type.
static Node *new_sub(Node *lhs, Node *rhs, Token *tok) {
add_type(lhs);
add_type(rhs);
// num - num
if (is_integer(lhs->ty) && is_integer(rhs->ty))
return new_binary(ND_SUB, lhs, rhs, tok);
// ptr - num
if (lhs->ty->base && is_integer(rhs->ty)) {
rhs = new_binary(ND_MUL, rhs, new_num(8, tok), tok);
add_type(rhs);
Node *node = new_binary(ND_SUB, lhs, rhs, tok);
node->ty = lhs->ty;
return node;
}
// ptr - ptr, which returns how many elements are between the two.
if (lhs->ty->base && rhs->ty->base) {
Node *node = new_binary(ND_SUB, lhs, rhs, tok);
node->ty = ty_int;
return new_binary(ND_DIV, node, new_num(8, tok), tok);
}
error_tok(tok, "invalid operands");
}
2020-10-07 14:11:16 +03:00
// add = mul ("+" mul | "-" mul)*
static Node *add(Token **rest, Token *tok) {
Node *node = mul(&tok, tok);
for (;;) {
Token *start = tok;
2020-10-07 14:11:16 +03:00
if (equal(tok, "+")) {
2020-09-04 07:39:06 +03:00
node = new_add(node, mul(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
if (equal(tok, "-")) {
2020-09-04 07:39:06 +03:00
node = new_sub(node, mul(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
*rest = tok;
return node;
}
}
// mul = unary ("*" unary | "/" unary)*
static Node *mul(Token **rest, Token *tok) {
Node *node = unary(&tok, tok);
for (;;) {
Token *start = tok;
2020-10-07 14:11:16 +03:00
if (equal(tok, "*")) {
node = new_binary(ND_MUL, node, unary(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
if (equal(tok, "/")) {
node = new_binary(ND_DIV, node, unary(&tok, tok->next), start);
2020-10-07 14:11:16 +03:00
continue;
}
*rest = tok;
return node;
}
}
2019-08-05 15:12:44 +03:00
// unary = ("+" | "-" | "*" | "&") unary
2020-10-07 14:11:16 +03:00
// | 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), tok);
2020-10-07 14:11:16 +03:00
2019-08-05 15:12:44 +03:00
if (equal(tok, "&"))
return new_unary(ND_ADDR, unary(rest, tok->next), tok);
if (equal(tok, "*"))
return new_unary(ND_DEREF, unary(rest, tok->next), tok);
2020-10-07 14:11:16 +03:00
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;
return new_var_node(var, tok);
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, tok);
2020-10-07 14:11:16 +03:00
*rest = tok->next;
return node;
}
error_tok(tok, "expected an expression");
}
// 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
}