Skip to content

Commit 9e8f9f0

Browse files
committed
Feature(compiler): Support IfStatement without else branch
1 parent d65fd53 commit 9e8f9f0

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

src/backend/llvm/index.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,37 @@ export function passIfStatement(parent: ts.IfStatement, ctx: Context, builder: l
3939
const positiveBlock = llvm.BasicBlock.create(ctx.llvmContext, "if.true");
4040
ctx.scope.currentFunction.addBasicBlock(positiveBlock);
4141

42-
const negativeBlock = llvm.BasicBlock.create(ctx.llvmContext, "if.false");
43-
ctx.scope.currentFunction.addBasicBlock(negativeBlock);
44-
4542
const next = llvm.BasicBlock.create(ctx.llvmContext, "if.end");
4643
ctx.scope.currentFunction.addBasicBlock(next);
4744

48-
emitCondition(
49-
parent.expression,
50-
ctx,
51-
builder,
52-
positiveBlock,
53-
negativeBlock
54-
);
45+
if (parent.elseStatement) {
46+
const negativeBlock = llvm.BasicBlock.create(ctx.llvmContext, "if.false");
47+
ctx.scope.currentFunction.addBasicBlock(negativeBlock);
5548

56-
builder.setInsertionPoint(positiveBlock);
57-
passNode(parent.thenStatement, ctx, builder);
49+
emitCondition(
50+
parent.expression,
51+
ctx,
52+
builder,
53+
positiveBlock,
54+
negativeBlock
55+
);
5856

59-
builder.createBr(next);
57+
builder.setInsertionPoint(negativeBlock);
58+
passNode(parent.elseStatement, ctx, builder);
6059

61-
builder.setInsertionPoint(negativeBlock);
62-
passNode(parent.elseStatement, ctx, builder);
60+
builder.createBr(next);
61+
} else {
62+
emitCondition(
63+
parent.expression,
64+
ctx,
65+
builder,
66+
positiveBlock,
67+
next
68+
);
69+
}
70+
71+
builder.setInsertionPoint(positiveBlock);
72+
passNode(parent.thenStatement, ctx, builder);
6373

6474
builder.createBr(next);
6575

0 commit comments

Comments
 (0)