From 1bff7d30e77f81bd129597e80af116843aa93eab Mon Sep 17 00:00:00 2001 From: yoyo Date: Sun, 3 Nov 2024 17:18:44 +0900 Subject: [PATCH] fix slicing only work with arrays --- assert.c | 4 ++-- com.c | 2 +- gen.c | 2 +- parse.c | 5 ++++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/assert.c b/assert.c index fd3fb17..75171b3 100644 --- a/assert.c +++ b/assert.c @@ -220,8 +220,8 @@ assertexpr(Node *n) case Oindex: t = l->ty; if(t->kind == Tslice) - t = t->tof; - assert(t->kind == Tarray || t->kind == Tslice); + t = typeofslice(t->tof); + assert(t->kind == Tarray); assertexpr(r); if(r->op == Oconst) assert(r->val < t->len); diff --git a/com.c b/com.c index e329c1e..3031c03 100644 --- a/com.c +++ b/com.c @@ -401,7 +401,7 @@ rewrite(Node *n) if(r->l == nil) r->l = mkconst(0); if(r->r == nil) - r->r = mkconst(t->len); + r->r = mkconst(n->ty->len); n->l = rewrite(n->l); n->r = rewrite(n->r); break; diff --git a/gen.c b/gen.c index 67311c5..c24b00d 100644 --- a/gen.c +++ b/gen.c @@ -247,7 +247,7 @@ genop(int op, Node *s, Node *m, Node *d) }; Inst *in = mkinst(); int iop = disoptab[op][d->ty->kind]; - assert(iop != nil); + assert(iop != 0); in->op = iop; if(s){ in->s = genaddr(s); diff --git a/parse.c b/parse.c index 41a221e..301b0ed 100644 --- a/parse.c +++ b/parse.c @@ -614,7 +614,7 @@ arraytype(void) Token t; want((int[]){'[',0}); - switch((t = try((int[]){Lid,Lconst,0})).kind){ + switch((t = try((int[]){Lid,Lconst,']',0})).kind){ case Lid: assert(t.sym->decl == _decl); want((int[]){']',0}); @@ -627,6 +627,9 @@ arraytype(void) ty = mktype(Tarray, 0, type(), nil); assert((ty->len = t.num) > 0); return ty; + case ']': + ty = mktype(Tslice, 0, type(), nil); + return ty; default : assert(0); } }