Skip to content

Commit 957187e

Browse files
committed
[Heavy] Add syntax name to rename env; Finish map procedure
1 parent 264f3fc commit 957187e

File tree

7 files changed

+104
-38
lines changed

7 files changed

+104
-38
lines changed

heavy/include/heavy/Ops.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ def heavy_RenameOp : HeavyOp<"rename"> {
508508
expansion.
509509
}];
510510

511-
let arguments = (ins StrAttr:$id, HeavyValue:$capture);
511+
let arguments = (ins StrAttr:$id,
512+
AnyTypeOf<[HeavyValue, HeavySyntax]>:$capture);
512513
let results = (outs HeavyValue:$result);
513514
let builders = [
514515
OpBuilder<(ins "::llvm::StringRef":$id, "::mlir::Value":$capture)>

heavy/include/heavy/base/list.sld

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,30 @@
4444
(define MaxLen
4545
(let Loop ((Lists InputLists)
4646
(MinLength -1))
47-
(dump Lists)
4847
(if (pair? Lists)
49-
(let ((Len (length (car Lists))))
50-
(Loop
51-
(cdr Lists)
52-
(if (<= 0 Len MinLength)
53-
Len
54-
MinLength)))
48+
(letrec
49+
((Len (length (car Lists)))
50+
(NextLists (cdr Lists))
51+
(NextMinLength
52+
(if (or (< Len MinLength)
53+
(< MinLength 0))
54+
Len
55+
MinLength)))
56+
(Loop NextLists NextMinLength))
5557
MinLength)))
56-
(dump 'wtf)
5758
(when (< MaxLen 0)
5859
(error "expecting at least one finite list" InputLists))
59-
(let Loop ((I 0)
60-
(Lists InputLists)
61-
(Result '()))
62-
(if (< I MaxLen)
63-
(let ((Args (MapFast car Lists))
64-
(NextLists (MapFast cdr Lists)))
65-
(Loop (+ I 1) NextLists (cons (apply Proc Args))))
66-
Result)))
67-
68-
60+
(let ()
61+
(define ReverseResult
62+
(let Loop ((I 0)
63+
(Lists InputLists)
64+
(Result '()))
65+
(if (< I MaxLen)
66+
(let ((Args (MapFast car Lists))
67+
(NextLists (MapFast cdr Lists)))
68+
(Loop (+ I 1) NextLists (cons (apply Proc Args) Result)))
69+
Result)))
70+
(reverse ReverseResult)))
6971

7072
) ; end of begin
7173
(export

heavy/lib/Lexer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ bool isDotSubsequent(char c) {
7272
bool isPeculiarIdentifierPrefix(char InitChar, char const* CurPtr) {
7373
// InitChar - The '+', '-' or '.'
7474
char Char = *CurPtr;
75-
char NextChar;
76-
char NextNextChar;
75+
char NextChar = '\0';
76+
char NextNextChar = '\0';
7777
if (Char != '\0')
7878
NextChar = *(CurPtr + 1);
7979
if (NextChar != '\0')

heavy/lib/PatternTemplate.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
7878
heavy::SourceLocation Loc = Pattern.getSourceLocation();
7979
if (isa_and_nonnull<Symbol>(Pattern.car())) {
8080
// Ignore the initial keyword.
81-
// FIXME We don't actually check name, but other
82-
// implementations simply ignore the first
83-
// element altogether.
8481
Pair* P = cast<Pair>(Pattern);
8582
auto MatchPairOp = OpGen.create<heavy::MatchPairOp>(Loc, E);
8683
Visit(P->Cdr, MatchPairOp.getCdr());
@@ -89,7 +86,7 @@ class PatternTemplate : ValueVisitor<PatternTemplate, mlir::Value> {
8986
}
9087

9188
if (!OpGen.CheckError()) {
92-
TemplateGen TG(OpGen, PatternVars, Ellipsis);
89+
TemplateGen TG(OpGen, Keyword, PatternVars, Ellipsis);
9390
TG.BuildTemplate(Template);
9491
}
9592

heavy/lib/TemplateBase.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ class TemplateBase : protected ValueVisitor<Derived, TemplateResult> {
145145
return OpGen.create<SpliceOp>(Loc, ValX, ValY);
146146
}
147147

148-
mlir::Value createRename(Symbol* P) {
148+
mlir::Value createRename(Symbol* P, mlir::Value ProvidedVal = nullptr) {
149149
String* UniqueStr = P->getString();
150150

151151
// Check for memoized SC.
152-
{
152+
if (!ProvidedVal) {
153153
auto [R, SC] = RenameOps.lookup(UniqueStr);
154154
if (SC)
155155
return SC;
@@ -158,19 +158,26 @@ class TemplateBase : protected ValueVisitor<Derived, TemplateResult> {
158158
// The result will be a symbol literal.
159159
// The built RenameOps are added to the RenameOps list.
160160
SourceLocation Loc = P->getSourceLocation();
161+
llvm::StringRef Id = P->getStringRef();
161162

162-
mlir::Value LiteralResult = createLiteral(P);
163-
mlir::Value SC = OpGen.create<SyntaxClosureOp>(Loc, LiteralResult,
164-
RenameEnv);
163+
mlir::Value CaptureVal = ProvidedVal ? ProvidedVal : createLiteral(P);
164+
mlir::Value SC;
165+
if (!isa<HeavySyntaxTy>(CaptureVal.getType()))
166+
SC = OpGen.create<SyntaxClosureOp>(Loc, CaptureVal, RenameEnv);
165167

166-
heavy::Context& Context = OpGen.getContext();
167-
EnvEntry Entry = Context.Lookup(P);
168-
llvm::StringRef Id = P->getStringRef();
168+
if (ProvidedVal) {
169+
mlir::Value R = OpGen.create<heavy::RenameOp>(Loc, Id, CaptureVal);
170+
RenameOps.insert({UniqueStr, {R, SC}});
171+
return SC;
172+
}
169173

170174
// Insert RenameOps before the RenameEnv EnvFrameOp.
171175
mlir::OpBuilder::InsertionGuard IG(OpGen.Builder);
172176
OpGen.Builder.setInsertionPoint(RenameEnv);
173177

178+
heavy::Context& Context = OpGen.getContext();
179+
EnvEntry Entry = Context.Lookup(P);
180+
174181
std::string MangledNameStr;
175182
llvm::StringRef MangledName;
176183
if (Entry && !Entry.MangledName) {

heavy/lib/TemplateGen.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class TemplateGen : TemplateBase<TemplateGen> {
2828
friend TemplateBase<TemplateGen>;
2929
friend ValueVisitor<TemplateGen, TemplateResult>;
3030

31+
Value Keyword;
3132
Value Ellipsis;
3233
NameSet& PatternVarNames;
3334
llvm::SmallVectorImpl<mlir::Value>* CurrentPacks = nullptr;
@@ -39,15 +40,36 @@ class TemplateGen : TemplateBase<TemplateGen> {
3940
public:
4041
using ErrorTy = TemplateError;
4142

42-
TemplateGen(heavy::OpGen& O, NameSet& PVNames,
43-
Value Ellipsis)
43+
TemplateGen(heavy::OpGen& O, Value Keyword,
44+
NameSet& PVNames, Value Ellipsis)
4445
: TemplateBase(O),
46+
Keyword(Keyword),
4547
Ellipsis(Ellipsis),
4648
PatternVarNames(PVNames)
4749
{ }
4850

4951
// Create operations to transform syntax and compile the result.
5052
void BuildTemplate(heavy::Value Template) {
53+
{
54+
Symbol* KeywordStr = nullptr;
55+
// FIXME Unwrapping the SC like this is probably not correct.
56+
if (auto* SC = dyn_cast<SyntaxClosure>(Keyword))
57+
KeywordStr = cast<Symbol>(SC->Node);
58+
else
59+
KeywordStr = cast<Symbol>(Keyword);
60+
// Add the Keyword (name of the syntax) as a Rename.
61+
auto SyntaxFuncOp = OpGen.Builder.getBlock()->getParentOp()
62+
->getParentOfType<FuncOp>();
63+
llvm::StringRef SyntaxFuncName = SyntaxFuncOp.getSymName();
64+
// Insert RenameOps before the RenameEnv EnvFrameOp.
65+
mlir::OpBuilder::InsertionGuard IG(OpGen.Builder);
66+
OpGen.Builder.setInsertionPoint(RenameEnv);
67+
auto SyntaxOp = OpGen.create<heavy::SyntaxOp>(
68+
Keyword.getSourceLocation(), SyntaxFuncName);
69+
createRename(KeywordStr, SyntaxOp);
70+
}
71+
72+
// Transform the syntax.
5173
heavy::SourceLocation Loc = Template.getSourceLocation();
5274
mlir::Value TransformedSyntax = transformSyntax(Template);
5375
finishRenameEnv();

heavy/test/Evaluate/list.scm

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,49 @@
159159
(cadr '((foo bar) (baz uhh))))
160160
(newline)
161161

162-
; COM-CHECK-NEXT: (#t #f #t #f)
163-
#|
162+
; CHECK-NEXT: (#t #f #t #f)
164163
(write
165164
(map <
166165
'(0 1 1 3)
167166
'(1 2 2 7)
168167
'(2 0 3 3)))
169168
(newline)
170-
|#
169+
170+
; CHECK-NEXT: ()
171+
(write
172+
(map + '() '() '()))
173+
(newline)
174+
175+
; CHECK-NEXT: (5)
176+
(write
177+
(map + '(5)))
178+
(newline)
179+
180+
; CHECK-NEXT: (7)
181+
(write
182+
(map + '(5) '(2)))
183+
(newline)
184+
185+
; CHECK-NEXT: (#(a b c) #(d e f) #(g h i))
186+
(write
187+
(map vector
188+
'(a d g 'oof)
189+
'(b e h)
190+
'(c f i)))
191+
(newline)
192+
193+
; CHECK-NEXT: (#(a b c) #(d e f) #(g h i))
194+
(write
195+
(map vector
196+
'(a d g)
197+
'(b e h 'oof)
198+
'(c f i)))
199+
(newline)
200+
201+
; CHECK-NEXT: (#(a b c) #(d e f) #(g h i))
202+
(write
203+
(map vector
204+
'(a d g)
205+
'(b e h)
206+
'(c f i 'oof)))
207+
(newline)

0 commit comments

Comments
 (0)