eval: support array literals

master
Andrew Kelley 2016-04-13 10:13:49 -07:00
parent 3c27cb2527
commit 3f9632bb40
2 changed files with 36 additions and 0 deletions

View File

@ -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<ConstExprValue*>(elem_count);
for (int i = 0; i < elem_count; i += 1) {
AstNode *elem_node = container_init_expr->entries.at(i);
ConstExprValue *elem_val = allocate<ConstExprValue>(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");
}

View File

@ -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, },
}
}