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

Fix postifix operator #647

Merged
merged 1 commit into from
Sep 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
IL_0002: add
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: ret
IL_0005: pop
IL_0006: ldloc.0
IL_0007: ldc.i4.1
IL_0008: sub
IL_0009: pop
IL_000a: ldloc.0
IL_000b: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
IL_0004: add
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: stloc.0
IL_0008: ldc.i4.0
IL_0009: ret
IL_0007: pop
IL_0008: ldloc.0
IL_0009: ldc.i4.1
IL_000a: sub
IL_000b: stloc.0
IL_000c: ldc.i4.0
IL_000d: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
IL_0003: mul
IL_0004: add
IL_0005: starg.s x
IL_0007: ret
IL_0007: ldarg.0
IL_0008: pop
IL_0009: ldarg.0
IL_000a: ldc.i4.1
IL_000b: sub
IL_000c: pop
IL_000d: ret
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
IL_0008: mul
IL_0009: add
IL_000a: stloc.0
IL_000b: ret
IL_000b: ldloc.0
IL_000c: pop
IL_000d: ldloc.0
IL_000e: ldc.i4.1
IL_000f: sub
IL_0010: pop
IL_0011: ret
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ public IExpression Lower(IDeclarationScope scope)
throw new CompilationException($"'{_prefixOperator.Text}' needs l-value");
}

return new AssignmentExpression(
return new CommaExpression(new AssignmentExpression(
valueTarget,
AssignmentOperator.Assign,
newValueExpression
).Lower(scope);
).Lower(scope), new BinaryOperatorExpression(
target,
GetReverseOperator(),
new ConstantLiteralExpression(new IntegerConstant("1"))
));
}

public void EmitTo(IEmitScope scope) => throw new AssertException("Should be lowered");
Expand All @@ -53,4 +57,11 @@ public IExpression Lower(IDeclarationScope scope)
CTokenType.Decrement => BinaryOperator.Subtract,
_ => throw new AssertException($"Token type {token.Kind} is invalid"),
};

private BinaryOperator GetReverseOperator() => _operator switch
{
BinaryOperator.Add => BinaryOperator.Subtract,
BinaryOperator.Subtract => BinaryOperator.Add,
_ => throw new AssertException($"Operator {_operator} is invalid"),
};
}
4 changes: 4 additions & 0 deletions Cesium.IntegrationTests/value_assignments.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ int main()

int j = i++;

int a = 10;
if (a++ != 10) return -1;
if (++a != 12) return -2;

return i += 41;
}
Loading