commit cee4722d861261b9458b0a2f99192bba00eb9d62
parent 21b8de43955c27fae984aa005d70469dd291cd30
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 4 May 2021 13:30:46 -0400
hare::types: sort struct fields
To match harec, which tbh is kind of stupid and might be worth
revisiting.
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/hare/types/+test.ha b/hare/types/+test.ha
@@ -94,7 +94,14 @@ fn parse_type(in: str) ast::_type = {
assert(z.offs == 16);
// Sort order
- // TODO
+ let atype = parse_type("struct { z: u8, y: u8, x: u8, q: u8 }");
+ defer ast::type_free(atype);
+ let htype = lookup(st, &atype)!;
+ let stype = htype._type as _struct;
+ assert(stype.fields[0].name == "q");
+ assert(stype.fields[1].name == "x");
+ assert(stype.fields[2].name == "y");
+ assert(stype.fields[3].name == "z");
// Embedded struct
// TODO
diff --git a/hare/types/store.ha b/hare/types/store.ha
@@ -1,5 +1,7 @@
+use ascii;
use errors;
use hare::ast;
+use sort;
export def BUCKETS: size = 65535;
@@ -290,7 +292,7 @@ fn struct_from_ast(
let fields: []struct_field = [];
let offs = 0z;
_struct_from_ast(store, membs, is_union, &fields, &offs)?;
- // TODO: Sort fields
+ sort::sort(fields, size(struct_field), &field_cmp);
return _struct {
kind = if (is_union) struct_union::UNION else struct_union::STRUCT,
fields = fields,
@@ -298,6 +300,11 @@ fn struct_from_ast(
};
};
+fn field_cmp(a: const *void, b: const *void) int = {
+ let a = a: const *struct_field, b = b: *const struct_field;
+ return ascii::strcmp(a.name, b.name) as int;
+};
+
fn type_finish(t: *_type) void = {
match (t._type) {
a: alias => ast::ident_free(a.id),