diff --git a/data/examples/config/config.ct b/data/examples/config/config.ct index f10e5330..10591dda 100644 --- a/data/examples/config/config.ct +++ b/data/examples/config/config.ct @@ -4,8 +4,8 @@ import win32; import cstdlib as libc; export struct StringView { - front: *char; - back: *char; + front: *const(char); + back: *const(char); } export struct ConfigEntry { @@ -60,17 +60,36 @@ def readUntil(current: *char, terminator: char): StringView { }; } +const kGlobalSectionName = "global"; + export def openConfigFile(path: str): ConfigFile { var file = mapFileHandle(path); if file.fileView == libc::null { return .{ }; } - var sections: [*]ConfigSection = libc::malloc(__sizeof(ConfigSection) * 4); + var sections: [*]ConfigSection = libc::malloc(__sizeof(ConfigSection) * 8); var entries: [*]ConfigEntry = libc::malloc(__sizeof(ConfigEntry) * 16); - var index: uint = 0; + file.sections = sections; + file.entries = entries; + + const innerName = kGlobalSectionName; + const globalName: StringView = .{ + front = kGlobalSectionName, + back = &kGlobalSectionName[6] + }; + + sections[0] = .{ + name = globalName, + firstEntry = 0, + lastEntry = 0 + }; + + file.sectionCount = 1; + + var index: uint = 0; while index < file.fileSize { const letter = file.fileView[index]; index = index + 1; diff --git a/src/language/ctu/src/sema/expr.c b/src/language/ctu/src/sema/expr.c index 316f7fd4..cfd275b1 100644 --- a/src/language/ctu/src/sema/expr.c +++ b/src/language/ctu/src/sema/expr.c @@ -215,6 +215,19 @@ static tree_t *sema_load(tree_t *sema, const ctu_t *expr, const tree_t *implicit tree_t *name = sema_decl_name(sema, expr->node, expr->path, &needs_load); + // TODO: this feels like a bit of a hack + // works around loading a string literal which turns into a char + // but we need to check if its a function first to prevent + // and assert from getting the type of a possibly unresolved function + if (!tree_is(name, eTreeDeclFunction)) + { + const tree_t *type = tree_get_type(name); + if (tree_is(type, eTreeTypeString)) + { + needs_load = false; + } + } + if (needs_load) { name = tree_expr_load(expr->node, name); @@ -614,22 +627,24 @@ static tree_t *sema_init(ctu_sema_t *sema, const ctu_t *expr, const tree_t *impl size_t len = vector_len(expr->inits); for (size_t i = 0; i < len; i++) { - ctu_t *init = vector_get(expr->inits, i); - CTASSERTF(init->kind == eCtuFieldInit, "invalid init kind %d", init->kind); + ctu_t *element = vector_get(expr->inits, i); + CTASSERTF(element->kind == eCtuFieldInit, "invalid init kind %d", element->kind); - tree_t *field = tree_ty_get_field(implicit_type, init->field); + tree_t *field = tree_ty_get_field(implicit_type, element->field); if (field == NULL) { - msg_notify(reports, &kEvent_FieldNotFound, init->node, - "field `%s` not found in struct `%s`", init->field, + msg_notify(reports, &kEvent_FieldNotFound, element->node, + "field `%s` not found in struct `%s`", element->field, tree_to_string(implicit_type)); continue; } - tree_t *value = ctu_sema_rvalue(sema, init->expr, tree_get_type(field)); - tree_t *ref_type = tree_type_reference(init->node, "", tree_get_type(field)); - tree_t *dst = tree_expr_field(init->node, ref_type, local, field); - tree_t *assign = tree_stmt_assign(init->node, dst, value); + const tree_t *field_type = tree_get_type(field); + + tree_t *value = ctu_sema_rvalue(sema, element->expr, field_type); + tree_t *ref_type = tree_type_reference(element->node, "", field_type); + tree_t *dst = tree_expr_field(element->node, ref_type, local, field); + tree_t *assign = tree_stmt_assign(element->node, dst, value); vector_push(&sema->block, assign); set_add(fields, field);