Skip to content

Commit

Permalink
Division unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Dec 29, 2024
1 parent 7aff19a commit 42a93b2
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// COMPILE ERROR
// COMPILE ERROR OD0011

var/a = 1 / 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// COMPILE ERROR
// COMPILE ERROR OD0011

var/static/a = 1 / 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// COMPILE ERROR
// COMPILE ERROR OD0011

var/const/a = 0
var/b = 1 / a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// COMPILE ERROR
// COMPILE ERROR OD0011

/proc/one()
return 1
Expand Down
5 changes: 2 additions & 3 deletions Content.Tests/DMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags, int errorCode) {

Assert.That(compiledFile is not null && File.Exists(compiledFile), "Failed to compile DM source file");
Assert.That(_dreamMan.LoadJson(compiledFile), $"Failed to load {compiledFile}");
_dreamMan.LastDMException = null; // Nuke any exception from a prior test
_dreamMan.StartWorld();

(bool successfulRun, DreamValue? returned, Exception? exception) = RunTest();
Expand Down Expand Up @@ -114,8 +115,6 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags, int errorCode) {
}

private (bool Success, DreamValue? Returned, Exception? except) RunTest() {
var prev = _dreamMan.LastDMException;

DreamValue? retValue = null;
Task<DreamValue> callTask = null!;

Expand Down Expand Up @@ -143,7 +142,7 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags, int errorCode) {
}
}

bool retSuccess = _dreamMan.LastDMException == prev; // Works because "null == null" is true in this language.
bool retSuccess = _dreamMan.LastDMException == null; // Works because "null == null" is true in this language.
return (retSuccess, retValue, _dreamMan.LastDMException);
}

Expand Down
5 changes: 5 additions & 0 deletions DMCompiler/Optimizer/PeepholeOptimizations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DMCompiler.Bytecode;
using DMCompiler.Compiler;

// ReSharper disable UnusedType.Global

Expand Down Expand Up @@ -458,6 +459,10 @@ public void Apply(DMCompiler compiler, List<IAnnotatedBytecode> input, int index

IOptimization.GetInstructionAndValue(input[index + 1], out var pushVal2);

if (pushVal2 == 0) {
compiler.Emit(WarningCode.BadExpression, input[index + 1].GetLocation(), "Division by zero");
}

// At runtime, given "A / B" we pop B then A
// In the peephole optimizer, index is "A", index+1 is "B"
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeFloat(pushVal1 / pushVal2, firstInstruction.Location)};
Expand Down

0 comments on commit 42a93b2

Please sign in to comment.