...
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
namespace main
|
namespace main
|
||||||
|
|
||||||
struct Name {
|
struct Name
|
||||||
|
{
|
||||||
first: cstring
|
first: cstring
|
||||||
last: cstring
|
last: cstring
|
||||||
}
|
}
|
||||||
@@ -11,6 +12,18 @@ struct Human
|
|||||||
age: i64
|
age: i64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Printable
|
||||||
|
{
|
||||||
|
func print()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Human : Printable
|
||||||
|
{
|
||||||
|
func print() {
|
||||||
|
c::puts(this.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export func main(args: []cstring): i64
|
export func main(args: []cstring): i64
|
||||||
{
|
{
|
||||||
let x = alloc Human
|
let x = alloc Human
|
||||||
@@ -22,10 +35,7 @@ export func main(args: []cstring): i64
|
|||||||
age = 23
|
age = 23
|
||||||
}
|
}
|
||||||
|
|
||||||
print_name(x&)
|
x.print()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func print_name(human: ^Human) {
|
|
||||||
c::puts(human^.name.last)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -121,7 +121,8 @@ foreach (var file in options.Files)
|
|||||||
var outFileName = $"{HexString.CreateUnique(8)}_{Path.GetFileNameWithoutExtension(file)}";
|
var outFileName = $"{HexString.CreateUnique(8)}_{Path.GetFileNameWithoutExtension(file)}";
|
||||||
|
|
||||||
var ssa = QBEGenerator.Emit(boundSyntaxTrees[file], boundDefinitionTable, file);
|
var ssa = QBEGenerator.Emit(boundSyntaxTrees[file], boundDefinitionTable, file);
|
||||||
File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.ssa"), ssa);
|
// File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.ssa"), ssa);
|
||||||
|
File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{Path.GetFileNameWithoutExtension(file)}.ssa"), ssa);
|
||||||
|
|
||||||
var asm = await QBE.Invoke(ssa);
|
var asm = await QBE.Invoke(ssa);
|
||||||
if (asm == null)
|
if (asm == null)
|
||||||
|
|||||||
@@ -54,16 +54,25 @@ public static class QBEGenerator
|
|||||||
foreach (var structDef in _definitionTable.GetStructs())
|
foreach (var structDef in _definitionTable.GetStructs())
|
||||||
{
|
{
|
||||||
EmitStructDefinition(structDef);
|
EmitStructDefinition(structDef);
|
||||||
|
_writer.NewLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var @interface in _definitionTable.GetInterfaces())
|
||||||
|
{
|
||||||
|
EmitInterfaceVTableType(@interface);
|
||||||
|
_writer.NewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var funcDef in _syntaxTree.Definitions.OfType<BoundLocalFuncDefinitionNode>())
|
foreach (var funcDef in _syntaxTree.Definitions.OfType<BoundLocalFuncDefinitionNode>())
|
||||||
{
|
{
|
||||||
EmitFuncDefinition(funcDef, FuncName(funcDef), funcDef.Parameters, funcDef.ReturnType, funcDef.Body, funcDef.Exported);
|
EmitFuncDefinition(funcDef, FuncName(funcDef), funcDef.Parameters, funcDef.ReturnType, funcDef.Body, funcDef.Exported);
|
||||||
|
_writer.NewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (_anonymousFunctions.TryDequeue(out var anon))
|
while (_anonymousFunctions.TryDequeue(out var anon))
|
||||||
{
|
{
|
||||||
EmitFuncDefinition(anon.Func, anon.Name, anon.Func.Parameters, anon.Func.ReturnType, anon.Func.Body, false);
|
EmitFuncDefinition(anon.Func, anon.Name, anon.Func.Parameters, anon.Func.ReturnType, anon.Func.Body, false);
|
||||||
|
_writer.NewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var cStringLiteral in _cStringLiterals)
|
foreach (var cStringLiteral in _cStringLiterals)
|
||||||
@@ -117,6 +126,11 @@ public static class QBEGenerator
|
|||||||
return $":{structDef.Namespace}_{structDef.Name}";
|
return $":{structDef.Namespace}_{structDef.Name}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string InterfaceVTableTypeName(BoundInterfaceDefinitionNode interfaceDef)
|
||||||
|
{
|
||||||
|
return $":{interfaceDef.Namespace}_{interfaceDef.Name}";
|
||||||
|
}
|
||||||
|
|
||||||
private static void EmitStore(NubType type, string value, string destination)
|
private static void EmitStore(NubType type, string value, string destination)
|
||||||
{
|
{
|
||||||
var store = type switch
|
var store = type switch
|
||||||
@@ -135,7 +149,7 @@ public static class QBEGenerator
|
|||||||
_ => throw new NotSupportedException($"'{type}' type cannot be used in store instructions")
|
_ => throw new NotSupportedException($"'{type}' type cannot be used in store instructions")
|
||||||
};
|
};
|
||||||
|
|
||||||
_writer.Code($"{store} {value}, {destination}");
|
_writer.Indented($"{store} {value}, {destination}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Val EmitLoad(NubType type, string from)
|
private static Val EmitLoad(NubType type, string from)
|
||||||
@@ -159,38 +173,38 @@ public static class QBEGenerator
|
|||||||
_ => throw new NotSupportedException($"'{type}' type cannot be used in load instructions")
|
_ => throw new NotSupportedException($"'{type}' type cannot be used in load instructions")
|
||||||
};
|
};
|
||||||
|
|
||||||
_writer.Code($"{into} {QBEAssign(type)} {load} {from}");
|
_writer.Indented($"{into} {QBEAssign(type)} {load} {from}");
|
||||||
|
|
||||||
return new Val(into, type, ValKind.Direct);
|
return new Val(into, type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EmitMemcpy(string source, string destination, string length)
|
private static void EmitMemcpy(string source, string destination, string length)
|
||||||
{
|
{
|
||||||
_writer.Code($"call $nub_memcpy(l {source}, l {destination}, l {length})");
|
_writer.Indented($"call $nub_memcpy(l {source}, l {destination}, l {length})");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string EmitArraySizeInBytes(NubArrayType type, string array)
|
private static string EmitArraySizeInBytes(NubArrayType type, string array)
|
||||||
{
|
{
|
||||||
var size = VarName();
|
var size = VarName();
|
||||||
_writer.Code($"{size} =l loadl {array}");
|
_writer.Indented($"{size} =l loadl {array}");
|
||||||
_writer.Code($"{size} =l mul {size}, {SizeOf(type.ElementType)}");
|
_writer.Indented($"{size} =l mul {size}, {SizeOf(type.ElementType)}");
|
||||||
_writer.Code($"{size} =l add {size}, 8");
|
_writer.Indented($"{size} =l add {size}, 8");
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string EmitCStringSizeInBytes(string cstring)
|
private static string EmitCStringSizeInBytes(string cstring)
|
||||||
{
|
{
|
||||||
var size = VarName();
|
var size = VarName();
|
||||||
_writer.Code($"{size} =l call $nub_cstring_length(l {cstring})");
|
_writer.Indented($"{size} =l call $nub_cstring_length(l {cstring})");
|
||||||
_writer.Code($"{size} =l add {size}, 1");
|
_writer.Indented($"{size} =l add {size}, 1");
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string EmitStringSizeInBytes(string nubstring)
|
private static string EmitStringSizeInBytes(string nubstring)
|
||||||
{
|
{
|
||||||
var size = VarName();
|
var size = VarName();
|
||||||
_writer.Code($"{size} =l loadl {nubstring}");
|
_writer.Indented($"{size} =l loadl {nubstring}");
|
||||||
_writer.Code($"{size} =l add {size}, 8");
|
_writer.Indented($"{size} =l add {size}, 8");
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +265,7 @@ public static class QBEGenerator
|
|||||||
case NubStringType:
|
case NubStringType:
|
||||||
{
|
{
|
||||||
var buffer = VarName();
|
var buffer = VarName();
|
||||||
_writer.Code($"{buffer} =l alloc8 {size}");
|
_writer.Indented($"{buffer} =l alloc8 {size}");
|
||||||
EmitMemcpy(value, buffer, size);
|
EmitMemcpy(value, buffer, size);
|
||||||
EmitStore(source.Type, buffer, destinationPointer);
|
EmitStore(source.Type, buffer, destinationPointer);
|
||||||
return;
|
return;
|
||||||
@@ -321,7 +335,7 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException(nameof(complexType))
|
_ => throw new ArgumentOutOfRangeException(nameof(complexType))
|
||||||
};
|
};
|
||||||
|
|
||||||
_writer.Code($"{destination} =l alloc8 {size}");
|
_writer.Indented($"{destination} =l alloc8 {size}");
|
||||||
EmitMemcpy(value, destination, size);
|
EmitMemcpy(value, destination, size);
|
||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
@@ -534,19 +548,22 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
if (returnType is NubVoidType)
|
if (returnType is NubVoidType)
|
||||||
{
|
{
|
||||||
_writer.Code("ret");
|
_writer.Indented("ret");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_writer.EndFunction();
|
_writer.EndFunction();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EmitStructDefinition(BoundStructDefinitionNode structDefinition)
|
private static void EmitStructDefinition(BoundStructDefinitionNode structDef)
|
||||||
{
|
{
|
||||||
_writer.Write($"type {StructName(structDefinition)} = {{ ");
|
_writer.WriteLine($"type {StructName(structDef)} = {{ ");
|
||||||
foreach (var structDefinitionField in structDefinition.Fields)
|
|
||||||
|
var types = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
foreach (var field in structDef.Fields)
|
||||||
{
|
{
|
||||||
var qbeType = structDefinitionField.Type switch
|
var qbeType = field.Type switch
|
||||||
{
|
{
|
||||||
NubPrimitiveType primitiveType => primitiveType.Kind switch
|
NubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||||
{
|
{
|
||||||
@@ -560,9 +577,29 @@ public static class QBEGenerator
|
|||||||
},
|
},
|
||||||
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
||||||
NubArrayType or NubPointerType or NubFuncType or NubCStringType or NubStringType => "l",
|
NubArrayType or NubPointerType or NubFuncType or NubCStringType or NubStringType => "l",
|
||||||
_ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs")
|
_ => throw new NotSupportedException($"'{field.Type}' type cannot be used in structs")
|
||||||
};
|
};
|
||||||
_writer.Write(qbeType + ", ");
|
|
||||||
|
types.Add(field.Name, qbeType);
|
||||||
|
}
|
||||||
|
|
||||||
|
var longest = types.Values.Max(x => x.Length);
|
||||||
|
foreach (var (name, type) in types)
|
||||||
|
{
|
||||||
|
var padding = longest - type.Length;
|
||||||
|
_writer.Indented($"{type},{new string(' ', padding)} # {name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
_writer.WriteLine("}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EmitInterfaceVTableType(BoundInterfaceDefinitionNode interfaceDef)
|
||||||
|
{
|
||||||
|
_writer.WriteLine($"type {InterfaceVTableTypeName(interfaceDef)} = {{");
|
||||||
|
|
||||||
|
foreach (var func in interfaceDef.Functions)
|
||||||
|
{
|
||||||
|
_writer.Indented($"l, # func {func.Name}({string.Join(", ", func.Parameters.Select(x => $"{x.Name}: {x.Type}"))}): {func.ReturnType}");
|
||||||
}
|
}
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
_writer.WriteLine("}");
|
||||||
@@ -637,13 +674,13 @@ public static class QBEGenerator
|
|||||||
|
|
||||||
private static void EmitBreak(BoundBreakNode @break)
|
private static void EmitBreak(BoundBreakNode @break)
|
||||||
{
|
{
|
||||||
_writer.Code($"jmp {_breakLabels.Peek()}");
|
_writer.Indented($"jmp {_breakLabels.Peek()}");
|
||||||
_codeIsReachable = false;
|
_codeIsReachable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EmitContinue(BoundContinueNode @continue)
|
private static void EmitContinue(BoundContinueNode @continue)
|
||||||
{
|
{
|
||||||
_writer.Code($"jmp {_continueLabels.Peek()}");
|
_writer.Indented($"jmp {_continueLabels.Peek()}");
|
||||||
_codeIsReachable = false;
|
_codeIsReachable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -654,10 +691,10 @@ public static class QBEGenerator
|
|||||||
var endLabel = LabelName();
|
var endLabel = LabelName();
|
||||||
|
|
||||||
var result = EmitUnwrap(EmitExpression(ifStatement.Condition));
|
var result = EmitUnwrap(EmitExpression(ifStatement.Condition));
|
||||||
_writer.Code($"jnz {result}, {trueLabel}, {falseLabel}");
|
_writer.Indented($"jnz {result}, {trueLabel}, {falseLabel}");
|
||||||
_writer.WriteLine(trueLabel);
|
_writer.WriteLine(trueLabel);
|
||||||
EmitBlock(ifStatement.Body);
|
EmitBlock(ifStatement.Body);
|
||||||
_writer.Code($"jmp {endLabel}");
|
_writer.Indented($"jmp {endLabel}");
|
||||||
_writer.WriteLine(falseLabel);
|
_writer.WriteLine(falseLabel);
|
||||||
if (ifStatement.Else.HasValue)
|
if (ifStatement.Else.HasValue)
|
||||||
{
|
{
|
||||||
@@ -676,18 +713,18 @@ public static class QBEGenerator
|
|||||||
if (@return.Value.HasValue)
|
if (@return.Value.HasValue)
|
||||||
{
|
{
|
||||||
var result = EmitUnwrap(EmitExpression(@return.Value.Value));
|
var result = EmitUnwrap(EmitExpression(@return.Value.Value));
|
||||||
_writer.Code($"ret {result}");
|
_writer.Indented($"ret {result}");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_writer.Code("ret");
|
_writer.Indented("ret");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EmitVariableDeclaration(BoundVariableDeclarationNode variableDeclaration)
|
private static void EmitVariableDeclaration(BoundVariableDeclarationNode variableDeclaration)
|
||||||
{
|
{
|
||||||
var variable = VarName();
|
var variable = VarName();
|
||||||
_writer.Code($"{variable} =l alloc8 {SizeOf(variableDeclaration.Type)}");
|
_writer.Indented($"{variable} =l alloc8 {SizeOf(variableDeclaration.Type)}");
|
||||||
|
|
||||||
if (variableDeclaration.Assignment.HasValue)
|
if (variableDeclaration.Assignment.HasValue)
|
||||||
{
|
{
|
||||||
@@ -706,12 +743,12 @@ public static class QBEGenerator
|
|||||||
_breakLabels.Push(endLabel);
|
_breakLabels.Push(endLabel);
|
||||||
_continueLabels.Push(conditionLabel);
|
_continueLabels.Push(conditionLabel);
|
||||||
|
|
||||||
_writer.Code($"jmp {conditionLabel}");
|
_writer.Indented($"jmp {conditionLabel}");
|
||||||
_writer.WriteLine(iterationLabel);
|
_writer.WriteLine(iterationLabel);
|
||||||
EmitBlock(whileStatement.Body);
|
EmitBlock(whileStatement.Body);
|
||||||
_writer.WriteLine(conditionLabel);
|
_writer.WriteLine(conditionLabel);
|
||||||
var result = EmitUnwrap(EmitExpression(whileStatement.Condition));
|
var result = EmitUnwrap(EmitExpression(whileStatement.Condition));
|
||||||
_writer.Code($"jnz {result}, {iterationLabel}, {endLabel}");
|
_writer.Indented($"jnz {result}, {iterationLabel}, {endLabel}");
|
||||||
_writer.WriteLine(endLabel);
|
_writer.WriteLine(endLabel);
|
||||||
|
|
||||||
_continueLabels.Pop();
|
_continueLabels.Pop();
|
||||||
@@ -754,34 +791,34 @@ public static class QBEGenerator
|
|||||||
var elementType = ((NubArrayType)arrayIndexAccess.Array.Type).ElementType;
|
var elementType = ((NubArrayType)arrayIndexAccess.Array.Type).ElementType;
|
||||||
|
|
||||||
var pointer = VarName();
|
var pointer = VarName();
|
||||||
_writer.Code($"{pointer} =l mul {index}, {SizeOf(elementType)}");
|
_writer.Indented($"{pointer} =l mul {index}, {SizeOf(elementType)}");
|
||||||
_writer.Code($"{pointer} =l add {pointer}, 8");
|
_writer.Indented($"{pointer} =l add {pointer}, 8");
|
||||||
_writer.Code($"{pointer} =l add {array}, {pointer}");
|
_writer.Indented($"{pointer} =l add {array}, {pointer}");
|
||||||
return new Val(pointer, arrayIndexAccess.Type, ValKind.Pointer);
|
return new Val(pointer, arrayIndexAccess.Type, ValKind.Pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EmitArrayBoundsCheck(string array, string index)
|
private static void EmitArrayBoundsCheck(string array, string index)
|
||||||
{
|
{
|
||||||
var count = VarName();
|
var count = VarName();
|
||||||
_writer.Code($"{count} =l loadl {array}");
|
_writer.Indented($"{count} =l loadl {array}");
|
||||||
|
|
||||||
var isNegative = VarName();
|
var isNegative = VarName();
|
||||||
_writer.Code($"{isNegative} =w csltl {index}, 0");
|
_writer.Indented($"{isNegative} =w csltl {index}, 0");
|
||||||
|
|
||||||
var isOob = VarName();
|
var isOob = VarName();
|
||||||
_writer.Code($"{isOob} =w csgel {index}, {count}");
|
_writer.Indented($"{isOob} =w csgel {index}, {count}");
|
||||||
|
|
||||||
var anyOob = VarName();
|
var anyOob = VarName();
|
||||||
_writer.Code($"{anyOob} =w or {isNegative}, {isOob}");
|
_writer.Indented($"{anyOob} =w or {isNegative}, {isOob}");
|
||||||
|
|
||||||
var oobLabel = LabelName();
|
var oobLabel = LabelName();
|
||||||
var notOobLabel = LabelName();
|
var notOobLabel = LabelName();
|
||||||
_writer.Code($"jnz {anyOob}, {oobLabel}, {notOobLabel}");
|
_writer.Indented($"jnz {anyOob}, {oobLabel}, {notOobLabel}");
|
||||||
|
|
||||||
_writer.Code(oobLabel);
|
_writer.Indented(oobLabel);
|
||||||
_writer.Code($"call $nub_panic_array_oob()");
|
_writer.Indented($"call $nub_panic_array_oob()");
|
||||||
|
|
||||||
_writer.Code(notOobLabel);
|
_writer.Indented(notOobLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Val EmitArrayInitializer(BoundArrayInitializerNode arrayInitializer)
|
private static Val EmitArrayInitializer(BoundArrayInitializerNode arrayInitializer)
|
||||||
@@ -790,17 +827,17 @@ public static class QBEGenerator
|
|||||||
var elementSize = SizeOf(arrayInitializer.ElementType);
|
var elementSize = SizeOf(arrayInitializer.ElementType);
|
||||||
|
|
||||||
var capacityInBytes = VarName();
|
var capacityInBytes = VarName();
|
||||||
_writer.Code($"{capacityInBytes} =l mul {capacity}, {elementSize}");
|
_writer.Indented($"{capacityInBytes} =l mul {capacity}, {elementSize}");
|
||||||
var totalSize = VarName();
|
var totalSize = VarName();
|
||||||
_writer.Code($"{totalSize} =l add {capacityInBytes}, 8");
|
_writer.Indented($"{totalSize} =l add {capacityInBytes}, 8");
|
||||||
|
|
||||||
var arrayPointer = VarName();
|
var arrayPointer = VarName();
|
||||||
_writer.Code($"{arrayPointer} =l alloc8 {totalSize}");
|
_writer.Indented($"{arrayPointer} =l alloc8 {totalSize}");
|
||||||
_writer.Code($"storel {capacity}, {arrayPointer}");
|
_writer.Indented($"storel {capacity}, {arrayPointer}");
|
||||||
|
|
||||||
var dataPointer = VarName();
|
var dataPointer = VarName();
|
||||||
_writer.Code($"{dataPointer} =l add {arrayPointer}, 8");
|
_writer.Indented($"{dataPointer} =l add {arrayPointer}, 8");
|
||||||
_writer.Code($"call $nub_memset(l {dataPointer}, w 0, l {capacityInBytes})");
|
_writer.Indented($"call $nub_memset(l {dataPointer}, w 0, l {capacityInBytes})");
|
||||||
|
|
||||||
return new Val(arrayPointer, arrayInitializer.Type, ValKind.Direct);
|
return new Val(arrayPointer, arrayInitializer.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
@@ -836,7 +873,7 @@ public static class QBEGenerator
|
|||||||
|
|
||||||
var instruction = EmitBinaryInstructionFor(binaryExpression.Operator, binaryExpression.Left.Type, left, right);
|
var instruction = EmitBinaryInstructionFor(binaryExpression.Operator, binaryExpression.Left.Type, left, right);
|
||||||
|
|
||||||
_writer.Code($"{outputName} {QBEAssign(binaryExpression.Left.Type)} {instruction} {left}, {right}");
|
_writer.Indented($"{outputName} {QBEAssign(binaryExpression.Left.Type)} {instruction} {left}, {right}");
|
||||||
return new Val(outputName, binaryExpression.Type, ValKind.Direct);
|
return new Val(outputName, binaryExpression.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -856,13 +893,13 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
if (type.IsSignedInteger)
|
if (type.IsSignedInteger)
|
||||||
{
|
{
|
||||||
_writer.Code($"{left} =w extsb {left}");
|
_writer.Indented($"{left} =w extsb {left}");
|
||||||
_writer.Code($"{right} =w extsb {right}");
|
_writer.Indented($"{right} =w extsb {right}");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_writer.Code($"{left} =w extub {left}");
|
_writer.Indented($"{left} =w extub {left}");
|
||||||
_writer.Code($"{right} =w extub {right}");
|
_writer.Indented($"{right} =w extub {right}");
|
||||||
}
|
}
|
||||||
|
|
||||||
suffix = 'w';
|
suffix = 'w';
|
||||||
@@ -871,13 +908,13 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
if (type.IsSignedInteger)
|
if (type.IsSignedInteger)
|
||||||
{
|
{
|
||||||
_writer.Code($"{left} =w extsh {left}");
|
_writer.Indented($"{left} =w extsh {left}");
|
||||||
_writer.Code($"{right} =w extsh {right}");
|
_writer.Indented($"{right} =w extsh {right}");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_writer.Code($"{left} =w extuh {left}");
|
_writer.Indented($"{left} =w extuh {left}");
|
||||||
_writer.Code($"{right} =w extuh {right}");
|
_writer.Indented($"{right} =w extuh {right}");
|
||||||
}
|
}
|
||||||
|
|
||||||
suffix = 'w';
|
suffix = 'w';
|
||||||
@@ -1049,7 +1086,7 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
destination = VarName();
|
destination = VarName();
|
||||||
var size = SizeOf(structInitializer.StructType);
|
var size = SizeOf(structInitializer.StructType);
|
||||||
_writer.Code($"{destination} =l alloc8 {size}");
|
_writer.Indented($"{destination} =l alloc8 {size}");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var field in structDefinition.Fields)
|
foreach (var field in structDefinition.Fields)
|
||||||
@@ -1062,7 +1099,7 @@ public static class QBEGenerator
|
|||||||
Debug.Assert(valueExpression != null);
|
Debug.Assert(valueExpression != null);
|
||||||
|
|
||||||
var offset = VarName();
|
var offset = VarName();
|
||||||
_writer.Code($"{offset} =l add {destination}, {OffsetOf(structDefinition, field.Name)}");
|
_writer.Indented($"{offset} =l add {destination}, {OffsetOf(structDefinition, field.Name)}");
|
||||||
EmitCopyIntoOrInitialize(valueExpression, offset);
|
EmitCopyIntoOrInitialize(valueExpression, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1081,16 +1118,16 @@ public static class QBEGenerator
|
|||||||
switch (unaryExpression.Operand.Type)
|
switch (unaryExpression.Operand.Type)
|
||||||
{
|
{
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
||||||
_writer.Code($"{outputName} =l neg {operand}");
|
_writer.Indented($"{outputName} =l neg {operand}");
|
||||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I32 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I8 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.I32 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I8 }:
|
||||||
_writer.Code($"{outputName} =w neg {operand}");
|
_writer.Indented($"{outputName} =w neg {operand}");
|
||||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
||||||
_writer.Code($"{outputName} =d neg {operand}");
|
_writer.Indented($"{outputName} =d neg {operand}");
|
||||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F32 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.F32 }:
|
||||||
_writer.Code($"{outputName} =s neg {operand}");
|
_writer.Indented($"{outputName} =s neg {operand}");
|
||||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1101,7 +1138,7 @@ public static class QBEGenerator
|
|||||||
switch (unaryExpression.Operand.Type)
|
switch (unaryExpression.Operand.Type)
|
||||||
{
|
{
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.Bool }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.Bool }:
|
||||||
_writer.Code($"{outputName} =w xor {operand}, 1");
|
_writer.Indented($"{outputName} =w xor {operand}, 1");
|
||||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1127,7 +1164,7 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
if (memberAccess.Member == "count")
|
if (memberAccess.Member == "count")
|
||||||
{
|
{
|
||||||
_writer.Code($"{output} =l loadl {item}");
|
_writer.Indented($"{output} =l loadl {item}");
|
||||||
return new Val(output, memberAccess.Type, ValKind.Direct);
|
return new Val(output, memberAccess.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1137,7 +1174,7 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
if (memberAccess.Member == "count")
|
if (memberAccess.Member == "count")
|
||||||
{
|
{
|
||||||
_writer.Code($"{output} =l call $nub_string_length(l {item})");
|
_writer.Indented($"{output} =l call $nub_string_length(l {item})");
|
||||||
return new Val(output, memberAccess.Type, ValKind.Direct);
|
return new Val(output, memberAccess.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1147,7 +1184,7 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
if (memberAccess.Member == "count")
|
if (memberAccess.Member == "count")
|
||||||
{
|
{
|
||||||
_writer.Code($"{output} =l call $nub_cstring_length(l {item})");
|
_writer.Indented($"{output} =l call $nub_cstring_length(l {item})");
|
||||||
return new Val(output, memberAccess.Type, ValKind.Direct);
|
return new Val(output, memberAccess.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1158,7 +1195,7 @@ public static class QBEGenerator
|
|||||||
var structDefinition = _definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue();
|
var structDefinition = _definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue();
|
||||||
var offset = OffsetOf(structDefinition, memberAccess.Member);
|
var offset = OffsetOf(structDefinition, memberAccess.Member);
|
||||||
|
|
||||||
_writer.Code($"{output} =l add {item}, {offset}");
|
_writer.Indented($"{output} =l add {item}, {offset}");
|
||||||
return new Val(output, memberAccess.Type, ValKind.Pointer);
|
return new Val(output, memberAccess.Type, ValKind.Pointer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1203,12 +1240,12 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
var outputName = VarName();
|
var outputName = VarName();
|
||||||
|
|
||||||
_writer.Code($"{outputName} {QBEAssign(funcCall.Type)} call {funcPointer}({string.Join(", ", parameterStrings)})");
|
_writer.Indented($"{outputName} {QBEAssign(funcCall.Type)} call {funcPointer}({string.Join(", ", parameterStrings)})");
|
||||||
return new Val(outputName, funcCall.Type, ValKind.Direct);
|
return new Val(outputName, funcCall.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_writer.Code($"call {funcPointer}({string.Join(", ", parameterStrings)})");
|
_writer.Indented($"call {funcPointer}({string.Join(", ", parameterStrings)})");
|
||||||
return new Val(string.Empty, funcCall.Type, ValKind.Direct);
|
return new Val(string.Empty, funcCall.Type, ValKind.Direct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ internal class QBEWriter
|
|||||||
public void StartFunction(string signature)
|
public void StartFunction(string signature)
|
||||||
{
|
{
|
||||||
_currentLine = -1;
|
_currentLine = -1;
|
||||||
_builder.AppendLine();
|
|
||||||
_builder.Append(signature);
|
_builder.Append(signature);
|
||||||
_builder.AppendLine(" {");
|
_builder.AppendLine(" {");
|
||||||
_builder.AppendLine("@start");
|
_builder.AppendLine("@start");
|
||||||
@@ -49,7 +48,7 @@ internal class QBEWriter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Code(string value)
|
public void Indented(string value)
|
||||||
{
|
{
|
||||||
_builder.Append('\t');
|
_builder.Append('\t');
|
||||||
_builder.AppendLine(value);
|
_builder.AppendLine(value);
|
||||||
|
|||||||
@@ -50,6 +50,34 @@ public class DefinitionTable
|
|||||||
.SelectMany(c => c.Definitions)
|
.SelectMany(c => c.Definitions)
|
||||||
.OfType<FuncDefinition>();
|
.OfType<FuncDefinition>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<LocalFuncDefinitionNode> GetLocalFunctions()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<LocalFuncDefinitionNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<ExternFuncDefinitionNode> GetExternFunctions()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<ExternFuncDefinitionNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<InterfaceDefinitionNode> GetInterfaces()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<InterfaceDefinitionNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<ImplementationDefinitionNode> GetImplementations()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<ImplementationDefinitionNode>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BoundDefinitionTable
|
public class BoundDefinitionTable
|
||||||
@@ -96,4 +124,32 @@ public class BoundDefinitionTable
|
|||||||
.SelectMany(c => c.Definitions)
|
.SelectMany(c => c.Definitions)
|
||||||
.OfType<BoundFuncDefinition>();
|
.OfType<BoundFuncDefinition>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<BoundLocalFuncDefinitionNode> GetLocalFunctions()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<BoundLocalFuncDefinitionNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<BoundExternFuncDefinitionNode> GetExternFunctions()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<BoundExternFuncDefinitionNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<BoundInterfaceDefinitionNode> GetInterfaces()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<BoundInterfaceDefinitionNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<BoundImplementationDefinitionNode> GetImplementations()
|
||||||
|
{
|
||||||
|
return _syntaxTrees
|
||||||
|
.SelectMany(c => c.Definitions)
|
||||||
|
.OfType<BoundImplementationDefinitionNode>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,3 +13,9 @@ public record ExternFuncDefinitionNode(IEnumerable<Token> Tokens, Optional<strin
|
|||||||
|
|
||||||
public record StructField(string Name, NubType Type, Optional<ExpressionNode> Value);
|
public record StructField(string Name, NubType Type, Optional<ExpressionNode> Value);
|
||||||
public record StructDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<StructField> Fields) : DefinitionNode(Tokens, Documentation, Namespace);
|
public record StructDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<StructField> Fields) : DefinitionNode(Tokens, Documentation, Namespace);
|
||||||
|
|
||||||
|
public record InterfaceFunc(string Name, List<FuncParameter> Parameters, NubType ReturnType);
|
||||||
|
public record InterfaceDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<InterfaceFunc> Functions) : DefinitionNode(Tokens, Documentation, Namespace);
|
||||||
|
|
||||||
|
public record ImplementationFunc(string Name, List<FuncParameter> Parameters, NubType ReturnType, BlockNode Body);
|
||||||
|
public record ImplementationDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, NubType Type, NubType Interface, List<ImplementationFunc> Functions) : DefinitionNode(Tokens, Documentation, Namespace);
|
||||||
@@ -81,6 +81,8 @@ public static class Parser
|
|||||||
{
|
{
|
||||||
Symbol.Func => ParseFuncDefinition(startIndex, modifiers, Optional.OfNullable(documentation)),
|
Symbol.Func => ParseFuncDefinition(startIndex, modifiers, Optional.OfNullable(documentation)),
|
||||||
Symbol.Struct => ParseStruct(startIndex, modifiers, Optional.OfNullable(documentation)),
|
Symbol.Struct => ParseStruct(startIndex, modifiers, Optional.OfNullable(documentation)),
|
||||||
|
Symbol.Interface => ParseInterface(startIndex, modifiers, Optional.OfNullable(documentation)),
|
||||||
|
Symbol.Impl => ParseImplementation(startIndex, modifiers, Optional.OfNullable(documentation)),
|
||||||
_ => throw new ParseException(Diagnostic
|
_ => throw new ParseException(Diagnostic
|
||||||
.Error($"Expected 'func' or 'struct', but found '{keyword.Symbol}'")
|
.Error($"Expected 'func' or 'struct', but found '{keyword.Symbol}'")
|
||||||
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
||||||
@@ -149,7 +151,7 @@ public static class Parser
|
|||||||
return new LocalFuncDefinitionNode(GetTokensForNode(startIndex), documentation, _namespace, name.Value, parameters, body, returnType, exported);
|
return new LocalFuncDefinitionNode(GetTokensForNode(startIndex), documentation, _namespace, name.Value, parameters, body, returnType, exported);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static StructDefinitionNode ParseStruct(int startIndex, List<ModifierToken> _, Optional<string> documentation)
|
private static StructDefinitionNode ParseStruct(int startIndex, List<ModifierToken> modifiers, Optional<string> documentation)
|
||||||
{
|
{
|
||||||
var name = ExpectIdentifier().Value;
|
var name = ExpectIdentifier().Value;
|
||||||
|
|
||||||
@@ -173,9 +175,109 @@ public static class Parser
|
|||||||
variables.Add(new StructField(variableName, variableType, variableValue));
|
variables.Add(new StructField(variableName, variableType, variableValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (modifiers.Count != 0)
|
||||||
|
{
|
||||||
|
throw new ParseException(Diagnostic
|
||||||
|
.Error($"Invalid modifiers for struct: {modifiers[0].Modifier}")
|
||||||
|
.WithHelp($"Structs cannot use the '{modifiers[0].Modifier}' modifier")
|
||||||
|
.At(modifiers[0])
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
|
||||||
return new StructDefinitionNode(GetTokensForNode(startIndex), documentation, _namespace, name, variables);
|
return new StructDefinitionNode(GetTokensForNode(startIndex), documentation, _namespace, name, variables);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static InterfaceDefinitionNode ParseInterface(int startIndex, List<ModifierToken> modifiers, Optional<string> documentation)
|
||||||
|
{
|
||||||
|
var name = ExpectIdentifier().Value;
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenBrace);
|
||||||
|
|
||||||
|
List<InterfaceFunc> functions = [];
|
||||||
|
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||||
|
{
|
||||||
|
ExpectSymbol(Symbol.Func);
|
||||||
|
|
||||||
|
var funcName = ExpectIdentifier().Value;
|
||||||
|
var parameters = new List<FuncParameter>();
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||||
|
{
|
||||||
|
var parameter = ParseFuncParameter();
|
||||||
|
parameters.Add(parameter);
|
||||||
|
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var nextToken) && nextToken is not SymbolToken { Symbol: Symbol.CloseParen })
|
||||||
|
{
|
||||||
|
_diagnostics.Add(Diagnostic
|
||||||
|
.Warning("Missing comma between function arguments")
|
||||||
|
.WithHelp("Add a ',' to separate arguments")
|
||||||
|
.At(nextToken)
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new NubVoidType();
|
||||||
|
|
||||||
|
if (modifiers.Count != 0)
|
||||||
|
{
|
||||||
|
throw new ParseException(Diagnostic
|
||||||
|
.Error($"Invalid modifiers for interface: {modifiers[0].Modifier}")
|
||||||
|
.WithHelp($"Interface cannot use the '{modifiers[0].Modifier}' modifier")
|
||||||
|
.At(modifiers[0])
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
functions.Add(new InterfaceFunc(funcName, parameters, returnType));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new InterfaceDefinitionNode(GetTokensForNode(startIndex), documentation, _namespace, name, functions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ImplementationDefinitionNode ParseImplementation(int startIndex, List<ModifierToken> modifiers, Optional<string> documentation)
|
||||||
|
{
|
||||||
|
var type = ParseType();
|
||||||
|
ExpectSymbol(Symbol.Colon);
|
||||||
|
var @interface = ParseType();
|
||||||
|
|
||||||
|
List<ImplementationFunc> functions = [];
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenBrace);
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||||
|
{
|
||||||
|
ExpectSymbol(Symbol.Func);
|
||||||
|
var functionName = ExpectIdentifier().Value;
|
||||||
|
var parameters = new List<FuncParameter>
|
||||||
|
{
|
||||||
|
new("this", type)
|
||||||
|
};
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
|
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||||
|
{
|
||||||
|
parameters.Add(ParseFuncParameter());
|
||||||
|
|
||||||
|
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var token) && token is not SymbolToken { Symbol: Symbol.CloseParen })
|
||||||
|
{
|
||||||
|
_diagnostics.Add(Diagnostic
|
||||||
|
.Warning("Missing comma between function parameters")
|
||||||
|
.WithHelp("Add a ',' to separate parameters")
|
||||||
|
.At(token)
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new NubVoidType();
|
||||||
|
|
||||||
|
var body = ParseBlock();
|
||||||
|
|
||||||
|
functions.AddRange(new ImplementationFunc(functionName, parameters, returnType, body));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ImplementationDefinitionNode(GetTokensForNode(startIndex), documentation, _namespace, type, @interface, functions);
|
||||||
|
}
|
||||||
|
|
||||||
private static FuncParameter ParseFuncParameter()
|
private static FuncParameter ParseFuncParameter()
|
||||||
{
|
{
|
||||||
var name = ExpectIdentifier();
|
var name = ExpectIdentifier();
|
||||||
|
|||||||
@@ -43,5 +43,7 @@ public enum Symbol
|
|||||||
Namespace,
|
Namespace,
|
||||||
Let,
|
Let,
|
||||||
Alloc,
|
Alloc,
|
||||||
Calls
|
Calls,
|
||||||
|
Interface,
|
||||||
|
Impl
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,8 @@ public static class Tokenizer
|
|||||||
["struct"] = Symbol.Struct,
|
["struct"] = Symbol.Struct,
|
||||||
["let"] = Symbol.Let,
|
["let"] = Symbol.Let,
|
||||||
["calls"] = Symbol.Calls,
|
["calls"] = Symbol.Calls,
|
||||||
|
["interface"] = Symbol.Interface,
|
||||||
|
["impl"] = Symbol.Impl,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<string, Modifier> Modifiers = new()
|
private static readonly Dictionary<string, Modifier> Modifiers = new()
|
||||||
|
|||||||
@@ -41,12 +41,53 @@ public static class Binder
|
|||||||
return node switch
|
return node switch
|
||||||
{
|
{
|
||||||
ExternFuncDefinitionNode definition => BindExternFuncDefinition(definition),
|
ExternFuncDefinitionNode definition => BindExternFuncDefinition(definition),
|
||||||
|
ImplementationDefinitionNode definition => BindImplementation(definition),
|
||||||
|
InterfaceDefinitionNode definition => BindInterfaceDefinition(definition),
|
||||||
LocalFuncDefinitionNode definition => BindLocalFuncDefinition(definition),
|
LocalFuncDefinitionNode definition => BindLocalFuncDefinition(definition),
|
||||||
StructDefinitionNode definition => BindStruct(definition),
|
StructDefinitionNode definition => BindStruct(definition),
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static BoundImplementationDefinitionNode BindImplementation(ImplementationDefinitionNode node)
|
||||||
|
{
|
||||||
|
_variables.Clear();
|
||||||
|
var functions = new List<BoundImplementationFunc>();
|
||||||
|
|
||||||
|
foreach (var function in node.Functions)
|
||||||
|
{
|
||||||
|
var parameters = new List<BoundFuncParameter>();
|
||||||
|
foreach (var parameter in function.Parameters)
|
||||||
|
{
|
||||||
|
parameters.Add(new BoundFuncParameter(parameter.Name, parameter.Type));
|
||||||
|
_variables[parameter.Name] = parameter.Type;
|
||||||
|
}
|
||||||
|
|
||||||
|
functions.Add(new BoundImplementationFunc(function.Name, parameters, function.ReturnType, BindBlock(function.Body)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BoundImplementationDefinitionNode(node.Tokens, node.Documentation, node.Namespace, node.Type, node.Interface, functions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BoundInterfaceDefinitionNode BindInterfaceDefinition(InterfaceDefinitionNode node)
|
||||||
|
{
|
||||||
|
var functions = new List<BoundInterfaceFunc>();
|
||||||
|
|
||||||
|
foreach (var func in node.Functions)
|
||||||
|
{
|
||||||
|
var parameters = new List<BoundFuncParameter>();
|
||||||
|
|
||||||
|
foreach (var parameter in func.Parameters)
|
||||||
|
{
|
||||||
|
parameters.Add(new BoundFuncParameter(parameter.Name, parameter.Type));
|
||||||
|
}
|
||||||
|
|
||||||
|
functions.Add(new BoundInterfaceFunc(func.Name, parameters, func.ReturnType));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BoundInterfaceDefinitionNode(node.Tokens, node.Documentation, node.Namespace, node.Name, functions);
|
||||||
|
}
|
||||||
|
|
||||||
private static BoundStructDefinitionNode BindStruct(StructDefinitionNode node)
|
private static BoundStructDefinitionNode BindStruct(StructDefinitionNode node)
|
||||||
{
|
{
|
||||||
var defOpt = _definitionTable.LookupStruct(node.Namespace, node.Name);
|
var defOpt = _definitionTable.LookupStruct(node.Namespace, node.Name);
|
||||||
@@ -354,6 +395,12 @@ public static class Binder
|
|||||||
|
|
||||||
NubType? type = null;
|
NubType? type = null;
|
||||||
|
|
||||||
|
var implementation = _definitionTable.GetImplementations().FirstOrDefault(x => x.Type.Equals(boundExpression.Type));
|
||||||
|
if (implementation != null)
|
||||||
|
{
|
||||||
|
if (implementation.Interface.)
|
||||||
|
}
|
||||||
|
|
||||||
switch (boundExpression.Type)
|
switch (boundExpression.Type)
|
||||||
{
|
{
|
||||||
case NubArrayType:
|
case NubArrayType:
|
||||||
|
|||||||
@@ -12,3 +12,9 @@ public record BoundExternFuncDefinitionNode(IEnumerable<Token> Tokens, Optional<
|
|||||||
|
|
||||||
public record BoundStructField(string Name, NubType Type, Optional<BoundExpressionNode> Value);
|
public record BoundStructField(string Name, NubType Type, Optional<BoundExpressionNode> Value);
|
||||||
public record BoundStructDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<BoundStructField> Fields) : BoundDefinitionNode(Tokens, Documentation, Namespace);
|
public record BoundStructDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<BoundStructField> Fields) : BoundDefinitionNode(Tokens, Documentation, Namespace);
|
||||||
|
|
||||||
|
public record BoundInterfaceFunc(string Name, List<BoundFuncParameter> Parameters, NubType ReturnType);
|
||||||
|
public record BoundInterfaceDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<BoundInterfaceFunc> Functions) : BoundDefinitionNode(Tokens, Documentation, Namespace);
|
||||||
|
|
||||||
|
public record BoundImplementationFunc(string Name, List<BoundFuncParameter> Parameters, NubType ReturnType, BoundBlockNode Body);
|
||||||
|
public record BoundImplementationDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, NubType Type, NubInterfaceType Interface, List<BoundImplementationFunc> Functions) : BoundDefinitionNode(Tokens, Documentation, Namespace);
|
||||||
Reference in New Issue
Block a user