aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <ted.sybil@gmail.com>2014-04-18 15:32:58 +0800
committerTeddy <ted.sybil@gmail.com>2014-04-18 15:32:58 +0800
commit1460ad1da5f294ea77e2e737b5523627b12104c3 (patch)
tree6e916e6d5b334f6f69c8fd2846d34f83eaac613f
parent950dd4b0688dee3f7a90105a1225c8ba41123282 (diff)
...
-rw-r--r--semantics.c56
1 files changed, 26 insertions, 30 deletions
diff --git a/semantics.c b/semantics.c
index 127783d..106c14d 100644
--- a/semantics.c
+++ b/semantics.c
@@ -855,12 +855,6 @@ ExpType exp_check_add(ExpType op1, ExpType op2, CNode *p, char kind) {
ERROR((p, "invalid use of undefined type"));
if (t2 == CPTR && calc_size(op2.type->rec.ref) == -1)
ERROR((p, "invalid use of undefined type"));
- if (t1 == CARR)
- {
- CType_t ptr = ctype_create("", CPTR, NULL);
- ptr->rec.ref = op1.type->rec.arr.elem;
- op1.type = ptr;
- }
if (kind == '-')
{
if (IS_PTR(t2) && !IS_PTR(t1))
@@ -871,32 +865,31 @@ ExpType exp_check_add(ExpType op1, ExpType op2, CNode *p, char kind) {
if (!((IS_INT(t1) || IS_PTR(t1)) && IS_INT(t2)))
ERROR((p, "invalid operands to binary operator"));
}
- if ((p->ext.is_const = rch->ext.is_const))
+ if ((p->ext.is_const = lch->ext.is_const && rch->ext.is_const))
{
- int r = rch->ext.const_val;
- if (IS_PTR(t1))
+ int r = rch->ext.const_val,
+ *a = &(p->ext.const_val);
+ if (t1 == CARR)
{
+ int l = p->chd->ext.offest;
CType_t type;
p->ext.var = p->chd->ext.var;
if (t1 == CPTR) type = op1.type->rec.ref;
else type = op1.type->rec.arr.elem;
- p->ext.offest = p->chd->ext.offest + calc_size(type) * r;
- }
- if ((p->ext.is_const &= lch->ext.is_const))
- {
- int l = lch->ext.const_val,
- *a = &(p->ext.const_val);
- if (IS_PTR(t1))
+ r *= calc_size(type);
+ switch (kind)
{
- /* TODO: const pointer */
+ case '+': p->ext.offest = l + r; break;
+ case '-': p->ext.offest = l - r; break;
}
- else
+ }
+ else
+ {
+ int l = lch->ext.const_val;
+ switch (kind)
{
- switch (kind)
- {
- case '+': *a = l + r; break;
- case '-': *a = l - r; break;
- }
+ case '+': *a = l + r; break;
+ case '-': *a = l - r; break;
}
}
}
@@ -1077,17 +1070,20 @@ ExpType exp_check_postfix(CNode *p, CScope_t scope) {
t2 = op2.type->type;
if (!IS_INT(t2))
ERROR((p, "array subscript is not an integer"));
+ p->ext.is_const = 0;
if (t1 == CARR)
+ {
op1.type = op1.type->rec.arr.elem;
+ if ((p->ext.is_const = p->chd->ext.is_const && \
+ post->chd->ext.is_const))
+ {
+ p->ext.offest = p->chd->ext.offest + \
+ calc_size(op1.type) * post->chd->ext.const_val;
+ p->ext.var = p->chd->ext.var;
+ }
+ }
else
op1.type = op1.type->rec.ref;
- if (post->chd->ext.is_const)
- {
- p->ext.offest = p->chd->ext.offest + \
- calc_size(op1.type) * post->chd->ext.const_val;
- p->ext.var = p->chd->ext.var;
- }
- p->ext.is_const = 0;
op1.lval = 1;
break;
case POSTFIX_CALL: