This commit is contained in:
nub31
2025-07-06 00:29:55 +02:00
parent 0afb18e4a6
commit f15073cac4
2 changed files with 80 additions and 70 deletions

View File

@@ -53,17 +53,17 @@ public static class QBEGenerator
_implFuncNameIndex = 0; _implFuncNameIndex = 0;
_codeIsReachable = true; _codeIsReachable = true;
// foreach (var structDef in _definitionTable.GetStructs()) foreach (var structDef in _definitionTable.GetStructs())
// { {
// EmitStructDefinition(structDef); EmitStructDefinition(structDef);
// _writer.NewLine(); _writer.NewLine();
// } }
//
// foreach (var trait in _definitionTable.GetTraits()) foreach (var trait in _definitionTable.GetTraits())
// { {
// EmitTraitVTable(trait); EmitTraitVTable(trait);
// _writer.NewLine(); _writer.NewLine();
// } }
foreach (var funcDef in _syntaxTree.TopLevelNodes.OfType<BoundLocalFuncNode>()) foreach (var funcDef in _syntaxTree.TopLevelNodes.OfType<BoundLocalFuncNode>())
{ {
@@ -574,65 +574,65 @@ public static class QBEGenerator
_writer.EndFunction(); _writer.EndFunction();
} }
// private static void EmitStructDefinition(BoundStructNode structDef) private static void EmitStructDefinition(BoundStructNode structDef)
// { {
// var structType = new BoundNubCustomType(structDef.Namespace, structDef.Name); _writer.WriteLine($"type {StructTypeName(new BoundNubStructType(structDef.Namespace, structDef.Name))} = {{ ");
// _writer.WriteLine($"type {CustomTypeName(structType)} = {{ ");
// var types = new Dictionary<string, string>();
// var types = new Dictionary<string, string>();
// foreach (var field in structDef.Fields)
// foreach (var field in structDef.Fields) {
// { types.Add(field.Name, StructDefQBEType(field));
// types.Add(field.Name, StructDefQBEType(field)); }
// }
// var longest = types.Values.Max(x => x.Length);
// var longest = types.Values.Max(x => x.Length); foreach (var (name, type) in types)
// foreach (var (name, type) in types) {
// { var padding = longest - type.Length;
// var padding = longest - type.Length; _writer.Indented($"{type},{new string(' ', padding)} # {name}");
// _writer.Indented($"{type},{new string(' ', padding)} # {name}"); }
// }
// _writer.WriteLine("}");
// _writer.WriteLine("}"); return;
// return;
// string StructDefQBEType(BoundStructFieldNode field)
// string StructDefQBEType(BoundStructFieldNode field) {
// { return field.Type switch
// return field.Type switch {
// { BoundNubStructType structType => StructTypeName(structType),
// BoundNubCustomType customType => CustomTypeName(customType), BoundNubTraitType traitType => TraitTypeName(traitType),
// BoundNubComplexType => "l", BoundNubComplexType => "l",
// BoundNubSimpleType simpleType => simpleType switch BoundNubSimpleType simpleType => simpleType switch
// { {
// BoundNubPointerType or BoundNubFuncType => "l", BoundNubPointerType or BoundNubFuncType => "l",
// BoundNubPrimitiveType primitiveType => primitiveType.Kind switch BoundNubPrimitiveType primitiveType => primitiveType.Kind switch
// { {
// PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "l", PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "l",
// PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "w", PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "w",
// PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "h", PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "h",
// PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "b", PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "b",
// PrimitiveTypeKind.F64 => "d", PrimitiveTypeKind.F64 => "d",
// PrimitiveTypeKind.F32 => "s", PrimitiveTypeKind.F32 => "s",
// _ => throw new ArgumentOutOfRangeException() _ => throw new ArgumentOutOfRangeException()
// }, },
// _ => throw new NotSupportedException($"'{field.Type}' type cannot be used in structs") _ => throw new NotSupportedException($"'{field.Type}' type cannot be used in structs")
// }, },
// _ => throw new UnreachableException() _ => throw new UnreachableException()
// }; };
// } }
// } }
//
// private static void EmitTraitVTable(BoundTraitNode traitDef) private static void EmitTraitVTable(BoundTraitNode traitDef)
// { {
// _writer.WriteLine($"type {CustomTypeName(new BoundNubCustomType(traitDef.Namespace, traitDef.Name))} = {{"); _writer.WriteLine($"type {TraitTypeName(new BoundNubTraitType(traitDef.Namespace, traitDef.Name))} = {{");
//
// foreach (var func in traitDef.Functions) foreach (var func in traitDef.Functions)
// { {
// _writer.Indented($"l, # func {func.Name}({string.Join(", ", func.Parameters.Select(x => $"{x.Name}: {x.Type}"))}): {func.ReturnType}"); _writer.Indented($"l, # func {func.Name}({string.Join(", ", func.Parameters.Select(x => $"{x.Name}: {x.Type}"))}): {func.ReturnType}");
// } }
//
// _writer.WriteLine("}"); _writer.WriteLine("}");
// } }
private static void EmitStatement(BoundStatementNode statement) private static void EmitStatement(BoundStatementNode statement)
{ {

View File

@@ -121,4 +121,14 @@ public sealed class BoundDefinitionTable
{ {
return trait.Functions.First(x => x.Name == name); return trait.Functions.First(x => x.Name == name);
} }
public IEnumerable<BoundStructNode> GetStructs()
{
return _topLevelNodes.OfType<BoundStructNode>();
}
public IEnumerable<BoundTraitNode> GetTraits()
{
return _topLevelNodes.OfType<BoundTraitNode>();
}
} }