Skip to content

Commit fd1ae26

Browse files
Bug fix
1 parent 23ab1b1 commit fd1ae26

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ else if (cbrInst.condition() instanceof Operand.ConstantOperand constantOperand)
108108
int reg = baseReg;
109109
for (Operand arg: callInst.args()) {
110110
if (arg instanceof Operand.RegisterOperand param) {
111-
execStack.stack[base + reg] = execStack.stack[base + param.frameSlot()];
111+
execStack.stack[reg] = execStack.stack[base + param.frameSlot()];
112112
}
113113
else if (arg instanceof Operand.ConstantOperand constantOperand) {
114-
execStack.stack[base + reg] = new Value.IntegerValue(constantOperand.value);
114+
execStack.stack[reg] = new Value.IntegerValue(constantOperand.value);
115115
}
116116
else if (arg instanceof Operand.NullConstantOperand) {
117-
execStack.stack[base + reg] = new Value.NullValue();
117+
execStack.stack[reg] = new Value.NullValue();
118118
}
119119
reg += 1;
120120
}
@@ -193,7 +193,7 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
193193
case "<": value = x < y ? 1: 0; break;
194194
case ">": value = x > y ? 1 : 0; break;
195195
case "<=": value = x <= y ? 1 : 0; break;
196-
case ">=": value = x <= y ? 1 : 0; break;
196+
case ">=": value = x >= y ? 1 : 0; break;
197197
default: throw new IllegalStateException();
198198
}
199199
execStack.stack[base + binaryInst.result().frameSlot()] = new Value.IntegerValue(value);

registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ else if (cbrInst.condition() instanceof Operand.ConstantOperand constantOperand)
108108
int reg = baseReg;
109109
for (Operand arg: callInst.args()) {
110110
if (arg instanceof Operand.RegisterOperand param) {
111-
execStack.stack[base + reg] = execStack.stack[base + param.frameSlot()];
111+
execStack.stack[reg] = execStack.stack[base + param.frameSlot()];
112112
}
113113
else if (arg instanceof Operand.ConstantOperand constantOperand) {
114-
execStack.stack[base + reg] = new Value.IntegerValue(constantOperand.value);
114+
execStack.stack[reg] = new Value.IntegerValue(constantOperand.value);
115115
}
116116
else if (arg instanceof Operand.NullConstantOperand) {
117-
execStack.stack[base + reg] = new Value.NullValue();
117+
execStack.stack[reg] = new Value.NullValue();
118118
}
119119
reg += 1;
120120
}
@@ -193,7 +193,7 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
193193
case "<": value = x < y ? 1: 0; break;
194194
case ">": value = x > y ? 1 : 0; break;
195195
case "<=": value = x <= y ? 1 : 0; break;
196-
case ">=": value = x <= y ? 1 : 0; break;
196+
case ">=": value = x >= y ? 1 : 0; break;
197197
default: throw new IllegalStateException();
198198
}
199199
execStack.stack[base + binaryInst.result().frameSlot()] = new Value.IntegerValue(value);

registervm/src/test/java/com/compilerprogramming/ezlang/interpreter/TestInterpreter.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,92 @@ func foo()->Int
244244
Assert.assertTrue(value instanceof Value.IntegerValue integerValue &&
245245
integerValue.value == 1);
246246
}
247+
248+
@Test
249+
public void testMergeSort() {
250+
String src = """
251+
// based on the top-down version from https://en.wikipedia.org/wiki/Merge_sort
252+
// via https://github.com/SeaOfNodes/Simple
253+
func merge_sort(a: [Int], b: [int], n: Int)
254+
{
255+
copy_array(a, 0, n, b)
256+
split_merge(a, 0, n, b)
257+
}
258+
259+
func split_merge(b: [Int], begin: Int, end: Int, a: [Int])
260+
{
261+
if (end - begin <= 1)
262+
return;
263+
var middle = (end + begin) / 2
264+
split_merge(a, begin, middle, b)
265+
split_merge(a, middle, end, b)
266+
merge(b, begin, middle, end, a)
267+
}
268+
269+
func merge(b: [Int], begin: Int, middle: Int, end: Int, a: [Int])
270+
{
271+
var i = begin
272+
var j = middle
273+
var k = begin
274+
while (k < end) {
275+
// && and ||
276+
var cond = 0
277+
if (i < middle) {
278+
if (j >= end) cond = 1;
279+
else if (a[i] <= a[j]) cond = 1;
280+
}
281+
if (cond)
282+
{
283+
b[k] = a[i]
284+
i = i + 1
285+
}
286+
else
287+
{
288+
b[k] = a[j]
289+
j = j + 1
290+
}
291+
k = k + 1
292+
}
293+
}
294+
295+
func copy_array(a: [Int], begin: Int, end: Int, b: [Int])
296+
{
297+
var k = begin
298+
while (k < end)
299+
{
300+
b[k] = a[k]
301+
k = k + 1
302+
}
303+
}
304+
305+
func eq(a: [Int], b: [Int], n: Int)->Int
306+
{
307+
var result = 1
308+
var i = 0
309+
while (i < n)
310+
{
311+
if (a[i] != b[i])
312+
{
313+
result = 0
314+
break
315+
}
316+
i = i + 1
317+
}
318+
return result
319+
}
320+
321+
func main()->Int
322+
{
323+
var a = new [Int]{10,9,8,7,6,5,4,3,2,1}
324+
var b = new [Int]{ 0,0,0,0,0,0,0,0,0,0}
325+
var expect = new [Int]{1,2,3,4,5,6,7,8,9,10}
326+
merge_sort(a, b, 10)
327+
return eq(a,expect,10)
328+
}
329+
""";
330+
var value = compileAndRun(src, "main");
331+
Assert.assertNotNull(value);
332+
Assert.assertTrue(value instanceof Value.IntegerValue integerValue &&
333+
integerValue.value == 1);
334+
}
247335
}

0 commit comments

Comments
 (0)