diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-21 10:27:21 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-21 10:27:21 -0400 |
commit | 965d5dee1fe6cd28ad92d7f5007c90b6d7d2ff58 (patch) | |
tree | e990cb390b9ba5b9b3eb2386e48ead476f6e6498 | |
parent | ee7525251681a3c0ab38e0356bc3733adb3e6566 (diff) |
compile time slice at index
-rw-r--r-- | eval.c | 28 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | out.c | 7 | ||||
-rw-r--r-- | test.toc | 29 |
4 files changed, 27 insertions, 38 deletions
@@ -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; @@ -1,6 +1,5 @@ /* TODO: -slice at index fix recursion with block_enter/exit arr => slice casting del slices @@ -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}; } @@ -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 }; |