aboutsummaryrefslogtreecommitdiff
path: root/src/compile
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-10-01 12:43:00 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-10-01 12:43:00 -0400
commit6583fe9b389a6b4698f9364945885e6783506886 (patch)
tree0464456d177eab051b03f29a74218a45b301f174 /src/compile
parent0cfae753aa131f949253f3fba1e3a36c2bde6ac0 (diff)
Convert to using more zero values for `none`
Diffstat (limited to 'src/compile')
-rw-r--r--src/compile/cli.c8
-rw-r--r--src/compile/expressions.c6
-rw-r--r--src/compile/functions.c16
-rw-r--r--src/compile/lists.c4
-rw-r--r--src/compile/optionals.c8
-rw-r--r--src/compile/statements.c2
-rw-r--r--src/compile/tables.c4
7 files changed, 24 insertions, 24 deletions
diff --git a/src/compile/cli.c b/src/compile/cli.c
index b082239d..d93e5f56 100644
--- a/src/compile/cli.c
+++ b/src/compile/cli.c
@@ -60,19 +60,19 @@ Text_t compile_cli_arg_call(env_t *env, Text_t fn_name, type_t *fn_type, const c
type_t *t = get_arg_type(main_env, arg);
if (arg->default_val || arg->type->tag == OptionalType) {
OptionalText_t flag = flagify(arg->name, true);
- assert(flag.length >= 0);
+ assert(flag.tag != TEXT_NONE);
OptionalText_t alias_flag = flagify(arg->alias, true);
- Text_t flags = alias_flag.length >= 0 ? Texts(flag, "|", alias_flag) : flag;
+ Text_t flags = alias_flag.tag != TEXT_NONE ? Texts(flag, "|", alias_flag) : flag;
if (t->tag == BoolType || (t->tag == OptionalType && Match(t, OptionalType)->type->tag == BoolType))
usage = Texts(usage, "[", flags, "]");
else if (t->tag == ListType) usage = Texts(usage, "[", flags, " ", get_flag_options(t, "|"), "]");
else usage = Texts(usage, "[", flags, "=", get_flag_options(t, "|"), "]");
} else {
OptionalText_t flag = flagify(arg->name, false);
- assert(flag.length >= 0);
+ assert(flag.tag != TEXT_NONE);
OptionalText_t alias_flag = flagify(arg->alias, true);
if (t->tag == BoolType)
- usage = Texts(usage, "<--", flag, alias_flag.length >= 0 ? Texts("|", alias_flag) : EMPTY_TEXT,
+ usage = Texts(usage, "<--", flag, alias_flag.tag != TEXT_NONE ? Texts("|", alias_flag) : EMPTY_TEXT,
"|--no-", flag, ">");
else if (t->tag == EnumType) usage = Texts(usage, get_flag_options(t, "|"));
else if (t->tag == ListType) usage = Texts(usage, "[", flag, "...]");
diff --git a/src/compile/expressions.c b/src/compile/expressions.c
index a4603cd5..72fc5e73 100644
--- a/src/compile/expressions.c
+++ b/src/compile/expressions.c
@@ -41,8 +41,8 @@ Text_t compile_empty(type_t *t) {
}
case ByteType: return Text("((Byte_t)0)");
case BoolType: return Text("((Bool_t)no)");
- case ListType: return Text("((List_t){})");
- case TableType: return Text("((Table_t){})");
+ case ListType: return Text("((List_t){.has_value=1})");
+ case TableType: return Text("((Table_t){.entries.has_value=1})");
case TextType: return Text("Text(\"\")");
case CStringType: return Text("\"\"");
case PointerType: {
@@ -177,7 +177,7 @@ Text_t compile(env_t *env, ast_t *ast) {
}
case List: {
DeclareMatch(list, ast, List);
- if (!list->items) return Text("(List_t){.length=0}");
+ if (!list->items) return Text("(List_t){.has_value=1, .length=0}");
type_t *list_type = get_type(env, ast);
return compile_typed_list(env, ast, list_type);
diff --git a/src/compile/functions.c b/src/compile/functions.c
index d2320e04..a1bc15ae 100644
--- a/src/compile/functions.c
+++ b/src/compile/functions.c
@@ -62,7 +62,7 @@ Text_t compile_convert_declaration(env_t *env, ast_t *ast) {
public
Text_t compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t *call_args) {
- Table_t used_args = {};
+ Table_t used_args = EMPTY_TABLE;
Text_t code = EMPTY_TEXT;
env_t *default_scope = new (env_t);
*default_scope = *env;
@@ -263,7 +263,7 @@ Text_t compile_lambda(env_t *env, ast_t *ast) {
Table_t closed_vars = get_closed_vars(env, lambda->args, ast);
if (Table$length(closed_vars) > 0) { // Create a typedef for the lambda's closure userdata
Text_t def = Text("typedef struct {");
- for (int64_t i = 0; i < closed_vars.entries.length; i++) {
+ for (int64_t i = 0; i < (int64_t)closed_vars.entries.length; i++) {
struct {
const char *name;
binding_t *b;
@@ -293,7 +293,7 @@ Text_t compile_lambda(env_t *env, ast_t *ast) {
userdata = Text("NULL");
} else {
userdata = Texts("new(", name, "$userdata_t");
- for (int64_t i = 0; i < closed_vars.entries.length; i++) {
+ for (int64_t i = 0; i < (int64_t)closed_vars.entries.length; i++) {
struct {
const char *name;
binding_t *b;
@@ -596,7 +596,7 @@ Table_t get_closed_vars(env_t *env, arg_ast_t *args, ast_t *block) {
set_binding(body_scope, arg->name, arg_type, Texts("_$", arg->name));
}
- Table_t closed_vars = {};
+ Table_t closed_vars = EMPTY_TABLE;
add_closed_vars(&closed_vars, env, body_scope, block);
return closed_vars;
}
@@ -633,7 +633,7 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static
}
Text_t arg_signature = Text("(");
- Table_t used_names = {};
+ Table_t used_names = EMPTY_TABLE;
for (arg_ast_t *arg = args; arg; arg = arg->next) {
type_t *arg_type = get_arg_ast_type(env, arg);
arg_signature = Texts(arg_signature, compile_declaration(arg_type, Texts("_$", arg->name)));
@@ -727,7 +727,7 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static
Text_t wrapper =
Texts(is_private ? EMPTY_TEXT : Text("public "), ret_type_code, " ", name_code, arg_signature,
"{\n"
- "static Table_t cache = {};\n",
+ "static Table_t cache = EMPTY_TABLE;\n",
"const TypeInfo_t *table_type = Table$info(", compile_type_info(arg_type), ", ",
compile_type_info(ret_t), ");\n",
compile_declaration(Type(PointerType, .pointed = ret_t), Text("cached")),
@@ -750,7 +750,7 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static
type_t *t = Type(StructType, .name = String("func$", get_line_number(ast->file, ast->start), "$args"),
.fields = fields, .env = env);
- int64_t num_fields = used_names.entries.length;
+ int64_t num_fields = (int64_t)used_names.entries.length;
const char *metamethods = is_packed_data(t) ? "PackedData$metamethods" : "Struct$metamethods";
Text_t args_typeinfo = Texts("((TypeInfo_t[1]){{.size=sizeof(args), "
".align=__alignof__(args), .metamethods=",
@@ -775,7 +775,7 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static
Text_t wrapper = Texts(
is_private ? EMPTY_TEXT : Text("public "), ret_type_code, " ", name_code, arg_signature,
"{\n"
- "static Table_t cache = {};\n",
+ "static Table_t cache = EMPTY_TABLE;\n",
args_type, " args = {", all_args,
"};\n"
"const TypeInfo_t *table_type = Table$info(",
diff --git a/src/compile/lists.c b/src/compile/lists.c
index e3aa951b..7bbf0471 100644
--- a/src/compile/lists.c
+++ b/src/compile/lists.c
@@ -20,7 +20,7 @@ static ast_t *add_to_list_comprehension(ast_t *item, ast_t *subject) {
public
Text_t compile_typed_list(env_t *env, ast_t *ast, type_t *list_type) {
DeclareMatch(list, ast, List);
- if (!list->items) return Text("(List_t){.length=0}");
+ if (!list->items) return Text("(List_t){.has_value=1, .length=0}");
type_t *item_type = Match(list_type, ListType)->item_type;
@@ -48,7 +48,7 @@ list_comprehension: {
LiteralCode(Texts("&", comprehension_name), .type = Type(PointerType, .pointed = list_type, .is_stack = true));
Closure_t comp_action = {.fn = add_to_list_comprehension, .userdata = comprehension_var};
scope->comprehension_action = &comp_action;
- Text_t code = Texts("({ List_t ", comprehension_name, " = {};");
+ Text_t code = Texts("({ List_t ", comprehension_name, " = {.has_value=1};");
// set_binding(scope, comprehension_name, list_type, comprehension_name);
for (ast_list_t *item = list->items; item; item = item->next) {
if (item->ast->tag == Comprehension) code = Texts(code, "\n", compile_statement(scope, item->ast));
diff --git a/src/compile/optionals.c b/src/compile/optionals.c
index d74f0f31..e855c73c 100644
--- a/src/compile/optionals.c
+++ b/src/compile/optionals.c
@@ -54,7 +54,7 @@ Text_t compile_none(type_t *t) {
if (t == NULL) compiler_err(NULL, NULL, NULL, "I can't compile a `none` value with no type");
if (t == PATH_TYPE) return Text("NONE_PATH");
- else if (t == PATH_TYPE_TYPE) return Text("((OptionalPathType_t){})");
+ else if (t == PATH_TYPE_TYPE) return Text("((OptionalPathType_t){.type = PATH_NONE})");
switch (t->tag) {
case BigIntType: return Text("NONE_INT");
@@ -99,10 +99,10 @@ Text_t check_none(type_t *t, Text_t value) {
else if (t->tag == ClosureType) return Texts("((", value, ").fn == NULL)");
else if (t->tag == NumType)
return Texts(Match(t, NumType)->bits == TYPE_NBITS64 ? "Num$isnan(" : "Num32$isnan(", value, ")");
- else if (t->tag == ListType) return Texts("((", value, ").length < 0)");
- else if (t->tag == TableType) return Texts("((", value, ").entries.length < 0)");
+ else if (t->tag == ListType) return Texts("!(", value, ").has_value");
+ else if (t->tag == TableType) return Texts("!(", value, ").entries.has_value");
else if (t->tag == BoolType) return Texts("((", value, ") == NONE_BOOL)");
- else if (t->tag == TextType) return Texts("((", value, ").length < 0)");
+ else if (t->tag == TextType) return Texts("!(", value, ").has_value");
else if (t->tag == IntType || t->tag == ByteType || t->tag == StructType) return Texts("(", value, ").is_none");
else if (t->tag == EnumType) {
if (enum_has_fields(t)) return Texts("((", value, ").$tag == 0)");
diff --git a/src/compile/statements.c b/src/compile/statements.c
index 37eff680..156cc8c0 100644
--- a/src/compile/statements.c
+++ b/src/compile/statements.c
@@ -91,7 +91,7 @@ static Text_t _compile_statement(env_t *env, ast_t *ast) {
static int defer_id = 0;
env_t *defer_env = fresh_scope(env);
Text_t code = EMPTY_TEXT;
- for (int64_t i = 0; i < closed_vars.entries.length; i++) {
+ for (int64_t i = 0; i < (int64_t)closed_vars.entries.length; i++) {
struct {
const char *name;
binding_t *b;
diff --git a/src/compile/tables.c b/src/compile/tables.c
index 814d81f5..1ceb96b3 100644
--- a/src/compile/tables.c
+++ b/src/compile/tables.c
@@ -60,8 +60,8 @@ table_comprehension: {
ast_t *comprehension_var =
LiteralCode(Texts("&", comprehension_name), .type = Type(PointerType, .pointed = table_type, .is_stack = true));
- Text_t code = Texts("({ Table_t ", comprehension_name, " = {");
- if (table->fallback) code = Texts(code, ".fallback=heap(", compile(env, table->fallback), "), ");
+ Text_t code = Texts("({ Table_t ", comprehension_name, " = {.entries.has_value=1");
+ if (table->fallback) code = Texts(code, ", .fallback=heap(", compile(env, table->fallback), "), ");
code = Texts(code, "};");