diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-09-01 13:20:39 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-09-01 13:20:39 -0400 |
| commit | d6d3f5711de85ab1c21f515b9d125d316d853c92 (patch) | |
| tree | dc083bbe83b2dae82c47fdd78950941d53399bad /src | |
| parent | db6107c33df6a4fdb592ad29074d1159c37dc048 (diff) | |
| parent | ee020c72d92c3807fa4afcd1e170d3df81688b97 (diff) | |
Merge branch 'main' into formatter
Diffstat (limited to 'src')
| -rw-r--r-- | src/compile/assertions.c | 35 | ||||
| -rw-r--r-- | src/compile/assignments.c | 7 | ||||
| -rw-r--r-- | src/compile/conditionals.c | 2 | ||||
| -rw-r--r-- | src/compile/doctests.c | 17 | ||||
| -rw-r--r-- | src/compile/enums.c | 20 | ||||
| -rw-r--r-- | src/compile/files.c | 4 | ||||
| -rw-r--r-- | src/compile/functions.c | 24 | ||||
| -rw-r--r-- | src/compile/indexing.c | 4 | ||||
| -rw-r--r-- | src/compile/lists.c | 2 | ||||
| -rw-r--r-- | src/compile/loops.c | 4 | ||||
| -rw-r--r-- | src/compile/optionals.c | 11 | ||||
| -rw-r--r-- | src/compile/promotions.c | 12 | ||||
| -rw-r--r-- | src/compile/sets.c | 2 | ||||
| -rw-r--r-- | src/compile/statements.c | 4 | ||||
| -rw-r--r-- | src/compile/structs.c | 9 | ||||
| -rw-r--r-- | src/compile/tables.c | 2 | ||||
| -rw-r--r-- | src/compile/types.c | 4 | ||||
| -rw-r--r-- | src/stdlib/integers.c | 2 | ||||
| -rw-r--r-- | src/stdlib/integers.h | 1 | ||||
| -rw-r--r-- | src/stdlib/nums.c | 36 | ||||
| -rw-r--r-- | src/stdlib/nums.h | 10 | ||||
| -rw-r--r-- | src/stdlib/pointers.c | 2 | ||||
| -rw-r--r-- | src/stdlib/text.c | 7 | ||||
| -rw-r--r-- | src/stdlib/text.h | 14 | ||||
| -rw-r--r-- | src/typecheck.c | 4 | ||||
| -rw-r--r-- | src/types.c | 5 |
26 files changed, 130 insertions, 114 deletions
diff --git a/src/compile/assertions.c b/src/compile/assertions.c index ce9abcbc..0f1d27b6 100644 --- a/src/compile/assertions.c +++ b/src/compile/assertions.c @@ -53,27 +53,26 @@ Text_t compile_assertion(env_t *env, ast_t *ast) { ast_t *var_comparison = new (ast_t, .file = expr->file, .start = expr->start, .end = expr->end, .tag = expr->tag, .__data.Equals = {.lhs = lhs_var, .rhs = rhs_var}); int64_t line = get_line_number(ast->file, ast->start); - return Texts("{ // assertion\n", compile_declaration(operand_t, Text("_lhs")), " = ", - compile_to_type(env, cmp.lhs, operand_t), ";\n", "\n#line ", String(line), "\n", - compile_declaration(operand_t, Text("_rhs")), " = ", compile_to_type(env, cmp.rhs, operand_t), - ";\n", "\n#line ", String(line), "\n", "if (!(", compile_condition(env, var_comparison), "))\n", - "#line ", String(line), "\n", - Texts("fail_source(", quoted_str(ast->file->filename), ", ", - String((int64_t)(expr->start - expr->file->text)), ", ", - String((int64_t)(expr->end - expr->file->text)), ", ", - message ? Texts("Text$as_c_string(", compile_to_type(env, message, Type(TextType)), ")") - : Text("\"This assertion failed!\""), - ", ", "\" (\", ", expr_as_text(Text("_lhs"), operand_t, Text("no")), - ", " - "\" ", - failure, " \", ", expr_as_text(Text("_rhs"), operand_t, Text("no")), ", \")\");\n"), - "}\n"); + return Texts( + "{ // assertion\n", compile_declaration(operand_t, Text("_lhs")), " = ", + compile_to_type(env, cmp.lhs, operand_t), ";\n", "\n#line ", line, "\n", + compile_declaration(operand_t, Text("_rhs")), " = ", compile_to_type(env, cmp.rhs, operand_t), ";\n", + "\n#line ", line, "\n", "if (!(", compile_condition(env, var_comparison), "))\n", "#line ", line, "\n", + Texts("fail_source(", quoted_str(ast->file->filename), ", ", (int64_t)(expr->start - expr->file->text), + ", ", (int64_t)(expr->end - expr->file->text), ", ", + message ? Texts("Text$as_c_string(", compile_to_type(env, message, Type(TextType)), ")") + : Text("\"This assertion failed!\""), + ", ", "\" (\", ", expr_as_text(Text("_lhs"), operand_t, Text("no")), + ", " + "\" ", + failure, " \", ", expr_as_text(Text("_rhs"), operand_t, Text("no")), ", \")\");\n"), + "}\n"); } default: { int64_t line = get_line_number(ast->file, ast->start); - return Texts("if (!(", compile_condition(env, expr), "))\n", "#line ", String(line), "\n", "fail_source(", - quoted_str(ast->file->filename), ", ", String((int64_t)(expr->start - expr->file->text)), ", ", - String((int64_t)(expr->end - expr->file->text)), ", ", + return Texts("if (!(", compile_condition(env, expr), "))\n", "#line ", line, "\n", "fail_source(", + quoted_str(ast->file->filename), ", ", (int64_t)(expr->start - expr->file->text), ", ", + (int64_t)(expr->end - expr->file->text), ", ", message ? Texts("Text$as_c_string(", compile_to_type(env, message, Type(TextType)), ")") : Text("\"This assertion failed!\""), ");\n"); diff --git a/src/compile/assignments.c b/src/compile/assignments.c index ab28b972..3cb60fd5 100644 --- a/src/compile/assignments.c +++ b/src/compile/assignments.c @@ -119,12 +119,12 @@ Text_t compile_assignment_statement(env_t *env, ast_t *ast) { "stack memory."); env_t *val_env = with_enum_scope(env, lhs_t); Text_t val = compile_to_type(val_env, value->ast, lhs_t); - code = Texts(code, compile_type(lhs_t), " $", String(i), " = ", val, ";\n"); + code = Texts(code, compile_type(lhs_t), " $", i, " = ", val, ";\n"); i += 1; } i = 1; for (ast_list_t *target = assign->targets; target; target = target->next) { - code = Texts(code, compile_assignment(env, target->ast, Texts("$", String(i))), ";\n"); + code = Texts(code, compile_assignment(env, target->ast, Texts("$", i)), ";\n"); i += 1; } return Texts(code, "\n}"); @@ -171,8 +171,7 @@ Text_t compile_lvalue(env_t *env, ast_t *ast) { ")"); } else { return Texts("List_lvalue(", compile_type(item_type), ", ", target_code, ", ", index_code, ", ", - String((int)(ast->start - ast->file->text)), ", ", - String((int)(ast->end - ast->file->text)), ")"); + (int64_t)(ast->start - ast->file->text), ", ", (int64_t)(ast->end - ast->file->text), ")"); } } else if (container_t->tag == TableType) { DeclareMatch(table_type, container_t, TableType); diff --git a/src/compile/conditionals.c b/src/compile/conditionals.c index 23d2a177..f9dfa751 100644 --- a/src/compile/conditionals.c +++ b/src/compile/conditionals.c @@ -59,7 +59,7 @@ Text_t compile_if_statement(env_t *env, ast_t *ast) { code = Texts(code, compile_block(nonnull_scope, if_->body)); if (if_->else_body) { - Text_t label = Texts("_falsey_", String((int64_t)(ast->start - ast->file->text))); + Text_t label = Texts("_falsey_", (int64_t)(ast->start - ast->file->text)); code = Texts(code, "else goto ", label, ";\n" "} else {\n", diff --git a/src/compile/doctests.c b/src/compile/doctests.c index 872684ed..20081ae7 100644 --- a/src/compile/doctests.c +++ b/src/compile/doctests.c @@ -4,7 +4,6 @@ #include "../config.h" #include "../environment.h" #include "../stdlib/datatypes.h" -#include "../stdlib/print.h" #include "../stdlib/text.h" #include "../stdlib/util.h" #include "../typecheck.h" @@ -69,12 +68,12 @@ Text_t compile_doctest(env_t *env, ast_t *ast) { if (target == assign->targets) expr_t = lhs_t; env_t *val_scope = with_enum_scope(env, lhs_t); Text_t val_code = compile_to_type(val_scope, value->ast, lhs_t); - test_code = Texts(test_code, compile_type(lhs_t), " $", String(i), " = ", val_code, ";\n"); + test_code = Texts(test_code, compile_type(lhs_t), " $", i, " = ", val_code, ";\n"); i += 1; } i = 1; for (ast_list_t *target = assign->targets; target; target = target->next) { - test_code = Texts(test_code, compile_assignment(env, target->ast, Texts("$", String(i))), ";\n"); + test_code = Texts(test_code, compile_assignment(env, target->ast, Texts("$", i)), ";\n"); i += 1; } @@ -104,16 +103,16 @@ Text_t compile_doctest(env_t *env, ast_t *ast) { if (test->expected) { return Texts(setup, "test(", compile_type(expr_t), ", ", test_code, ", ", compile_to_type(env, test->expected, expr_t), ", ", compile_type_info(expr_t), ", ", - String((int64_t)(test->expr->start - test->expr->file->text)), ", ", - String((int64_t)(test->expr->end - test->expr->file->text)), ");"); + (int64_t)(test->expr->start - test->expr->file->text), ", ", + (int64_t)(test->expr->end - test->expr->file->text), ");"); } else { if (expr_t->tag == VoidType || expr_t->tag == AbortType) { return Texts(setup, "inspect_void(", test_code, ", ", compile_type_info(expr_t), ", ", - String((int64_t)(test->expr->start - test->expr->file->text)), ", ", - String((int64_t)(test->expr->end - test->expr->file->text)), ");"); + (int64_t)(test->expr->start - test->expr->file->text), ", ", + (int64_t)(test->expr->end - test->expr->file->text), ");"); } return Texts(setup, "inspect(", compile_type(expr_t), ", ", test_code, ", ", compile_type_info(expr_t), ", ", - String((int64_t)(test->expr->start - test->expr->file->text)), ", ", - String((int64_t)(test->expr->end - test->expr->file->text)), ");"); + (int64_t)(test->expr->start - test->expr->file->text), ", ", + (int64_t)(test->expr->end - test->expr->file->text), ");"); } } diff --git a/src/compile/enums.c b/src/compile/enums.c index d9c29f26..f5500831 100644 --- a/src/compile/enums.c +++ b/src/compile/enums.c @@ -30,12 +30,12 @@ Text_t compile_enum_typeinfo(env_t *env, ast_t *ast) { type_t *t = Table$str_get(*env->types, def->name); const char *metamethods = is_packed_data(t) ? "PackedDataEnum$metamethods" : "Enum$metamethods"; Text_t info = namespace_name(env, env->namespace, Texts(def->name, "$$info")); - Text_t typeinfo = Texts("public const TypeInfo_t ", info, " = {", String((int64_t)type_size(t)), "u, ", - String((int64_t)type_align(t)), "u, .metamethods=", metamethods, - ", {.tag=EnumInfo, .EnumInfo={.name=\"", def->name, - "\", " - ".num_tags=", - String((int64_t)num_tags), ", .tags=(NamedType_t[]){"); + Text_t typeinfo = + Texts("public const TypeInfo_t ", info, " = {", (int64_t)type_size(t), "u, ", (int64_t)type_align(t), + "u, .metamethods=", metamethods, ", {.tag=EnumInfo, .EnumInfo={.name=\"", def->name, + "\", " + ".num_tags=", + (int64_t)num_tags, ", .tags=(NamedType_t[]){"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { const char *tag_type_name = String(def->name, "$", tag->name); @@ -144,10 +144,10 @@ Text_t compile_empty_enum(type_t *t) { assert(tag); assert(tag->type); if (Match(tag->type, StructType)->fields) - return Texts("((", compile_type(t), "){.$tag=", String(tag->tag_value), ", .", tag->name, "=", - compile_empty(tag->type), "})"); - else if (enum_has_fields(t)) return Texts("((", compile_type(t), "){.$tag=", String(tag->tag_value), "})"); - else return Texts("((", compile_type(t), ")", String(tag->tag_value), ")"); + return Texts("((", compile_type(t), "){.$tag=", tag->tag_value, ", .", tag->name, "=", compile_empty(tag->type), + "})"); + else if (enum_has_fields(t)) return Texts("((", compile_type(t), "){.$tag=", tag->tag_value, "})"); + else return Texts("((", compile_type(t), ")", tag->tag_value, ")"); } public diff --git a/src/compile/files.c b/src/compile/files.c index 2b0ac4bf..a6af2300 100644 --- a/src/compile/files.c +++ b/src/compile/files.c @@ -103,7 +103,7 @@ static Text_t compile_top_level_code(env_t *env, ast_t *ast) { "types, not ", type_to_str(Match(type, FunctionType)->ret)); Text_t name_code = - namespace_name(env, env->namespace, Texts(name, "$", String(get_line_number(ast->file, ast->start)))); + namespace_name(env, env->namespace, Texts(name, "$", get_line_number(ast->file, ast->start))); return compile_function(env, name_code, ast, &env->code->staticdefs); } case StructDef: { @@ -125,7 +125,7 @@ static Text_t compile_top_level_code(env_t *env, ast_t *ast) { DeclareMatch(def, ast, LangDef); Text_t code = Texts("public const TypeInfo_t ", namespace_name(env, env->namespace, Texts(def->name, "$$info")), " = {", - String((int64_t)sizeof(Text_t)), ", ", String((int64_t)__alignof__(Text_t)), + (int64_t)sizeof(Text_t), ", ", (int64_t)__alignof__(Text_t), ", .metamethods=Text$metamethods, .tag=TextInfo, .TextInfo={", quoted_str(def->name), "}};\n"); env_t *ns_env = namespace_env(env, def->name); return Texts(code, def->namespace ? compile_top_level_code(ns_env, def->namespace) : EMPTY_TEXT); diff --git a/src/compile/functions.c b/src/compile/functions.c index 01de26e3..6caefa8b 100644 --- a/src/compile/functions.c +++ b/src/compile/functions.c @@ -32,7 +32,7 @@ Text_t compile_function_declaration(env_t *env, ast_t *ast) { if (ret_t->tag == AbortType) ret_type_code = Texts("__attribute__((noreturn)) _Noreturn ", ret_type_code); Text_t name = namespace_name(env, env->namespace, Text$from_str(decl_name)); if (env->namespace && env->namespace->parent && env->namespace->name && streq(decl_name, env->namespace->name)) - name = namespace_name(env, env->namespace, Text$from_str(String(get_line_number(ast->file, ast->start)))); + name = namespace_name(env, env->namespace, Texts(get_line_number(ast->file, ast->start))); return Texts(ret_type_code, " ", name, arg_signature, ";\n"); } @@ -56,8 +56,7 @@ Text_t compile_convert_declaration(env_t *env, ast_t *ast) { "Conversions are only supported for text, struct, and enum " "types, not ", type_to_str(ret_t)); - Text_t name_code = - namespace_name(env, env->namespace, Texts(name, "$", String(get_line_number(ast->file, ast->start)))); + Text_t name_code = namespace_name(env, env->namespace, Texts(name, "$", get_line_number(ast->file, ast->start))); return Texts(ret_type_code, " ", name_code, arg_signature, ";\n"); } @@ -248,7 +247,7 @@ Text_t compile_function_call(env_t *env, ast_t *ast) { public Text_t compile_lambda(env_t *env, ast_t *ast) { DeclareMatch(lambda, ast, Lambda); - Text_t name = namespace_name(env, env->namespace, Texts("lambda$", String(lambda->id))); + Text_t name = namespace_name(env, env->namespace, Texts("lambda$", lambda->id)); env_t *body_scope = fresh_scope(env); body_scope->deferred = NULL; @@ -736,7 +735,7 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static // FIXME: this currently just deletes the first entry, but this // should be more like a least-recently-used cache eviction policy // or least-frequently-used - pop_code = Texts("if (cache.entries.length > ", String(cache_size.value), + pop_code = Texts("if (cache.entries.length > ", cache_size.value, ") Table$remove(&cache, cache.entries.data + " "cache.entries.stride*0, table_type);\n"); } @@ -772,14 +771,13 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static int64_t num_fields = 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=", - metamethods, - ", .tag=StructInfo, " - ".StructInfo.name=\"FunctionArguments\", " - ".StructInfo.num_fields=", - String(num_fields), ", .StructInfo.fields=(NamedType_t[", String(num_fields), "]){"); + Text_t args_typeinfo = Texts("((TypeInfo_t[1]){{.size=sizeof(args), " + ".align=__alignof__(args), .metamethods=", + metamethods, + ", .tag=StructInfo, " + ".StructInfo.name=\"FunctionArguments\", " + ".StructInfo.num_fields=", + num_fields, ", .StructInfo.fields=(NamedType_t[", num_fields, "]){"); Text_t args_type = Text("struct { "); for (arg_t *f = fields; f; f = f->next) { args_typeinfo = Texts(args_typeinfo, "{\"", f->name, "\", ", compile_type_info(f->type), "}"); diff --git a/src/compile/indexing.c b/src/compile/indexing.c index e99feeb2..39af1160 100644 --- a/src/compile/indexing.c +++ b/src/compile/indexing.c @@ -44,8 +44,8 @@ Text_t compile_indexing(env_t *env, ast_t *ast) { return Texts("List_get_unchecked(", compile_type(item_type), ", ", list, ", ", index_code, ")"); else return Texts("List_get(", compile_type(item_type), ", ", list, ", ", index_code, ", ", - String((int64_t)(indexing->index->start - f->text)), ", ", - String((int64_t)(indexing->index->end - f->text)), ")"); + (int64_t)(indexing->index->start - f->text), ", ", (int64_t)(indexing->index->end - f->text), + ")"); } else if (container_t->tag == TableType) { DeclareMatch(table_type, container_t, TableType); if (indexing->unchecked) code_err(ast, "Table indexes cannot be unchecked"); diff --git a/src/compile/lists.c b/src/compile/lists.c index 5df39863..d9d71278 100644 --- a/src/compile/lists.c +++ b/src/compile/lists.c @@ -33,7 +33,7 @@ Text_t compile_typed_list(env_t *env, ast_t *ast, type_t *list_type) { { env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : env; if (is_incomplete_type(item_type)) code_err(ast, "This list's type can't be inferred!"); - Text_t code = Texts("TypedListN(", compile_type(item_type), ", ", String(n)); + Text_t code = Texts("TypedListN(", compile_type(item_type), ", ", n); for (ast_list_t *item = list->items; item; item = item->next) { code = Texts(code, ", ", compile_to_type(scope, item->ast, item_type)); } diff --git a/src/compile/loops.c b/src/compile/loops.c index c742589a..332024f4 100644 --- a/src/compile/loops.c +++ b/src/compile/loops.c @@ -405,7 +405,7 @@ Text_t compile_skip(env_t *env, ast_t *ast) { if (matched) { if (ctx->skip_label.length == 0) { static int64_t skip_label_count = 1; - ctx->skip_label = Texts("skip_", String(skip_label_count)); + ctx->skip_label = Texts("skip_", skip_label_count); ++skip_label_count; } Text_t code = EMPTY_TEXT; @@ -431,7 +431,7 @@ Text_t compile_stop(env_t *env, ast_t *ast) { if (matched) { if (ctx->stop_label.length == 0) { static int64_t stop_label_count = 1; - ctx->stop_label = Texts("stop_", String(stop_label_count)); + ctx->stop_label = Texts("stop_", stop_label_count); ++stop_label_count; } Text_t code = EMPTY_TEXT; diff --git a/src/compile/optionals.c b/src/compile/optionals.c index b3d94005..cd50b1bf 100644 --- a/src/compile/optionals.c +++ b/src/compile/optionals.c @@ -125,10 +125,9 @@ Text_t compile_non_optional(env_t *env, ast_t *ast) { type_t *t = get_type(env, value); Text_t value_code = compile(env, value); int64_t line = get_line_number(ast->file, ast->start); - return Texts("({ ", compile_declaration(t, Text("opt")), " = ", value_code, "; ", "if unlikely (", - check_none(t, Text("opt")), ")\n", "#line ", String(line), "\n", "fail_source(", - quoted_str(ast->file->filename), ", ", String((int64_t)(value->start - value->file->text)), ", ", - String((int64_t)(value->end - value->file->text)), ", ", - "\"This was expected to be a value, but it's none\");\n", optional_into_nonnone(t, Text("opt")), - "; })"); + return Texts( + "({ ", compile_declaration(t, Text("opt")), " = ", value_code, "; ", "if unlikely (", + check_none(t, Text("opt")), ")\n", "#line ", line, "\n", "fail_source(", quoted_str(ast->file->filename), ", ", + (int64_t)(value->start - value->file->text), ", ", (int64_t)(value->end - value->file->text), ", ", + "\"This was expected to be a value, but it's none\");\n", optional_into_nonnone(t, Text("opt")), "; })"); } diff --git a/src/compile/promotions.c b/src/compile/promotions.c index fcedce3f..6595e5aa 100644 --- a/src/compile/promotions.c +++ b/src/compile/promotions.c @@ -43,12 +43,12 @@ bool promote(env_t *env, ast_t *ast, Text_t *code, type_t *actual, type_t *neede // Automatic optional checking for nums: if (needed->tag == NumType && actual->tag == OptionalType && Match(actual, OptionalType)->type->tag == NumType) { int64_t line = get_line_number(ast->file, ast->start); - *code = Texts("({ ", compile_declaration(actual, Text("opt")), " = ", *code, "; ", "if unlikely (", - check_none(actual, Text("opt")), ")\n", "#line ", String(line), "\n", "fail_source(", - quoted_str(ast->file->filename), ", ", String((int64_t)(ast->start - ast->file->text)), ", ", - String((int64_t)(ast->end - ast->file->text)), ", ", - "\"This was expected to be a value, but it's none\");\n", - optional_into_nonnone(actual, Text("opt")), "; })"); + *code = + Texts("({ ", compile_declaration(actual, Text("opt")), " = ", *code, "; ", "if unlikely (", + check_none(actual, Text("opt")), ")\n", "#line ", line, "\n", "fail_source(", + quoted_str(ast->file->filename), ", ", (int64_t)(ast->start - ast->file->text), ", ", + (int64_t)(ast->end - ast->file->text), ", ", "\"This was expected to be a value, but it's none\");\n", + optional_into_nonnone(actual, Text("opt")), "; })"); return true; } diff --git a/src/compile/sets.c b/src/compile/sets.c index d33677cc..3346a9aa 100644 --- a/src/compile/sets.c +++ b/src/compile/sets.c @@ -25,7 +25,7 @@ Text_t compile_typed_set(env_t *env, ast_t *ast, type_t *set_type) { } { // No comprehension: - Text_t code = Texts("Set(", compile_type(item_type), ", ", compile_type_info(item_type), ", ", String(n)); + Text_t code = Texts("Set(", compile_type(item_type), ", ", compile_type_info(item_type), ", ", n); env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : env; for (ast_list_t *item = set->items; item; item = item->next) { code = Texts(code, ", ", compile_to_type(scope, item->ast, item_type)); diff --git a/src/compile/statements.c b/src/compile/statements.c index 7c58559d..e296795b 100644 --- a/src/compile/statements.c +++ b/src/compile/statements.c @@ -22,7 +22,7 @@ public Text_t with_source_info(env_t *env, ast_t *ast, Text_t code) { if (code.length == 0 || !ast || !ast->file || !env->do_source_mapping) return code; int64_t line = get_line_number(ast->file, ast->start); - return Texts("\n#line ", String(line), "\n", code); + return Texts("\n#line ", line, "\n", code); } static Text_t _compile_statement(env_t *env, ast_t *ast) { @@ -121,7 +121,7 @@ static Text_t _compile_statement(env_t *env, ast_t *ast) { if (Text$starts_with(entry->b->code, Text("userdata->"), NULL)) { Table$str_set(defer_env->locals, entry->name, entry->b); } else { - Text_t defer_name = Texts("defer$", String(++defer_id), "$", entry->name); + Text_t defer_name = Texts("defer$", ++defer_id, "$", entry->name); defer_id += 1; code = Texts(code, compile_declaration(entry->b->type, defer_name), " = ", entry->b->code, ";\n"); set_binding(defer_env, entry->name, entry->b->type, defer_name); diff --git a/src/compile/structs.c b/src/compile/structs.c index 526c11c7..8560a790 100644 --- a/src/compile/structs.c +++ b/src/compile/structs.c @@ -18,7 +18,7 @@ Text_t compile_struct_typeinfo(env_t *env, type_t *t, const char *name, arg_ast_ ? Text$from_str(name) : Texts("struct ", namespace_name(env, env->namespace, Texts(name, "$$struct"))); - int num_fields = 0; + int64_t num_fields = 0; for (arg_ast_t *f = fields; f; f = f->next) num_fields += 1; const char *short_name = name; @@ -33,9 +33,9 @@ Text_t compile_struct_typeinfo(env_t *env, type_t *t, const char *name, arg_ast_ ", " ".tag=StructInfo, .StructInfo.name=\"", short_name, "\"", is_secret ? Text(", .StructInfo.is_secret=true") : EMPTY_TEXT, - is_opaque ? Text(", .StructInfo.is_opaque=true") : EMPTY_TEXT, ", .StructInfo.num_fields=", String(num_fields)); + is_opaque ? Text(", .StructInfo.is_opaque=true") : EMPTY_TEXT, ", .StructInfo.num_fields=", num_fields); if (fields) { - typeinfo = Texts(typeinfo, ", .StructInfo.fields=(NamedType_t[", String(num_fields), "]){"); + typeinfo = Texts(typeinfo, ", .StructInfo.fields=(NamedType_t[", num_fields, "]){"); for (arg_ast_t *f = fields; f; f = f->next) { type_t *field_type = get_arg_ast_type(env, f); typeinfo = Texts(typeinfo, "{\"", f->name, "\", ", compile_type_info(field_type), "}"); @@ -70,8 +70,7 @@ Text_t compile_struct_header(env_t *env, ast_t *ast) { Text_t struct_code = def->external ? EMPTY_TEXT : Texts(type_code, " {\n", fields, "};\n"); type_t *t = Table$str_get(*env->types, def->name); - Text_t unpadded_size = - def->opaque ? Texts("sizeof(", type_code, ")") : Text$from_str(String((int64_t)unpadded_struct_size(t))); + Text_t unpadded_size = def->opaque ? Texts("sizeof(", type_code, ")") : Texts((int64_t)unpadded_struct_size(t)); Text_t typeinfo_code = Texts("extern const TypeInfo_t ", typeinfo_name, ";\n"); Text_t optional_code = EMPTY_TEXT; if (!def->opaque) { diff --git a/src/compile/tables.c b/src/compile/tables.c index b955178e..dde8669a 100644 --- a/src/compile/tables.c +++ b/src/compile/tables.c @@ -43,7 +43,7 @@ Text_t compile_typed_table(env_t *env, ast_t *ast, type_t *table_type) { size_t n = 0; for (ast_list_t *entry = table->entries; entry; entry = entry->next) ++n; - code = Texts(code, ", ", String((int64_t)n)); + code = Texts(code, ", ", (int64_t)n); for (ast_list_t *entry = table->entries; entry; entry = entry->next) { DeclareMatch(e, entry->ast, TableEntry); diff --git a/src/compile/types.c b/src/compile/types.c index aa06e2fd..f6f276a5 100644 --- a/src/compile/types.c +++ b/src/compile/types.c @@ -23,10 +23,10 @@ Text_t compile_type(type_t *t) { case ByteType: return Text("Byte_t"); case CStringType: return Text("const char*"); case BigIntType: return Text("Int_t"); - case IntType: return Texts("Int", String(Match(t, IntType)->bits), "_t"); + case IntType: return Texts("Int", (int32_t)Match(t, IntType)->bits, "_t"); case NumType: return Match(t, NumType)->bits == TYPE_NBITS64 ? Text("Num_t") - : Texts("Num", String(Match(t, NumType)->bits), "_t"); + : Texts("Num", (int32_t)Match(t, NumType)->bits, "_t"); case TextType: { DeclareMatch(text, t, TextType); if (!text->lang || streq(text->lang, "Text")) return Text("Text_t"); diff --git a/src/stdlib/integers.c b/src/stdlib/integers.c index 7dda77bd..5dc9ac55 100644 --- a/src/stdlib/integers.c +++ b/src/stdlib/integers.c @@ -618,6 +618,8 @@ void Int32$deserialize(FILE *in, void *outval, List_t *pointers, const TypeInfo_ return colorize ? Texts(Text("\033[35m"), text, Text("\033[m")) : text; \ } \ public \ + Text_t KindOfInt##$value_as_text(c_type i) { return _int64_to_text((int64_t)i); } \ + public \ PUREFUNC int32_t KindOfInt##$compare(const void *x, const void *y, const TypeInfo_t *info) { \ (void)info; \ return (*(c_type *)x > *(c_type *)y) - (*(c_type *)x < *(c_type *)y); \ diff --git a/src/stdlib/integers.h b/src/stdlib/integers.h index 40c40754..34195d23 100644 --- a/src/stdlib/integers.h +++ b/src/stdlib/integers.h @@ -22,6 +22,7 @@ bool is_none : 1; \ } Optional##type_name##_t; \ Text_t type_name##$as_text(const void *i, bool colorize, const TypeInfo_t *type); \ + Text_t type_name##$value_as_text(c_type i); \ PUREFUNC int32_t type_name##$compare(const void *x, const void *y, const TypeInfo_t *type); \ PUREFUNC bool type_name##$equal(const void *x, const void *y, const TypeInfo_t *type); \ Text_t type_name##$hex(c_type i, Int_t digits, bool uppercase, bool prefix); \ diff --git a/src/stdlib/nums.c b/src/stdlib/nums.c index 55131cfd..4bbb1f6a 100644 --- a/src/stdlib/nums.c +++ b/src/stdlib/nums.c @@ -14,13 +14,18 @@ #include "types.h" public -PUREFUNC Text_t Num$as_text(const void *f, bool colorize, const TypeInfo_t *info) { - (void)info; - if (!f) return Text("Num"); +PUREFUNC Text_t Num$value_as_text(double x) { char *str = GC_MALLOC_ATOMIC(24); - int len = fpconv_dtoa(*(double *)f, str); + int len = fpconv_dtoa(x, str); + return Text$from_strn(str, (size_t)len); +} + +public +PUREFUNC Text_t Num$as_text(const void *x, bool colorize, const TypeInfo_t *info) { + (void)info; + if (!x) return Text("Num"); static const Text_t color_prefix = Text("\x1b[35m"), color_suffix = Text("\x1b[m"); - Text_t text = Text$from_strn(str, (size_t)len); + Text_t text = Num$value_as_text(*(double *)x); return colorize ? Texts(color_prefix, text, color_suffix) : text; } @@ -60,10 +65,10 @@ CONSTFUNC bool Num$near(double a, double b, double ratio, double absolute) { } public -Text_t Num$percent(double f, double precision) { - double d = 100. * f; +Text_t Num$percent(double x, double precision) { + double d = 100. * x; d = Num$with_precision(d, precision); - return Texts(Num$as_text(&d, false, &Num$info), Text("%")); + return Texts(Num$value_as_text(d), Text("%")); } public @@ -142,10 +147,13 @@ const TypeInfo_t Num$info = { }; public -PUREFUNC Text_t Num32$as_text(const void *f, bool colorize, const TypeInfo_t *info) { +PUREFUNC Text_t Num32$value_as_text(float x) { return Num$value_as_text((double)x); } + +public +PUREFUNC Text_t Num32$as_text(const void *x, bool colorize, const TypeInfo_t *info) { (void)info; - if (!f) return Text("Num32"); - double d = (double)(*(float *)f); + if (!x) return Text("Num32"); + double d = (double)(*(float *)x); return Num$as_text(&d, colorize, &Num$info); } @@ -178,10 +186,10 @@ CONSTFUNC bool Num32$near(float a, float b, float ratio, float absolute) { } public -Text_t Num32$percent(float f, float precision) { - double d = 100. * (double)f; +Text_t Num32$percent(float x, float precision) { + double d = 100. * (double)x; d = Num$with_precision(d, (double)precision); - return Texts(Num$as_text(&d, false, &Num$info), Text("%")); + return Texts(Num$value_as_text(d), Text("%")); } public diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h index db051ed2..303aa362 100644 --- a/src/stdlib/nums.h +++ b/src/stdlib/nums.h @@ -15,11 +15,12 @@ #define N32(n) ((float)(n)) #define N64(n) ((double)(n)) -Text_t Num$as_text(const void *f, bool colorize, const TypeInfo_t *type); +Text_t Num$as_text(const void *x, bool colorize, const TypeInfo_t *type); +Text_t Num$value_as_text(double x); PUREFUNC int32_t Num$compare(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC bool Num$equal(const void *x, const void *y, const TypeInfo_t *type); CONSTFUNC bool Num$near(double a, double b, double ratio, double absolute); -Text_t Num$percent(double f, double precision); +Text_t Num$percent(double x, double precision); double CONSTFUNC Num$with_precision(double num, double precision); double Num$mod(double num, double modulus); double Num$mod1(double num, double modulus); @@ -70,11 +71,12 @@ MACROLIKE CONSTFUNC double Num$from_byte(Byte_t i) { return (double)i; } extern const TypeInfo_t Num$info; -Text_t Num32$as_text(const void *f, bool colorize, const TypeInfo_t *type); +Text_t Num32$as_text(const void *x, bool colorize, const TypeInfo_t *type); +Text_t Num32$value_as_text(float x); PUREFUNC int32_t Num32$compare(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC bool Num32$equal(const void *x, const void *y, const TypeInfo_t *type); CONSTFUNC bool Num32$near(float a, float b, float ratio, float absolute); -Text_t Num32$percent(float f, float precision); +Text_t Num32$percent(float x, float precision); float CONSTFUNC Num32$with_precision(float num, float precision); float Num32$mod(float num, float modulus); float Num32$mod1(float num, float modulus); diff --git a/src/stdlib/pointers.c b/src/stdlib/pointers.c index b5e6400f..0a1623a0 100644 --- a/src/stdlib/pointers.c +++ b/src/stdlib/pointers.c @@ -44,7 +44,7 @@ Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo_t *type) { TypeInfo_t rec_table = *Table$info(type, &Int64$info); int64_t *id = Table$get(pending, x, &rec_table); if (id) { - Text_t text = Texts(Text$from_str(ptr_info.sigil), Int64$as_text(id, false, &Int64$info)); + Text_t text = Texts(Text$from_str(ptr_info.sigil), Int64$value_as_text(*id)); return colorize ? Texts(Text("\x1b[34;1m"), text, Text("\x1b[m")) : text; } int64_t next_id = pending.entries.length + 2; diff --git a/src/stdlib/text.c b/src/stdlib/text.c index 57465034..d3757a0d 100644 --- a/src/stdlib/text.c +++ b/src/stdlib/text.c @@ -1729,10 +1729,9 @@ Int_t Text$memory_size(Text_t text) { public Text_t Text$layout(Text_t text) { switch (text.tag) { - case TEXT_ASCII: return Texts(Text("ASCII("), Int64$as_text((int64_t[1]){text.length}, false, NULL), Text(")")); - case TEXT_GRAPHEMES: - return Texts(Text("Graphemes("), Int64$as_text((int64_t[1]){text.length}, false, NULL), Text(")")); - case TEXT_BLOB: return Texts(Text("Blob("), Int64$as_text((int64_t[1]){text.length}, false, NULL), Text(")")); + case TEXT_ASCII: return Texts(Text("ASCII("), Int64$value_as_text(text.length), Text(")")); + case TEXT_GRAPHEMES: return Texts(Text("Graphemes("), Int64$value_as_text(text.length), Text(")")); + case TEXT_BLOB: return Texts(Text("Blob("), Int64$value_as_text(text.length), Text(")")); case TEXT_CONCAT: return Texts(Text("Concat("), Text$layout(*text.left), Text(", "), Text$layout(*text.right), Text(")")); default: errx(1, "Invalid text tag: %d", text.tag); diff --git a/src/stdlib/text.h b/src/stdlib/text.h index d118cffd..7f7fc2c6 100644 --- a/src/stdlib/text.h +++ b/src/stdlib/text.h @@ -7,7 +7,9 @@ #include <stdint.h> #include "datatypes.h" +#include "integers.h" // IWYU pragma: export #include "mapmacro.h" +#include "nums.h" // IWYU pragma: export #include "types.h" #include "util.h" @@ -31,7 +33,17 @@ static inline Text_t Text_from_str_literal(const char *str) { static inline Text_t Text_from_text(Text_t t) { return t; } -#define convert_to_text(x) _Generic(x, Text_t: Text_from_text, char *: Text$from_str, const char *: Text$from_str)(x) +#define convert_to_text(x) \ + _Generic(x, \ + Text_t: Text_from_text, \ + char *: Text$from_str, \ + const char *: Text$from_str, \ + int8_t: Int8$value_as_text, \ + int16_t: Int16$value_as_text, \ + int32_t: Int32$value_as_text, \ + int64_t: Int64$value_as_text, \ + double: Num$value_as_text, \ + float: Num32$value_as_text)(x) Text_t Text$_concat(int n, Text_t items[n]); #define Text$concat(...) Text$_concat(sizeof((Text_t[]){__VA_ARGS__}) / sizeof(Text_t), (Text_t[]){__VA_ARGS__}) diff --git a/src/typecheck.c b/src/typecheck.c index 9e808111..89123c26 100644 --- a/src/typecheck.c +++ b/src/typecheck.c @@ -349,8 +349,8 @@ void bind_statement(env_t *env, ast_t *statement) { code_err(statement, "Conversions are only supported for text, struct, and enum types, not ", type_to_str(ret_t)); - Text_t code = namespace_name(env, env->namespace, - Texts(name, "$", String(get_line_number(statement->file, statement->start)))); + Text_t code = + namespace_name(env, env->namespace, Texts(name, "$", get_line_number(statement->file, statement->start))); binding_t binding = {.type = type, .code = code}; env_t *type_ns = get_namespace_by_type(env, ret_t); List$insert(&type_ns->namespace->constructors, &binding, I(0), sizeof(binding)); diff --git a/src/types.c b/src/types.c index a4dcc5e5..b0caca1a 100644 --- a/src/types.c +++ b/src/types.c @@ -8,7 +8,6 @@ #include "environment.h" #include "stdlib/integers.h" -#include "stdlib/print.h" #include "stdlib/text.h" #include "stdlib/util.h" #include "types.h" @@ -30,7 +29,7 @@ Text_t type_to_text(type_t *t) { case CStringType: return Text("CString"); case TextType: return Match(t, TextType)->lang ? Text$from_str(Match(t, TextType)->lang) : Text("Text"); case BigIntType: return Text("Int"); - case IntType: return Texts("Int", String(Match(t, IntType)->bits)); + case IntType: return Texts("Int", (int32_t)Match(t, IntType)->bits); case NumType: return Match(t, NumType)->bits == TYPE_NBITS32 ? Text("Num32") : Text("Num"); case ListType: { DeclareMatch(list, t, ListType); @@ -84,7 +83,7 @@ Text_t type_to_text(type_t *t) { } default: { raise(SIGABRT); - return Texts("Unknown type: ", String(t->tag)); + return Texts("Unknown type: ", (int32_t)t->tag); } } } |
