Skip to content

Commit

Permalink
Changed binder signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Nov 4, 2023
1 parent 599b68d commit b57a3f0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
43 changes: 22 additions & 21 deletions src/Draco.Compiler/Internal/Binding/Binder_Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Draco.Compiler.Api.Diagnostics;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Binding.Tasks;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Diagnostics;
using Draco.Compiler.Internal.Solver;
Expand All @@ -23,7 +24,7 @@ internal partial class Binder
/// <param name="constraints">The constraints that has been collected during the binding process.</param>
/// <param name="diagnostics">The diagnostics produced during the process.</param>
/// <returns>The untyped expression for <paramref name="syntax"/>.</returns>
protected virtual BoundExpression BindExpression(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics) => syntax switch
protected virtual BindingTask<BoundExpression> BindExpression(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics) => syntax switch
{
// NOTE: The syntax error is already reported
UnexpectedExpressionSyntax => new BoundUnexpectedExpression(syntax),
Expand All @@ -48,7 +49,7 @@ internal partial class Binder
_ => throw new ArgumentOutOfRangeException(nameof(syntax)),
};

private BoundExpression BindStatementExpression(StatementExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindStatementExpression(StatementExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
// We just desugar stmt; into { stmt; }
var stmt = this.BindStatement(syntax.Statement, constraints, diagnostics);
Expand All @@ -59,7 +60,7 @@ private BoundExpression BindStatementExpression(StatementExpressionSyntax syntax
value: BoundUnitExpression.Default);
}

private BoundExpression BindLiteralExpression(LiteralExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindLiteralExpression(LiteralExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
if (!BinderFacts.TryGetLiteralType(syntax.Literal.Value, this.IntrinsicSymbols, out var literalType))
{
Expand All @@ -68,7 +69,7 @@ private BoundExpression BindLiteralExpression(LiteralExpressionSyntax syntax, Co
return new BoundLiteralExpression(syntax, syntax.Literal.Value, literalType);
}

private BoundExpression BindStringExpression(StringExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindStringExpression(StringExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var lastNewline = true;
var cutoff = SyntaxFacts.ComputeCutoff(syntax);
Expand Down Expand Up @@ -107,15 +108,15 @@ private BoundExpression BindStringExpression(StringExpressionSyntax syntax, Cons
return new BoundStringExpression(syntax, parts.ToImmutable(), this.IntrinsicSymbols.String);
}

private BoundExpression BindNameExpression(NameExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindNameExpression(NameExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var symbol = BinderFacts.SyntaxMustNotReferenceTypes(syntax)
? this.LookupNonTypeValueSymbol(syntax.Name.Text, syntax, diagnostics)
: this.LookupValueSymbol(syntax.Name.Text, syntax, diagnostics);
return this.SymbolToExpression(syntax, symbol, constraints, diagnostics);
}

private BoundExpression BindBlockExpression(BlockExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindBlockExpression(BlockExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var binder = this.GetBinder(syntax);
var locals = binder.DeclaredSymbols
Expand All @@ -130,13 +131,13 @@ private BoundExpression BindBlockExpression(BlockExpressionSyntax syntax, Constr
return new BoundBlockExpression(syntax, locals, statements, value);
}

private BoundExpression BindGotoExpression(GotoExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindGotoExpression(GotoExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var target = (LabelSymbol)this.BindLabel(syntax.Target, constraints, diagnostics);
return new BoundGotoExpression(syntax, target);
}

private BoundExpression BindReturnExpression(ReturnExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindReturnExpression(ReturnExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var value = syntax.Value is null
? BoundUnitExpression.Default
Expand All @@ -147,7 +148,7 @@ private BoundExpression BindReturnExpression(ReturnExpressionSyntax syntax, Cons
return new BoundReturnExpression(syntax, value);
}

private BoundExpression BindIfExpression(IfExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindIfExpression(IfExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var condition = this.BindExpression(syntax.Condition, constraints, diagnostics);
// Condition must be bool
Expand Down Expand Up @@ -178,7 +179,7 @@ private BoundExpression BindIfExpression(IfExpressionSyntax syntax, ConstraintSo
return new BoundIfExpression(syntax, condition, then, @else, resultType);
}

private BoundExpression BindWhileExpression(WhileExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindWhileExpression(WhileExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var binder = this.GetBinder(syntax);

Expand All @@ -201,7 +202,7 @@ private BoundExpression BindWhileExpression(WhileExpressionSyntax syntax, Constr
return new BoundWhileExpression(syntax, condition, then, continueLabel, breakLabel);
}

private BoundExpression BindForExpression(ForExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindForExpression(ForExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var binder = this.GetBinder(syntax);

Expand Down Expand Up @@ -332,7 +333,7 @@ private BoundExpression BindForExpression(ForExpressionSyntax syntax, Constraint
return new BoundDelayedExpression(syntax, exprPromise, IntrinsicSymbols.Unit);
}

private BoundExpression BindCallExpression(CallExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindCallExpression(CallExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var method = this.BindExpression(syntax.Function, constraints, diagnostics);
var args = syntax.ArgumentList.Values
Expand All @@ -342,7 +343,7 @@ private BoundExpression BindCallExpression(CallExpressionSyntax syntax, Constrai
return this.BindCallExpression(syntax, method, args, constraints, diagnostics);
}

private BoundExpression BindCallExpression(
private BindingTask<BoundExpression> BindCallExpression(
CallExpressionSyntax syntax,
BoundExpression method,
ImmutableArray<BoundExpression> args,
Expand Down Expand Up @@ -425,7 +426,7 @@ private BoundExpression BindCallExpression(
}
}

private BoundExpression BindUnaryExpression(UnaryExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindUnaryExpression(UnaryExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
// Get the unary operator symbol
var operatorName = FunctionSymbol.GetUnaryOperatorName(syntax.Operator.Kind);
Expand All @@ -443,7 +444,7 @@ private BoundExpression BindUnaryExpression(UnaryExpressionSyntax syntax, Constr
return new BoundUnaryExpression(syntax, symbolPromise, operand, resultType);
}

private BoundExpression BindBinaryExpression(BinaryExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindBinaryExpression(BinaryExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
if (syntax.Operator.Kind == TokenKind.Assign)
{
Expand Down Expand Up @@ -511,7 +512,7 @@ private BoundExpression BindBinaryExpression(BinaryExpressionSyntax syntax, Cons
}
}

private BoundExpression BindRelationalExpression(RelationalExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindRelationalExpression(RelationalExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var first = this.BindExpression(syntax.Left, constraints, diagnostics);
var comparisons = ImmutableArray.CreateBuilder<UntypedComparison>();
Expand All @@ -525,7 +526,7 @@ private BoundExpression BindRelationalExpression(RelationalExpressionSyntax synt
return new BoundRelationalExpression(syntax, first, comparisons.ToImmutable(), this.IntrinsicSymbols.Bool);
}

private UntypedComparison BindComparison(
private BindingTask<BoundComparison> BindComparison(
BoundExpression prev,
ComparisonElementSyntax syntax,
ConstraintSolver constraints,
Expand All @@ -547,10 +548,10 @@ private UntypedComparison BindComparison(
// For safety, we assume it has to be bool
constraints.SameType(this.IntrinsicSymbols.Bool, resultType, syntax.Operator);

return new UntypedComparison(syntax, symbolPromise, right);
return new BoundComparison(syntax, symbolPromise, right);
}

private BoundExpression BindMemberExpression(MemberExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindMemberExpression(MemberExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var left = this.BindExpression(syntax.Accessed, constraints, diagnostics);
var memberName = syntax.Member.Text;
Expand Down Expand Up @@ -586,7 +587,7 @@ private BoundExpression BindMemberExpression(MemberExpressionSyntax syntax, Cons
}
}

private BoundExpression BindIndexExpression(IndexExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindIndexExpression(IndexExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var receiver = this.BindExpression(syntax.Indexed, constraints, diagnostics);
if (receiver is BoundReferenceErrorExpression err)
Expand Down Expand Up @@ -629,7 +630,7 @@ private BoundExpression BindIndexExpression(IndexExpressionSyntax syntax, Constr
return new BoundIndexGetExpression(syntax, receiver, promise, args, returnType);
}

private BoundExpression BindGenericExpression(GenericExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundExpression> BindGenericExpression(GenericExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var instantiated = this.BindExpression(syntax.Instantiated, constraints, diagnostics);
var args = syntax.Arguments.Values
Expand Down
11 changes: 6 additions & 5 deletions src/Draco.Compiler/Internal/Binding/Binder_Lvalue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Draco.Compiler.Api.Diagnostics;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Binding.Tasks;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Diagnostics;
using Draco.Compiler.Internal.Solver;
Expand All @@ -22,7 +23,7 @@ internal partial class Binder
/// <param name="constraints">The constraints that has been collected during the binding process.</param>
/// <param name="diagnostics">The diagnostics produced during the process.</param>
/// <returns>The untyped lvalue for <paramref name="syntax"/>.</returns>
protected virtual BoundLvalue BindLvalue(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics) => syntax switch
protected virtual BindingTask<BoundLvalue> BindLvalue(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics) => syntax switch
{
// NOTE: The syntax error is already reported
UnexpectedExpressionSyntax => new BoundUnexpectedLvalue(syntax),
Expand All @@ -33,7 +34,7 @@ internal partial class Binder
_ => this.BindIllegalLvalue(syntax, constraints, diagnostics),
};

private BoundLvalue BindNameLvalue(NameExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundLvalue> BindNameLvalue(NameExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var symbol = this.LookupValueSymbol(syntax.Name.Text, syntax, diagnostics);
switch (symbol)
Expand All @@ -56,7 +57,7 @@ private BoundLvalue BindNameLvalue(NameExpressionSyntax syntax, ConstraintSolver
}
}

private BoundLvalue BindMemberLvalue(MemberExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundLvalue> BindMemberLvalue(MemberExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var left = this.BindExpression(syntax.Accessed, constraints, diagnostics);
var memberName = syntax.Member.Text;
Expand Down Expand Up @@ -88,7 +89,7 @@ private BoundLvalue BindMemberLvalue(MemberExpressionSyntax syntax, ConstraintSo
}
}

private BoundLvalue BindIllegalLvalue(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundLvalue> BindIllegalLvalue(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
// TODO: Should illegal lvalues contain an expression we still bind?
// It could result in more errors within the expression, which might be useful
Expand All @@ -98,7 +99,7 @@ private BoundLvalue BindIllegalLvalue(SyntaxNode syntax, ConstraintSolver constr
return new BoundIllegalLvalue(syntax);
}

private BoundLvalue BindIndexLvalue(IndexExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundLvalue> BindIndexLvalue(IndexExpressionSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var receiver = this.BindExpression(syntax.Indexed, constraints, diagnostics);
if (receiver is BoundReferenceErrorExpression err)
Expand Down
15 changes: 8 additions & 7 deletions src/Draco.Compiler/Internal/Binding/Binder_Statement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using System.Linq;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Binding.Tasks;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Diagnostics;
using Draco.Compiler.Internal.Solver;
Expand All @@ -19,7 +20,7 @@ internal partial class Binder
/// <param name="constraints">The constraints that has been collected during the binding process.</param>
/// <param name="diagnostics">The diagnostics produced during the process.</param>
/// <returns>The untyped statement for <paramref name="syntax"/>.</returns>
protected virtual BoundStatement BindStatement(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics) => syntax switch
protected virtual BindingTask<BoundStatement> BindStatement(SyntaxNode syntax, ConstraintSolver constraints, DiagnosticBag diagnostics) => syntax switch
{
// NOTE: The syntax error is already reported
UnexpectedFunctionBodySyntax or UnexpectedStatementSyntax => new BoundUnexpectedStatement(syntax),
Expand All @@ -35,21 +36,21 @@ internal partial class Binder
_ => throw new System.ArgumentOutOfRangeException(nameof(syntax)),
};

private BoundStatement BindFunctionDeclaration(FunctionDeclarationSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundStatement> BindFunctionDeclaration(FunctionDeclarationSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var symbol = this.DeclaredSymbols
.OfType<SourceFunctionSymbol>()
.First(s => s.DeclaringSyntax == syntax);
return new UntypedLocalFunction(syntax, symbol);
}

private BoundStatement BindExpressionStatement(ExpressionStatementSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundStatement> BindExpressionStatement(ExpressionStatementSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var expr = this.BindExpression(syntax.Expression, constraints, diagnostics);
return new BoundExpressionStatement(syntax, expr);
}

private BoundStatement BindBlockFunctionBody(BlockFunctionBodySyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundStatement> BindBlockFunctionBody(BlockFunctionBodySyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var binder = this.GetBinder(syntax);
var locals = binder.DeclaredSymbols
Expand All @@ -73,7 +74,7 @@ private BoundStatement BindBlockFunctionBody(BlockFunctionBodySyntax syntax, Con
new BoundBlockExpression(syntax, locals, statements.ToImmutable(), BoundUnitExpression.Default));
}

private BoundStatement BindInlineFunctionBody(InlineFunctionBodySyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundStatement> BindInlineFunctionBody(InlineFunctionBodySyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
var binder = this.GetBinder(syntax);
var value = binder.BindExpression(syntax.Value, constraints, diagnostics);
Expand All @@ -83,7 +84,7 @@ private BoundStatement BindInlineFunctionBody(InlineFunctionBodySyntax syntax, C
return new BoundExpressionStatement(syntax, new BoundReturnExpression(syntax, value));
}

private BoundStatement BindLabelStatement(LabelDeclarationSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundStatement> BindLabelStatement(LabelDeclarationSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
// Look up the corresponding symbol defined
var labelSymbol = this.DeclaredSymbols
Expand All @@ -93,7 +94,7 @@ private BoundStatement BindLabelStatement(LabelDeclarationSyntax syntax, Constra
return new BoundLabelStatement(syntax, labelSymbol);
}

private BoundStatement BindVariableDeclaration(VariableDeclarationSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
private BindingTask<BoundStatement> BindVariableDeclaration(VariableDeclarationSyntax syntax, ConstraintSolver constraints, DiagnosticBag diagnostics)
{
// Look up the corresponding symbol defined
var localSymbol = this.DeclaredSymbols
Expand Down

0 comments on commit b57a3f0

Please sign in to comment.