aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-18 00:12:23 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-18 00:12:23 -0500
commit24a703199b06e483fd28823be64b87ec2ca44645 (patch)
tree4f510e7bbd5c1883ff5db9bd79f9504ec31abeaa
parentfbf39cd7e907f32824cc0bb763f38851b1726cfd (diff)
Compile tables
-rw-r--r--builtins/array.h2
-rw-r--r--compile.c32
2 files changed, 30 insertions, 4 deletions
diff --git a/builtins/array.h b/builtins/array.h
index ae36795a..7c83e17d 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -10,7 +10,7 @@
// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1
#define $index(x, i) _Generic(x, array_t: ({ __typeof(x) $obj; int64_t $offset = i; $offset += ($offset < 0) * ($obj.length + 1) - 1; assert($offset >= 0 && offset < $obj.length); $obj.data + $obj.stride * $offset;}))
#define $safe_index(x, i) _Generic(x, array_t: ({ __typeof(x) $obj; int64_t $offset = i - 1; $obj.data + $obj.stride * $offset;}))
-#define $array(x, ...) ({ __typeof(x) $items[] = {x, __VA_ARGS__}; \
+#define $Array(x, ...) ({ __typeof(x) $items[] = {x, __VA_ARGS__}; \
(array_t){.length=sizeof($items)/sizeof($items[0]), \
.stride=(int64_t)&$items[1] - (int64_t)&$items[0], \
.data=memcpy(GC_MALLOC(sizeof($items)), $items, sizeof($items)), \
diff --git a/compile.c b/compile.c
index 133cff0a..704898c7 100644
--- a/compile.c
+++ b/compile.c
@@ -325,19 +325,45 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_asprintf("max(%r, %r)", compile(env, Match(ast, Max)->lhs), compile(env, Match(ast, Max)->rhs));
}
// Min, Max,
- // Array, Table, TableEntry,
case Array: {
auto array = Match(ast, Array);
if (!array->items)
return "(array_t){}";
- CORD code = "$array(";
+ CORD code = "$Array(";
for (ast_list_t *item = array->items; item; item = item->next) {
code = CORD_cat(code, compile(env, item->ast));
if (item->next) code = CORD_cat(code, ", ");
}
- return CORD_cat_char(code, ')');
+ return CORD_cat(code, ")");
+ }
+ case Table: {
+ auto table = Match(ast, Table);
+ if (!table->entries) {
+ CORD code = "(table_t){";
+ if (table->fallback)
+ code = CORD_all(code, ".fallback=", compile(env, table->fallback),",");
+ if (table->default_value)
+ code = CORD_all(code, ".default_value=", compile(env, table->default_value),",");
+ return CORD_cat(code, "}");
+ }
+
+ type_t *table_t = get_type(env, ast);
+ // TODO: figure out a clever way to optimize table literals:
+ CORD code = CORD_all("({ table_t $table = {}; TypeInfo *$table_type = ", compile_type_info(env, table_t), ";");
+ for (ast_list_t *entry = table->entries; entry; entry = entry->next) {
+ auto entry = Match(entry->ast, TableEntry);
+ code = CORD_all(code, " Table_set(&$table, $stack(",
+ compile(env, entry->key), "), $stack(", compile(env, entry->value), "), $table_type);");
+ }
+ if (table->fallback)
+ code = CORD_all(code, " $table.fallback = $heap(", compile(env, table->fallback), ");");
+ if (table->default_value)
+ code = CORD_all(code, " $table.default_value = $heap(", compile(env, table->default_value), ");");
+ return CORD_cat(code, " $table; })");
+
}
+ // Table, TableEntry,
case FunctionDef: {
auto fndef = Match(ast, FunctionDef);
CORD name = compile(env, fndef->name);