@@ -24,17 +24,16 @@ namespace heavy {
2424// TemplateGen
2525// - Substitute a template using the syntactic environment
2626// created by a pattern as part of the syntax-rules syntax.
27- class TemplateGen : TemplateBase<TemplateGen>,
28- ValueVisitor<TemplateGen, TemplateResult> {
27+ class TemplateGen : TemplateBase<TemplateGen> {
2928 friend TemplateBase<TemplateGen>;
3029 friend ValueVisitor<TemplateGen, TemplateResult>;
30+ using Base = TemplateBase<TemplateGen>;
3131
3232 Symbol* Ellipsis;
3333 NameSet& PatternVarNames;
3434 llvm::SmallVectorImpl<mlir::Value>* CurrentPacks = nullptr ;
3535
3636public:
37- using ResultTy = TemplateResult;
3837 using ErrorTy = TemplateError;
3938
4039 TemplateGen (heavy::OpGen& O, NameSet& PVNames,
@@ -44,40 +43,18 @@ class TemplateGen : TemplateBase<TemplateGen>,
4443 PatternVarNames(PVNames)
4544 { }
4645
47- // VisitTemplate - Create operations to transform syntax and
48- // evaluate it.
49- void VisitTemplate (heavy::Value Template) {
46+ // Create operations to transform syntax and compile the result.
47+ void BuildTemplate (heavy::Value Template) {
5048 heavy::SourceLocation Loc = Template.getSourceLocation ();
51- ResultTy Result = Visit (Template);
52-
53- mlir::Value TransformedSyntax;
54- if (mlir::Value* MVP = std::get_if<mlir::Value>(&Result))
55- TransformedSyntax = *MVP;
56- else
57- TransformedSyntax = createLiteral (std::get<heavy::Value>(Result));
58-
59- {
60- // Move the RenameOps to the end of the PatternOp.
61- mlir::Block* Block = OpGen.Builder .getBlock ();
62- auto InsertionPoint = OpGen.Builder .getInsertionPoint ();
63- for (mlir::Value RV : RenameOps)
64- RV.getDefiningOp ()->moveBefore (Block, InsertionPoint);
65- }
66-
67- // Add the RenameOps to an EnvFrame
68- // to serve as the base of the environment.
69- mlir::Value TemplateEnv = OpGen.create <EnvFrameOp>(Loc, RenameOps);
70- OpGen.createOpGen (Loc, TransformedSyntax, TemplateEnv);
49+ mlir::Value TransformedSyntax = VisitTemplate (Template);
50+ mlir::Value RenameEnv = createRenameEnv ();
51+ OpGen.createOpGen (Loc, TransformedSyntax, RenameEnv);
7152 }
7253
7354private:
74- ResultTy VisitValue (Value P) {
75- return P;
76- }
77-
7855 mlir::Value ExpandPack (heavy::SourceLocation Loc,
7956 heavy::Value Car, heavy::Value Cdr) {
80- ResultTy CdrResult = Visit (Cdr);
57+ TemplateResult CdrResult = Visit (Cdr);
8158 auto Body = std::make_unique<mlir::Region>();
8259 llvm::SmallVector<mlir::Value, 4 > Packs;
8360 llvm::SmallVectorImpl<mlir::Value>* PrevPacks = CurrentPacks;
@@ -86,7 +63,7 @@ class TemplateGen : TemplateBase<TemplateGen>,
8663 {
8764 mlir::OpBuilder::InsertionGuard IG (OpGen.Builder );
8865 OpGen.Builder .setInsertionPointToStart (&Block);
89- ResultTy Last = Visit (Car);
66+ TemplateResult Last = Visit (Car);
9067 if (OpGen.CheckError ())
9168 return mlir::Value ();
9269 if (std::holds_alternative<heavy::Value>(Last))
@@ -100,27 +77,15 @@ class TemplateGen : TemplateBase<TemplateGen>,
10077 return EPO.getResult ();
10178 }
10279
103- ResultTy VisitPair (Pair* P) {
80+ TemplateResult VisitPair (Pair* P) {
10481 heavy::SourceLocation Loc = P->getSourceLocation ();
10582 if (auto * P2 = dyn_cast<Pair>(P->Cdr );
10683 P2 && isa<Symbol>(P2->Car ) &&
10784 cast<Symbol>(P2->Car )->Equiv (Ellipsis)) {
10885 return ExpandPack (Loc, P->Car , P2->Cdr );
10986 }
11087
111- ResultTy CarResult = Visit (P->Car );
112- ResultTy CdrResult = Visit (P->Cdr );
113-
114- // If nothing changed
115- auto * HCar = std::get_if<heavy::Value>(&CarResult);
116- auto * HCdr = std::get_if<heavy::Value>(&CdrResult);
117- if (HCar && HCdr && *HCar == P->Car && *HCdr == P->Cdr )
118- return P;
119-
120- if (OpGen.CheckError ())
121- return mlir::Value ();
122-
123- return createCons (Loc, CarResult, CdrResult);
88+ return Base::VisitPair (P);
12489 }
12590
12691 mlir::Value GetPatternVar (heavy::Symbol* S) {
@@ -161,7 +126,7 @@ class TemplateGen : TemplateBase<TemplateGen>,
161126 return Block->addArgument (HeavyValueT, MLoc);
162127 }
163128
164- ResultTy VisitSymbol (Symbol* P) {
129+ TemplateResult VisitSymbol (Symbol* P) {
165130 if (PatternVarNames.contains (P->getString ()))
166131 return CurrentPacks ? CaptureExpandArg (P) : GetPatternVar (P);
167132
0 commit comments