Skip to content
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
189 changes: 189 additions & 0 deletions Coder.Test/Languages/CompoundIntegerDivisionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.Coder.Test.Languages;

using System.Globalization;
using System.Text;
using ktsu.Coder.Ast;
using ktsu.Coder.Languages;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Tests that integer <c>/=</c> and <c>%=</c> mean the same thing in Python and JavaScript as in the C
/// family.
/// </summary>
/// <remarks>
/// The compound-assignment counterpart of <see cref="IntegerDivisionTests"/>. Python's and JavaScript's
/// <c>/=</c> are true division, so <c>a = 5; a /= 2</c> leaves <c>2.5</c>, and Python's <c>%=</c> floors,
/// so <c>a = -7; a %= 2</c> leaves <c>1</c> where the C family leaves <c>-1</c>. The generated code is
/// run rather than read.
/// </remarks>
[TestClass]
public class CompoundIntegerDivisionTests
{
private static readonly (int Left, int Right)[] Operands = [(-7, 2), (7, -2), (-7, -2), (7, 2), (-6, 3), (0, -5), (5, 2)];

/// <summary>
/// The functions under test, each of which applies a compound assignment to an integer and returns
/// it: to a parameter, to a typed local, with a literal divisor, and with a divisor that is a call,
/// which must not be made twice.
/// </summary>
private static IEnumerable<FunctionDeclaration> Functions()
{
foreach (AssignmentOperator op in new[] { AssignmentOperator.DivideAssign, AssignmentOperator.ModuloAssign })
{
string name = op == AssignmentOperator.DivideAssign ? "div" : "mod";

FunctionDeclaration parameters = new($"{name}_params") { ReturnType = new TypeReference("int") };
parameters.Parameters.Add(new Parameter("a", "int"));
parameters.Parameters.Add(new Parameter("b", "int"));
parameters.Body.Add(new AssignmentStatement(new VariableReference("a"), new VariableReference("b"), op));
parameters.Body.Add(new ReturnStatement(new VariableReference("a")));
yield return parameters;

FunctionDeclaration local = new($"{name}_local") { ReturnType = new TypeReference("int") };
local.Parameters.Add(new Parameter("a", "int"));
local.Parameters.Add(new Parameter("b", "int"));
local.Body.Add(new VariableDeclaration("q", "int", new VariableReference("a")));
local.Body.Add(new AssignmentStatement(new VariableReference("q"), new VariableReference("b"), op));
local.Body.Add(new ReturnStatement(new VariableReference("q")));
yield return local;

FunctionDeclaration literal = new($"{name}_literal") { ReturnType = new TypeReference("int") };
literal.Parameters.Add(new Parameter("a", "int"));
literal.Parameters.Add(new Parameter("b", "int"));
literal.Body.Add(new AssignmentStatement(new VariableReference("a"), new LiteralExpression<int>(2), op));
literal.Body.Add(new ReturnStatement(new VariableReference("a")));
yield return literal;

FunctionDeclaration call = new($"{name}_call") { ReturnType = new TypeReference("int") };
call.Parameters.Add(new Parameter("a", "int"));
call.Parameters.Add(new Parameter("b", "int"));
CallExpression counted = new("counted") { ExpectedType = "int" };
counted.Arguments.Add(new VariableReference("b"));
call.Body.Add(new AssignmentStatement(new VariableReference("a"), counted, op));
call.Body.Add(new ReturnStatement(new VariableReference("a")));
yield return call;
}
}

private static int Expected(string function, int left, int right)
{
int divisor = function.EndsWith("_literal", StringComparison.Ordinal) ? 2 : right;
return function.StartsWith("div", StringComparison.Ordinal) ? left / divisor : left % divisor;
}

/// <summary>
/// Tests that Python computes the C family's quotient and remainder, as an <c>int</c>, and makes
/// the divisor's call once.
/// </summary>
[TestMethod]
public void Python_MatchesTheCFamily()
{
string? python = ToolchainHarness.FindOnPath("--version", "python3", "python");
if (python is null)
{
Assert.Inconclusive("No Python interpreter on the path, so nothing was run.");
return;
}

PythonGenerator generator = new();
StringBuilder module = new();
module.AppendLine("calls = 0");
module.AppendLine("def counted(x):");
module.AppendLine(" global calls");
module.AppendLine(" calls += 1");
module.AppendLine(" return x");
foreach (FunctionDeclaration function in Functions())
{
module.AppendLine(generator.Generate(function));
}

foreach (FunctionDeclaration function in Functions())
{
foreach ((int left, int right) in Operands)
{
string arguments = string.Create(CultureInfo.InvariantCulture, $"{left}, {right}");
int expected = Expected(function.Name!, left, right);
module.AppendLine("calls = 0");
module.AppendLine(CultureInfo.InvariantCulture, $"r = {function.Name}({arguments})");
module.AppendLine(CultureInfo.InvariantCulture, $"assert type(r) is int and r == {expected}, f\"{function.Name}({arguments}) = {{r!r}}, expected {expected}\"");
if (function.Name!.EndsWith("_call", StringComparison.Ordinal))
{
module.AppendLine(CultureInfo.InvariantCulture, $"assert calls == 1, f\"{function.Name}({arguments}) made {{calls}} calls\"");
}
}
}

Run(python, "check.py", module.ToString());
}

/// <summary>
/// Tests that JavaScript computes the C family's quotient and remainder, and makes the divisor's
/// call once.
/// </summary>
[TestMethod]
public void JavaScript_MatchesTheCFamily()
{
string? node = ToolchainHarness.FindOnPath("--version", "node");
if (node is null)
{
Assert.Inconclusive("No JavaScript runtime on the path, so nothing was run.");
return;
}

JavaScriptGenerator generator = new();
StringBuilder module = new();
module.AppendLine("let calls = 0;");
module.AppendLine("function counted(x) { calls++; return x; }");
foreach (FunctionDeclaration function in Functions())
{
module.AppendLine(generator.Generate(function));
}

foreach (FunctionDeclaration function in Functions())
{
foreach ((int left, int right) in Operands)
{
string arguments = string.Create(CultureInfo.InvariantCulture, $"{left}, {right}");
int expected = Expected(function.Name!, left, right);

// Strict equality rather than Object.is: JavaScript's -0 is an integer zero to every
// arithmetic and comparison a consumer would make of it.
module.AppendLine(CultureInfo.InvariantCulture, $"{{ calls = 0; const r = {function.Name}({arguments}); if (r !== {expected}) {{ throw new Error(`{function.Name}({arguments}) = ${{r}}, expected {expected}`); }} if (calls > 1) {{ throw new Error(`{function.Name}({arguments}) made ${{calls}} calls`); }} }}");
}
}

Run(node, "check.js", module.ToString());
}

/// <summary>
/// Tests that a compound assignment the AST does not know to be on integers, and every other
/// compound operator, is left as it is.
/// </summary>
/// <param name="language">The generator to ask.</param>
[TestMethod]
[DataRow("python")]
[DataRow("javascript")]
public void OtherCompoundAssignments_AreUnchanged(string language)
{
ILanguageGenerator generator = language == "python" ? new PythonGenerator() : new JavaScriptGenerator();

AssignmentStatement unknown = new(new VariableReference("a"), new VariableReference("b"), AssignmentOperator.DivideAssign);
AssignmentStatement doubles = new(new VariableReference("a"), new LiteralExpression<double>(2.0), AssignmentOperator.DivideAssign);
AssignmentStatement add = new(new VariableReference("a"), new LiteralExpression<int>(2), AssignmentOperator.AddAssign);
string end = language == "python" ? string.Empty : ";";

Assert.AreEqual($"a /= b{end}", generator.Generate(unknown).Trim());
Assert.AreEqual($"a /= 2.0{end}", generator.Generate(doubles).Trim());
Assert.AreEqual($"a += 2{end}", generator.Generate(add).Trim());
}

private static void Run(string runtime, string fileName, string source) =>
ToolchainHarness.InTemporaryDirectory(directory =>
{
File.WriteAllText(Path.Combine(directory, fileName), source);
(int exitCode, string output) = ToolchainHarness.Run(runtime, fileName, directory);
Assert.AreEqual(0, exitCode, $"{output}{Environment.NewLine}{source}");
});
}
27 changes: 27 additions & 0 deletions Coder/Languages/JavaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,33 @@ protected override void GenerateBinary(BinaryExpression binary, CodeBlocker code
code.Write(")");
}

/// <inheritdoc/>
/// <remarks>
/// <c>x /= y</c> is true division, the same as <c>/</c>, so on integers it gives <c>2.5</c> for
/// <c>5 /= 2</c> where the C family gives <c>2</c>. When both sides are known to be integers the
/// assignment is written out as <c>x = Math.trunc(x / y)</c> through <see cref="GenerateBinary"/>.
/// <c>%=</c> already matches the C family, and every other compound assignment is left as it is.
/// </remarks>
protected override void GenerateAssignmentStatement(AssignmentStatement assignment, CodeBlocker code)
{
Ensure.NotNull(assignment);
Ensure.NotNull(code);

BinaryExpression? binary = assignment.Operator == AssignmentOperator.DivideAssign
? new(assignment.Target, BinaryOperator.Divide, assignment.Value)
: null;
if (binary is null || !IsIntegerArithmetic(binary))
{
base.GenerateAssignmentStatement(assignment, code);
return;
}

GenerateInternal(assignment.Target, code);
code.Write(" = ");
GenerateBinary(binary, code);
EndStatement(code);
}

/// <summary>
/// Maps a binary operator to its JavaScript spelling.
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions Coder/Languages/PythonGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,40 @@ protected override void GenerateBinary(BinaryExpression binary, CodeBlocker code
code.Write(")");
}

/// <inheritdoc/>
/// <remarks>
/// <c>x /= y</c> and <c>x %= y</c> are Python's true division and floored remainder, the same as
/// the binary operators, so on integers they give <c>2.5</c> for <c>5 /= 2</c> and <c>1</c> for
/// <c>-7 %= 2</c> where the C family gives <c>2</c> and <c>-1</c>. When both sides are known to be
/// integers the assignment is written out as <c>x = x / y</c>, whose quotient or remainder
/// <see cref="GenerateBinary"/> truncates. Every other compound assignment, and one on operands
/// the AST does not know to be integers, is left as it is.
/// </remarks>
protected override void GenerateAssignmentStatement(AssignmentStatement assignment, CodeBlocker code)
{
Ensure.NotNull(assignment);
Ensure.NotNull(code);

BinaryOperator? op = assignment.Operator switch
{
AssignmentOperator.DivideAssign => BinaryOperator.Divide,
AssignmentOperator.ModuloAssign => BinaryOperator.Modulo,
_ => null,
};

BinaryExpression? binary = op is null ? null : new(assignment.Target, op.Value, assignment.Value);
if (binary is null || !IsIntegerArithmetic(binary))
{
base.GenerateAssignmentStatement(assignment, code);
return;
}

GenerateInternal(assignment.Target, code);
code.Write(" = ");
GenerateBinary(binary, code);
EndStatement(code);
}

/// <summary>
/// Writes the C family's truncating quotient or remainder of two integers in integer arithmetic.
/// </summary>
Expand Down
Loading