This commit is contained in:
nub31
2025-07-22 22:10:31 +02:00
parent 2d2d346da0
commit 4055002a8c
25 changed files with 413 additions and 972 deletions

View File

@@ -63,10 +63,10 @@ public sealed class Binder
foreach (var function in node.Functions)
{
functions.Add(new BoundTraitFunc(node.Tokens, function.Name, BindFuncSignature(function.Signature)));
functions.Add(new BoundTraitFunc(function.Name, BindFuncSignature(function.Signature)));
}
return new BoundTrait(node.Tokens, node.Namespace, node.Name, functions);
return new BoundTrait(node.Namespace, node.Name, functions);
}
private BoundStruct BindStruct(StructSyntax node)
@@ -82,15 +82,15 @@ public sealed class Binder
value = BindExpression(field.Value.Value, BindType(field.Type));
}
structFields.Add(new BoundStructField(field.Tokens, field.Index, field.Name, BindType(field.Type), value));
structFields.Add(new BoundStructField(field.Index, field.Name, BindType(field.Type), value));
}
return new BoundStruct(node.Tokens, node.Namespace, node.Name, structFields);
return new BoundStruct(node.Namespace, node.Name, structFields);
}
private BoundExternFunc BindExternFuncDefinition(ExternFuncSyntax node)
{
return new BoundExternFunc(node.Tokens, node.Namespace, node.Name, node.CallName, BindFuncSignature(node.Signature));
return new BoundExternFunc(node.Namespace, node.Name, node.CallName, BindFuncSignature(node.Signature));
}
private BoundLocalFunc BindLocalFuncDefinition(LocalFuncSyntax node)
@@ -98,7 +98,7 @@ public sealed class Binder
var signature = BindFuncSignature(node.Signature);
var body = BindFuncBody(node.Body, signature.ReturnType, signature.Parameters);
return new BoundLocalFunc(node.Tokens, node.Namespace, node.Name, signature, body);
return new BoundLocalFunc(node.Namespace, node.Name, signature, body);
}
private BoundStatement BindStatement(StatementSyntax node)
@@ -106,8 +106,8 @@ public sealed class Binder
return node switch
{
AssignmentSyntax statement => BindAssignment(statement),
BreakSyntax statement => BindBreak(statement),
ContinueSyntax statement => BindContinue(statement),
BreakSyntax => new BoundBreak(),
ContinueSyntax => new BoundContinue(),
IfSyntax statement => BindIf(statement),
ReturnSyntax statement => BindReturn(statement),
StatementExpressionSyntax statement => BindStatementExpression(statement),
@@ -121,17 +121,7 @@ public sealed class Binder
{
var expression = BindExpression(statement.Target);
var value = BindExpression(statement.Value, expression.Type);
return new BoundAssignment(statement.Tokens, expression, value);
}
private BoundBreak BindBreak(BreakSyntax statement)
{
return new BoundBreak(statement.Tokens);
}
private BoundContinue BindContinue(ContinueSyntax statement)
{
return new BoundContinue(statement.Tokens);
return new BoundAssignment(expression, value);
}
private BoundIf BindIf(IfSyntax statement)
@@ -147,7 +137,7 @@ public sealed class Binder
);
}
return new BoundIf(statement.Tokens, BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body), elseStatement);
return new BoundIf(BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body), elseStatement);
}
private BoundReturn BindReturn(ReturnSyntax statement)
@@ -159,12 +149,12 @@ public sealed class Binder
value = BindExpression(statement.Value.Value, _funcReturnTypes.Peek());
}
return new BoundReturn(statement.Tokens, value);
return new BoundReturn(value);
}
private BoundStatementExpression BindStatementExpression(StatementExpressionSyntax statement)
{
return new BoundStatementExpression(statement.Tokens, BindExpression(statement.Expression));
return new BoundStatementExpression(BindExpression(statement.Expression));
}
private BoundVariableDeclaration BindVariableDeclaration(VariableDeclarationSyntax statement)
@@ -191,12 +181,12 @@ public sealed class Binder
Scope.Declare(new Variable(statement.Name, type));
return new BoundVariableDeclaration(statement.Tokens, statement.Name, assignment, type);
return new BoundVariableDeclaration(statement.Name, assignment, type);
}
private BoundWhile BindWhile(WhileSyntax statement)
{
return new BoundWhile(statement.Tokens, BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body));
return new BoundWhile(BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body));
}
private BoundExpression BindExpression(ExpressionSyntax node, NubType? expectedType = null)
@@ -222,19 +212,19 @@ public sealed class Binder
private BoundAddressOf BindAddressOf(AddressOfSyntax expression)
{
var inner = BindExpression(expression.Expression);
return new BoundAddressOf(expression.Tokens, new NubPointerType(inner.Type), inner);
return new BoundAddressOf(new NubPointerType(inner.Type), inner);
}
private BoundArrowFunc BindArrowFunc(ArrowFuncSyntax expression, NubType? expectedType = null)
{
if (expectedType == null)
{
throw new BindException(Diagnostic.Error("Cannot infer argument types for arrow function").At(expression).Build());
throw new BindException(Diagnostic.Error("Cannot infer argument types for arrow function").Build());
}
if (expectedType is not NubFuncType funcType)
{
throw new BindException(Diagnostic.Error($"Expected {expectedType}, but got arrow function").At(expression).Build());
throw new BindException(Diagnostic.Error($"Expected {expectedType}, but got arrow function").Build());
}
var parameters = new List<BoundFuncParameter>();
@@ -248,40 +238,40 @@ public sealed class Binder
var expectedParameterType = funcType.Parameters[i];
var parameter = expression.Parameters[i];
parameters.Add(new BoundFuncParameter(parameter.Tokens, parameter.Name, expectedParameterType));
parameters.Add(new BoundFuncParameter(parameter.Name, expectedParameterType));
}
var body = BindFuncBody(expression.Body, funcType.ReturnType, parameters);
return new BoundArrowFunc(expression.Tokens, new NubFuncType(parameters.Select(x => x.Type).ToList(), funcType.ReturnType), parameters, funcType.ReturnType, body);
return new BoundArrowFunc(new NubFuncType(parameters.Select(x => x.Type).ToList(), funcType.ReturnType), parameters, funcType.ReturnType, body);
}
private BoundArrayIndexAccess BindArrayIndexAccess(ArrayIndexAccessSyntax expression)
{
var boundArray = BindExpression(expression.Target);
var elementType = ((NubArrayType)boundArray.Type).ElementType;
return new BoundArrayIndexAccess(expression.Tokens, elementType, boundArray, BindExpression(expression.Index, new NubPrimitiveType(PrimitiveTypeKind.U64)));
return new BoundArrayIndexAccess(elementType, boundArray, BindExpression(expression.Index, new NubPrimitiveType(PrimitiveTypeKind.U64)));
}
private BoundArrayInitializer BindArrayInitializer(ArrayInitializerSyntax expression)
{
var capacity = BindExpression(expression.Capacity, new NubPrimitiveType(PrimitiveTypeKind.U64));
var type = new NubArrayType(BindType(expression.ElementType));
return new BoundArrayInitializer(expression.Tokens, type, capacity, BindType(expression.ElementType));
return new BoundArrayInitializer(type, capacity, BindType(expression.ElementType));
}
private BoundBinaryExpression BindBinaryExpression(BinaryExpressionSyntax expression)
{
var boundLeft = BindExpression(expression.Left);
var boundRight = BindExpression(expression.Right, boundLeft.Type);
return new BoundBinaryExpression(expression.Tokens, boundLeft.Type, boundLeft, BindBinaryOperator(expression.Operator), boundRight);
return new BoundBinaryExpression(boundLeft.Type, boundLeft, BindBinaryOperator(expression.Operator), boundRight);
}
private BoundDereference BindDereference(DereferenceSyntax expression)
{
var boundExpression = BindExpression(expression.Expression);
var dereferencedType = ((NubPointerType)boundExpression.Type).BaseType;
return new BoundDereference(expression.Tokens, dereferencedType, boundExpression);
return new BoundDereference(dereferencedType, boundExpression);
}
private BoundFuncCall BindFuncCall(FuncCallSyntax expression)
@@ -304,7 +294,7 @@ public sealed class Binder
parameters.Add(BindExpression(parameter, expectedType));
}
return new BoundFuncCall(expression.Tokens, funcType.ReturnType, boundExpression, parameters);
return new BoundFuncCall(funcType.ReturnType, boundExpression, parameters);
}
private BoundExpression BindIdentifier(IdentifierSyntax expression)
@@ -323,7 +313,7 @@ public sealed class Binder
var returnType = BindType(localFunc.Signature.ReturnType);
var parameterTypes = localFunc.Signature.Parameters.Select(p => BindType(p.Type)).ToList();
var type = new NubFuncType(parameterTypes, returnType);
return new BoundLocalFuncIdent(expression.Tokens, type, @namespace, expression.Name);
return new BoundLocalFuncIdent(type, @namespace, expression.Name);
}
var externFuncs = _definitionTable.LookupExternFunc(@namespace, expression.Name).ToArray();
@@ -339,7 +329,7 @@ public sealed class Binder
var returnType = BindType(externFunc.Signature.ReturnType);
var parameterTypes = externFunc.Signature.Parameters.Select(p => BindType(p.Type)).ToList();
var type = new NubFuncType(parameterTypes, returnType);
return new BoundExternFuncIdent(expression.Tokens, type, @namespace, expression.Name);
return new BoundExternFuncIdent(type, @namespace, expression.Name);
}
if (!expression.Namespace.HasValue)
@@ -347,7 +337,7 @@ public sealed class Binder
var variable = Scope.Lookup(expression.Name);
if (variable != null)
{
return new BoundVariableIdent(expression.Tokens, variable.Type, variable.Name);
return new BoundVariableIdent(variable.Type, variable.Name);
}
}
@@ -365,7 +355,7 @@ public sealed class Binder
_ => throw new ArgumentOutOfRangeException()
};
return new BoundLiteral(expression.Tokens, type, expression.Value, expression.Kind);
return new BoundLiteral(type, expression.Value, expression.Kind);
}
private BoundExpression BindMemberAccess(MemberAccessSyntax expression)
@@ -385,7 +375,7 @@ public sealed class Binder
// var returnType = BindType(impl.Signature.ReturnType);
// var parameterTypes = impl.Signature.Parameters.Select(p => BindType(p.Type)).ToList();
// var type = new NubFuncType(parameterTypes, returnType);
// return new BoundTraitImplFuncAccess(expression.Tokens, type, boundExpression, expression.Member);
// return new BoundTraitImplFuncAccess(type, boundExpression, expression.Member);
// }
if (boundExpression.Type is NubCustomType customType)
@@ -413,7 +403,7 @@ public sealed class Binder
var returnType = BindType(traitFunc.Signature.ReturnType);
var parameterTypes = traitFunc.Signature.Parameters.Select(p => BindType(p.Type)).ToList();
var type = new NubFuncType(parameterTypes, returnType);
return new BoundTraitFuncAccess(expression.Tokens, type, customType, boundExpression, expression.Member);
return new BoundInterfaceFuncAccess(type, customType, boundExpression, expression.Member);
}
}
@@ -437,7 +427,7 @@ public sealed class Binder
var field = fields[0];
return new BoundStructFieldAccess(expression.Tokens, BindType(field.Type), customType, boundExpression, expression.Member);
return new BoundStructFieldAccess(BindType(field.Type), customType, boundExpression, expression.Member);
}
}
}
@@ -487,7 +477,7 @@ public sealed class Binder
initializers[field] = BindExpression(initializer, BindType(fields[0].Type));
}
return new BoundStructInitializer(expression.Tokens, structType, initializers);
return new BoundStructInitializer(structType, initializers);
}
private BoundUnaryExpression BindUnaryExpression(UnaryExpressionSyntax expression)
@@ -523,7 +513,7 @@ public sealed class Binder
throw new NotImplementedException("Diagnostics not implemented");
}
return new BoundUnaryExpression(expression.Tokens, type, BindBinaryOperator(expression.Operator), boundOperand);
return new BoundUnaryExpression(type, BindBinaryOperator(expression.Operator), boundOperand);
}
private BoundFuncSignature BindFuncSignature(FuncSignatureSyntax node)
@@ -532,10 +522,10 @@ public sealed class Binder
foreach (var parameter in node.Parameters)
{
parameters.Add(new BoundFuncParameter(parameter.Tokens, parameter.Name, BindType(parameter.Type)));
parameters.Add(new BoundFuncParameter(parameter.Name, BindType(parameter.Type)));
}
return new BoundFuncSignature(node.Tokens, parameters, BindType(node.ReturnType));
return new BoundFuncSignature(parameters, BindType(node.ReturnType));
}
private BoundBinaryOperator BindBinaryOperator(BinaryOperator op)
@@ -579,7 +569,7 @@ public sealed class Binder
_scopes.Pop();
return new BoundBlock(node.Tokens, statements);
return new BoundBlock(statements);
}
private BoundBlock BindFuncBody(BlockSyntax block, NubType returnType, IReadOnlyList<BoundFuncParameter> parameters)