summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-10-07 14:55:02 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-10-07 14:55:02 -0400
commit5fc9df5d515274efeed73ebce325b71e202c4ac5 (patch)
treee87c4ff465ea8488f0bdf8170ead8689db2f0822
parentf08f00ea3bd2e1a99b35a4811c584534b1232254 (diff)
compile time at index
-rw-r--r--eval.c31
-rw-r--r--test.toc2
2 files changed, 29 insertions, 4 deletions
diff --git a/eval.c b/eval.c
index 3835a3f..f9563b0 100644
--- a/eval.c
+++ b/eval.c
@@ -403,9 +403,31 @@ static bool eval_set(Evaluator *ev, Expression *set, Value *to) {
break;
case EXPR_BINARY_OP:
switch (set->binary.op) {
- case BINARY_AT_INDEX:
+ case BINARY_AT_INDEX: {
/* TODO */
- break;
+ Value arr;
+ if (!eval_expr(ev, set->binary.lhs, &arr)) return false;
+ Value index;
+ if (!eval_expr(ev, set->binary.rhs, &index)) return false;
+ U64 i;
+ U64 arr_sz = set->binary.lhs->type.arr.n;
+ assert(set->binary.rhs->type.kind == TYPE_BUILTIN);
+ if (set->binary.rhs->type.builtin == BUILTIN_U64) {
+ i = index.u64;
+ } else {
+ I64 signed_index = val_to_i64(&index, set->binary.rhs->type.builtin);
+ if (signed_index < 0) {
+ err_print(set->where, "Array out of bounds (%ld, array size = %lu)\n", (long)signed_index, (unsigned long)arr_sz);
+ return false;
+ }
+ i = (U64)signed_index;
+ }
+ if (i >= arr_sz) {
+ err_print(set->where, "Array out of bounds (%lu, array size = %lu)\n", (unsigned long)i, (unsigned long)arr_sz);
+ return false;
+ }
+ eval_deref_set((char *)arr.arr + compiler_sizeof(&set->binary.lhs->type) * i, to, &set->binary.lhs->type);
+ } break;
default: break;
}
case EXPR_TUPLE:
@@ -499,6 +521,9 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
Value of;
if (!eval_expr(ev, e->unary.of, &of)) return false;
switch (e->unary.op) {
+ case UNARY_ADDRESS:
+ /* TODO */
+ break;
case UNARY_DEREF:
eval_deref(v, of.ptr, &e->type);
break;
@@ -566,7 +591,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
index = (U64)signed_index;
}
if (index >= arr_sz) {
- err_print(e->where, "Array out of bounds (%lu, array size = %lu)\n", (long)index, (unsigned long)arr_sz);
+ err_print(e->where, "Array out of bounds (%lu, array size = %lu)\n", (unsigned long)index, (unsigned long)arr_sz);
return false;
}
eval_deref(v, (void *)((char *)(lhs.arr) + index * compiler_sizeof(&e->type)), &e->type);
diff --git a/test.toc b/test.toc
index c472568..0d12e27 100644
--- a/test.toc
+++ b/test.toc
@@ -3,5 +3,5 @@ main @= fn() {
// N, M @= 8 * if foo { 8 } else { 3} - 3, 894;
// foo @= new[3]int;
// x : [foo[0]]int;
- y : [{x := (439 as &u8); *x = 7; *x }]f32;
+ y : [{x : [3]int; x[0] = 7; x[0] }]f32;
}; \ No newline at end of file