From 25519d243485ab1906304adc99d400684b04ef5b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 13 Aug 2024 02:28:14 -0400 Subject: Fixing up more stuff --- structs.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'structs.c') diff --git a/structs.c b/structs.c index fba22c04..bec9d42f 100644 --- a/structs.c +++ b/structs.c @@ -47,7 +47,13 @@ static CORD compile_compare_method(env_t *env, ast_t *ast) for (arg_ast_t *field = def->fields; field; field = field->next) { type_t *field_type = get_arg_ast_type(env, field); switch (field_type->tag) { - case BoolType: case IntType: case NumType: case PointerType: case FunctionType: + case IntType: + if (Match(field_type, IntType)->bits == 0) + cmp_func = CORD_all(cmp_func, "diff = Int$compare_value(x->$", field->name, ", y->$", field->name, ");"); + else + cmp_func = CORD_all(cmp_func, "diff = (x->$", field->name, " > y->$", field->name, ") - (x->$", field->name, " < y->$", field->name, ");"); + break; + case BoolType: 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: @@ -77,7 +83,13 @@ static CORD compile_equals_method(env_t *env, ast_t *ast) condition = CORD_all(condition, " && "); type_t *field_type = get_arg_ast_type(env, field); switch (field_type->tag) { - case BoolType: case IntType: case NumType: case PointerType: case FunctionType: + case IntType: + if (Match(field_type, IntType)->bits == 0) + condition = CORD_all(condition, "Int$equal_value(x->$", field->name, ", y->$", field->name, ")"); + else + condition = CORD_all(condition, "(x->$", field->name, " == y->$", field->name, ")"); + break; + case BoolType: case NumType: case PointerType: case FunctionType: condition = CORD_all(condition, "(x->$", field->name, " == y->$", field->name, ")"); break; case TextType: @@ -132,13 +144,11 @@ void compile_struct_def(env_t *env, ast_t *ast) if (struct_->fields && !struct_->fields->next) { // Single member, can just use its methods type_t *member_t = struct_->fields->type; switch (member_t->tag) { - case TextType: - typeinfo = CORD_all(typeinfo, ".hash=(void*)", type_to_cord(member_t), "$hash", ", "); - // fallthrough - case IntType: case NumType: - typeinfo = CORD_all(typeinfo, ".compare=(void*)", type_to_cord(member_t), "$compare, " + case TextType: case IntType: case NumType: + typeinfo = CORD_all(typeinfo, ".hash=(void*)", type_to_cord(member_t), "$hash", ", ", + ".compare=(void*)", type_to_cord(member_t), "$compare, " ".equal=(void*)", type_to_cord(member_t), "$equal, "); - // fallthrough + goto got_methods; case BoolType: goto got_methods; default: break; } -- cgit v1.2.3