Skip to content

Commit f561155

Browse files
committed
Refactor
1 parent c257d32 commit f561155

File tree

3 files changed

+53
-59
lines changed

3 files changed

+53
-59
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -626,23 +626,19 @@ class Lowering()(using Config, TL, Raise, State, Ctx):
626626
def quoteSplit(split: Split)(k: Result => Block)(using Subst): Block = split match
627627
case Split.Cons(Branch(scrutinee, pattern, continuation), tail) => quote(scrutinee): r1 =>
628628
val l1, l2, l3, l4, l5 = new TempSymbol(N)
629-
Assign(l1, r1, quotePattern(pattern): r2 =>
630-
Assign(l2, r2, quoteSplit(continuation): r3 =>
631-
Assign(l3, r3, setupTerm("Branch", (l1 :: l2 :: l3 :: Nil).map(s => Value.Ref(s))): r4 =>
632-
Assign(l4, r4, quoteSplit(tail): r5 =>
633-
Assign(l5, r5, setupTerm("Cons", (l4 :: l5 :: Nil).map(s => Value.Ref(s)))(k))
634-
)
635-
)
636-
)
637-
)
629+
blockBuilder.assign(l1, r1)
630+
.chain(b => quotePattern(pattern)(r2 => Assign(l2, r2, b)))
631+
.chain(b => quoteSplit(continuation)(r3 => Assign(l3, r3, b)))
632+
.chain(b => setupTerm("Branch", (l1 :: l2 :: l3 :: Nil).map(s => Value.Ref(s)))(r4 => Assign(l4, r4, b)))
633+
.chain(b => quoteSplit(tail)(r5 => Assign(l5, r5, b)))
634+
.rest(setupTerm("Cons", (l4 :: l5 :: Nil).map(s => Value.Ref(s)))(k))
638635
case Split.Let(sym, term, tail) => setupSymbol(sym, true): r1 =>
639636
val l1, l2, l3 = new TempSymbol(N)
640-
Assign(l1, r1, setupTerm("Ref", Value.Ref(l1) :: Nil): r =>
641-
Assign(sym, r, quote(term)(r2 =>
642-
Assign(l2, r2, quoteSplit(tail)(r3 =>
643-
Assign(l3, r3, setupTerm("Let", (l1 :: l2 :: l3 :: Nil).map(s => Value.Ref(s)))(k))
644-
)))
645-
))
637+
blockBuilder.assign(l1, r1)
638+
.chain(b => setupTerm("Ref", Value.Ref(l1) :: Nil)(r => Assign(sym, r, b)))
639+
.chain(b => quote(term)(r2 => Assign(l2, r2, b)))
640+
.chain(b => quoteSplit(tail)(r3 => Assign(l3, r3, b)))
641+
.rest(setupTerm("Let", (l1 :: l2 :: l3 :: Nil).map(s => Value.Ref(s)))(k))
646642
case Split.Else(default) => quote(default): r =>
647643
val l = new TempSymbol(N)
648644
Assign(l, r, setupTerm("Else", Value.Ref(l) :: Nil)(k))
@@ -705,18 +701,16 @@ class Lowering()(using Config, TL, Raise, State, Ctx):
705701
rec(rhs, Nil)(k)
706702
case Blk(LetDecl(sym, _) :: DefineVar(sym2, rhs) :: Nil, res) => // Let bindings
707703
require(sym2 is sym)
708-
setupSymbol(sym, true): r1 =>
704+
setupSymbol(sym, true){r1 =>
709705
val l1, l2, l3, l4, l5 = new TempSymbol(N)
710-
Assign(l1, r1, setupTerm("Ref", Value.Ref(l1) :: Nil): r =>
711-
Assign(sym, r, quote(rhs)(r2 =>
712-
Assign(l2, r2, quote(res)(r3 =>
713-
Assign(l3, r3, setupTerm("LetDecl", Value.Ref(l1) :: Nil)(r4 =>
714-
Assign(l4, r4, setupTerm("DefineVar", Value.Ref(l1) :: Value.Ref(l2) :: Nil)(r5 =>
715-
Assign(l5, r5, setupTerm("Blk", Value.Arr((l4 :: l5 :: Nil).map(s => Value.Ref(s).asArg)) :: Value.Ref(l3) :: Nil)(k))
716-
))
717-
))
718-
))
719-
)))
706+
blockBuilder.assign(l1, r1)
707+
.chain(b => setupTerm("Ref", Value.Ref(l1) :: Nil)(r => Assign(sym, r, b)))
708+
.chain(b => quote(rhs)(r2 => Assign(l2, r2, b)))
709+
.chain(b => quote(res)(r3 => Assign(l3, r3, b)))
710+
.chain(b => setupTerm("LetDecl", Value.Ref(l1) :: Nil)(r4 => Assign(l4, r4, b)))
711+
.chain(b => setupTerm("DefineVar", Value.Ref(l1) :: Value.Ref(l2) :: Nil)(r5 => Assign(l5, r5, b)))
712+
.rest(setupTerm("Blk", Value.Arr((l4 :: l5 :: Nil).map(s => Value.Ref(s).asArg)) :: Value.Ref(l3) :: Nil)(k))
713+
}
720714
case IfLike(syntax.Keyword.`if`, split) => quoteSplit(split): r =>
721715
val l = new TempSymbol(N)
722716
Assign(l, r, setupTerm("IfLike", setupQuotedKeyword("If") :: Value.Ref(l) :: Nil)(k))

hkmc2/shared/src/test/mlscript-compile/QuoteExample.mjs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,36 @@ let QuoteExample1;
128128
tmp6 = Term.freshName("scrut");
129129
tmp7 = new Term.Symbol(tmp6);
130130
scrut = new Term.Ref(tmp7);
131-
tmp10 = y1;
132-
tmp11 = new Term.Lit(0.0);
133-
tmp12 = new Term.Builtin("==");
134-
tmp13 = new Term.Tup([
135-
tmp10,
136-
tmp11
131+
tmp23 = y1;
132+
tmp24 = new Term.Lit(0.0);
133+
tmp25 = new Term.Builtin("==");
134+
tmp26 = new Term.Tup([
135+
tmp23,
136+
tmp24
137137
]);
138-
tmp8 = new Term.App(tmp12, tmp13);
139-
tmp14 = scrut;
140-
tmp15 = new Term.LitPattern(true);
141-
tmp19 = d;
142-
tmp16 = new Term.Else(tmp19);
143-
tmp17 = new Term.Branch(tmp14, tmp15, tmp16);
144-
tmp20 = x2;
145-
tmp21 = y1;
146-
tmp22 = new Term.Builtin("/");
147-
tmp23 = new Term.Tup([
148-
tmp20,
149-
tmp21
138+
tmp8 = new Term.App(tmp25, tmp26);
139+
tmp12 = scrut;
140+
tmp13 = new Term.LitPattern(true);
141+
tmp22 = d;
142+
tmp14 = new Term.Else(tmp22);
143+
tmp15 = new Term.Branch(tmp12, tmp13, tmp14);
144+
tmp17 = x2;
145+
tmp18 = y1;
146+
tmp19 = new Term.Builtin("/");
147+
tmp20 = new Term.Tup([
148+
tmp17,
149+
tmp18
150150
]);
151-
tmp24 = new Term.App(tmp22, tmp23);
152-
tmp18 = new Term.Else(tmp24);
153-
tmp9 = new Term.Cons(tmp17, tmp18);
154-
tmp25 = new Term.Let(tmp7, tmp8, tmp9);
155-
tmp26 = new Term.IfLike(Term.Keyword.If, tmp25);
151+
tmp21 = new Term.App(tmp19, tmp20);
152+
tmp16 = new Term.Else(tmp21);
153+
tmp9 = new Term.Cons(tmp15, tmp16);
154+
tmp10 = new Term.Let(tmp7, tmp8, tmp9);
155+
tmp11 = new Term.IfLike(Term.Keyword.If, tmp10);
156156
return new Term.Lam([
157157
tmp1,
158158
tmp3,
159159
tmp5
160-
], tmp26)
160+
], tmp11)
161161
}
162162
static toString() { return "QuoteExample"; }
163163
});

hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ f`(`0)
7474
//│ tmp36 = new Term.Symbol(tmp35);
7575
//│ scrut = new Term.Ref(tmp36);
7676
//│ tmp37 = new Term.Lit(true);
77-
//│ tmp39 = scrut;
78-
//│ tmp40 = new Term.LitPattern(true);
79-
//│ tmp44 = new Term.Lit(true);
80-
//│ tmp41 = new Term.Else(tmp44);
81-
//│ tmp42 = new Term.Branch(tmp39, tmp40, tmp41);
77+
//│ tmp40 = scrut;
78+
//│ tmp41 = new Term.LitPattern(true);
79+
//│ tmp46 = new Term.Lit(true);
80+
//│ tmp42 = new Term.Else(tmp46);
81+
//│ tmp43 = new Term.Branch(tmp40, tmp41, tmp42);
8282
//│ tmp45 = new Term.Lit(false);
83-
//│ tmp43 = new Term.Else(tmp45);
84-
//│ tmp38 = new Term.Cons(tmp42, tmp43);
85-
//│ tmp46 = new Term.Let(tmp36, tmp37, tmp38);
86-
//│ new Term.IfLike(Term.Keyword.If, tmp46)
83+
//│ tmp44 = new Term.Else(tmp45);
84+
//│ tmp38 = new Term.Cons(tmp43, tmp44);
85+
//│ tmp39 = new Term.Let(tmp36, tmp37, tmp38);
86+
//│ new Term.IfLike(Term.Keyword.If, tmp39)
8787
//│ = IfLike(If, Let(Symbol("scrut_0"), Lit(true), Cons(Branch(Ref(Symbol("scrut_0")), LitPattern(true), Else(Lit(true))), Else(Lit(false)))))
8888

8989

0 commit comments

Comments
 (0)