diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-18 04:26:26 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-18 04:26:26 -0500 |
| commit | 5ce55a6a29b240e7f0818fff0475f924188c74c8 (patch) | |
| tree | 6bcf53929257a8cd1f071ddba54c539c775b66a9 /builtins | |
| parent | 30c34a7243db7357733bd11c9ee3a093c35891cc (diff) | |
Better table literals
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/table.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/builtins/table.h b/builtins/table.h index 14893253..13262bbe 100644 --- a/builtins/table.h +++ b/builtins/table.h @@ -7,6 +7,19 @@ #include "datatypes.h" #include "array.h" +#define $Table(key_t, val_t, key_info, value_info, fb, def, ...) ({ \ + struct { key_t k; val_t v; } $ents[] = {__VA_ARGS__}; \ + table_t $table = Table_from_entries((array_t){ \ + .data=memcpy(GC_MALLOC(sizeof($ents)), $ents, sizeof($ents)), \ + .length=sizeof($ents)/sizeof($ents[0]), \ + .stride=(void*)&$ents[1] - (void*)&$ents[0], \ + }, &((TypeInfo){.size=sizeof(table_t), .align=__alignof__(table_t), .tag=TableInfo, \ + .TableInfo.key=key_info, .TableInfo.value=value_info})); \ + $table.fallback = fb; \ + $table.default_value = def; \ + $table; }) + + table_t Table_from_entries(array_t entries, const TypeInfo *type); void *Table_get(const table_t *t, const void *key, const TypeInfo *type); void *Table_get_raw(const table_t *t, const void *key, const TypeInfo *type); |
