Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing side effects not triggered in binary expression #102

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions samples/Fibonacci.Loop/Fibonacci.Loop.tdl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ int fibonacci(int i) {
let a = 1;
let b = 1;

while i > 2 {
--i
while i-- > 2 {
const c = a + b;
a = b;
b = c;
Expand Down
2 changes: 1 addition & 1 deletion samples/Fibonacci.Loop/Fibonacci.Loop.tdlproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
6 changes: 3 additions & 3 deletions src/Todl.Compiler.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ internal static TBoundMember BindMember<TBoundMember>(

internal static void EmitExpressionAndVerify(string input, params TestInstruction[] expectedInstructions)
{
var boundExpression = BindExpression<BoundExpression>(input);
boundExpression.GetDiagnostics().Should().BeEmpty();
var boundExpressionStatement = BindStatement<BoundExpressionStatement>(input);
boundExpressionStatement.GetDiagnostics().Should().BeEmpty();

var emitter = new TestEmitter();
emitter.EmitExpression(boundExpression);
emitter.EmitStatement(boundExpressionStatement);
emitter.Emit();

emitter.ILProcessor.Body.Instructions.ShouldHaveExactInstructionSequence(expectedInstructions);
Expand Down
10 changes: 5 additions & 5 deletions src/Todl.Compiler/CodeGeneration/Emitter.Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal partial class InstructionEmitter
&& m.GetParameters().Length == 2
&& m.GetParameters()[0].ParameterType.Equals(typeof(string)));

public void EmitExpression(BoundExpression boundExpression, bool emitSideEffect = false)
public void EmitExpression(BoundExpression boundExpression)
{
switch (boundExpression)
{
Expand All @@ -37,7 +37,7 @@ public void EmitExpression(BoundExpression boundExpression, bool emitSideEffect
EmitTodlFunctionCallExpression(boundTodlFunctionCallExpression);
return;
case BoundUnaryExpression boundUnaryExpression:
EmitUnaryExpression(boundUnaryExpression, emitSideEffect);
EmitUnaryExpression(boundUnaryExpression, true);
return;
case BoundBinaryExpression boundBinaryExpression:
EmitBinaryExpression(boundBinaryExpression);
Expand Down Expand Up @@ -274,7 +274,7 @@ private void EmitTodlFunctionCallExpression(BoundTodlFunctionCallExpression boun
{
foreach (var argument in boundTodlFunctionCallExpression.BoundArguments.Values)
{
EmitExpression(argument, true);
EmitExpression(argument);
}

var methodReference = ResolveMethodReference(boundTodlFunctionCallExpression);
Expand Down Expand Up @@ -418,7 +418,7 @@ private void EmitStore(BoundExpression left, Action assignmentAction)
if (left is BoundMemberAccessExpression boundMemberAccessExpression
&& !boundMemberAccessExpression.IsStatic)
{
EmitExpression(boundMemberAccessExpression.BoundBaseExpression, true);
EmitExpression(boundMemberAccessExpression.BoundBaseExpression);
}

assignmentAction();
Expand Down Expand Up @@ -489,7 +489,7 @@ private void EmitAssignmentExpression(BoundAssignmentExpression boundAssignmentE
{
EmitStore(boundAssignmentExpression.Left, () =>
{
EmitExpression(boundAssignmentExpression.Right, true);
EmitExpression(boundAssignmentExpression.Right);

switch (boundAssignmentExpression.Operator.BoundAssignmentOperatorKind)
{
Expand Down
16 changes: 14 additions & 2 deletions src/Todl.Compiler/CodeGeneration/Emitter.Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void EmitStatement(BoundStatement boundStatement)
EmitReturnStatement(boundReturnStatement);
return;
case BoundExpressionStatement boundExpressionStatement:
EmitExpression(boundExpressionStatement.Expression);
EmitExpressionStatement(boundExpressionStatement);
return;
case BoundConditionalStatement boundConditionalStatement:
EmitConditionalStatement(boundConditionalStatement);
Expand All @@ -35,6 +35,18 @@ public void EmitStatement(BoundStatement boundStatement)
}
}

private void EmitExpressionStatement(BoundExpressionStatement boundExpressionStatement)
{
// standalone a++ shouldn't trigger side effect
if (boundExpressionStatement.Expression is BoundUnaryExpression boundUnaryExpression)
{
EmitUnaryExpression(boundUnaryExpression, false);
return;
}

EmitExpression(boundExpressionStatement.Expression);
}

private void EmitBlockStatement(BoundBlockStatement boundBlockStatement)
{
foreach (var statement in boundBlockStatement.Statements)
Expand Down Expand Up @@ -84,7 +96,7 @@ private void EmitVariableDeclarationStatement(BoundVariableDeclarationStatement

if (boundVariableDeclarationStatement.InitializerExpression is not null)
{
EmitExpression(boundVariableDeclarationStatement.InitializerExpression, true);
EmitExpression(boundVariableDeclarationStatement.InitializerExpression);
EmitLocalStore(variableDefinition);
}
}
Expand Down
Loading