Skip to content

Commit 94d094d

Browse files
committed
[Heavy] Fix some subpattern issues
1 parent 9adf917 commit 94d094d

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

heavy/lib/Context.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ class Writer : public ValueVisitor<Writer> {
583583
});
584584
OS << '}';
585585
}
586+
587+
void VisitSyntaxClosure(SyntaxClosure* E) {
588+
// The context of closure is lost when printing this.
589+
OS << "#<SyntaxClosure {";
590+
Visit(E->Node);
591+
OS << '}';
592+
}
586593
};
587594

588595
} // end anon namespace

heavy/lib/OpEval.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ class OpEvalImpl {
682682
for (unsigned i = 0; i < Packs.size(); i++)
683683
Packs[i] = getValue(PackInputs[i]);
684684

685-
// Recall each pack is a list in reverse ordered
685+
// Recall that each pack is a list in reverse order.
686686
while (isa<heavy::Pair>(Packs.front())) {
687687
// Set each block argument to the cdr of each pack.
688688
push_scope();
@@ -713,19 +713,22 @@ class OpEvalImpl {
713713
if (isa<heavy::Empty>(E)) {
714714
for (mlir::Value Pack : Op.getPacks())
715715
setValue(Pack, Empty());
716-
setValue(Op.getCdr(), Empty());
717716
return next(Op);
718717
}
719718

720719
// Initialize result packs for storage as reverse ordered lists.
721720
for (mlir::Value PackResult : Op.getPacks())
722721
setValue(PackResult, Empty());
723722

723+
assert(Op.getBody().getNumArguments() == 1
724+
&& "body should have single argument");
725+
mlir::Value BodyArg = Op.getBody().getArgument(0);
724726
// Visit the subpattern body for each element in E.
725727
// Each "pack" should be a list.
726728
while (auto* Pair = dyn_cast<heavy::Pair>(E)) {
727729
push_scope();
728730
BlockItrTy Itr = Op.getBody().front().begin();
731+
setValue(BodyArg, Pair);
729732
while (Itr != BlockItrTy())
730733
Itr = Visit(&*Itr);
731734
E = Pair->Cdr;

heavy/lib/PatternTemplate.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
4141
return OpGen.getContext().getLoc();
4242
}
4343

44+
mlir::Location createLoc(heavy::SourceLocation Loc) {
45+
return mlir::OpaqueLoc::get(Loc.getOpaqueEncoding(),
46+
OpGen.Builder.getContext());
47+
}
48+
4449
public:
4550
PatternTemplate(heavy::OpGen& O,
4651
heavy::Symbol* Keyword,
@@ -101,7 +106,7 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
101106
return OpGen.SetError("invalid pattern node", P);
102107
}
103108

104-
mlir::Value VisitSubPattern(heavy::SourceLocation Loc,
109+
mlir::Value VisitSubpattern(heavy::SourceLocation Loc,
105110
Value P, Value Cdr, mlir::Value E) {
106111
// Visit the subpattern.
107112
auto Body = std::make_unique<mlir::Region>();
@@ -110,8 +115,10 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
110115
{
111116
mlir::OpBuilder::InsertionGuard IG(OpGen.Builder);
112117
OpGen.Builder.setInsertionPointToStart(&Block);
113-
114-
Visit(P, E);
118+
mlir::Type HeavyValueT = OpGen.Builder.getType<HeavyValueTy>();
119+
mlir::Location MLoc = createLoc(Loc);
120+
mlir::Value BodyArg = Block.addArgument(HeavyValueT, MLoc);
121+
Visit(P, BodyArg);
115122

116123
// Create range for pack values by finding the
117124
// SyntaxClosureOps in the Body region.
@@ -124,13 +131,16 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
124131
OpGen.create<ResolveOp>(Loc, Packs);
125132
}
126133

127-
// Create the SubPatternOp.
134+
// Create the SubpatternOp.
128135
auto SubpatternOp = OpGen.create<heavy::SubpatternOp>(
129136
Loc, E, std::move(Body), Packs.size());
130137

131138
if (!OpGen.CheckError())
132139
Visit(Cdr, SubpatternOp.getCdr());
133140

141+
// In the template, the nested syntax closures
142+
// will be looked up and check if its parent is
143+
// a subpattern op finding its result.
134144
return mlir::Value();
135145
}
136146

@@ -141,12 +151,14 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
141151
if (auto* P2 = dyn_cast<Pair>(P->Cdr);
142152
P2 && isa<Symbol>(P2->Car) &&
143153
cast<Symbol>(P2->Car)->equals(Ellipsis)) {
144-
VisitSubPattern(Loc, P->Car, P2->Cdr, E);
154+
// P->Car is the subpattern.
155+
// P2->Cdr is the rest of the pattern after the ...
156+
VisitSubpattern(Loc, P->Car, P2->Cdr, E);
145157
} else {
146-
auto MatchPairOp = OpGen.create<heavy::MatchPairOp>(Loc, E);
147-
Visit(P->Car, MatchPairOp.getCar());
158+
auto M1 = OpGen.create<MatchPairOp>(Loc, E);
159+
Visit(P->Car, M1.getCar());
148160
if (!OpGen.CheckError())
149-
Visit(P->Cdr, MatchPairOp.getCdr());
161+
Visit(P->Cdr, M1.getCdr());
150162
}
151163

152164
return mlir::Value();
@@ -180,9 +192,8 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
180192
}
181193

182194
// <ellipsis>
183-
if (P->equals(Ellipsis)) {
195+
if (P->equals(Ellipsis))
184196
return OpGen.SetError("<ellipsis> is not a valid pattern", P);
185-
}
186197

187198
// everything else is a pattern variable
188199
return bindPatternVar(P, E);

0 commit comments

Comments
 (0)