Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
262 commits
Select commit Hold shift + click to select a range
1ff0f5a
Merge pull request #2 from ChingLongTin/block-mls
ChingLongTin Oct 28, 2025
ac3a2f3
Merge pull request #3 from hkust-taco/hkmc2
ChingLongTin Oct 28, 2025
539bca3
Instrumentation progress following formalization
ChingLongTin Oct 29, 2025
3d43c81
Merge branch 'hkmc2' into instrument
ChingLongTin Oct 29, 2025
9298f36
add Block.mls
ChingLongTin Oct 31, 2025
4796cad
Add Block.mls into DiffMaker and prepare the function for lowering
yeungsinchun Oct 28, 2025
b072e4a
add block.mjs also in MLsCompiler
yeungsinchun Oct 30, 2025
35106c0
lower 'staged' annotations to Block and add display of 'staged' anno…
yeungsinchun Oct 30, 2025
4efccdf
amend! Add Block.mls into DiffMaker and prepare the function for lowe…
ChingLongTin Oct 31, 2025
f9ad612
clean up Syntax.mls
ChingLongTin Oct 31, 2025
f763b03
fiddle with printing staged keyword in FunDefn
ChingLongTin Oct 31, 2025
85f86c3
revert whitespace
ChingLongTin Oct 31, 2025
a39765e
revert change to printing
ChingLongTin Oct 31, 2025
ee681c9
move isStaged inside Fun
ChingLongTin Oct 31, 2025
f77c44c
more formatting fixes
ChingLongTin Oct 31, 2025
9c4c754
add new symbols to Elaborator
ChingLongTin Oct 31, 2025
4e5b281
remove Shape and Pattern
ChingLongTin Oct 31, 2025
d3aa471
rework function signatures
ChingLongTin Oct 31, 2025
b251526
clean up Block.mls
ChingLongTin Oct 31, 2025
84730bf
add Shape.mls
ChingLongTin Oct 31, 2025
f44b167
copy over more rules
ChingLongTin Oct 31, 2025
dcb0ed6
remove import of block inside MLsCompiler
yeungsinchun Oct 31, 2025
bfac96d
update diff test result (for the staged annotation)
yeungsinchun Oct 31, 2025
a81c0e9
fix importing for Block.mls
ChingLongTin Nov 1, 2025
4b0fa45
draft ruleInst
ChingLongTin Nov 1, 2025
3761d19
generalize recursion
ChingLongTin Nov 1, 2025
3ca8a98
add more rules
ChingLongTin Nov 1, 2025
71d5df3
remove length parameter from Shape.Arr
ChingLongTin Nov 1, 2025
cca32c6
stub all current rules
ChingLongTin Nov 1, 2025
ea13fe3
change transform* signature
ChingLongTin Nov 1, 2025
47919c4
change staged* to mls*
ChingLongTin Nov 1, 2025
217fb90
simplify getShape, getCode
ChingLongTin Nov 1, 2025
3827db5
reword transformArgs
ChingLongTin Nov 2, 2025
2d9c3d2
remove extractResult
ChingLongTin Nov 2, 2025
c5be8ec
introduce StagedPath
ChingLongTin Nov 2, 2025
b4eaf5c
introduce Shape
ChingLongTin Nov 2, 2025
2563eca
link to functions in Shape.mls
ChingLongTin Nov 2, 2025
edd7436
move function
ChingLongTin Nov 2, 2025
c17c3e1
implement more rules
ChingLongTin Nov 2, 2025
369195f
add stubs for function instrumentation
ChingLongTin Nov 2, 2025
ded7260
add instrumentation context
ChingLongTin Nov 2, 2025
5e08d17
refactor transformArgs
ChingLongTin Nov 2, 2025
3de4643
add Context to some functions
ChingLongTin Nov 2, 2025
c3a1f5c
dedup Pattern with Block.Case
ChingLongTin Nov 2, 2025
d1170e9
rename mls{Block, Shape, Symbol} helpers
ChingLongTin Nov 3, 2025
36107df
amend! link to functions in Shape.mls
ChingLongTin Nov 3, 2025
ed393fc
flesh out {Block, Shape}.mls
ChingLongTin Nov 3, 2025
1c5fc1a
formatting
ChingLongTin Nov 3, 2025
38069b7
implement ruleMatch
ChingLongTin Nov 3, 2025
f1a209b
remove Label from Block.mls
ChingLongTin Nov 3, 2025
55f4fa1
rename helpers
ChingLongTin Nov 3, 2025
124232f
update Block.mls
ChingLongTin Nov 3, 2025
7c8f3d9
adapt NeilKleistGao's example code
ChingLongTin Nov 3, 2025
6997e89
rename helpers
ChingLongTin Nov 3, 2025
36abad1
refactor ruleMatch
ChingLongTin Nov 3, 2025
64960a8
fix ruleTup
ChingLongTin Nov 3, 2025
a264406
reduce supported cases to make mvp
ChingLongTin Nov 3, 2025
4ae8897
Update Syntax.mls
ChingLongTin Nov 1, 2025
88c3a5e
move detecting staged annotation to ClsLikeDefn
ChingLongTin Nov 4, 2025
17701b9
move importing to diff testing compiler flag
ChingLongTin Nov 4, 2025
5ccb64c
revert formatting changes
ChingLongTin Nov 4, 2025
980d89c
Update Printer.scala
ChingLongTin Nov 5, 2025
ce1e38c
Update Lowering.scala
ChingLongTin Nov 5, 2025
ec3e7b8
Merge branch 'hkmc2' into staged-syntax
ChingLongTin Nov 5, 2025
317c6f6
remove redundant :pt
ChingLongTin Nov 5, 2025
9023eec
fix syntax error
ChingLongTin Nov 5, 2025
9fc83dc
fix type hint for arms in Match
ChingLongTin Nov 5, 2025
7b702f3
add preliminary printCode function
yeungsinchun Nov 5, 2025
e4d7b22
move instrumentation to JSBackendDiffMaker
ChingLongTin Nov 6, 2025
6576f28
rename compiler flag to :staging
ChingLongTin Nov 6, 2025
4191464
Revert "remove redundant :pt"
ChingLongTin Nov 6, 2025
817e828
combine import statements to list
ChingLongTin Nov 6, 2025
e4f947d
Merge branch 'staged-syntax' into instrument
ChingLongTin Nov 6, 2025
33a3749
reword function
ChingLongTin Nov 6, 2025
b10e975
Revert "move instrumentation to JSBackendDiffMaker"
ChingLongTin Nov 6, 2025
b93b5eb
Merge branch 'staged-syntax' into instrument-simple
ChingLongTin Nov 6, 2025
791b5b4
prune implementation for mvp
ChingLongTin Nov 6, 2025
578dfd1
implement rudimentary instrumentation logic
ChingLongTin Nov 6, 2025
27bf507
implement printing for DynSelect
ChingLongTin Nov 6, 2025
9c975f6
only stage functions in staged modules
ChingLongTin Nov 6, 2025
ff6b121
update Syntax.mls
ChingLongTin Nov 6, 2025
068fd37
remove bugs in pretty printing
yeungsinchun Nov 7, 2025
f70f346
Merge branch 'pretty-print' into instrument-simple
yeungsinchun Nov 7, 2025
3f32b39
formatting
ChingLongTin Nov 8, 2025
d3bd300
add back Symbol class
ChingLongTin Nov 8, 2025
8ef91e9
reuse original function name
ChingLongTin Nov 8, 2025
3c7f58d
update test cases
ChingLongTin Nov 8, 2025
2ecda34
fix ruleVal
ChingLongTin Nov 8, 2025
9ebf26b
add some debug symbol names
ChingLongTin Nov 8, 2025
0198eb9
rename PathLike to ArgWrappable
ChingLongTin Nov 8, 2025
8fa331b
fix applyBlock impl
ChingLongTin Nov 9, 2025
e167b20
refactor transformFunDefn
ChingLongTin Nov 10, 2025
0e08e07
remove spread from Arg
ChingLongTin Nov 10, 2025
827f6af
clean up outdated comments and helpers
ChingLongTin Nov 10, 2025
57b45fd
implement ruleTup
ChingLongTin Nov 10, 2025
37eb7a1
formatting
ChingLongTin Nov 10, 2025
69f5997
implement ruleSel and ruleInst
ChingLongTin Nov 10, 2025
6831c61
remove outdated comment
ChingLongTin Nov 10, 2025
ab92537
clean up symName printing
ChingLongTin Nov 10, 2025
29c1109
formatting
ChingLongTin Nov 10, 2025
55fdf26
Add shape in diff testing
yeungsinchun Nov 11, 2025
1e0eb65
update note
ChingLongTin Nov 17, 2025
b93a91c
update PrintCode printout
ChingLongTin Nov 17, 2025
772c93a
update Shape constructors
ChingLongTin Nov 17, 2025
fbe45ea
add context
ChingLongTin Nov 17, 2025
873c8cc
remove outdated comments
ChingLongTin Nov 17, 2025
31809c7
correc comment
yeungsinchun Nov 20, 2025
91e5197
fix pretty print to print mlscript
yeungsinchun Nov 20, 2025
f1886ec
refactor
ChingLongTin Nov 20, 2025
d99eda9
follow JSBuilder pretty printing for DynSelect in Printer
ChingLongTin Nov 20, 2025
02303a3
remove Field from Case
ChingLongTin Nov 20, 2025
d6dc21e
change transformBlock signature
ChingLongTin Nov 20, 2025
cde7a68
implement more rules
ChingLongTin Nov 20, 2025
1f3ee7c
remove import Option from Shape.mls
ChingLongTin Nov 20, 2025
0499166
replace Multiple with ShapeSet
ChingLongTin Nov 20, 2025
fbb869c
misc fixes to Shape.mls
ChingLongTin Nov 20, 2025
adf5590
patch showLiteral for null and undefined
ChingLongTin Nov 20, 2025
ada9c3a
replace ruleVal
ChingLongTin Nov 21, 2025
9f6b1ab
clean up
ChingLongTin Nov 21, 2025
ba80ea4
add symbol to Class Shape for matching
ChingLongTin Nov 21, 2025
8efd61d
add spread field to Arg
ChingLongTin Nov 21, 2025
e19dbd6
update PrintCode.mls
ChingLongTin Nov 21, 2025
146a198
add ruleCls
ChingLongTin Nov 21, 2025
e1aedc7
formatting
ChingLongTin Nov 21, 2025
ed85e75
progress on rest of the rules
ChingLongTin Nov 21, 2025
53a0aa4
replace Block.Case with Pattern
ChingLongTin Nov 21, 2025
3a992ad
implement ShapeSet and ShapeMap
ChingLongTin Nov 21, 2025
5840db4
fix equality checks for Symbol
ChingLongTin Nov 22, 2025
bc2f624
implement Shape{Map, Set}
ChingLongTin Nov 22, 2025
e2454b4
fill out ShapeSet functionailty
ChingLongTin Nov 22, 2025
746cbd3
link ShapeSet symbols for instrumentation
ChingLongTin Nov 22, 2025
9915656
fix Shape, ShapeSet implementations
ChingLongTin Nov 22, 2025
4930252
remove TrivialResult
ChingLongTin Nov 22, 2025
b3c707f
replace Shape with ShapeSet during instrumentation
ChingLongTin Nov 23, 2025
e76c1fd
add message to assertions
ChingLongTin Nov 23, 2025
d792553
refactor match rules
ChingLongTin Nov 23, 2025
1020d38
add debug print Shape
ChingLongTin Nov 23, 2025
eece7ac
print more information for ClsLikeDefn
ChingLongTin Nov 23, 2025
df68b1d
combine Pattern with Case
ChingLongTin Nov 23, 2025
aa3b19a
fill out more match instrumentation
ChingLongTin Nov 23, 2025
4af5839
update comments
ChingLongTin Nov 23, 2025
2c67c8e
update PrintCode.mls
ChingLongTin Nov 23, 2025
c90118c
move optionNme inside stageCode scope
ChingLongTin Nov 23, 2025
f149026
Merge remote-tracking branch 'mlscript/hkmc2' into instrument
ChingLongTin Nov 23, 2025
aae0086
Revert "move optionNme inside stageCode scope"
ChingLongTin Nov 23, 2025
986ace7
use more meaningful names in Functions.mls
ChingLongTin Nov 23, 2025
f0456e1
refactor collectApply
ChingLongTin Nov 23, 2025
d510452
formatting
ChingLongTin Nov 23, 2025
0b2b7ea
update comments and debug info
ChingLongTin Nov 23, 2025
b6c86e1
remove redundant import
ChingLongTin Nov 24, 2025
869c519
match pretty printing syntax for DynSelect with formalization
ChingLongTin Nov 24, 2025
cff8896
fix mkClass
ChingLongTin Nov 24, 2025
0d95197
Merge branch 'hkmc2' into instrument
ChingLongTin Nov 24, 2025
8853371
formatting
ChingLongTin Nov 24, 2025
60c777c
Merge branch 'instrument' of https://github.com/ChingLongTin/mlscript…
ChingLongTin Nov 24, 2025
a343c36
fix syntax errors
ChingLongTin Nov 24, 2025
3c4faec
patch in instrumentation
ChingLongTin Nov 24, 2025
b7c3698
unreserve Option in Elaborator
ChingLongTin Nov 24, 2025
c149a11
remove argument names from class shape
ChingLongTin Nov 24, 2025
1f5c8d7
move functions
ChingLongTin Nov 24, 2025
a6b0162
add ClassSymbol
ChingLongTin Nov 24, 2025
c9f3c6a
formatting
ChingLongTin Nov 24, 2025
db7395d
clean up
ChingLongTin Nov 24, 2025
af3f436
Revert "move functions"
ChingLongTin Nov 24, 2025
89f414c
update diff output
yeungsinchun Nov 25, 2025
6db62ea
pass staged annotation in lowering
yeungsinchun Nov 26, 2025
c1f00e8
Revert "patch in instrumentation"
ChingLongTin Nov 26, 2025
143ce9b
rename functions
ChingLongTin Nov 24, 2025
fcbe8c9
add debug information to rules
ChingLongTin Nov 24, 2025
87b5457
move paramsOpt into ClassSymbol
ChingLongTin Nov 25, 2025
67a099c
implement pruning of bad arms from Match statement
ChingLongTin Nov 26, 2025
9f148af
add debug name to transformSymbol
ChingLongTin Nov 26, 2025
cd26ae1
remove code duplication from ruleBranch
ChingLongTin Nov 26, 2025
2b5b30a
fix typo
ChingLongTin Nov 26, 2025
8b459b3
Merge branch 'fix-pretty-print' into instrument
yeungsinchun Nov 26, 2025
5d63091
update pretty printing to print actual mlscript
yeungsinchun Nov 26, 2025
cc24cae
only pass the "staged" annotation for non-companion module
yeungsinchun Nov 26, 2025
2b1d157
Merge remote-tracking branch 'origin/instrument' into instrument
yeungsinchun Nov 26, 2025
bcafb98
reduce test cases
yeungsinchun Nov 26, 2025
b9eee38
fix class definition printing
yeungsinchun Nov 26, 2025
50f07ca
add match error on pretty printing
yeungsinchun Nov 26, 2025
678dbc9
fix test cases
yeungsinchun Nov 26, 2025
71a8918
some unimportant change
yeungsinchun Nov 26, 2025
7151735
some more unimportant change
yeungsinchun Nov 26, 2025
2548c78
Merge branch 'instrument-with-pretty-print' into instrument
yeungsinchun Nov 26, 2025
f582fa5
printed correctly now
yeungsinchun Nov 26, 2025
0a6087f
Revert "printed correctly now"
yeungsinchun Nov 26, 2025
cd7f7bc
Revert "Merge branch 'instrument-with-pretty-print' into instrument"
yeungsinchun Nov 26, 2025
34d0914
Revert "some more unimportant change"
yeungsinchun Nov 26, 2025
a3925e5
Revert "Merge remote-tracking branch 'origin/instrument' into instrum…
yeungsinchun Nov 26, 2025
79c90ee
Revert "update pretty printing to print actual mlscript"
yeungsinchun Nov 26, 2025
430d941
Merge branch 'hkmc2' into instrument
ChingLongTin Nov 26, 2025
ff58a1a
Reapply "Merge remote-tracking branch 'origin/instrument' into instru…
ChingLongTin Nov 27, 2025
50b6fd5
fix static impl
ChingLongTin Nov 27, 2025
087c960
add inf back to Tup
ChingLongTin Nov 26, 2025
302e310
fix showArg printing
ChingLongTin Nov 26, 2025
4d8e46c
add more Tup test cases
ChingLongTin Nov 26, 2025
f3ee1c6
formatting
ChingLongTin Nov 26, 2025
c703795
fix params type in FunDefn
ChingLongTin Nov 27, 2025
c124135
add Context to ruleEnd
ChingLongTin Nov 28, 2025
45b7b5e
uppdate Match rules
ChingLongTin Nov 28, 2025
408733a
mark pretty printing equality as fixme
ChingLongTin Nov 28, 2025
06312fb
Merge branch 'hkmc2' into instrument
ChingLongTin Dec 11, 2025
0e2fec8
progress: deprecate ShapeSet from StagedPath
ChingLongTin Dec 12, 2025
97ede40
progress: strip shape logic from rules
ChingLongTin Dec 12, 2025
d507d3f
remove inferring shape from Instrumentation
ChingLongTin Dec 12, 2025
ff7385d
progress: add StagedPath to ArgWrappable
ChingLongTin Dec 12, 2025
6db1ee7
progress: simplify impl
ChingLongTin Dec 12, 2025
977433a
progress: formatting
ChingLongTin Dec 12, 2025
b79613a
Merge branch 'hkmc2' into instrument
ChingLongTin Dec 12, 2025
f58ecc5
Merge remote-tracking branch 'mlscript/hkmc2' into instrument
ChingLongTin Dec 18, 2025
e21bff2
fix printCode syntax
ChingLongTin Dec 18, 2025
55e1075
remove equality hack in Shape.mls
ChingLongTin Dec 18, 2025
aa72ded
switch from ValDefn to Scoped
ChingLongTin Dec 18, 2025
326ac69
fix: make new dSym when cloning FunDefn
ChingLongTin Dec 18, 2025
77acb04
remove context from ruleEnd
ChingLongTin Dec 18, 2025
ccc950d
add staging for functions with arguments
ChingLongTin Dec 18, 2025
dee92b1
progress: simplify StagedPath
ChingLongTin Dec 18, 2025
e6fd90a
progress: Make blockCtor produce StagedPath
ChingLongTin Dec 18, 2025
59a4af2
use ClassSymbol for ruleApp
ChingLongTin Dec 18, 2025
5adcfa3
formatting
ChingLongTin Dec 18, 2025
a4c8df0
update tests
ChingLongTin Dec 18, 2025
7abddf9
remove StagedPath
ChingLongTin Dec 22, 2025
f0c6f52
formatting
ChingLongTin Dec 22, 2025
a407e01
inline functions
ChingLongTin Dec 22, 2025
e40a1e6
formatting
ChingLongTin Dec 22, 2025
69a142f
formatting
ChingLongTin Dec 22, 2025
6b012b2
remove Shape{Map, Set} definitions
ChingLongTin Dec 22, 2025
8a713b5
Merge branch 'hkmc2' into instrument
ChingLongTin Dec 22, 2025
51e5513
formatting
ChingLongTin Dec 23, 2025
0cc778e
imrpove symbol information for select
ChingLongTin Dec 23, 2025
e3f052b
simplify applyRuleBranch
ChingLongTin Dec 23, 2025
ed36c03
import option file during MLsDiffMaker init
ChingLongTin Dec 23, 2025
b18776f
remove import statement for option in MLsDiffMaker
ChingLongTin Dec 23, 2025
3df66e2
fixup! remove import statement for option in MLsDiffMaker
ChingLongTin Dec 24, 2025
9212c53
include staging tests in diff tests
ChingLongTin Dec 24, 2025
9f2ca50
remove redundant codegen
ChingLongTin Dec 24, 2025
504d21a
Merge branch 'hkmc2' into instrument
ChingLongTin Dec 24, 2025
247a4c9
Merge branch 'hkmc2' into instrument
ChingLongTin Jan 8, 2026
7da3eea
add Block symbol following pr #375
ChingLongTin Jan 8, 2026
7e115d7
formatting
ChingLongTin Jan 8, 2026
9bcdf99
add Scoped, Loop, Break nodes to Block IR
ChingLongTin Jan 8, 2026
8585c5e
Merge branch 'hkmc2' into instrument
ChingLongTin Jan 13, 2026
6f456bb
Merge branch 'hkmc2' into instrument
ChingLongTin Jan 20, 2026
b16fdf4
replace parameters of functions with its symbol instead of undefined
ChingLongTin Jan 14, 2026
b516237
retain scoped symbols after instrumentation
ChingLongTin Jan 21, 2026
0b968ed
update test
ChingLongTin Jan 21, 2026
78e754f
replace calls to staged functions
ChingLongTin Jan 23, 2026
fe08c3b
Wrap function body
ChingLongTin Jan 23, 2026
efb6ae8
move printing into constructor
ChingLongTin Jan 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 34 additions & 36 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Instrumentation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,26 @@ class InstrumentationImpl(using State):
blockCtor("End", Ls(), symName)(k)

def ruleBranches(x: Path, p: Path, arms: Ls[Case -> Block], dflt: Opt[Block], symName: String = "branches")(using Context)(k: (Path, Context) => Block): Block =
def applyRuleBranch(cse: Case, block: Block)(f: Path => (Context, Path) => Block)(ctx: Context, x: Path): Block =
def applyRuleBranch(cse: Case, block: Block)(f: Path => Context => Block)(ctx: Context): Block =
transformCase(cse): cse =>
transformBlock(block)(using ctx.clone() += p -> x): (y, ctx) =>
// TODO: use Arm type instead of Tup
tuple(Ls(cse, y), "branch"): cde =>
f(cde)(ctx.clone() -= p, x)

val a = arms.map(applyRuleBranch).collectApply
((f: (Ls[Path], Context) => Block) => a(ys => (ctx, _) => f(ys, ctx))(summon, x)): (arms, ctx) =>
tuple(arms): arms =>
ruleEnd(): e =>
// TODO: use transformOption here
def dfltStaged(k: (Path, Context) => Block) =
dflt match
case S(dflt) =>
transformBlock(dflt)(using ctx.clone() += p -> x): (dflt, ctx) =>
optionSome(dflt)(k(_, ctx.clone() -= p))
case N => optionNone()(k(_, ctx))
dfltStaged: (dflt, ctx) =>
blockCtor("Match", Ls(x, arms, dflt, e), symName)(k(_, ctx))
f(cde)(ctx.clone() -= p)

(arms.map(applyRuleBranch).collectApply(_: Ls[Path] => Context => Block)(summon)): arms =>
ctx =>
tuple(arms): arms =>
ruleEnd(): e =>
// TODO: use transformOption here
def dfltStaged(k: (Path, Context) => Block) =
dflt match
case S(dflt) =>
transformBlock(dflt)(using ctx.clone() += p -> x): (dflt, ctx) =>
optionSome(dflt)(k(_, ctx.clone() -= p))
case N => optionNone()(k(_, ctx))
dfltStaged: (dflt, ctx) =>
blockCtor("Match", Ls(x, arms, dflt, e), symName)(k(_, ctx))

// transformations of Block

Expand All @@ -137,15 +137,18 @@ class InstrumentationImpl(using State):
ctx.get(p).map(k).getOrElse:
p match
case Value.Ref(l, disamb) =>
// not in formalization
transformSymbol(disamb.getOrElse(l)): sym =>
blockCtor("ValueRef", Ls(sym), "var")(k)
case l: Value.Lit =>
blockCtor("ValueLit", Ls(l), "lit")(k)
case Select(p, i @ Tree.Ident(name)) =>
case s @ Select(p, i @ Tree.Ident(name)) =>
transformPath(p): x =>
blockCtor("Symbol", Ls(toValue(name))): name =>
blockCtor("Select", Ls(x, name), "sel")(k)
val sym =
if s.symbol.isDefined
then transformSymbol(s.symbol.get)
else blockCtor("Symbol", Ls(toValue(name)))
sym: sym =>
blockCtor("Select", Ls(x, sym), "sel")(k)
case DynSelect(qual, fld, arrayIdx) =>
transformPath(qual): x =>
transformPath(fld): y =>
Expand Down Expand Up @@ -212,8 +215,7 @@ class InstrumentationImpl(using State):
transformResult(r): y =>
transformSymbol(x): xSym =>
blockCtor("ValueRef", Ls(xSym)): xStaged =>
// x should always be defined, either as an argument to the function or in a Scope Block
assert(ctx.get(x.asPath).isDefined)
assert(ctx.get(x.asPath).isDefined, "x should always be defined, either as an argument to the function or in a Scope Block")
(Assign(x, xStaged, _)):
given Context = ctx.clone() += x.asPath -> xStaged
transformBlock(b): (z, ctx) =>
Expand All @@ -228,14 +230,13 @@ class InstrumentationImpl(using State):
blockCtor("ClsLikeDefn", Ls(c, none)): cls =>
blockCtor("Define", Ls(cls, p)): p =>
ruleEnd(): end =>
fnPrintCode(p)(k(end, summon))
case End(_) => ruleEnd()(k(_, summon))
fnPrintCode(p)(k(end, ctx))
case End(_) => ruleEnd()(k(_, ctx))
case Match(p, ks, dflt, rest) =>
transformPath(p): x =>
ruleBranches(x, p, ks, dflt): (stagedMatch, ctx1) =>
transformBlock(rest)(using ctx1): (z, ctx2) =>
fnConcat(stagedMatch, z, "match"): cde =>
k(cde, ctx2)
ruleBranches(x, p, ks, dflt): (stagedMatch, ctx) =>
transformBlock(rest)(using ctx): (z, ctx) =>
fnConcat(stagedMatch, z, "match")(k(_, ctx))
case Begin(sub, rest) =>
// TODO: This is untested as there is no test case that generates the Begin block yet
transformBlock(sub): (sub, ctx) =>
Expand All @@ -255,18 +256,15 @@ class InstrumentationImpl(using State):
// NOTE: this debug printing only works for top-level modules, nested modules don't work
// TODO: remove it. only for test
val debug = blockCtor("ValueLit", Ls(Value.Lit(Tree.UnitLit(false)))): undef =>
// TODO: put correct parameters instead of End
val argsList = f.params.map(ps => List.fill(ps.params.length)(undef))
def makeCalls(k: Path => Block) =
argsList.foldRight(k)((args, cont) => res => call(res, args)(cont))(sym)
makeCalls(fnPrintCode(_)(End()))
// TODO: put correct parameters instead of undefined
f.params.map(ps => List.fill(ps.params.length)(undef))
.foldRight((p: Path) => fnPrintCode(p)(End()))((args, cont) => call(_, args)(cont))(sym)

val dSym = TermSymbol(f.dSym.k, f.dSym.owner, Tree.Ident(f.sym.nme + "_gen"))
val args = f.params.flatMap(_.params).map(_.sym)
val newBody =
ruleEnd(): end =>
given Context = HashMap(args.map(s => Value.Ref(s, N) -> Value.Ref(s, N))*)
transformBlock(f.body)(p => Return(p, false))
given Context = HashMap(args.map(s => Value.Ref(s, N) -> Value.Ref(s, N))*)
transformBlock(f.body)(Return(_, false))
val newFun = f.copy(sym = genSym, dSym = dSym, body = newBody)(false)
(newFun, debug)

Expand Down
26 changes: 13 additions & 13 deletions hkmc2/shared/src/test/mlscript/staging/Functions.mls
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ staged module Expressions with
//│ > Assign(Symbol("scrut"), Tuple([...Ref(Symbol("x"))]), Match(Ref(Symbol("scrut")), [Tup(0, false) -> Return(Lit(1), false), Tup(1, true) -> Assign(Symbol("element0$"), Call(Select(Select(Ref(Symbol("runtime")), Symbol("Tuple")), Symbol("get")), [Ref(Symbol("scrut")), Lit(0)]), Assign(Symbol("middleElements"), Call(Select(Select(Ref(Symbol("runtime")), Symbol("Tuple")), Symbol("slice")), [Ref(Symbol("scrut")), Lit(1), Lit(0)]), Assign(Symbol("a"), Ref(Symbol("element0$")), Return(Lit(3), false))))], Return(Lit(0), false), End))
//│ x = [1, 2, 3]

:js
:staging
:fixme
staged module OtherBlocks with
fun breakAndLabel() =
if 1 is
2 then 0
3 then 0
else 0
//│ /!!!\ Uncaught error: scala.NotImplementedError: an implementation is missing

:js
:staging
class Outside(a)
Expand All @@ -57,9 +46,9 @@ staged module ClassInstrumentation with
//│ > Define(ClsLikeDefn(ClassSymbol("NoArg"), TODO), End)
//│ > Define(ClsLikeDefn(ClassSymbol("Inside":[Symbol("a"), Symbol("b")]), TODO), End)
//│ > Return(Instantiate(Ref(ClassSymbol("Outside":[Symbol("a")])), [Lit(1)]), false)
//│ > Return(Instantiate(Select(Ref(Symbol("ClassInstrumentation")), Symbol("NoArg")), []), false)
//│ > Return(Instantiate(Select(Ref(Symbol("ClassInstrumentation")), ClassSymbol("NoArg")), []), false)
//│ > Return(Call(Ref(ClassSymbol("Outside":[Symbol("a")])), [Lit(1)]), false)
//│ > Return(Call(Select(Ref(Symbol("ClassInstrumentation")), Symbol("Inside")), [Lit(1), Lit(2)]), false)
//│ > Return(Call(Select(Ref(Symbol("ClassInstrumentation")), ClassSymbol("Inside":[Symbol("a"), Symbol("b")])), [Lit(1), Lit(2)]), false)

:js
:staging
Expand All @@ -71,6 +60,17 @@ staged module Arguments with
//│ > Assign(Symbol("x"), Lit(1), Return(Ref(Symbol("x")), false))
//│ > Return(Lit(undefined), false)

:js
:staging
:fixme
staged module OtherBlocks with
fun breakAndLabel() =
if 1 is
2 then 0
3 then 0
else 0
//│ /!!!\ Uncaught error: scala.NotImplementedError: an implementation is missing

// debug printing fails, collision with class name?
:js
:staging
Expand Down
4 changes: 3 additions & 1 deletion hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ abstract class MLsDiffMaker extends DiffMaker:
if stageCode.isSet then
given Config = mkConfig
processTrees(
PrefixApp(Keywrd(`import`), StrLit(blockFile.toString)) :: Nil)
PrefixApp(Keywrd(`import`), StrLit(blockFile.toString))
:: PrefixApp(Keywrd(`import`), StrLit(optionFile.toString))
:: Nil)
super.init()


Expand Down
Loading