From b0a8404bd7068c02f274978263fec00b8538d814 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 2 Sep 2024 19:18:21 -0400 Subject: [PATCH] Fix enums/structs --- builtins/text.c | 2 +- compile.c | 6 ++---- enums.c | 22 +++++++++++----------- structs.c | 22 ++++++++-------------- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/builtins/text.c b/builtins/text.c index 3c90a06..800c555 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -631,7 +631,7 @@ int32_t get_grapheme(Text_t text, int64_t index) return _next_grapheme(text, &state, index); } -int32_t Text$compare(const Text_t *a, const Text_t *b) +public int32_t Text$compare(const Text_t *a, const Text_t *b) { int64_t len = MAX(a->length, b->length); iteration_state_t a_state = {0, 0}, b_state = {0, 0}; diff --git a/compile.c b/compile.c index b72b6c2..19a8b4c 100644 --- a/compile.c +++ b/compile.c @@ -1763,7 +1763,7 @@ CORD compile(env_t *env, ast_t *ast) case BINOP_CONCAT: { switch (operand_t->tag) { case TextType: { - return CORD_all("CORD_cat(", lhs, ", ", rhs, ")"); + return CORD_all("Text$concat(", lhs, ", ", rhs, ")"); } case ArrayType: { CORD padded_item_size = CORD_asprintf("%ld", padded_type_size(Match(operand_t, ArrayType)->item_type)); @@ -1890,8 +1890,6 @@ CORD compile(env_t *env, ast_t *ast) comparison = CORD_all("(Int$compare_value(", lhs_key, ", ", rhs_key, ")", (ast->tag == Min ? "<=" : ">="), "0)"); else if (key_t->tag == IntType || key_t->tag == NumType || key_t->tag == BoolType || key_t->tag == PointerType) comparison = CORD_all("((", lhs_key, ")", (ast->tag == Min ? "<=" : ">="), "(", rhs_key, "))"); - else if (key_t->tag == TextType) - comparison = CORD_all("CORD_cmp(", lhs_key, ", ", rhs_key, ")", (ast->tag == Min ? "<=" : ">="), "0"); else comparison = CORD_all("generic_compare(stack(", lhs_key, "), stack(", rhs_key, "), ", compile_type_info(env, key_t), ")", (ast->tag == Min ? "<=" : ">="), "0"); @@ -2558,7 +2556,7 @@ CORD compile(env_t *env, ast_t *ast) if (!call->args || call->args->next) code_err(call->fn, "This constructor takes exactly 1 argument"); type_t *actual = get_type(env, call->args->value); - return CORD_all("CORD_to_char_star(", expr_as_text(env, compile(env, call->args->value), actual, "no"), ")"); + return CORD_all("Text$as_c_string(", expr_as_text(env, compile(env, call->args->value), actual, "no"), ")"); } else { code_err(call->fn, "This is not a type that has a constructor"); } diff --git a/enums.c b/enums.c index 12d53ef..1ad9628 100644 --- a/enums.c +++ b/enums.c @@ -24,33 +24,33 @@ static CORD compile_str_method(env_t *env, ast_t *ast) { auto def = Match(ast, EnumDef); CORD full_name = CORD_cat(namespace_prefix(env->libname, env->namespace), def->name); - CORD str_func = CORD_all("static CORD ", full_name, "$as_text(", full_name, "_t *obj, bool use_color) {\n" - "\tif (!obj) return \"", def->name, "\";\n" + CORD str_func = CORD_all("static Text_t ", full_name, "$as_text(", full_name, "_t *obj, bool use_color) {\n" + "\tif (!obj) return Text$from_str(\"", def->name, "\");\n" "switch (obj->tag) {\n"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { if (!tag->fields) { - str_func = CORD_all(str_func, "\tcase ", full_name, "$tag$", tag->name, ": return use_color ? \"\\x1b[36;1m", - def->name, ".", tag->name, "\\x1b[m\" : \"", def->name, ".", tag->name, "\";\n"); + str_func = CORD_all(str_func, "\tcase ", full_name, "$tag$", tag->name, ": return Text$from_str(use_color ? \"\\x1b[36;1m", + def->name, ".", tag->name, "\\x1b[m\" : \"", def->name, ".", tag->name, "\");\n"); continue; } - str_func = CORD_all(str_func, "\tcase ", full_name, "$tag$", tag->name, ": return CORD_all(use_color ? \"\\x1b[36;1m", - def->name, ".", tag->name, "\\x1b[m(\" : \"", def->name, ".", tag->name, "(\""); + str_func = CORD_all(str_func, "\tcase ", full_name, "$tag$", tag->name, ": return Text$concat(Text$from_str(use_color ? \"\\x1b[36;1m", + def->name, ".", tag->name, "\\x1b[m(\" : \"", def->name, ".", tag->name, "(\")"); if (tag->secret) { - str_func = CORD_cat(str_func, ", use_color ? \"\\x1b[2m...\\x1b[m\" : \"...\", \")\");\n"); + str_func = CORD_cat(str_func, ", Text$from_str(use_color ? \"\\x1b[2m...\\x1b[m\" : \"...\", \")\"));\n"); continue; } for (arg_ast_t *field = tag->fields; field; field = field->next) { type_t *field_t = get_arg_ast_type(env, field); CORD field_str = expr_as_text(env, CORD_all("obj->$", tag->name, ".$", field->name), field_t, "use_color"); - str_func = CORD_all(str_func, ", \"", field->name, "=\", ", field_str); - if (field->next) str_func = CORD_cat(str_func, ", \", \""); + str_func = CORD_all(str_func, ", Text$from_str(\"", field->name, "=\"), ", field_str); + if (field->next) str_func = CORD_cat(str_func, ", Text$from_str(\", \")"); } - str_func = CORD_cat(str_func, ", \")\");\n"); + str_func = CORD_cat(str_func, ", Text$from_str(\")\"));\n"); } - str_func = CORD_cat(str_func, "\tdefault: return CORD_EMPTY;\n\t}\n}\n"); + str_func = CORD_cat(str_func, "\tdefault: return (Text_t){.length=0};\n\t}\n}\n"); return str_func; } diff --git a/structs.c b/structs.c index 66ca3ff..4428587 100644 --- a/structs.c +++ b/structs.c @@ -18,20 +18,20 @@ static CORD compile_str_method(env_t *env, ast_t *ast) const char *name = def->name; const char *dollar = strrchr(name, '$'); if (dollar) name = dollar + 1; - CORD str_func = CORD_asprintf("static CORD %r$as_text(%r_t *obj, bool use_color) {\n" - "\tif (!obj) return \"%s\";\n", full_name, full_name, name); + CORD str_func = CORD_asprintf("static Text_t %r$as_text(%r_t *obj, bool use_color) {\n" + "\tif (!obj) return Text$from_str(\"%s\");\n", full_name, full_name, name); if (def->secret) { - CORD_appendf(&str_func, "\treturn use_color ? \"\\x1b[0;1m%s\\x1b[m(\\x1b[2m...\\x1b[m)\" : \"%s(...)\";\n}", + CORD_appendf(&str_func, "\treturn Text$from_str(use_color ? \"\\x1b[0;1m%s\\x1b[m(\\x1b[2m...\\x1b[m)\" : \"%s(...)\");\n}", name, name); } else { - CORD_appendf(&str_func, "\treturn CORD_all(use_color ? \"\\x1b[0;1m%s\\x1b[m(\" : \"%s(\"", name, name); + CORD_appendf(&str_func, "\treturn Text$concat(Text$from_str(use_color ? \"\\x1b[0;1m%s\\x1b[m(\" : \"%s(\")", name, name); for (arg_ast_t *field = def->fields; field; field = field->next) { type_t *field_type = get_arg_ast_type(env, field); CORD field_str = expr_as_text(env, CORD_cat("obj->$", field->name), field_type, "use_color"); - CORD_appendf(&str_func, ", \"%s=\", %r", field->name, field_str); - if (field->next) CORD_appendf(&str_func, ", \", \""); + CORD_appendf(&str_func, ", Text$from_str(\"%s=\"), %r", field->name, field_str); + if (field->next) CORD_appendf(&str_func, ", Text$from_str(\", \")"); } - CORD_appendf(&str_func, ", \")\");\n}\n"); + CORD_appendf(&str_func, ", Text$from_str(\")\"));\n}\n"); } return str_func; } @@ -53,9 +53,6 @@ static CORD compile_compare_method(env_t *env, ast_t *ast) case BoolType: case IntType: case NumType: case PointerType: case FunctionType: cmp_func = CORD_all(cmp_func, "diff = (x->$", field->name, " > y->$", field->name, ") - (x->$", field->name, " < y->$", field->name, ");"); break; - case TextType: - cmp_func = CORD_all(cmp_func, "diff = CORD_cmp(x->$", field->name, ", y->$", field->name, ");"); - break; default: cmp_func = CORD_all(cmp_func, "diff = generic_compare(&x->$", field->name, ", &y->$", field->name, ", ", compile_type_info(env, field_type), ");\n"); @@ -86,9 +83,6 @@ static CORD compile_equals_method(env_t *env, ast_t *ast) case BoolType: case IntType: case NumType: case PointerType: case FunctionType: condition = CORD_all(condition, "(x->$", field->name, " == y->$", field->name, ")"); break; - case TextType: - condition = CORD_all(condition, "(CORD_cmp(x->$", field->name, ", y->$", field->name, ") == 0)"); - break; default: condition = CORD_all(condition, "generic_equal(&x->$", field->name, ", &y->$", field->name, ", ", compile_type_info(env, field_type), ")"); @@ -166,7 +160,7 @@ void compile_struct_def(env_t *env, ast_t *ast) } else { // If there are no fields, we can use an EmptyStruct typeinfo, which generates less code: CORD typeinfo = CORD_asprintf("public const TypeInfo %r = {%zu, %zu, {.tag=EmptyStruct, .EmptyStruct.name=%r}};\n", - full_name, type_size(t), type_align(t), Text$quoted(Text$from_str(def->name), false)); + full_name, type_size(t), type_align(t), CORD_quoted(def->name)); env->code->typeinfos = CORD_all(env->code->typeinfos, typeinfo); }