Sorry, I thought it was something obvious that I just don't get.
I try to give more info and code:
It does not crash or give segmentation faults anymore, now it only has problems with the hashmap. It seems it knows more than me. It should be a hash map with Entity Types but it tells me its values are InterpretedFuntions, which is weird as those two Types don't come near each other anywhere in my code.
This is the struct that holds the Hashmap:
const Status = struct {
allocator: std.mem.Allocator,
imports: ArrayList([]const u8),
function_stack: ArrayList([]const u8),
function_map: StringHashMap(InterpretedFunction),
current_function: *InterpretedFunction,
current_line: usize = 0,
var_count: usize = 0,
entity_map: StringHashMap(Entity),
fn init(allocator: std.mem.Allocator) !Status {
return Status {
.allocator = allocator,
.imports = ArrayList([]const u8).init(allocator),
.function_stack = ArrayList([]const u8).init(allocator),
.function_map = StringHashMap(InterpretedFunction).init(allocator),
.current_function = undefined,
.entity_map = StringHashMap(Entity).init(allocator),
};
}
fn deinit(self: *Status) void {
self.imports.deinit();
self.function_stack.deinit();
self.function_map.deinit();
self.entity_map.deinit();
}
fn spawnEntity(self: *Status, entity: Entity) !void {
if (self.entity_map.contains(entity.uuid)) return DatapackErrors.EntityAlreadyExists;
std.debug.print("Spawning: {s}\n", .{entity.uuid});
try self.entity_map.put(entity.uuid, entity);
}
}
This is the Entity, which the Hashmap should use as values
pub const EntityType = enum {
TextDisplay,
fn idToEnum(id: []const u8) !EntityType {
return if (std.mem.eql(u8, id, "text_display")) .TextDisplay
else DatapackErrors.UnknownEntityType;
}
};
pub const Entity = struct {
allocator: std.mem.Allocator,
entity_type: EntityType,
uuid: []const u8,
nbt: union(EntityType) {
TextDisplay: struct {
CustomName: []const u8,
text: []const u8,
}
},
pub fn init(allocator: std.mem.Allocator, entity: []const u8, nbt: []const u8) !Entity {
const entity_type = try EntityType.idToEnum(entity);
return Entity {
.allocator = allocator,
.entity_type = entity_type,
.uuid = uuid_blk: {
const uuid_str = getDataValue(nbt, "UUID:[I;", .array);
break :uuid_blk try sanatizeUuid(allocator, uuid_str);
},
.nbt = switch (entity_type) {
.TextDisplay => .{
.TextDisplay = .{
.CustomName = getDataValue(nbt, "CustomName:\"", .string),
.text = getDataValue(nbt, "text:\"", .string)
}
}
}
};
}
pub fn deinit(self: *Entity) void {
self.allocator.free(self.uuid);
}
};
The InterpretedFunction struct:
const InterpretedFunction = struct {
allocator: std.mem.Allocator,
name: []const u8,
returns: bool = false,
top_vars: ArrayList([]const u8),
code: ArrayList(u8),
fn init(allocator: std.mem.Allocator, name: []const u8) InterpretedFunction {
return InterpretedFunction {
.allocator = allocator,
.name = name,
.top_vars = ArrayList([]const u8).init(allocator),
.code = ArrayList(u8).init(allocator)
};
}
fn deinit(self: *InterpretedFunction) void {
self.allocator.free(self.name);
self.top_vars.deinit();
self.code.deinit();
}
};
.