Skip to content

Commit c92c664

Browse files
committed
[Heavy] Fix if with continuations and set!; Fix append_rec error checking
1 parent 36fe13b commit c92c664

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

heavy/lib/Builtins.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,11 @@ Value append_rec(Context& C, Value List, Value Cdr) {
645645
C.setLoc(List);
646646
if (isa<Empty>(List))
647647
return Cdr;
648-
else if (Pair* P = dyn_cast<Pair>(List))
649-
return C.CreatePair(P->Car, append_rec(C, P->Cdr, Cdr));
650-
else
651-
return Value();
648+
if (Pair* P = dyn_cast<Pair>(List)) {
649+
if (Value V = append_rec(C, P->Cdr, Cdr))
650+
return C.CreatePair(P->Car, V);
651+
}
652+
return Value();
652653
};
653654
}
654655
void append(Context& C, ValueRefs Args) {

heavy/lib/OpGen.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -877,25 +877,18 @@ mlir::Value OpGen::createIf(SourceLocation Loc, Value Cond, Value Then,
877877
TailPosScope TPS(*this);
878878
IsTailPos = true;
879879

880-
// Then
881-
{
880+
auto handleBranch = [&](heavy::Value Value, mlir::Block* Block) {
882881
LambdaScope LS(*this, IfContOp);
883882
mlir::OpBuilder::InsertionGuard IG(Builder);
884-
Builder.setInsertionPointToStart(ThenBlock);
885-
mlir::Value Result = Visit(Then);
886-
if (ThenBlock->empty() || !isa<ApplyOp>(ThenBlock->back()))
883+
Builder.setInsertionPointToStart(Block);
884+
mlir::Value Result = Visit(Value);
885+
Block = Builder.getBlock();
886+
if (Block->empty() || !isa<ApplyOp>(Block->back()))
887887
create<ContOp>(Loc, Result);
888-
}
888+
};
889889

890-
// Else
891-
{
892-
LambdaScope LS(*this, IfContOp);
893-
mlir::OpBuilder::InsertionGuard IG(Builder);
894-
Builder.setInsertionPointToStart(ElseBlock);
895-
mlir::Value Result = Visit(Else);
896-
if (ElseBlock->empty() || !isa<ApplyOp>(ElseBlock->back()))
897-
create<ContOp>(Loc, Result);
898-
}
890+
handleBranch(Then, ThenBlock);
891+
handleBranch(Else, ElseBlock);
899892
}
900893

901894
if (TailPos) return mlir::Value();

heavy/test/Evaluate/if.scm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@
6666
(write temp)))
6767
(newline)
6868

69+
; CHECK-NEXT: 5
70+
((lambda ()
71+
(define temp 42)
72+
(if (eqv? temp 42)
73+
(set! temp (+ 5))
74+
(set! temp 6))
75+
(write temp)))
76+
(newline)
77+
78+
; CHECK-NEXT: 6
79+
((lambda ()
80+
(define temp 42)
81+
(if (eqv? temp 43)
82+
(set! temp 5)
83+
(set! temp (+ 5 1)))
84+
(write temp)))
85+
(newline)
86+
6987
; CHECK-NEXT: foo
7088
(write
7189
(if (null? '()) 'return-foo 'return-bar))

0 commit comments

Comments
 (0)