Skip to content

Commit b19c4a0

Browse files
Merge pull request #42 from CompilerProgramming/develop
Add compiling of boolean expressions to stack IR
2 parents 35eb283 + b4e4dcb commit b19c4a0

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,47 @@ private boolean compileSymbolExpr(AST.NameExpr symbolExpr) {
343343
return false;
344344
}
345345

346+
private boolean codeBoolean(AST.BinaryExpr binaryExpr) {
347+
boolean isAnd = binaryExpr.op.str.equals("&&");
348+
BasicBlock l1 = createBlock();
349+
BasicBlock l2 = createBlock();
350+
BasicBlock l3 = createBlock();
351+
boolean indexed = compileExpr(binaryExpr.expr1);
352+
if (indexed)
353+
codeIndexedLoad();
354+
if (isAnd) {
355+
code(new Instruction.ConditionalBranch(currentBlock, l1, l2));
356+
} else {
357+
code(new Instruction.ConditionalBranch(currentBlock, l2, l1));
358+
}
359+
startBlock(l1);
360+
indexed = compileExpr(binaryExpr.expr2);
361+
if (indexed)
362+
codeIndexedLoad();
363+
jumpTo(l3);
364+
startBlock(l2);
365+
// Below we must write to the same temp
366+
code(new Instruction.PushConst(isAnd ? 0 : 1));
367+
jumpTo(l3);
368+
startBlock(l3);
369+
// leaves result on stack
370+
return false;
371+
}
372+
346373
private boolean compileBinaryExpr(AST.BinaryExpr binaryExpr) {
374+
String binOp = binaryExpr.op.str;
375+
if (binOp.equals("&&") ||
376+
binOp.equals("||")) {
377+
return codeBoolean(binaryExpr);
378+
}
347379
int opCode = 0;
348380
boolean indexed = compileExpr(binaryExpr.expr1);
349381
if (indexed)
350382
codeIndexedLoad();
351383
indexed = compileExpr(binaryExpr.expr2);
352384
if (indexed)
353385
codeIndexedLoad();
354-
switch (binaryExpr.op.str) {
386+
switch (binOp) {
355387
case "+" -> opCode = Instruction.ADD_I;
356388
case "-" -> opCode = Instruction.SUB_I;
357389
case "*" -> opCode = Instruction.MUL_I;

stackvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestCompiler.java

+30
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,34 @@ public void testFunction28() {
634634
L1:
635635
""", result);
636636
}
637+
638+
@Test
639+
public void testFunction104() {
640+
String src = """
641+
func foo()->Int
642+
{
643+
return 1 == 1 && 2 == 2
644+
}
645+
""";
646+
String result = compileSrc(src);
647+
Assert.assertEquals("""
648+
L0:
649+
pushi 1
650+
pushi 1
651+
eq
652+
cbr L2 L3
653+
L2:
654+
pushi 2
655+
pushi 2
656+
eq
657+
jump L4
658+
L4:
659+
jump L1
660+
L1:
661+
L3:
662+
pushi 0
663+
jump L4
664+
""", result);
665+
}
666+
637667
}

0 commit comments

Comments
 (0)