mirror of
https://github.com/rui314/chibicc
synced 2024-11-30 01:43:08 +03:00
e6307ad374
This patch allows writing a comma expression on the left-hand side of an assignment expression. This is called the "generalized lvalue" which is a deprecated GCC language extension. I'm implementing it anyway because it's useful to implement other features.
109 lines
2.2 KiB
C
109 lines
2.2 KiB
C
#include "chibicc.h"
|
|
|
|
Type *ty_char = &(Type){TY_CHAR, 1};
|
|
Type *ty_int = &(Type){TY_INT, 8};
|
|
|
|
bool is_integer(Type *ty) {
|
|
return ty->kind == TY_CHAR || ty->kind == TY_INT;
|
|
}
|
|
|
|
Type *copy_type(Type *ty) {
|
|
Type *ret = calloc(1, sizeof(Type));
|
|
*ret = *ty;
|
|
return ret;
|
|
}
|
|
|
|
Type *pointer_to(Type *base) {
|
|
Type *ty = calloc(1, sizeof(Type));
|
|
ty->kind = TY_PTR;
|
|
ty->size = 8;
|
|
ty->base = base;
|
|
return ty;
|
|
}
|
|
|
|
Type *func_type(Type *return_ty) {
|
|
Type *ty = calloc(1, sizeof(Type));
|
|
ty->kind = TY_FUNC;
|
|
ty->return_ty = return_ty;
|
|
return ty;
|
|
}
|
|
|
|
Type *array_of(Type *base, int len) {
|
|
Type *ty = calloc(1, sizeof(Type));
|
|
ty->kind = TY_ARRAY;
|
|
ty->size = base->size * len;
|
|
ty->base = base;
|
|
ty->array_len = len;
|
|
return ty;
|
|
}
|
|
|
|
void add_type(Node *node) {
|
|
if (!node || node->ty)
|
|
return;
|
|
|
|
add_type(node->lhs);
|
|
add_type(node->rhs);
|
|
add_type(node->cond);
|
|
add_type(node->then);
|
|
add_type(node->els);
|
|
add_type(node->init);
|
|
add_type(node->inc);
|
|
|
|
for (Node *n = node->body; n; n = n->next)
|
|
add_type(n);
|
|
for (Node *n = node->args; n; n = n->next)
|
|
add_type(n);
|
|
|
|
switch (node->kind) {
|
|
case ND_ADD:
|
|
case ND_SUB:
|
|
case ND_MUL:
|
|
case ND_DIV:
|
|
case ND_NEG:
|
|
node->ty = node->lhs->ty;
|
|
return;
|
|
case ND_ASSIGN:
|
|
if (node->lhs->ty->kind == TY_ARRAY)
|
|
error_tok(node->lhs->tok, "not an lvalue");
|
|
node->ty = node->lhs->ty;
|
|
return;
|
|
case ND_EQ:
|
|
case ND_NE:
|
|
case ND_LT:
|
|
case ND_LE:
|
|
case ND_NUM:
|
|
case ND_FUNCALL:
|
|
node->ty = ty_int;
|
|
return;
|
|
case ND_VAR:
|
|
node->ty = node->var->ty;
|
|
return;
|
|
case ND_COMMA:
|
|
node->ty = node->rhs->ty;
|
|
return;
|
|
case ND_ADDR:
|
|
if (node->lhs->ty->kind == TY_ARRAY)
|
|
node->ty = pointer_to(node->lhs->ty->base);
|
|
else
|
|
node->ty = pointer_to(node->lhs->ty);
|
|
return;
|
|
case ND_DEREF:
|
|
if (!node->lhs->ty->base)
|
|
error_tok(node->tok, "invalid pointer dereference");
|
|
node->ty = node->lhs->ty->base;
|
|
return;
|
|
case ND_STMT_EXPR:
|
|
if (node->body) {
|
|
Node *stmt = node->body;
|
|
while (stmt->next)
|
|
stmt = stmt->next;
|
|
if (stmt->kind == ND_EXPR_STMT) {
|
|
node->ty = stmt->lhs->ty;
|
|
return;
|
|
}
|
|
}
|
|
error_tok(node->tok, "statement expression returning void is not supported");
|
|
return;
|
|
}
|
|
}
|