add type analysis of enums
This commit is contained in:
parent
0b0a8896bb
commit
5188dac3c0
|
@ -280,6 +280,9 @@ pub const Struct = struct {
|
||||||
|
|
||||||
pub const Enum = struct {
|
pub const Enum = struct {
|
||||||
name: Token,
|
name: Token,
|
||||||
|
|
||||||
|
// TODO allow custom values for the enum fields
|
||||||
|
// just like c enums can skip values
|
||||||
fields: TokenList,
|
fields: TokenList,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -351,8 +351,7 @@ fn prettyType(typ: SymbolUnderlyingType) []const u8 {
|
||||||
|
|
||||||
.OpaqueType => |ident| retWithName("opaque", ident),
|
.OpaqueType => |ident| retWithName("opaque", ident),
|
||||||
.Struct => |ident| retWithName("struct", ident),
|
.Struct => |ident| retWithName("struct", ident),
|
||||||
|
.Enum => |ident| retWithName("enum", ident),
|
||||||
else => unreachable,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -395,7 +394,21 @@ pub fn printContext(ctx: CompilationContext) void {
|
||||||
kv.key,
|
kv.key,
|
||||||
kv.value,
|
kv.value,
|
||||||
),
|
),
|
||||||
else => unreachable,
|
|
||||||
|
.Enum => |identmap| {
|
||||||
|
std.debug.warn("enum {}:", kv.key);
|
||||||
|
|
||||||
|
var mapit = identmap.iterator();
|
||||||
|
|
||||||
|
while (mapit.next()) |field_kv| {
|
||||||
|
std.debug.warn("\t{} => {}\n", field_kv.key, field_kv.value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
else => {
|
||||||
|
std.debug.warn("TODO handle print of {}\n", kv.value);
|
||||||
|
unreachable;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -270,6 +270,9 @@ pub const Codegen = struct {
|
||||||
std.debug.warn("cgen: generated function '{}'\n", name);
|
std.debug.warn("cgen: generated function '{}'\n", name);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// NOTE: enums don't have specific llvm ir code generated for them
|
||||||
|
.Enum => {},
|
||||||
|
|
||||||
else => {
|
else => {
|
||||||
std.debug.warn("TODO handle node type {}\n", @tagName(node.*));
|
std.debug.warn("TODO handle node type {}\n", @tagName(node.*));
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -59,8 +59,8 @@ pub const FunctionSymbol = struct {
|
||||||
// structs are hashmaps pointing to SymbolUnderlyingType
|
// structs are hashmaps pointing to SymbolUnderlyingType
|
||||||
pub const UnderlyingTypeMap = std.hash_map.StringHashMap(SymbolUnderlyingType);
|
pub const UnderlyingTypeMap = std.hash_map.StringHashMap(SymbolUnderlyingType);
|
||||||
|
|
||||||
// enums have lists of identifiers
|
// enums have maps from fields to u32s
|
||||||
pub const IdentifierList = std.ArrayList([]const u8);
|
pub const IdentifierMap = std.StringHashMap(u32);
|
||||||
|
|
||||||
// TODO const
|
// TODO const
|
||||||
pub const SymbolType = enum {
|
pub const SymbolType = enum {
|
||||||
|
@ -73,7 +73,7 @@ pub const SymbolType = enum {
|
||||||
pub const SymbolData = union(SymbolType) {
|
pub const SymbolData = union(SymbolType) {
|
||||||
Function: FunctionSymbol,
|
Function: FunctionSymbol,
|
||||||
Struct: UnderlyingTypeMap,
|
Struct: UnderlyingTypeMap,
|
||||||
Enum: IdentifierList,
|
Enum: IdentifierMap,
|
||||||
|
|
||||||
// variables (parameters and variables), for the type system
|
// variables (parameters and variables), for the type system
|
||||||
// only have types
|
// only have types
|
||||||
|
@ -161,4 +161,18 @@ pub const CompilationContext = struct {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insertEnum(self: *@This(), enu: ast.Enum) !void {
|
||||||
|
var ident_map = IdentifierMap.init(self.allocator);
|
||||||
|
errdefer ident_map.deinit();
|
||||||
|
|
||||||
|
// TODO change this when enums get support for custom values
|
||||||
|
for (enu.fields.toSlice()) |token, idx| {
|
||||||
|
_ = try ident_map.put(token.lexeme, @intCast(u32, idx));
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = try self.symbol_table.put(enu.name.lexeme, SymbolData{
|
||||||
|
.Enum = ident_map,
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -143,8 +143,10 @@ pub const TypeSolver = struct {
|
||||||
try ctx.insertStruct(struc, types);
|
try ctx.insertStruct(struc, types);
|
||||||
},
|
},
|
||||||
|
|
||||||
// TODO enums are u32
|
// TODO change enums to u32
|
||||||
//.Enum => {},
|
.Enum => |enu| {
|
||||||
|
try ctx.insertEnum(enu);
|
||||||
|
},
|
||||||
|
|
||||||
// TODO infer type of expr in const
|
// TODO infer type of expr in const
|
||||||
//.ConstDecl => {},
|
//.ConstDecl => {},
|
||||||
|
|
Loading…
Reference in New Issue