Skip to content

Commit

Permalink
Initial table litmod done.
Browse files Browse the repository at this point in the history
  • Loading branch information
viega committed Jul 9, 2024
1 parent 724cf03 commit d2c2fbb
Show file tree
Hide file tree
Showing 21 changed files with 703 additions and 243 deletions.
1 change: 0 additions & 1 deletion include/con4m.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

// #define C4M_FULL_MEMCHECK
// #define C4M_DEBUG
// #define C4M_GC_STATS
// #define C4M_TRACE_GC

// #define C4M_GCT_MOVE 1
Expand Down
8 changes: 4 additions & 4 deletions include/con4m/datatypes/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
**/

#define C4M_STR_HASH_KEY_POINTER_OFFSET 0
#define C4M_HASH_CACHE_OBJ_OFFSET (-4 * (int32_t)sizeof(uint64_t))
#define C4M_HASH_CACHE_RAW_OFFSET (-2 * (int32_t)sizeof(uint64_t))
#define C4M_HASH_CACHE_OBJ_OFFSET (-4 * (int32_t)sizeof(uint64_t))
#define C4M_HASH_CACHE_RAW_OFFSET (-2 * (int32_t)sizeof(uint64_t))

typedef struct c4m_str_t {
char *data;
Expand Down Expand Up @@ -50,8 +50,8 @@ typedef enum {
const struct c4m_internal_string_st _static_##id = { \
.base_data_type = (c4m_dt_info_t *)&c4m_base_type_info[C4M_T_UTF8], \
.concrete_type = (struct c4m_type_t *)&c4m_bi_types[C4M_T_UTF8], \
.s.byte_len = sizeof(val), \
.s.codepoints = sizeof(val), \
.s.byte_len = sizeof(val) - 1, \
.s.codepoints = sizeof(val) - 1, \
.s.styling = NULL, \
.s.data = val, \
}; \
Expand Down
1 change: 1 addition & 0 deletions include/con4m/dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
#include "con4m.h"

#define c4m_dict(x, y) c4m_new(c4m_type_dict(x, y))
c4m_dict_t *c4m_dict_copy(c4m_dict_t *dict);
2 changes: 1 addition & 1 deletion include/con4m/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
#ifndef C4M_DEFAULT_ARENA_SIZE

// This is the size any test case that prints a thing grows to awfully fast.
#define C4M_DEFAULT_ARENA_SIZE (1 << 24)
#define C4M_DEFAULT_ARENA_SIZE (1 << 25)
#endif

// In the future, we would expect that a writer seeing the
Expand Down
68 changes: 1 addition & 67 deletions include/con4m/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,73 +86,7 @@ extern c4m_grid_t *c4m_grid_horizontal_flow(c4m_list_t *,
char *,
char *);

static inline void
c4m_grid_set_cell_contents(c4m_grid_t *g, int row, int col, c4m_obj_t item)
{
c4m_renderable_t *cell;

if (row >= g->num_rows) {
c4m_grid_expand_rows(g, row - (g->num_rows - 1));
}

switch (c4m_base_type(item)) {
case C4M_T_RENDERABLE:
cell = (c4m_renderable_t *)item;
break;
case C4M_T_GRID: {
c4m_grid_t *subobj = (c4m_grid_t *)item;
int tcells = subobj->num_rows * subobj->num_cols;
cell = subobj->self;

for (int i = 0; i < tcells; i++) {
c4m_renderable_t *item = subobj->cells[i];
if (item == NULL) {
continue;
}
c4m_obj_t sub = item->raw_item;

if (c4m_base_type(sub) == C4M_T_GRID) {
c4m_layer_styles(g->self->current_style,
((c4m_grid_t *)sub)->self->current_style);
}
}

break;
}
case C4M_T_UTF8:
case C4M_T_UTF32: {
char *tag;
if (row < g->header_rows || col < g->header_cols) {
tag = c4m_get_th_tag(g);
}
else {
tag = c4m_get_td_tag(g);
}

cell = c4m_new(c4m_type_renderable(),
c4m_kw("tag",
c4m_ka(tag),
"obj",
c4m_ka(item)));
break;
}
default:
abort();
}

c4m_layer_styles(g->self->current_style, cell->current_style);
c4m_install_renderable(g, cell, row, row + 1, col, col + 1);
if (row >= g->row_cursor) {
if (col + 1 == g->num_cols) {
g->row_cursor = row + 1;
g->col_cursor = 0;
}
else {
g->row_cursor = row;
g->col_cursor = col + 1;
}
}
}
extern void c4m_grid_set_cell_contents(c4m_grid_t *, int, int, c4m_obj_t);

static inline void
c4m_grid_add_cell(c4m_grid_t *grid, c4m_obj_t container)
Expand Down
3 changes: 2 additions & 1 deletion include/con4m/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern c4m_utf8_t *c4m_utf8_repeat(c4m_codepoint_t, int64_t);
extern c4m_utf32_t *c4m_utf32_repeat(c4m_codepoint_t, int64_t);
extern c4m_utf32_t *_c4m_str_strip(const c4m_str_t *s, ...);
extern c4m_str_t *_c4m_str_truncate(const c4m_str_t *s, int64_t, ...);
extern c4m_utf32_t *_c4m_str_join(c4m_list_t *,
extern c4m_str_t *_c4m_str_join(c4m_list_t *,
const c4m_str_t *,
...);
extern c4m_utf8_t *c4m_str_from_int(int64_t n);
Expand All @@ -35,6 +35,7 @@ extern c4m_utf32_t *c4m_str_upper(c4m_str_t *);
extern c4m_utf32_t *c4m_str_lower(c4m_str_t *);
extern c4m_utf32_t *c4m_title_case(c4m_str_t *);
extern c4m_str_t *c4m_str_pad(c4m_str_t *, int64_t);
extern c4m_utf8_t *c4m_str_to_hex(c4m_str_t *, bool);

#define c4m_str_split(x, y) c4m_str_xsplit(x, y)
// This is in richlit.c
Expand Down
4 changes: 4 additions & 0 deletions include/con4m/styledb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ c4m_copy_render_style(const c4m_render_style_t *style)
{
c4m_render_style_t *result = c4m_new(c4m_type_render_style());

if (!style) {
return result;
}

memcpy(result, style, sizeof(c4m_render_style_t));

return result;
Expand Down
1 change: 1 addition & 0 deletions include/con4m/wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ extern c4m_str_t *c4m_wrapper_os();
extern c4m_str_t *c4m_wrapper_arch();
extern c4m_str_t *c4m_wrapper_repr(c4m_obj_t);
extern c4m_str_t *c4m_wrapper_to_str(c4m_obj_t);
extern void c4m_snap_column(c4m_grid_t *, int64_t);
125 changes: 74 additions & 51 deletions src/con4m/compiler/check_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ type_check_node_vs_type_no_err(c4m_tree_node_t *n, c4m_type_t *t)
}

void
c4m_fold_container(c4m_tree_node_t *n, c4m_lit_info_t *li)
c4m_fold_container(c4m_tree_node_t *n,
c4m_lit_info_t *li,
c4m_list_t *item_types)
{
c4m_pnode_t *pn = c4m_get_pnode(n);

Expand All @@ -234,10 +236,10 @@ c4m_fold_container(c4m_tree_node_t *n, c4m_lit_info_t *li)
}
}
c4m_list_t *items = c4m_new(c4m_type_list(c4m_type_ref()));
c4m_list_t *tlist = li->type->details->items;
c4m_list_t *tlist = item_types;
c4m_obj_t obj;

if (li->num_items == 1) {
if (li->num_items <= 1) {
bool val_type = c4m_type_is_value_type(c4m_list_get(tlist, 0, NULL));

for (int i = 0; i < n->num_kids; i++) {
Expand All @@ -251,8 +253,6 @@ c4m_fold_container(c4m_tree_node_t *n, c4m_lit_info_t *li)
}
}
else {
c4m_list_t *item_types = li->type->details->items;

c4m_tuple_t *t = c4m_new(c4m_type_tuple_from_xlist(item_types));
for (int i = 0; i < n->num_kids; i++) {
c4m_pnode_t *kid_pnode = c4m_get_pnode(n->children[i]);
Expand All @@ -278,8 +278,9 @@ c4m_fold_container(c4m_tree_node_t *n, c4m_lit_info_t *li)
static void
calculate_container_type(pass2_ctx *ctx, c4m_tree_node_t *n)
{
c4m_pnode_t *pn = c4m_get_pnode(n);
c4m_lit_info_t *li = (c4m_lit_info_t *)pn->extra_info;
c4m_pnode_t *pn = c4m_get_pnode(n);
c4m_lit_info_t *li = (c4m_lit_info_t *)pn->extra_info;
bool concrete = false;

li->base_type = c4m_base_type_from_litmod(li->st, li->litmod);

Expand Down Expand Up @@ -307,8 +308,14 @@ calculate_container_type(pass2_ctx *ctx, c4m_tree_node_t *n)
return;
}

li->type = c4m_new(c4m_type_typespec(),
li->base_type);
if (c4m_base_type_info[li->base_type].dt_kind == C4M_DT_KIND_primitive) {
li->type = c4m_bi_types[li->base_type];
concrete = true;
}
else {
li->type = c4m_new(c4m_type_typespec(),
li->base_type);
}
pn->type = li->type;

if (pn->kind == c4m_nt_lit_empty_dict_or_set) {
Expand All @@ -318,31 +325,41 @@ calculate_container_type(pass2_ctx *ctx, c4m_tree_node_t *n)
return;
}

c4m_list_t *items = li->type->details->items;

switch (li->st) {
case ST_List:
li->num_items = 1;
break;
c4m_list_t *items;

case ST_Tuple:
li->num_items = n->num_kids;
break;
if (concrete) {
items = c4m_list(c4m_type_typespec());
c4m_list_append(items, c4m_new_typevar());
}
else {
items = li->type->details->items;
}

case ST_Dict:
if (pn->kind == c4m_nt_lit_set) {
if (!concrete) {
switch (li->st) {
case ST_List:
li->num_items = 1;
break;

case ST_Tuple:
li->num_items = n->num_kids;
break;

case ST_Dict:
if (pn->kind == c4m_nt_lit_set) {
li->num_items = 1;
}
else {
li->num_items = 2;
}
break;
default:
c4m_unreachable();
}
else {
li->num_items = 2;
}
break;
default:
c4m_unreachable();
}

for (int i = 0; i < li->num_items; i++) {
c4m_list_append(items, c4m_new_typevar());
for (int i = 0; i < li->num_items; i++) {
c4m_list_append(items, c4m_new_typevar());
}
}

for (int i = 0; i < n->num_kids; i++) {
Expand All @@ -352,34 +369,40 @@ calculate_container_type(pass2_ctx *ctx, c4m_tree_node_t *n)
ctx->node = n->children[i];
base_check_pass_dispatch(ctx);

if (merge_or_ret_ignore_err(t, kid_pnode->type)) {
if (c4m_can_coerce(kid_pnode->type, t)) {
c4m_lit_info_t *li = (c4m_lit_info_t *)kid_pnode->extra_info;
li->cast_to = t;
if (kid_pnode->value != NULL) {
kid_pnode->value = c4m_coerce(kid_pnode->value,
kid_pnode->type,
t);
if (!concrete) {
if (merge_or_ret_ignore_err(t, kid_pnode->type)) {
if (c4m_can_coerce(kid_pnode->type, t)) {
c4m_lit_info_t *li = kid_pnode->extra_info;
li->cast_to = t;
if (kid_pnode->value != NULL) {
kid_pnode->value = c4m_coerce(kid_pnode->value,
kid_pnode->type,
t);
}
}
}
else {
char *p = (char *)c4m_base_type_info[li->base_type].name;
c4m_utf8_t *s = c4m_new_utf8(p);
else {
char *p = (char *)c4m_base_type_info[li->base_type].name;

c4m_add_error(ctx->file_ctx,
c4m_err_inconsistent_item_type,
n->children[i],
s,
t,
kid_pnode->type);
c4m_calculate_type_hash(li->type);
return;
c4m_utf8_t *s = c4m_new_utf8(p);

c4m_add_error(ctx->file_ctx,
c4m_err_inconsistent_item_type,
n->children[i],
s,
t,
kid_pnode->type);
c4m_calculate_type_hash(li->type);
return;
}
}
}
}

c4m_calculate_type_hash(li->type);
c4m_fold_container(n, li);
if (!concrete) {
c4m_calculate_type_hash(li->type);
}

c4m_fold_container(n, li, items);
}

// This maps names to how many arguments the function takes. If
Expand Down
4 changes: 2 additions & 2 deletions src/con4m/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ dict_coerce_to(c4m_dict_t *dict, c4m_type_t *dst_type)
}

c4m_dict_t *
dict_copy(c4m_dict_t *dict)
c4m_dict_copy(c4m_dict_t *dict)
{
return dict_coerce_to(dict, c4m_get_my_type(dict));
}
Expand Down Expand Up @@ -343,7 +343,7 @@ const c4m_vtable_t c4m_dict_vtable = {
[C4M_BI_UNMARSHAL] = (c4m_vtable_entry)c4m_dict_unmarshal,
[C4M_BI_COERCIBLE] = (c4m_vtable_entry)dict_can_coerce_to,
[C4M_BI_COERCE] = (c4m_vtable_entry)dict_coerce_to,
[C4M_BI_COPY] = (c4m_vtable_entry)dict_copy,
[C4M_BI_COPY] = (c4m_vtable_entry)c4m_dict_copy,
[C4M_BI_ADD] = (c4m_vtable_entry)dict_plus,
[C4M_BI_LEN] = (c4m_vtable_entry)dict_len,
[C4M_BI_INDEX_GET] = (c4m_vtable_entry)dict_get,
Expand Down
8 changes: 0 additions & 8 deletions src/con4m/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,6 @@ assemble_formatted_result(const c4m_str_t *fmt, c4m_list_t *arg_strings)
arg = c4m_to_utf32(arg);
argp = (c4m_codepoint_t *)arg->data;

// If we have a null string and there's a style around it,
// things are broken without this kludge. But to be quite
// honest, I don't understand why it's broken or why this
// kludge works??
if (alen == 0) {
alen = 1;
}

style_adjustment(result, out_ix, alen - 2);

for (int64_t j = 0; j < alen; j++) {
Expand Down
Loading

0 comments on commit d2c2fbb

Please sign in to comment.