Skip to content

Commit

Permalink
Fix postifix operator
Browse files Browse the repository at this point in the history
- Add integration test for validation

Right now I use inefficient codegen, to make it more efficient we need introduce new node, and lowering.

Closes #646
  • Loading branch information
kant2002 committed Sep 7, 2024
1 parent d842fea commit 89fe1d1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
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;
}

0 comments on commit 89fe1d1

Please sign in to comment.