summaryrefslogtreecommitdiff
path: root/types.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-11-01 15:25:08 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-11-01 15:25:08 -0400
commit456ff7743aeed28a6837a7dbc9cc0ab28331931d (patch)
tree424b05bd695d6e18b0f4f318b867fe55b8110a58 /types.c
parent7f84a8f28c3d0f8d99d2d88373ee7c1595266d2d (diff)
.len as an lvalue for slices
Diffstat (limited to 'types.c')
-rw-r--r--types.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/types.c b/types.c
index 9cb10b7..dde2193 100644
--- a/types.c
+++ b/types.c
@@ -163,14 +163,25 @@ static bool expr_must_lval(Expression *e) {
}
case EXPR_UNARY_OP:
if (e->unary.op == UNARY_DEREF) return true;
- break;
+ if (e->unary.op == UNARY_LEN) {
+ Type *of_type = &e->unary.of->type;
+ if (of_type->kind != TYPE_PTR && !expr_must_lval(e->unary.of)) { /* can't set length of a non-lvalue slice */
+ return false;
+ }
+
+ return of_type->kind == TYPE_SLICE
+ || (of_type->kind == TYPE_PTR
+ && of_type->kind == TYPE_SLICE);
+ }
+ err_print(e->where, "Cannot use operator %s as l-value.", unary_op_to_str(e->unary.op));
+ return false;
case EXPR_BINARY_OP:
switch (e->binary.op) {
case BINARY_AT_INDEX: return true;
case BINARY_DOT: return true;
default: break;
}
- err_print(e->where, "Cannot use operator %s as l-value", binary_op_to_str(e->binary.op));
+ err_print(e->where, "Cannot use operator %s as l-value.", binary_op_to_str(e->binary.op));
return false;
case EXPR_TUPLE:
/* x, y is an lval, but 3, "hello" is not. */