From 3f9632bb404584db872ed8e2318f770cde8b16ee Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 13 Apr 2016 10:13:49 -0700 Subject: [PATCH] eval: support array literals --- src/eval.cpp | 22 ++++++++++++++++++++++ test/self_hosted.zig | 14 ++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/eval.cpp b/src/eval.cpp index 34656c41e..b3120118c 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -359,6 +359,28 @@ static bool eval_container_init_expr(EvalFn *ef, AstNode *node, ConstExprValue * add_error_note(ef->root->codegen, msg, ef->root->call_node, buf_sprintf("called from here")); add_error_note(ef->root->codegen, msg, node, buf_sprintf("unreachable expression here")); return true; + } else if (container_type->id == TypeTableEntryIdStruct && + container_type->data.structure.is_unknown_size_array && + kind == ContainerInitKindArray) + { + + int elem_count = container_init_expr->entries.length; + + out_val->ok = true; + out_val->data.x_array.fields = allocate(elem_count); + + for (int i = 0; i < elem_count; i += 1) { + AstNode *elem_node = container_init_expr->entries.at(i); + + ConstExprValue *elem_val = allocate(1); + if (eval_expr(ef, elem_node, elem_val)) return true; + + assert(elem_val->ok); + + out_val->data.x_array.fields[i] = elem_val; + out_val->depends_on_compile_var = out_val->depends_on_compile_var || + elem_val->depends_on_compile_var; + } } else { zig_panic("TODO"); } diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 4513b06dd..b36627855 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -961,3 +961,17 @@ fn static_while_loop_2() -> i32 { return 1; } } + +#attribute("test") +fn static_eval_list_init() { + assert(static_vec3.data[2] == 1.0); +} +const static_vec3 = vec3(0.0, 0.0, 1.0); +pub struct Vec3 { + data: [3]f32, +} +pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { + Vec3 { + .data = []f32 { x, y, z, }, + } +}