aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-13 02:28:14 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-13 02:28:14 -0400
commit25519d243485ab1906304adc99d400684b04ef5b (patch)
treeef041872fd695d4b6d218791fbab3b90575e3532
parent80af2ea5476e6bc93fe3d7d2c297c021aa29b497 (diff)
Fixing up more stuff
-rw-r--r--builtins/integers.c2
-rw-r--r--builtins/integers.h1
-rw-r--r--structs.c26
3 files changed, 20 insertions, 9 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index 08da553e..1faae4c2 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -70,7 +70,7 @@ public bool Int$equal_value(const Int_t x, const Int_t y) {
return x.small == y.small || (__builtin_expect(((x.small & y.small) & 1) == 0, 0) && mpz_cmp(*x.big, *y.big) == 0);
}
-public bool Int$hash(const Int_t *x, const TypeInfo *type) {
+public uint32_t Int$hash(const Int_t *x, const TypeInfo *type) {
(void)type;
uint32_t hash;
if (__builtin_expect(x->small & 1, 1)) {
diff --git a/builtins/integers.h b/builtins/integers.h
index e41fd061..2fc2688e 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -46,6 +46,7 @@ DEFINE_INT_TYPE(int8_t, Int8);
#define Int8$abs(...) I8(abs(__VA_ARGS__))
CORD Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type);
+uint32_t Int$hash(const Int_t *x, const TypeInfo *type);
int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type);
int32_t Int$compare_value(const Int_t x, const Int_t y);
bool Int$equal(const Int_t *x, const Int_t *y, const TypeInfo *type);
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;
}