aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index a9a0d2c7..0ed42f69 100644
--- a/compile.c
+++ b/compile.c
@@ -886,6 +886,26 @@ CORD compile(env_t *env, ast_t *ast)
}
code_err(ast, "The field '%s' is not a valid field name of %T", f->field, value_t);
}
+ case TableType: {
+ if (streq(f->field, "keys")) {
+ return CORD_all("({ table_t $t = ", compile_to_pointer_depth(env, f->fielded, 0, false), ";\n"
+ "(array_t){.data = $t.entries.data,\n .length=$t.entries.length,\n .stride=$t.entries.stride,\n .copy_on_write=yes};})");
+ } else if (streq(f->field, "values")) {
+ auto table = Match(value_t, TableType);
+ size_t offset = type_size(table->key_type);
+ size_t align = type_align(table->value_type);
+ if (align > 1 && offset % align > 0)
+ offset += align - (offset % align);
+ return CORD_all("({ table_t $t = ", compile_to_pointer_depth(env, f->fielded, 0, false), ";\n"
+ "(array_t){.data = $t.entries.data + ", CORD_asprintf("%zu", offset),
+ ",\n .length=$t.entries.length,\n .stride=$t.entries.stride,\n .copy_on_write=yes};})");
+ } else if (streq(f->field, "fallback")) {
+ return CORD_all("(", compile_to_pointer_depth(env, f->fielded, 0, false), ").fallback");
+ } else if (streq(f->field, "default")) {
+ return CORD_all("(", compile_to_pointer_depth(env, f->fielded, 0, false), ").default_value");
+ }
+ code_err(ast, "There is no '%s' field on tables", f->field);
+ }
default:
code_err(ast, "Field accesses are only supported on struct and enum values");
}