Merge pull request #129 from birth-software/polymorphic-function

Basic polymorphic function
This commit is contained in:
David 2024-04-08 19:46:41 -06:00 committed by GitHub
commit 5398895e5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 51 additions and 4 deletions

View File

@ -6625,6 +6625,8 @@ pub const Builder = struct {
_ = &scope;
const type_index = global_scope.type;
assert(type_index != .null);
const ty = unit.types.get(type_index);
assert(ty.* != .polymorphic);
return type_index;
}
},
@ -10920,6 +10922,20 @@ pub const Builder = struct {
else => |t| @panic(@tagName(t)),
},
},
.@"struct" => |struct_index| switch (unit.structs.get(struct_index).kind) {
.@"struct" => |*struct_type| if (struct_type.options.sliceable) |sliceable| b: {
const field_index = struct_type.fields.slice()[sliceable.pointer];
const field = unit.struct_fields.get(field_index);
const child_pointer_type = field.type;
const pointer_type = unit.types.get(field.type).pointer;
const child_base_type = pointer_type.type;
const v = try builder.build_slice_indexed_access(unit, context, array_like_expression, pointer.type, child_pointer_type, child_base_type, pointer_type.mutability, sliceable, index);
break :b v;
} else {
unreachable;
},
else => |t| @panic(@tagName(t)),
},
else => |t| @panic(@tagName(t)),
},
},
@ -12056,6 +12072,7 @@ pub const Builder = struct {
}
} else {
const look_in_parent_scopes = false;
if (struct_type.scope.scope.lookupDeclaration(right_identifier_hash, look_in_parent_scopes)) |lookup| {
const right_symbol = try builder.referenceGlobalDeclaration(unit, context, lookup.scope, lookup.declaration, .{}, &.{});
switch (right_symbol.initial_value) {
@ -12116,6 +12133,8 @@ pub const Builder = struct {
else => |t| @panic(@tagName(t)),
}
} else {
const id = unit.getIdentifier( struct_type.scope.scope.declarations.key_pointer[0]);
_ = id; // autofix
unreachable;
}
}

View File

@ -1,5 +1,15 @@
const std = #import("std");
const assert = std.assert;
const PinnedArray = struct(.{ .sliceable = true }, $T) {
pointer: [&]T = #cast(0),
length: u32 = 0,
capacity: u32 = 0,
const append_with_capacity = fn (pinned_array: &Self, item: T) void {
const index = pinned_array.length;
assert(index < pinned_array.capacity);
pinned_array.length += 1;
pinned_array[index] = item;
}
};

View File

@ -9,8 +9,8 @@ const main = fn () *!void {
.length = 0,
.capacity = 1,
};
//const n = 5;
//pinned_array.append_with_capacity(n);
//try expect(pinned_array.length == 1);
//try expect(pinned_array[0] == n);
const n = 5;
pinned_array.append_with_capacity(n);
try expect(pinned_array.length == 1);
try expect(pinned_array[0] == n);
}

View File

@ -27,4 +27,5 @@ const main = fn () *!void {
try expect(s.length == 1);
try expect(foo[0] == 5);
try expect(s[0] == 5);
}

View File

@ -0,0 +1,17 @@
const std = #import("std");
const assert = std.assert;
const expect = std.testing.expect;
const Foo = struct(.{ .sliceable = true }) {
pointer: [&]u32,
length: u32,
};
const main = fn () *!void {
var foo = [1]u32{123};
var f = Foo{
.pointer = foo.&,
.length = foo.length,
};
try expect(f[0] == foo[0]);
}