summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-10-21 10:27:21 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-10-21 10:27:21 -0400
commit965d5dee1fe6cd28ad92d7f5007c90b6d7d2ff58 (patch)
treee990cb390b9ba5b9b3eb2386e48ead476f6e6498
parentee7525251681a3c0ab38e0356bc3733adb3e6566 (diff)
compile time slice at index
-rw-r--r--eval.c28
-rw-r--r--main.c1
-rw-r--r--out.c7
-rw-r--r--test.toc29
4 files changed, 27 insertions, 38 deletions
diff --git a/eval.c b/eval.c
index c60f05b..60bb15f 100644
--- a/eval.c
+++ b/eval.c
@@ -405,7 +405,7 @@ static void eval_deref_set(void *set, Value *to, Type *type) {
}
}
-static bool eval_pointer_at_index(Evaluator *ev, Expression *e, void **ptr, Type **type) {
+static bool eval_ptr_at_index(Evaluator *ev, Expression *e, void **ptr, Type **type) {
Value arr;
if (!eval_expr(ev, e->binary.lhs, &arr)) return false;
Value index;
@@ -473,7 +473,7 @@ static bool eval_set(Evaluator *ev, Expression *set, Value *to) {
case BINARY_AT_INDEX: {
void *ptr;
Type *type;
- if (!eval_pointer_at_index(ev, set, &ptr, &type))
+ if (!eval_ptr_at_index(ev, set, &ptr, &type))
return false;
eval_deref_set(ptr, to, type);
} break;
@@ -607,7 +607,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
case BINARY_AT_INDEX: {
void *ptr;
Type *type;
- if (!eval_pointer_at_index(ev, o, &ptr, &type))
+ if (!eval_ptr_at_index(ev, o, &ptr, &type))
return false;
v->ptr = ptr;
} break;
@@ -672,24 +672,10 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
if (!eval_set(ev, e->binary.lhs, &rhs)) return false;
break;
case BINARY_AT_INDEX: {
- U64 index;
- U64 arr_sz = e->binary.lhs->type.arr.n;
- assert(e->binary.rhs->type.kind == TYPE_BUILTIN);
- if (e->binary.rhs->type.builtin == BUILTIN_U64) {
- index = rhs.u64;
- } else {
- I64 signed_index = val_to_i64(&rhs, e->binary.rhs->type.builtin);
- if (signed_index < 0) {
- err_print(e->where, "Array out of bounds (%ld, array size = %lu)\n", (long)signed_index, (unsigned long)arr_sz);
- return false;
- }
- index = (U64)signed_index;
- }
- if (index >= 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);
+ void *ptr;
+ Type *type;
+ eval_ptr_at_index(ev, e, &ptr, &type);
+ eval_deref(v, ptr, type);
} break;
}
} break;
diff --git a/main.c b/main.c
index b8c2997..5e8a306 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,5 @@
/*
TODO:
-slice at index
fix recursion with block_enter/exit
arr => slice casting
del slices
diff --git a/out.c b/out.c
index cc44ba0..249b3e0 100644
--- a/out.c
+++ b/out.c
@@ -26,6 +26,8 @@ int main() {
return 0;
}
+#include <stdio.h>
+#define kasfdhkjasdfhjk ;
void puti(i64 x) {
printf("%ld\n", (long)x);
@@ -44,6 +46,7 @@ i64 foo(i64 x) {
};
i64 total; {
i64 expr__; expr__ = 0;total = expr__;}
+ i = 0;;
while ((i<x)) {
total = (total+(((i64(*))(C.data))[i]));;
i = (i+1);;
@@ -54,8 +57,8 @@ i64 foo(i64 x) {
void main__(void) {
- i64( A[0]) = {0};
- i64( B[0]) = {0};
+ i64( A[45]) = {0};
+ i64( B[4950]) = {0};
}
diff --git a/test.toc b/test.toc
index b1bcbc8..42c8a36 100644
--- a/test.toc
+++ b/test.toc
@@ -1,5 +1,5 @@
-// #C("#include <stdio.h>
-// #define kasfdhkjasdfhjk ");
+#C("#include <stdio.h>
+#define kasfdhkjasdfhjk ");
puti @= fn(x: int) {
#C("printf(\"%ld\\n\", (long)x)");
@@ -15,18 +15,19 @@ puti @= fn(x: int) {
// };
foo @= fn(x: int) int {
- C := new(int, x);
- i := 0;
- while i < x {
- C[i] = i;
- i = i + 1;
- }
- total := 0;
- while i < x {
- total = total + C[i];
- i = i + 1;
- }
- total
+ C := new(int, x);
+ i := 0;
+ while i < x {
+ C[i] = i;
+ i = i + 1;
+ }
+ total := 0;
+ i = 0;
+ while i < x {
+ total = total + C[i];
+ i = i + 1;
+ }
+ total
};