rename node -> syntax
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using Common;
|
||||
using NubLang.Diagnostics;
|
||||
using NubLang.Syntax.Node;
|
||||
using NubLang.Syntax.Parsing.Node;
|
||||
using NubLang.Syntax.Tokenization;
|
||||
using Node_UnaryExpressionNode = NubLang.Syntax.Node.UnaryExpressionNode;
|
||||
|
||||
namespace NubLang.Syntax.Binding;
|
||||
|
||||
@@ -28,13 +28,13 @@ public sealed class Binder
|
||||
_functionReturnType = null;
|
||||
|
||||
var diagnostics = new List<Diagnostic>();
|
||||
var topLevelNodes = new List<BoundTopLevelNode>();
|
||||
var definitions = new List<BoundDefinitionNode>();
|
||||
|
||||
foreach (var topLevel in _syntaxTree.TopLevelNodes)
|
||||
foreach (var definition in _syntaxTree.Definitions)
|
||||
{
|
||||
try
|
||||
{
|
||||
topLevelNodes.Add(BindTopLevel(topLevel));
|
||||
definitions.Add(BindTopLevel(definition));
|
||||
}
|
||||
catch (BindException e)
|
||||
{
|
||||
@@ -42,23 +42,23 @@ public sealed class Binder
|
||||
}
|
||||
}
|
||||
|
||||
return new BoundSyntaxTree(_syntaxTree.Namespace, topLevelNodes, diagnostics);
|
||||
return new BoundSyntaxTree(_syntaxTree.Namespace, definitions, diagnostics);
|
||||
}
|
||||
|
||||
private BoundTopLevelNode BindTopLevel(TopLevelNode node)
|
||||
private BoundDefinitionNode BindTopLevel(DefinitionSyntax node)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
ExternFuncNode topLevel => BindExternFuncDefinition(topLevel),
|
||||
TraitImplNode topLevel => BindTraitImplementation(topLevel),
|
||||
TraitNode topLevel => BindTraitDefinition(topLevel),
|
||||
LocalFuncNode topLevel => BindLocalFuncDefinition(topLevel),
|
||||
StructNode topLevel => BindStruct(topLevel),
|
||||
ExternFuncSyntax topLevel => BindExternFuncDefinition(topLevel),
|
||||
TraitImplSyntax topLevel => BindTraitImplementation(topLevel),
|
||||
TraitSyntax topLevel => BindTraitDefinition(topLevel),
|
||||
LocalFuncSyntax topLevel => BindLocalFuncDefinition(topLevel),
|
||||
StructSyntax topLevel => BindStruct(topLevel),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private BoundTraitImplNode BindTraitImplementation(TraitImplNode node)
|
||||
private BoundTraitImplNode BindTraitImplementation(TraitImplSyntax node)
|
||||
{
|
||||
_variables.Clear();
|
||||
var functions = new List<BoundTraitFuncImplNode>();
|
||||
@@ -79,7 +79,7 @@ public sealed class Binder
|
||||
return new BoundTraitImplNode(node.Tokens, node.Namespace, node.TraitType, node.ForType, functions);
|
||||
}
|
||||
|
||||
private BoundTraitNode BindTraitDefinition(TraitNode node)
|
||||
private BoundTraitNode BindTraitDefinition(TraitSyntax node)
|
||||
{
|
||||
var functions = new List<BoundTraitFuncNode>();
|
||||
|
||||
@@ -98,7 +98,7 @@ public sealed class Binder
|
||||
return new BoundTraitNode(node.Tokens, node.Namespace, node.Name, functions);
|
||||
}
|
||||
|
||||
private BoundStructNode BindStruct(StructNode node)
|
||||
private BoundStructNode BindStruct(StructSyntax node)
|
||||
{
|
||||
var structFields = new List<BoundStructFieldNode>();
|
||||
|
||||
@@ -117,7 +117,7 @@ public sealed class Binder
|
||||
return new BoundStructNode(node.Tokens, node.Namespace, node.Name, structFields);
|
||||
}
|
||||
|
||||
private BoundExternFuncNode BindExternFuncDefinition(ExternFuncNode node)
|
||||
private BoundExternFuncNode BindExternFuncDefinition(ExternFuncSyntax node)
|
||||
{
|
||||
var parameters = new List<BoundFuncParameterNode>();
|
||||
|
||||
@@ -129,7 +129,7 @@ public sealed class Binder
|
||||
return new BoundExternFuncNode(node.Tokens, node.Namespace, node.Name, node.CallName, parameters, node.ReturnType);
|
||||
}
|
||||
|
||||
private BoundLocalFuncNode BindLocalFuncDefinition(LocalFuncNode node)
|
||||
private BoundLocalFuncNode BindLocalFuncDefinition(LocalFuncSyntax node)
|
||||
{
|
||||
_variables.Clear();
|
||||
_functionReturnType = node.ReturnType;
|
||||
@@ -147,7 +147,7 @@ public sealed class Binder
|
||||
return new BoundLocalFuncNode(node.Tokens, node.Namespace, node.Name, parameters, body, node.ReturnType, node.Exported);
|
||||
}
|
||||
|
||||
private BoundBlock BindBlock(BlockNode node)
|
||||
private BoundBlock BindBlock(BlockSyntax node)
|
||||
{
|
||||
var statements = new List<BoundStatementNode>();
|
||||
|
||||
@@ -159,40 +159,40 @@ public sealed class Binder
|
||||
return new BoundBlock(node.Tokens, statements);
|
||||
}
|
||||
|
||||
private BoundStatementNode BindStatement(StatementNode node)
|
||||
private BoundStatementNode BindStatement(StatementSyntax node)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
AssignmentNode statement => BindAssignment(statement),
|
||||
BreakNode statement => BindBreak(statement),
|
||||
ContinueNode statement => BindContinue(statement),
|
||||
IfNode statement => BindIf(statement),
|
||||
ReturnNode statement => BindReturn(statement),
|
||||
StatementExpressionNode statement => BindStatementExpression(statement),
|
||||
VariableDeclarationNode statement => BindVariableDeclaration(statement),
|
||||
WhileNode statement => BindWhile(statement),
|
||||
AssignmentSyntax statement => BindAssignment(statement),
|
||||
BreakSyntax statement => BindBreak(statement),
|
||||
ContinueSyntax statement => BindContinue(statement),
|
||||
IfSyntax statement => BindIf(statement),
|
||||
ReturnSyntax statement => BindReturn(statement),
|
||||
StatementExpressionSyntax statement => BindStatementExpression(statement),
|
||||
VariableDeclarationSyntax statement => BindVariableDeclaration(statement),
|
||||
WhileSyntax statement => BindWhile(statement),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private BoundStatementNode BindAssignment(AssignmentNode statement)
|
||||
private BoundStatementNode BindAssignment(AssignmentSyntax statement)
|
||||
{
|
||||
var expression = BindExpression(statement.Target);
|
||||
var value = BindExpression(statement.Value, expression.Type);
|
||||
return new BoundAssignmentNode(statement.Tokens, expression, value);
|
||||
}
|
||||
|
||||
private BoundBreakNode BindBreak(BreakNode statement)
|
||||
private BoundBreakNode BindBreak(BreakSyntax statement)
|
||||
{
|
||||
return new BoundBreakNode(statement.Tokens);
|
||||
}
|
||||
|
||||
private BoundContinueNode BindContinue(ContinueNode statement)
|
||||
private BoundContinueNode BindContinue(ContinueSyntax statement)
|
||||
{
|
||||
return new BoundContinueNode(statement.Tokens);
|
||||
}
|
||||
|
||||
private BoundIfNode BindIf(IfNode statement)
|
||||
private BoundIfNode BindIf(IfSyntax statement)
|
||||
{
|
||||
var elseStatement = Optional.Empty<Variant<BoundIfNode, BoundBlock>>();
|
||||
|
||||
@@ -208,7 +208,7 @@ public sealed class Binder
|
||||
return new BoundIfNode(statement.Tokens, BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body), elseStatement);
|
||||
}
|
||||
|
||||
private BoundReturnNode BindReturn(ReturnNode statement)
|
||||
private BoundReturnNode BindReturn(ReturnSyntax statement)
|
||||
{
|
||||
var value = Optional.Empty<BoundExpressionNode>();
|
||||
|
||||
@@ -220,12 +220,12 @@ public sealed class Binder
|
||||
return new BoundReturnNode(statement.Tokens, value);
|
||||
}
|
||||
|
||||
private BoundStatementExpressionNode BindStatementExpression(StatementExpressionNode statement)
|
||||
private BoundStatementExpressionNode BindStatementExpression(StatementExpressionSyntax statement)
|
||||
{
|
||||
return new BoundStatementExpressionNode(statement.Tokens, BindExpression(statement.Expression));
|
||||
}
|
||||
|
||||
private BoundVariableDeclarationNode BindVariableDeclaration(VariableDeclarationNode statement)
|
||||
private BoundVariableDeclarationNode BindVariableDeclaration(VariableDeclarationSyntax statement)
|
||||
{
|
||||
NubType? type = null;
|
||||
|
||||
@@ -252,38 +252,38 @@ public sealed class Binder
|
||||
return new BoundVariableDeclarationNode(statement.Tokens, statement.Name, assignment, type);
|
||||
}
|
||||
|
||||
private BoundWhileNode BindWhile(WhileNode statement)
|
||||
private BoundWhileNode BindWhile(WhileSyntax statement)
|
||||
{
|
||||
return new BoundWhileNode(statement.Tokens, BindExpression(statement.Condition, new NubPrimitiveType(PrimitiveTypeKind.Bool)), BindBlock(statement.Body));
|
||||
}
|
||||
|
||||
private BoundExpressionNode BindExpression(ExpressionNode node, NubType? expectedType = null)
|
||||
private BoundExpressionNode BindExpression(ExpressionSyntax node, NubType? expectedType = null)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
AddressOfNode expression => BindAddressOf(expression),
|
||||
AnonymousFuncNode expression => BindAnonymousFunc(expression),
|
||||
ArrayIndexAccessNode expression => BindArrayIndexAccess(expression),
|
||||
ArrayInitializerNode expression => BindArrayInitializer(expression),
|
||||
BinaryExpressionNode expression => BindBinaryExpression(expression),
|
||||
DereferenceNode expression => BindDereference(expression),
|
||||
FuncCallNode expression => BindFuncCall(expression),
|
||||
IdentifierNode expression => BindIdentifier(expression),
|
||||
LiteralNode expression => BindLiteral(expression, expectedType),
|
||||
MemberAccessNode expression => BindMemberAccess(expression),
|
||||
StructInitializerNode expression => BindStructInitializer(expression),
|
||||
Node_UnaryExpressionNode expression => BindUnaryExpression(expression),
|
||||
AddressOfSyntax expression => BindAddressOf(expression),
|
||||
AnonymousFuncSyntax expression => BindAnonymousFunc(expression),
|
||||
ArrayIndexAccessSyntax expression => BindArrayIndexAccess(expression),
|
||||
ArrayInitializerSyntax expression => BindArrayInitializer(expression),
|
||||
BinaryExpressionSyntax expression => BindBinaryExpression(expression),
|
||||
DereferenceSyntax expression => BindDereference(expression),
|
||||
FuncCallSyntax expression => BindFuncCall(expression),
|
||||
IdentifierSyntax expression => BindIdentifier(expression),
|
||||
LiteralSyntax expression => BindLiteral(expression, expectedType),
|
||||
MemberAccessSyntax expression => BindMemberAccess(expression),
|
||||
StructInitializerSyntax expression => BindStructInitializer(expression),
|
||||
UnaryExpressionSyntax expression => BindUnaryExpression(expression),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private BoundAddressOfNode BindAddressOf(AddressOfNode expression)
|
||||
private BoundAddressOfNode BindAddressOf(AddressOfSyntax expression)
|
||||
{
|
||||
var inner = BindExpression(expression.Expression);
|
||||
return new BoundAddressOfNode(expression.Tokens, new NubPointerType(inner.Type), inner);
|
||||
}
|
||||
|
||||
private BoundAnonymousFuncNode BindAnonymousFunc(AnonymousFuncNode expression)
|
||||
private BoundAnonymousFuncNode BindAnonymousFunc(AnonymousFuncSyntax expression)
|
||||
{
|
||||
var parameters = new List<BoundFuncParameterNode>();
|
||||
|
||||
@@ -297,33 +297,33 @@ public sealed class Binder
|
||||
return new BoundAnonymousFuncNode(expression.Tokens, new NubFuncType(expression.ReturnType, parameters.Select(x => x.Type).ToList()), parameters, body, expression.ReturnType);
|
||||
}
|
||||
|
||||
private BoundArrayIndexAccessNode BindArrayIndexAccess(ArrayIndexAccessNode expression)
|
||||
private BoundArrayIndexAccessNode BindArrayIndexAccess(ArrayIndexAccessSyntax expression)
|
||||
{
|
||||
var boundArray = BindExpression(expression.Target);
|
||||
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)
|
||||
private BoundArrayInitializerNode BindArrayInitializer(ArrayInitializerSyntax expression)
|
||||
{
|
||||
return new BoundArrayInitializerNode(expression.Tokens, new NubArrayType(expression.ElementType), BindExpression(expression.Capacity, new NubPrimitiveType(PrimitiveTypeKind.U64)), expression.ElementType);
|
||||
}
|
||||
|
||||
private BoundBinaryExpressionNode BindBinaryExpression(BinaryExpressionNode expression)
|
||||
private BoundBinaryExpressionNode BindBinaryExpression(BinaryExpressionSyntax expression)
|
||||
{
|
||||
var boundLeft = BindExpression(expression.Left);
|
||||
var boundRight = BindExpression(expression.Right, boundLeft.Type);
|
||||
return new BoundBinaryExpressionNode(expression.Tokens, boundLeft.Type, boundLeft, expression.Operator, boundRight);
|
||||
return new BoundBinaryExpressionNode(expression.Tokens, boundLeft.Type, boundLeft, BindBinaryOperator(expression.Operator), boundRight);
|
||||
}
|
||||
|
||||
private BoundDereferenceNode BindDereference(DereferenceNode expression)
|
||||
private BoundDereferenceNode BindDereference(DereferenceSyntax expression)
|
||||
{
|
||||
var boundExpression = BindExpression(expression.Expression);
|
||||
var dereferencedType = ((NubPointerType)boundExpression.Type).BaseType;
|
||||
return new BoundDereferenceNode(expression.Tokens, dereferencedType, boundExpression);
|
||||
}
|
||||
|
||||
private BoundFuncCallNode BindFuncCall(FuncCallNode expression)
|
||||
private BoundFuncCallNode BindFuncCall(FuncCallSyntax expression)
|
||||
{
|
||||
var boundExpression = BindExpression(expression.Expression);
|
||||
|
||||
@@ -346,7 +346,7 @@ public sealed class Binder
|
||||
return new BoundFuncCallNode(expression.Tokens, funcType.ReturnType, boundExpression, parameters);
|
||||
}
|
||||
|
||||
private BoundExpressionNode BindIdentifier(IdentifierNode expression)
|
||||
private BoundExpressionNode BindIdentifier(IdentifierSyntax expression)
|
||||
{
|
||||
var @namespace = expression.Namespace.Or(_syntaxTree.Namespace);
|
||||
var localFuncs = _definitionTable.LookupLocalFunc(@namespace, expression.Name).ToArray();
|
||||
@@ -385,7 +385,7 @@ 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, NubType? expectedType = null)
|
||||
private BoundLiteralNode BindLiteral(LiteralSyntax expression, NubType? expectedType = null)
|
||||
{
|
||||
var type = expectedType ?? expression.Kind switch
|
||||
{
|
||||
@@ -399,7 +399,7 @@ public sealed class Binder
|
||||
return new BoundLiteralNode(expression.Tokens, type, expression.Literal, expression.Kind);
|
||||
}
|
||||
|
||||
private BoundExpressionNode BindMemberAccess(MemberAccessNode expression)
|
||||
private BoundExpressionNode BindMemberAccess(MemberAccessSyntax expression)
|
||||
{
|
||||
var boundExpression = BindExpression(expression.Target);
|
||||
|
||||
@@ -472,7 +472,7 @@ public sealed class Binder
|
||||
throw new BindException(Diagnostic.Error($"{boundExpression.Type} does not have a member with the name {expression.Member}").Build());
|
||||
}
|
||||
|
||||
private BoundStructInitializerNode BindStructInitializer(StructInitializerNode expression)
|
||||
private BoundStructInitializerNode BindStructInitializer(StructInitializerSyntax expression)
|
||||
{
|
||||
if (expression.StructType is not NubCustomType structType)
|
||||
{
|
||||
@@ -515,7 +515,7 @@ public sealed class Binder
|
||||
return new BoundStructInitializerNode(expression.Tokens, structType, new NubCustomType(@struct.Namespace, @struct.Name), initializers);
|
||||
}
|
||||
|
||||
private BoundUnaryExpressionNode BindUnaryExpression(Node_UnaryExpressionNode expression)
|
||||
private BoundUnaryExpressionNode BindUnaryExpression(UnaryExpressionSyntax expression)
|
||||
{
|
||||
var boundOperand = BindExpression(expression.Operand);
|
||||
|
||||
@@ -523,7 +523,7 @@ public sealed class Binder
|
||||
|
||||
switch (expression.Operator)
|
||||
{
|
||||
case UnaryExpressionOperator.Negate:
|
||||
case UnaryOperator.Negate:
|
||||
{
|
||||
boundOperand = BindExpression(expression.Operand, new NubPrimitiveType(PrimitiveTypeKind.I64));
|
||||
|
||||
@@ -534,7 +534,7 @@ public sealed class Binder
|
||||
|
||||
break;
|
||||
}
|
||||
case UnaryExpressionOperator.Invert:
|
||||
case UnaryOperator.Invert:
|
||||
{
|
||||
boundOperand = BindExpression(expression.Operand, new NubPrimitiveType(PrimitiveTypeKind.Bool));
|
||||
|
||||
@@ -548,7 +548,35 @@ public sealed class Binder
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
}
|
||||
|
||||
return new BoundUnaryExpressionNode(expression.Tokens, type, expression.Operator, boundOperand);
|
||||
return new BoundUnaryExpressionNode(expression.Tokens, type, BindBinaryOperator(expression.Operator), boundOperand);
|
||||
}
|
||||
|
||||
private BoundBinaryOperator BindBinaryOperator(BinaryOperator op)
|
||||
{
|
||||
return op switch
|
||||
{
|
||||
BinaryOperator.Equal => BoundBinaryOperator.Equal,
|
||||
BinaryOperator.NotEqual => BoundBinaryOperator.NotEqual,
|
||||
BinaryOperator.GreaterThan => BoundBinaryOperator.GreaterThan,
|
||||
BinaryOperator.GreaterThanOrEqual => BoundBinaryOperator.GreaterThanOrEqual,
|
||||
BinaryOperator.LessThan => BoundBinaryOperator.LessThan,
|
||||
BinaryOperator.LessThanOrEqual => BoundBinaryOperator.LessThanOrEqual,
|
||||
BinaryOperator.Plus => BoundBinaryOperator.Plus,
|
||||
BinaryOperator.Minus => BoundBinaryOperator.Minus,
|
||||
BinaryOperator.Multiply => BoundBinaryOperator.Multiply,
|
||||
BinaryOperator.Divide => BoundBinaryOperator.Divide,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(op), op, null)
|
||||
};
|
||||
}
|
||||
|
||||
private BoundUnaryOperator BindBinaryOperator(UnaryOperator op)
|
||||
{
|
||||
return op switch
|
||||
{
|
||||
UnaryOperator.Negate => BoundUnaryOperator.Negate,
|
||||
UnaryOperator.Invert => BoundUnaryOperator.Invert,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(op), op, null)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user