From 965d5dee1fe6cd28ad92d7f5007c90b6d7d2ff58 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 21 Oct 2019 10:27:21 -0400 Subject: compile time slice at index --- eval.c | 28 +++++++--------------------- main.c | 1 - out.c | 7 +++++-- test.toc | 29 +++++++++++++++-------------- 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 +#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 -// #define kasfdhkjasdfhjk "); +#C("#include +#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 }; -- cgit v1.2.3