This commit is contained in:
nub31
2025-07-06 22:38:00 +02:00
parent 91e93fa0bf
commit 0d216d249e
8 changed files with 503 additions and 713 deletions

View File

@@ -1,4 +1,3 @@
using System.Diagnostics;
using Common;
using NubLang.Diagnostics;
using NubLang.Syntax.Node;
@@ -14,8 +13,8 @@ public sealed class Binder
private readonly DefinitionTable _definitionTable;
// TODO: Implement proper variable tracking and scoping
private Dictionary<string, BoundNubType> _variables = new();
private BoundNubType? _functionReturnType;
private Dictionary<string, NubType> _variables = new();
private NubType? _functionReturnType;
public Binder(SyntaxTree syntaxTree, DefinitionTable definitionTable)
{
@@ -70,14 +69,14 @@ public sealed class Binder
foreach (var parameter in function.Parameters)
{
_variables[parameter.Name] = BindType(parameter.Type);
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, BindType(parameter.Type)));
_variables[parameter.Name] = parameter.Type;
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, parameter.Type));
}
functions.Add(new BoundTraitFuncImplNode(function.Tokens, function.Name, parameters, BindType(function.ReturnType), BindBlock(function.Body)));
functions.Add(new BoundTraitFuncImplNode(function.Tokens, function.Name, parameters, function.ReturnType, BindBlock(function.Body)));
}
return new BoundTraitImplNode(node.Tokens, node.Namespace, BindType(node.TraitType), BindType(node.ForType), functions);
return new BoundTraitImplNode(node.Tokens, node.Namespace, node.TraitType, node.ForType, functions);
}
private BoundTraitNode BindTraitDefinition(TraitNode node)
@@ -90,10 +89,10 @@ public sealed class Binder
foreach (var parameter in function.Parameters)
{
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, BindType(parameter.Type)));
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, parameter.Type));
}
functions.Add(new BoundTraitFuncNode(node.Tokens, function.Name, parameters, BindType(function.ReturnType)));
functions.Add(new BoundTraitFuncNode(node.Tokens, function.Name, parameters, function.ReturnType));
}
return new BoundTraitNode(node.Tokens, node.Namespace, node.Name, functions);
@@ -109,10 +108,10 @@ public sealed class Binder
if (field.Value.HasValue)
{
value = BindExpression(field.Value.Value, BindType(field.Type));
value = BindExpression(field.Value.Value, field.Type);
}
structFields.Add(new BoundStructFieldNode(field.Tokens, field.Index, field.Name, BindType(field.Type), value));
structFields.Add(new BoundStructFieldNode(field.Tokens, field.Index, field.Name, field.Type, value));
}
return new BoundStructNode(node.Tokens, node.Namespace, node.Name, structFields);
@@ -124,28 +123,28 @@ public sealed class Binder
foreach (var parameter in node.Parameters)
{
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, BindType(parameter.Type)));
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, parameter.Type));
}
return new BoundExternFuncNode(node.Tokens, node.Namespace, node.Name, node.CallName, parameters, BindType(node.ReturnType));
return new BoundExternFuncNode(node.Tokens, node.Namespace, node.Name, node.CallName, parameters, node.ReturnType);
}
private BoundLocalFuncNode BindLocalFuncDefinition(LocalFuncNode node)
{
_variables.Clear();
_functionReturnType = BindType(node.ReturnType);
_functionReturnType = node.ReturnType;
var parameters = new List<BoundFuncParameterNode>();
foreach (var parameter in node.Parameters)
{
_variables[parameter.Name] = BindType(parameter.Type);
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, BindType(parameter.Type)));
_variables[parameter.Name] = parameter.Type;
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, parameter.Type));
}
var body = BindBlock(node.Body);
return new BoundLocalFuncNode(node.Tokens, node.Namespace, node.Name, parameters, body, BindType(node.ReturnType), node.Exported);
return new BoundLocalFuncNode(node.Tokens, node.Namespace, node.Name, parameters, body, node.ReturnType, node.Exported);
}
private BoundBlock BindBlock(BlockNode node)
@@ -206,7 +205,7 @@ public sealed class Binder
);
}
return new BoundIfNode(statement.Tokens, BindExpression(statement.Condition, BoundNubPrimitiveType.Bool), BindBlock(statement.Body), elseStatement);
return new BoundIfNode(statement.Tokens, BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body), elseStatement);
}
private BoundReturnNode BindReturn(ReturnNode statement)
@@ -228,11 +227,11 @@ public sealed class Binder
private BoundVariableDeclarationNode BindVariableDeclaration(VariableDeclarationNode statement)
{
BoundNubType? type = null;
NubType? type = null;
if (statement.ExplicitType.HasValue)
{
type = BindType(statement.ExplicitType.Value);
type = statement.ExplicitType.Value;
}
var assignment = Optional<BoundExpressionNode>.Empty();
@@ -255,10 +254,10 @@ public sealed class Binder
private BoundWhileNode BindWhile(WhileNode statement)
{
return new BoundWhileNode(statement.Tokens, BindExpression(statement.Condition, BoundNubPrimitiveType.Bool), BindBlock(statement.Body));
return new BoundWhileNode(statement.Tokens, BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body));
}
private BoundExpressionNode BindExpression(ExpressionNode node, BoundNubType? expectedType = null)
private BoundExpressionNode BindExpression(ExpressionNode node, NubType? expectedType = null)
{
return node switch
{
@@ -281,7 +280,7 @@ public sealed class Binder
private BoundAddressOfNode BindAddressOf(AddressOfNode expression)
{
var inner = BindExpression(expression.Expression);
return new BoundAddressOfNode(expression.Tokens, new BoundNubPointerType(inner.Type), inner);
return new BoundAddressOfNode(expression.Tokens, new NubPointerType(inner.Type), inner);
}
private BoundAnonymousFuncNode BindAnonymousFunc(AnonymousFuncNode expression)
@@ -290,24 +289,24 @@ public sealed class Binder
foreach (var parameter in expression.Parameters)
{
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, BindType(parameter.Type)));
parameters.Add(new BoundFuncParameterNode(parameter.Tokens, parameter.Name, parameter.Type));
}
var body = BindBlock(expression.Body);
return new BoundAnonymousFuncNode(expression.Tokens, new BoundNubFuncType(BindType(expression.ReturnType), parameters.Select(x => x.Type).ToList()), parameters, body, BindType(expression.ReturnType));
return new BoundAnonymousFuncNode(expression.Tokens, new NubFuncType(expression.ReturnType, parameters.Select(x => x.Type).ToList()), parameters, body, expression.ReturnType);
}
private BoundArrayIndexAccessNode BindArrayIndexAccess(ArrayIndexAccessNode expression)
{
var boundArray = BindExpression(expression.Target);
var elementType = ((BoundNubArrayType)boundArray.Type).ElementType;
return new BoundArrayIndexAccessNode(expression.Tokens, elementType, boundArray, BindExpression(expression.Index, BoundNubPrimitiveType.U64));
var elementType = ((NubArrayType)boundArray.Type).ElementType;
return new BoundArrayIndexAccessNode(expression.Tokens, elementType, boundArray, BindExpression(expression.Index, new NubPrimitiveType(PrimitiveTypeKind.U64)));
}
private BoundArrayInitializerNode BindArrayInitializer(ArrayInitializerNode expression)
{
return new BoundArrayInitializerNode(expression.Tokens, new BoundNubArrayType(BindType(expression.ElementType)), BindExpression(expression.Capacity, BoundNubPrimitiveType.U64), BindType(expression.ElementType));
return new BoundArrayInitializerNode(expression.Tokens, new NubArrayType(expression.ElementType), BindExpression(expression.Capacity, new NubPrimitiveType(PrimitiveTypeKind.U64)), expression.ElementType);
}
private BoundBinaryExpressionNode BindBinaryExpression(BinaryExpressionNode expression)
@@ -320,7 +319,7 @@ public sealed class Binder
private BoundDereferenceNode BindDereference(DereferenceNode expression)
{
var boundExpression = BindExpression(expression.Expression);
var dereferencedType = ((BoundNubPointerType)boundExpression.Type).BaseType;
var dereferencedType = ((NubPointerType)boundExpression.Type).BaseType;
return new BoundDereferenceNode(expression.Tokens, dereferencedType, boundExpression);
}
@@ -328,7 +327,7 @@ public sealed class Binder
{
var boundExpression = BindExpression(expression.Expression);
var funcType = (BoundNubFuncType)boundExpression.Type;
var funcType = (NubFuncType)boundExpression.Type;
var parameters = new List<BoundExpressionNode>();
@@ -361,7 +360,7 @@ public sealed class Binder
var localFunc = localFuncs[0];
var type = new NubFuncType(localFunc.ReturnType, localFunc.Parameters.Select(p => p.Type).ToList());
return new BoundLocalFuncIdentNode(expression.Tokens, BindType(type), @namespace, expression.Name);
return new BoundLocalFuncIdentNode(expression.Tokens, type, @namespace, expression.Name);
}
var externFuncs = _definitionTable.LookupExternFunc(@namespace, expression.Name).ToArray();
@@ -375,7 +374,7 @@ public sealed class Binder
var externFunc = externFuncs[0];
var type = new NubFuncType(externFunc.ReturnType, externFunc.Parameters.Select(p => p.Type).ToList());
return new BoundExternFuncIdentNode(expression.Tokens, BindType(type), @namespace, expression.Name);
return new BoundExternFuncIdentNode(expression.Tokens, type, @namespace, expression.Name);
}
if (!expression.Namespace.HasValue)
@@ -386,14 +385,14 @@ public sealed class Binder
throw new BindException(Diagnostic.Error($"No identifier with then name {(expression.Namespace.HasValue ? $"{expression.Namespace.Value}::" : "")}{expression.Name} exists").Build());
}
private BoundLiteralNode BindLiteral(LiteralNode expression, BoundNubType? expectedType = null)
private BoundLiteralNode BindLiteral(LiteralNode expression, NubType? expectedType = null)
{
var type = expectedType ?? expression.Kind switch
{
LiteralKind.Integer => BoundNubPrimitiveType.I64,
LiteralKind.Float => BoundNubPrimitiveType.F64,
LiteralKind.String => new BoundNubStringType(),
LiteralKind.Bool => BoundNubPrimitiveType.Bool,
LiteralKind.Integer => new NubPrimitiveType(PrimitiveTypeKind.I64),
LiteralKind.Float => new NubPrimitiveType(PrimitiveTypeKind.F64),
LiteralKind.String => new NubStringType(),
LiteralKind.Bool => new NubPrimitiveType(PrimitiveTypeKind.Bool),
_ => throw new ArgumentOutOfRangeException()
};
@@ -404,7 +403,7 @@ public sealed class Binder
{
var boundExpression = BindExpression(expression.Target);
var traitFuncImpls = _definitionTable.LookupTraitFuncImpl(UnbindType(boundExpression.Type), expression.Member).ToArray();
var traitFuncImpls = _definitionTable.LookupTraitFuncImpl(boundExpression.Type, expression.Member).ToArray();
if (traitFuncImpls.Length > 0)
{
if (traitFuncImpls.Length > 1)
@@ -414,18 +413,18 @@ public sealed class Binder
var impl = traitFuncImpls[0];
var type = new BoundNubFuncType(BindType(impl.ReturnType), impl.Parameters.Select(p => BindType(p.Type)).ToList());
var type = new NubFuncType(impl.ReturnType, impl.Parameters.Select(p => p.Type).ToList());
return new BoundTraitImplFuncAccessNode(expression.Tokens, type, boundExpression, expression.Member);
}
if (boundExpression.Type is BoundNubTraitType traitType)
if (boundExpression.Type is NubCustomType customType)
{
var traits = _definitionTable.LookupTrait(traitType.Namespace, traitType.Name).ToArray();
var traits = _definitionTable.LookupTrait(customType.Namespace, customType.Name).ToArray();
if (traits.Length > 0)
{
if (traits.Length > 1)
{
throw new BindException(Diagnostic.Error($"Trait {traitType.Namespace}::{traitType.Name} has multiple definitions").Build());
throw new BindException(Diagnostic.Error($"Trait {customType.Namespace}::{customType.Name} has multiple definitions").Build());
}
var trait = traits[0];
@@ -440,20 +439,17 @@ public sealed class Binder
var traitFunc = traitFuncs[0];
var type = new BoundNubFuncType(BindType(traitFunc.ReturnType), traitFunc.Parameters.Select(p => BindType(p.Type)).ToList());
return new BoundTraitFuncAccessNode(expression.Tokens, type, traitType, boundExpression, expression.Member);
var type = new NubFuncType(traitFunc.ReturnType, traitFunc.Parameters.Select(p => p.Type).ToList());
return new BoundTraitFuncAccessNode(expression.Tokens, type, customType, boundExpression, expression.Member);
}
}
}
if (boundExpression.Type is BoundNubStructType structType)
{
var structs = _definitionTable.LookupStruct(structType.Namespace, structType.Name).ToArray();
var structs = _definitionTable.LookupStruct(customType.Namespace, customType.Name).ToArray();
if (structs.Length > 0)
{
if (structs.Length > 1)
{
throw new BindException(Diagnostic.Error($"Struct {structType.Namespace}::{structType.Name} has multiple definitions").Build());
throw new BindException(Diagnostic.Error($"Struct {customType.Namespace}::{customType.Name} has multiple definitions").Build());
}
var @struct = structs[0];
@@ -468,7 +464,7 @@ public sealed class Binder
var field = fields[0];
return new BoundStructFieldAccessNode(expression.Tokens, BindType(field.Type), structType, boundExpression, expression.Member);
return new BoundStructFieldAccessNode(expression.Tokens, field.Type, customType, boundExpression, expression.Member);
}
}
}
@@ -513,23 +509,23 @@ public sealed class Binder
throw new BindException(Diagnostic.Error($"Struct {@struct.Namespace}::{@struct.Name} has multiple fields with the name {field}").Build());
}
initializers[field] = BindExpression(initializer, BindType(fields[0].Type));
initializers[field] = BindExpression(initializer, fields[0].Type);
}
return new BoundStructInitializerNode(expression.Tokens, BindType(structType), new BoundNubStructType(@struct.Namespace, @struct.Name), initializers);
return new BoundStructInitializerNode(expression.Tokens, structType, new NubCustomType(@struct.Namespace, @struct.Name), initializers);
}
private BoundUnaryExpressionNode BindUnaryExpression(Node_UnaryExpressionNode expression)
{
var boundOperand = BindExpression(expression.Operand);
BoundNubType? type = null;
NubType? type = null;
switch (expression.Operator)
{
case UnaryExpressionOperator.Negate:
{
boundOperand = BindExpression(expression.Operand, BoundNubPrimitiveType.I64);
boundOperand = BindExpression(expression.Operand, new NubPrimitiveType(PrimitiveTypeKind.I64));
if (boundOperand.Type.IsNumber)
{
@@ -540,9 +536,9 @@ public sealed class Binder
}
case UnaryExpressionOperator.Invert:
{
boundOperand = BindExpression(expression.Operand, BoundNubPrimitiveType.Bool);
boundOperand = BindExpression(expression.Operand, new NubPrimitiveType(PrimitiveTypeKind.Bool));
type = new BoundNubPrimitiveType(PrimitiveTypeKind.Bool);
type = new NubPrimitiveType(PrimitiveTypeKind.Bool);
break;
}
}
@@ -554,82 +550,6 @@ public sealed class Binder
return new BoundUnaryExpressionNode(expression.Tokens, type, expression.Operator, boundOperand);
}
private BoundNubType BindType(NubType type)
{
switch (type)
{
case NubCustomType customType:
{
var structs = _definitionTable.LookupStruct(customType.Namespace, customType.Name).ToArray();
if (structs.Length > 1)
{
throw new BindException(Diagnostic.Error($"Struct {type} has multiple definitions").Build());
}
var traits = _definitionTable.LookupTrait(customType.Namespace, customType.Name).ToArray();
if (traits.Length > 1)
{
throw new BindException(Diagnostic.Error($"Trait {type} has multiple definitions").Build());
}
if (structs.Length == 0 && traits.Length == 0)
{
throw new BindException(Diagnostic.Error($"Failed to resolve type {type} to a struct or trait").Build());
}
if (structs.Length > 0 && traits.Length > 0)
{
throw new BindException(Diagnostic.Error($"Unable to determine if type {type} is a struct or trait").WithHelp($"Make {type} is not defined as bot a struct and trait").Build());
}
if (structs.Length == 1)
{
return new BoundNubStructType(customType.Namespace, customType.Name);
}
if (traits.Length == 1)
{
return new BoundNubTraitType(customType.Namespace, customType.Name);
}
throw new UnreachableException();
}
case NubArrayType arrayType:
return new BoundNubArrayType(BindType(arrayType.ElementType));
case NubCStringType:
return new BoundNubCStringType();
case NubStringType:
return new BoundNubStringType();
case NubFuncType funcType:
return new BoundNubFuncType(BindType(funcType.ReturnType), funcType.Parameters.Select(BindType).ToList());
case NubPointerType pointerType:
return new BoundNubPointerType(BindType(pointerType.BaseType));
case NubPrimitiveType primitiveType:
return new BoundNubPrimitiveType(primitiveType.Kind);
case NubVoidType:
return new BoundNubVoidType();
default:
throw new ArgumentOutOfRangeException(nameof(type));
}
}
private NubType UnbindType(BoundNubType type)
{
return type switch
{
BoundNubArrayType arrayType => new NubArrayType(UnbindType(arrayType.ElementType)),
BoundNubCStringType => new NubCStringType(),
BoundNubStringType => new NubStringType(),
BoundNubStructType structType => new NubCustomType(structType.Namespace, structType.Name),
BoundNubTraitType traitType => new NubCustomType(traitType.Namespace, traitType.Name),
BoundNubFuncType funcType => new NubFuncType(UnbindType(funcType.ReturnType), funcType.Parameters.Select(UnbindType).ToList()),
BoundNubPointerType pointerType => new NubPointerType(UnbindType(pointerType.BaseType)),
BoundNubPrimitiveType primitiveType => new NubPrimitiveType(primitiveType.Kind),
BoundNubVoidType => new NubVoidType(),
_ => throw new ArgumentOutOfRangeException(nameof(type))
};
}
}
public class BindException : Exception