not working
This commit is contained in:
@@ -1,21 +1,23 @@
|
||||
using Common;
|
||||
using Syntax.Parsing;
|
||||
using Syntax.Parsing.Node;
|
||||
using Syntax.Typing;
|
||||
using Syntax.Typing.BoundNode;
|
||||
|
||||
namespace Syntax;
|
||||
|
||||
public class DefinitionTable
|
||||
{
|
||||
private readonly IEnumerable<CompilationUnit> _compilationUnits;
|
||||
private readonly IEnumerable<SyntaxTree> _syntaxTrees;
|
||||
|
||||
public DefinitionTable(IEnumerable<CompilationUnit> compilationUnits)
|
||||
public DefinitionTable(IEnumerable<SyntaxTree> syntaxTrees)
|
||||
{
|
||||
_compilationUnits = compilationUnits;
|
||||
_syntaxTrees = syntaxTrees;
|
||||
}
|
||||
|
||||
public Optional<IFuncSignature> LookupFunc(string @namespace, string name)
|
||||
{
|
||||
var definition = _compilationUnits
|
||||
var definition = _syntaxTrees
|
||||
.Where(c => c.Namespace == @namespace)
|
||||
.SelectMany(c => c.Definitions)
|
||||
.OfType<IFuncSignature>()
|
||||
@@ -26,7 +28,7 @@ public class DefinitionTable
|
||||
|
||||
public Optional<StructDefinitionNode> LookupStruct(string @namespace, string name)
|
||||
{
|
||||
var definition = _compilationUnits
|
||||
var definition = _syntaxTrees
|
||||
.Where(c => c.Namespace == @namespace)
|
||||
.SelectMany(c => c.Definitions)
|
||||
.OfType<StructDefinitionNode>()
|
||||
@@ -37,8 +39,47 @@ public class DefinitionTable
|
||||
|
||||
public IEnumerable<StructDefinitionNode> GetStructs()
|
||||
{
|
||||
return _compilationUnits
|
||||
return _syntaxTrees
|
||||
.SelectMany(c => c.Definitions)
|
||||
.OfType<StructDefinitionNode>();
|
||||
}
|
||||
}
|
||||
|
||||
public class BoundDefinitionTable
|
||||
{
|
||||
private readonly IEnumerable<BoundSyntaxTree> _syntaxTrees;
|
||||
|
||||
public BoundDefinitionTable(IEnumerable<BoundSyntaxTree> syntaxTrees)
|
||||
{
|
||||
_syntaxTrees = syntaxTrees;
|
||||
}
|
||||
|
||||
public Optional<IBoundFuncSignature> LookupFunc(string @namespace, string name)
|
||||
{
|
||||
var definition = _syntaxTrees
|
||||
.Where(c => c.Namespace == @namespace)
|
||||
.SelectMany(c => c.Definitions)
|
||||
.OfType<IBoundFuncSignature>()
|
||||
.SingleOrDefault(f => f.Name == name);
|
||||
|
||||
return Optional.OfNullable(definition);
|
||||
}
|
||||
|
||||
public Optional<BoundStructDefinitionNode> LookupStruct(string @namespace, string name)
|
||||
{
|
||||
var definition = _syntaxTrees
|
||||
.Where(c => c.Namespace == @namespace)
|
||||
.SelectMany(c => c.Definitions)
|
||||
.OfType<BoundStructDefinitionNode>()
|
||||
.SingleOrDefault(f => f.Name == name);
|
||||
|
||||
return Optional.OfNullable(definition);
|
||||
}
|
||||
|
||||
public IEnumerable<BoundStructDefinitionNode> GetStructs()
|
||||
{
|
||||
return _syntaxTrees
|
||||
.SelectMany(c => c.Definitions)
|
||||
.OfType<BoundStructDefinitionNode>();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using Syntax.Parsing.Node;
|
||||
|
||||
namespace Syntax.Parsing;
|
||||
|
||||
public class CompilationUnit(string @namespace, List<DefinitionNode> definitions)
|
||||
{
|
||||
public string Namespace { get; } = @namespace;
|
||||
public List<DefinitionNode> Definitions { get; } = definitions;
|
||||
}
|
||||
@@ -4,20 +4,9 @@ using Syntax.Typing;
|
||||
|
||||
namespace Syntax.Parsing.Node;
|
||||
|
||||
public abstract record ExpressionNode(IEnumerable<Token> Tokens) : Node(Tokens)
|
||||
{
|
||||
private NubType? _type;
|
||||
public NubType Type
|
||||
{
|
||||
get => _type ?? throw new Exception("Tried to access expression type before type was populated");
|
||||
set => _type = value;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract record ExpressionNode(IEnumerable<Token> Tokens) : Node(Tokens);
|
||||
public abstract record LValueNode(IEnumerable<Token> Tokens) : ExpressionNode(Tokens);
|
||||
|
||||
public record DereferenceNode(IEnumerable<Token> Tokens, ExpressionNode Expression) : LValueNode(Tokens);
|
||||
|
||||
public record BinaryExpressionNode(IEnumerable<Token> Tokens, ExpressionNode Left, BinaryExpressionOperator Operator, ExpressionNode Right) : ExpressionNode(Tokens);
|
||||
|
||||
public enum BinaryExpressionOperator
|
||||
@@ -52,3 +41,4 @@ public record FixedArrayInitializerNode(IEnumerable<Token> Tokens, NubType Eleme
|
||||
public record LiteralNode(IEnumerable<Token> Tokens, string Literal, LiteralKind Kind) : ExpressionNode(Tokens);
|
||||
public record MemberAccessNode(IEnumerable<Token> Tokens, ExpressionNode Expression, string Member) : LValueNode(Tokens);
|
||||
public record StructInitializerNode(IEnumerable<Token> Tokens, NubStructType StructType, Dictionary<string, ExpressionNode> Initializers) : ExpressionNode(Tokens);
|
||||
public record DereferenceNode(IEnumerable<Token> Tokens, ExpressionNode Expression) : LValueNode(Tokens);
|
||||
@@ -15,7 +15,7 @@ public static class Parser
|
||||
private static IEnumerable<Token> _tokens = [];
|
||||
private static int _index;
|
||||
|
||||
public static CompilationUnit? ParseFile(IEnumerable<Token> tokens, out IEnumerable<Diagnostic> diagnostics)
|
||||
public static SyntaxTree? ParseFile(IEnumerable<Token> tokens, string filePath, out IEnumerable<Diagnostic> diagnostics)
|
||||
{
|
||||
_tokens = tokens;
|
||||
_namespace = null!;
|
||||
@@ -46,7 +46,7 @@ public static class Parser
|
||||
}
|
||||
|
||||
diagnostics = _diagnostics;
|
||||
return new CompilationUnit(_namespace, definitions);
|
||||
return new SyntaxTree(filePath, _namespace, definitions);
|
||||
}
|
||||
catch (ParseException ex)
|
||||
{
|
||||
|
||||
5
src/Syntax/Parsing/SyntaxTree.cs
Normal file
5
src/Syntax/Parsing/SyntaxTree.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using Syntax.Parsing.Node;
|
||||
|
||||
namespace Syntax.Parsing;
|
||||
|
||||
public record SyntaxTree(string FilePath, string Namespace, List<DefinitionNode> Definitions);
|
||||
400
src/Syntax/Typing/Binder.cs
Normal file
400
src/Syntax/Typing/Binder.cs
Normal file
@@ -0,0 +1,400 @@
|
||||
using Common;
|
||||
using Syntax.Parsing;
|
||||
using Syntax.Parsing.Node;
|
||||
using Syntax.Typing.BoundNode;
|
||||
using UnaryExpressionNode = Syntax.Parsing.Node.UnaryExpressionNode;
|
||||
|
||||
namespace Syntax.Typing;
|
||||
|
||||
public static class Binder
|
||||
{
|
||||
private static SyntaxTree _syntaxTree = null!;
|
||||
private static DefinitionTable _definitionTable = null!;
|
||||
|
||||
private static Dictionary<string, NubType> _variables = new();
|
||||
|
||||
public static BoundSyntaxTree Bind(SyntaxTree syntaxTree, DefinitionTable definitionTable)
|
||||
{
|
||||
_syntaxTree = syntaxTree;
|
||||
_definitionTable = definitionTable;
|
||||
var definitions = new List<BoundDefinitionNode>();
|
||||
|
||||
foreach (var definition in syntaxTree.Definitions)
|
||||
{
|
||||
definitions.Add(BindDefinition(definition));
|
||||
}
|
||||
|
||||
return new BoundSyntaxTree(syntaxTree.FilePath, syntaxTree.Namespace, definitions);
|
||||
}
|
||||
|
||||
private static BoundDefinitionNode BindDefinition(DefinitionNode node)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
ExternFuncDefinitionNode definition => BindExternFuncDefinition(definition),
|
||||
LocalFuncDefinitionNode definition => BindLocalFuncDefinition(definition),
|
||||
StructDefinitionNode definition => BindStruct(definition),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private static BoundStructDefinitionNode BindStruct(StructDefinitionNode node)
|
||||
{
|
||||
var structFields = new List<BoundStructField>();
|
||||
|
||||
foreach (var structField in node.Fields)
|
||||
{
|
||||
var value = Optional.Empty<BoundExpressionNode>();
|
||||
|
||||
if (structField.Value.HasValue)
|
||||
{
|
||||
value = BindExpression(structField.Value.Value);
|
||||
}
|
||||
|
||||
structFields.Add(new BoundStructField(structField.Name, structField.Type, value));
|
||||
}
|
||||
|
||||
return new BoundStructDefinitionNode(node.Tokens, node.Documentation, node.Namespace, node.Name, structFields);
|
||||
}
|
||||
|
||||
private static BoundExternFuncDefinitionNode BindExternFuncDefinition(ExternFuncDefinitionNode node)
|
||||
{
|
||||
var parameters = new List<BoundFuncParameter>();
|
||||
|
||||
foreach (var parameter in node.Parameters)
|
||||
{
|
||||
parameters.Add(new BoundFuncParameter(parameter.Name, parameter.Type));
|
||||
}
|
||||
|
||||
return new BoundExternFuncDefinitionNode(node.Tokens, node.Documentation, node.Namespace, node.Name, node.CallName, parameters, node.ReturnType);
|
||||
}
|
||||
|
||||
private static BoundLocalFuncDefinitionNode BindLocalFuncDefinition(LocalFuncDefinitionNode node)
|
||||
{
|
||||
_variables.Clear();
|
||||
|
||||
var parameters = new List<BoundFuncParameter>();
|
||||
|
||||
foreach (var parameter in node.Parameters)
|
||||
{
|
||||
parameters.Add(new BoundFuncParameter(parameter.Name, parameter.Type));
|
||||
_variables[parameter.Name] = parameter.Type;
|
||||
}
|
||||
|
||||
var body = BindBlock(node.Body);
|
||||
|
||||
return new BoundLocalFuncDefinitionNode(node.Tokens, node.Documentation, node.Namespace, node.Name, parameters, body, node.ReturnType, node.Exported);
|
||||
}
|
||||
|
||||
private static BoundBlockNode BindBlock(BlockNode node)
|
||||
{
|
||||
var statements = new List<BoundStatementNode>();
|
||||
|
||||
foreach (var statement in node.Statements)
|
||||
{
|
||||
statements.Add(BindStatement(statement));
|
||||
}
|
||||
|
||||
return new BoundBlockNode(node.Tokens, statements);
|
||||
}
|
||||
|
||||
private static BoundStatementNode BindStatement(StatementNode node)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
ArrayIndexAssignmentNode statement => BindArrayIndex(statement),
|
||||
BreakNode statement => BindBreak(statement),
|
||||
ContinueNode statement => BindContinue(statement),
|
||||
DereferenceAssignmentNode statement => BindDereferenceAssignment(statement),
|
||||
IfNode statement => BindIf(statement),
|
||||
MemberAssignmentNode statement => BindMemberAssignment(statement),
|
||||
ReturnNode statement => BindReturn(statement),
|
||||
StatementExpressionNode statement => BindStatementExpression(statement),
|
||||
VariableAssignmentNode statement => BindVariableAssignment(statement),
|
||||
VariableDeclarationNode statement => BindVariableDeclaration(statement),
|
||||
WhileNode statement => BindWhile(statement),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private static BoundArrayIndexAssignmentNode BindArrayIndex(ArrayIndexAssignmentNode statement)
|
||||
{
|
||||
return new BoundArrayIndexAssignmentNode(statement.Tokens, BindArrayIndexAccess(statement.ArrayIndexAccess), BindExpression(statement.Value));
|
||||
}
|
||||
|
||||
private static BoundBreakNode BindBreak(BreakNode statement)
|
||||
{
|
||||
return new BoundBreakNode(statement.Tokens);
|
||||
}
|
||||
|
||||
private static BoundContinueNode BindContinue(ContinueNode statement)
|
||||
{
|
||||
return new BoundContinueNode(statement.Tokens);
|
||||
}
|
||||
|
||||
private static BoundDereferenceAssignmentNode BindDereferenceAssignment(DereferenceAssignmentNode statement)
|
||||
{
|
||||
return new BoundDereferenceAssignmentNode(statement.Tokens, BindDereference(statement.Dereference), BindExpression(statement.Value));
|
||||
}
|
||||
|
||||
private static BoundIfNode BindIf(IfNode statement)
|
||||
{
|
||||
var elseStatement = Optional.Empty<Variant<BoundIfNode, BoundBlockNode>>();
|
||||
|
||||
if (statement.Else.HasValue)
|
||||
{
|
||||
elseStatement = statement.Else.Value.Match<Variant<BoundIfNode, BoundBlockNode>>
|
||||
(
|
||||
elseIf => BindIf(elseIf),
|
||||
@else => BindBlock(@else)
|
||||
);
|
||||
}
|
||||
|
||||
return new BoundIfNode(statement.Tokens, BindExpression(statement.Condition), BindBlock(statement.Body), elseStatement);
|
||||
}
|
||||
|
||||
private static BoundMemberAssignmentNode BindMemberAssignment(MemberAssignmentNode statement)
|
||||
{
|
||||
return new BoundMemberAssignmentNode(statement.Tokens, BindMemberAccess(statement.MemberAccess), BindExpression(statement.Value));
|
||||
}
|
||||
|
||||
private static BoundReturnNode BindReturn(ReturnNode statement)
|
||||
{
|
||||
var value = Optional.Empty<BoundExpressionNode>();
|
||||
|
||||
if (statement.Value.HasValue)
|
||||
{
|
||||
value = BindExpression(statement.Value.Value);
|
||||
}
|
||||
|
||||
return new BoundReturnNode(statement.Tokens, value);
|
||||
}
|
||||
|
||||
private static BoundStatementExpressionNode BindStatementExpression(StatementExpressionNode statement)
|
||||
{
|
||||
return new BoundStatementExpressionNode(statement.Tokens, BindExpression(statement.Expression));
|
||||
}
|
||||
|
||||
private static BoundVariableAssignmentNode BindVariableAssignment(VariableAssignmentNode statement)
|
||||
{
|
||||
return new BoundVariableAssignmentNode(statement.Tokens, BindIdentifier(statement.Identifier), BindExpression(statement.Value));
|
||||
}
|
||||
|
||||
private static BoundVariableDeclarationNode BindVariableDeclaration(VariableDeclarationNode statement)
|
||||
{
|
||||
_variables[statement.Name] = statement.Type;
|
||||
return new BoundVariableDeclarationNode(statement.Tokens, statement.Name, statement.Type);
|
||||
}
|
||||
|
||||
private static BoundWhileNode BindWhile(WhileNode statement)
|
||||
{
|
||||
return new BoundWhileNode(statement.Tokens, BindExpression(statement.Condition), BindBlock(statement.Body));
|
||||
}
|
||||
|
||||
private static BoundExpressionNode BindExpression(ExpressionNode node)
|
||||
{
|
||||
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),
|
||||
FixedArrayInitializerNode expression => BindFixedArrayInitializer(expression),
|
||||
FuncCallNode expression => BindFuncCall(expression),
|
||||
IdentifierNode expression => BindIdentifier(expression),
|
||||
LiteralNode expression => BindLiteral(expression),
|
||||
MemberAccessNode expression => BindMemberAccess(expression),
|
||||
StructInitializerNode expression => BindStructInitializer(expression),
|
||||
UnaryExpressionNode expression => BindUnaryExpression(expression),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private static BoundAddressOfNode BindAddressOf(AddressOfNode expression)
|
||||
{
|
||||
var inner = (BoundLValueNode)BindExpression(expression.Expression);
|
||||
return new BoundAddressOfNode(expression.Tokens, new NubPointerType(inner.Type), inner);
|
||||
}
|
||||
|
||||
private static BoundAnonymousFuncNode BindAnonymousFunc(AnonymousFuncNode expression)
|
||||
{
|
||||
var parameters = new List<BoundFuncParameter>();
|
||||
var parameterTypes = new List<NubType>();
|
||||
|
||||
foreach (var parameter in expression.Parameters)
|
||||
{
|
||||
var boundParameter = new BoundFuncParameter(parameter.Name, parameter.Type);
|
||||
parameters.Add(boundParameter);
|
||||
parameterTypes.Add(boundParameter.Type);
|
||||
}
|
||||
|
||||
var body = BindBlock(expression.Body);
|
||||
|
||||
return new BoundAnonymousFuncNode(expression.Tokens, new NubFuncType(expression.ReturnType, parameterTypes), parameters, body, expression.ReturnType);
|
||||
}
|
||||
|
||||
private static BoundArrayIndexAccessNode BindArrayIndexAccess(ArrayIndexAccessNode expression)
|
||||
{
|
||||
var boundArray = BindExpression(expression.Array);
|
||||
var elementType = ((NubArrayType)boundArray.Type).ElementType;
|
||||
return new BoundArrayIndexAccessNode(expression.Tokens, elementType, boundArray, BindExpression(expression.Index));
|
||||
}
|
||||
|
||||
private static BoundArrayInitializerNode BindArrayInitializer(ArrayInitializerNode expression)
|
||||
{
|
||||
return new BoundArrayInitializerNode(expression.Tokens, new NubArrayType(expression.ElementType), BindExpression(expression.Capacity), expression.ElementType);
|
||||
}
|
||||
|
||||
private static BoundBinaryExpressionNode BindBinaryExpression(BinaryExpressionNode expression)
|
||||
{
|
||||
var boundLeft = BindExpression(expression.Left);
|
||||
var boundRight = BindExpression(expression.Right);
|
||||
return new BoundBinaryExpressionNode(expression.Tokens, boundLeft.Type, boundLeft, expression.Operator, boundRight);
|
||||
}
|
||||
|
||||
private static BoundDereferenceNode BindDereference(DereferenceNode expression)
|
||||
{
|
||||
var boundExpression = BindExpression(expression.Expression);
|
||||
var dereferencedType = ((NubPointerType)boundExpression.Type).BaseType;
|
||||
return new BoundDereferenceNode(expression.Tokens, dereferencedType, boundExpression);
|
||||
}
|
||||
|
||||
private static BoundFixedArrayInitializerNode BindFixedArrayInitializer(FixedArrayInitializerNode expression)
|
||||
{
|
||||
return new BoundFixedArrayInitializerNode(expression.Tokens, new NubArrayType(expression.ElementType), expression.ElementType, expression.Capacity);
|
||||
}
|
||||
|
||||
private static BoundFuncCallNode BindFuncCall(FuncCallNode expression)
|
||||
{
|
||||
var boundExpression = BindExpression(expression.Expression);
|
||||
|
||||
var returnType = ((NubFuncType)boundExpression.Type).ReturnType;
|
||||
|
||||
var parameters = new List<BoundExpressionNode>();
|
||||
|
||||
foreach (var parameter in expression.Parameters)
|
||||
{
|
||||
parameters.Add(BindExpression(parameter));
|
||||
}
|
||||
|
||||
return new BoundFuncCallNode(expression.Tokens, returnType, boundExpression, parameters);
|
||||
}
|
||||
|
||||
private static BoundIdentifierNode BindIdentifier(IdentifierNode expression)
|
||||
{
|
||||
NubType? type = null;
|
||||
|
||||
var definition = _definitionTable.LookupFunc(expression.Namespace.Or(_syntaxTree.Namespace), expression.Name);
|
||||
if (definition.HasValue)
|
||||
{
|
||||
type = new NubFuncType(definition.Value.ReturnType, definition.Value.Parameters.Select(p => p.Type).ToList());
|
||||
}
|
||||
|
||||
if (type == null && !expression.Namespace.HasValue)
|
||||
{
|
||||
type = _variables[expression.Name];
|
||||
}
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
}
|
||||
|
||||
return new BoundIdentifierNode(expression.Tokens, type, expression.Namespace, expression.Name);
|
||||
}
|
||||
|
||||
private static BoundLiteralNode BindLiteral(LiteralNode expression)
|
||||
{
|
||||
throw new NotImplementedException("Literal requires context");
|
||||
}
|
||||
|
||||
private static BoundMemberAccessNode BindMemberAccess(MemberAccessNode expression)
|
||||
{
|
||||
var boundExpression = BindExpression(expression.Expression);
|
||||
|
||||
NubType? type = null;
|
||||
|
||||
switch (boundExpression.Type)
|
||||
{
|
||||
case NubArrayType:
|
||||
{
|
||||
if (expression.Member == "count")
|
||||
{
|
||||
type = NubPrimitiveType.I64;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case NubStructType structType:
|
||||
{
|
||||
var defOpt = _definitionTable.LookupStruct(structType.Namespace, structType.Name);
|
||||
if (!defOpt.TryGetValue(out var definition))
|
||||
{
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
}
|
||||
|
||||
var field = definition.Fields.FirstOrDefault(f => f.Name == expression.Member);
|
||||
if (field == null)
|
||||
{
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
}
|
||||
|
||||
type = field.Type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
}
|
||||
|
||||
return new BoundMemberAccessNode(expression.Tokens, type, boundExpression, expression.Member);
|
||||
}
|
||||
|
||||
private static BoundStructInitializerNode BindStructInitializer(StructInitializerNode expression)
|
||||
{
|
||||
var initializers = new Dictionary<string, BoundExpressionNode>();
|
||||
|
||||
foreach (var (member, initializer) in expression.Initializers)
|
||||
{
|
||||
initializers[member] = BindExpression(initializer);
|
||||
}
|
||||
|
||||
return new BoundStructInitializerNode(expression.Tokens, expression.StructType, expression.StructType, initializers);
|
||||
}
|
||||
|
||||
private static BoundUnaryExpressionNode BindUnaryExpression(UnaryExpressionNode expression)
|
||||
{
|
||||
var boundOperand = BindExpression(expression.Operand);
|
||||
|
||||
NubType? type = null;
|
||||
|
||||
switch (expression.Operator)
|
||||
{
|
||||
case UnaryExpressionOperator.Negate:
|
||||
{
|
||||
if (boundOperand.Type.IsNumber)
|
||||
{
|
||||
type = boundOperand.Type;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case UnaryExpressionOperator.Invert:
|
||||
{
|
||||
type = new NubPrimitiveType(PrimitiveTypeKind.Bool);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
}
|
||||
|
||||
return new BoundUnaryExpressionNode(expression.Tokens, type, expression.Operator, boundOperand);
|
||||
}
|
||||
}
|
||||
33
src/Syntax/Typing/BoundNode/Definition.cs
Normal file
33
src/Syntax/Typing/BoundNode/Definition.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Common;
|
||||
using Syntax.Tokenization;
|
||||
|
||||
namespace Syntax.Typing.BoundNode;
|
||||
|
||||
public abstract record BoundDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace) : BoundNode(Tokens);
|
||||
|
||||
public record BoundFuncParameter(string Name, NubType Type)
|
||||
{
|
||||
public override string ToString() => $"{Name}: {Type}";
|
||||
}
|
||||
|
||||
public interface IBoundFuncSignature
|
||||
{
|
||||
public string Name { get; }
|
||||
public List<BoundFuncParameter> Parameters { get; }
|
||||
public NubType ReturnType { get; }
|
||||
|
||||
public string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){": " + ReturnType}";
|
||||
}
|
||||
|
||||
public record BoundLocalFuncDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, List<BoundFuncParameter> Parameters, BoundBlockNode Body, NubType ReturnType, bool Exported) : BoundDefinitionNode(Tokens, Documentation, Namespace), IBoundFuncSignature
|
||||
{
|
||||
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){": " + ReturnType}";
|
||||
}
|
||||
|
||||
public record BoundExternFuncDefinitionNode(IEnumerable<Token> Tokens, Optional<string> Documentation, string Namespace, string Name, string CallName, List<BoundFuncParameter> Parameters, NubType ReturnType) : BoundDefinitionNode(Tokens, Documentation, Namespace), IBoundFuncSignature
|
||||
{
|
||||
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){": " + ReturnType}";
|
||||
}
|
||||
|
||||
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);
|
||||
21
src/Syntax/Typing/BoundNode/Expression.cs
Normal file
21
src/Syntax/Typing/BoundNode/Expression.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Common;
|
||||
using Syntax.Parsing.Node;
|
||||
using Syntax.Tokenization;
|
||||
|
||||
namespace Syntax.Typing.BoundNode;
|
||||
|
||||
public abstract record BoundExpressionNode(IEnumerable<Token> Tokens, NubType Type) : BoundNode(Tokens);
|
||||
public abstract record BoundLValueNode(IEnumerable<Token> Tokens, NubType Type) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundBinaryExpressionNode(IEnumerable<Token> Tokens, NubType Type, BoundExpressionNode Left, BinaryExpressionOperator Operator, BoundExpressionNode Right) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundUnaryExpressionNode(IEnumerable<Token> Tokens, NubType Type, UnaryExpressionOperator Operator, BoundExpressionNode Operand) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundFuncCallNode(IEnumerable<Token> Tokens, NubType Type, BoundExpressionNode Expression, List<BoundExpressionNode> Parameters) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundIdentifierNode(IEnumerable<Token> Tokens, NubType Type, Optional<string> Namespace, string Name) : BoundLValueNode(Tokens, Type);
|
||||
public record BoundArrayInitializerNode(IEnumerable<Token> Tokens, NubType Type, BoundExpressionNode Capacity, NubType ElementType) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundArrayIndexAccessNode(IEnumerable<Token> Tokens, NubType Type, BoundExpressionNode Array, BoundExpressionNode Index) : BoundLValueNode(Tokens, Type);
|
||||
public record BoundAnonymousFuncNode(IEnumerable<Token> Tokens, NubType Type, List<BoundFuncParameter> Parameters, BoundBlockNode Body, NubType ReturnType) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundAddressOfNode(IEnumerable<Token> Tokens, NubType Type, BoundLValueNode Expression) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundFixedArrayInitializerNode(IEnumerable<Token> Tokens, NubType Type, NubType ElementType, int Capacity) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundLiteralNode(IEnumerable<Token> Tokens, NubType Type, string Literal, LiteralKind Kind) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundMemberAccessNode(IEnumerable<Token> Tokens, NubType Type, BoundExpressionNode Expression, string Member) : BoundLValueNode(Tokens, Type);
|
||||
public record BoundStructInitializerNode(IEnumerable<Token> Tokens, NubType Type, NubStructType StructType, Dictionary<string, BoundExpressionNode> Initializers) : BoundExpressionNode(Tokens, Type);
|
||||
public record BoundDereferenceNode(IEnumerable<Token> Tokens, NubType Type, BoundExpressionNode Expression) : BoundLValueNode(Tokens, Type);
|
||||
6
src/Syntax/Typing/BoundNode/Node.cs
Normal file
6
src/Syntax/Typing/BoundNode/Node.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using Syntax.Tokenization;
|
||||
|
||||
namespace Syntax.Typing.BoundNode;
|
||||
|
||||
public abstract record BoundNode(IEnumerable<Token> Tokens);
|
||||
public record BoundBlockNode(IEnumerable<Token> Tokens, List<BoundStatementNode> Statements) : BoundNode(Tokens);
|
||||
17
src/Syntax/Typing/BoundNode/Statement.cs
Normal file
17
src/Syntax/Typing/BoundNode/Statement.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Common;
|
||||
using Syntax.Tokenization;
|
||||
|
||||
namespace Syntax.Typing.BoundNode;
|
||||
|
||||
public record BoundStatementNode(IEnumerable<Token> Tokens) : BoundNode(Tokens);
|
||||
public record BoundStatementExpressionNode(IEnumerable<Token> Tokens, BoundExpressionNode Expression) : BoundStatementNode(Tokens);
|
||||
public record BoundReturnNode(IEnumerable<Token> Tokens, Optional<BoundExpressionNode> Value) : BoundStatementNode(Tokens);
|
||||
public record BoundMemberAssignmentNode(IEnumerable<Token> Tokens, BoundMemberAccessNode MemberAccess, BoundExpressionNode Value) : BoundStatementNode(Tokens);
|
||||
public record BoundIfNode(IEnumerable<Token> Tokens, BoundExpressionNode Condition, BoundBlockNode Body, Optional<Variant<BoundIfNode, BoundBlockNode>> Else) : BoundStatementNode(Tokens);
|
||||
public record BoundDereferenceAssignmentNode(IEnumerable<Token> Tokens, BoundDereferenceNode Dereference, BoundExpressionNode Value) : BoundStatementNode(Tokens);
|
||||
public record BoundVariableAssignmentNode(IEnumerable<Token> Tokens, BoundIdentifierNode Identifier, BoundExpressionNode Value) : BoundStatementNode(Tokens);
|
||||
public record BoundVariableDeclarationNode(IEnumerable<Token> Tokens, string Name, NubType Type) : BoundStatementNode(Tokens);
|
||||
public record BoundContinueNode(IEnumerable<Token> Tokens) : BoundStatementNode(Tokens);
|
||||
public record BoundBreakNode(IEnumerable<Token> Tokens) : BoundStatementNode(Tokens);
|
||||
public record BoundArrayIndexAssignmentNode(IEnumerable<Token> Tokens, BoundArrayIndexAccessNode ArrayIndexAccess, BoundExpressionNode Value) : BoundStatementNode(Tokens);
|
||||
public record BoundWhileNode(IEnumerable<Token> Tokens, BoundExpressionNode Condition, BoundBlockNode Body) : BoundStatementNode(Tokens);
|
||||
5
src/Syntax/Typing/BoundSyntaxTree.cs
Normal file
5
src/Syntax/Typing/BoundSyntaxTree.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using Syntax.Typing.BoundNode;
|
||||
|
||||
namespace Syntax.Typing;
|
||||
|
||||
public record BoundSyntaxTree(string FilePath, string Namespace, List<BoundDefinitionNode> Definitions);
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user