Skip to content

Move UCS helper functions to the runtime module #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ trait DesugaringBase(using state: State):
protected lazy val matchResultClass: Ctxl[(Term.Sel | Term.SynthSel, ClassSymbol)] =
(State.runtimeSymbol.ref().selNoSym("MatchResult", synth=true), State.matchResultClsSymbol)

/** Make a pattern looks like `runtime.MatchResult.class`. */
/** Make a pattern that looks like `runtime.MatchResult.class`. */
protected def matchResultPattern(parameters: Opt[List[BlockLocalSymbol]]): Ctxl[Pattern.ClassLike] =
val (classRef, classSym) = matchResultClass
val classSel = Term.SynthSel(classRef, Ident("class"))(S(classSym))
Pattern.ClassLike(classSym, classSel, parameters, false)(Empty())

/** Make a term looks like `runtime.MatchFailure` with its symbol. */
/** Make a term that looks like `runtime.MatchFailure` with its symbol. */
protected lazy val matchFailureClass: Ctxl[(Term.Sel | Term.SynthSel, ClassSymbol)] =
(State.runtimeSymbol.ref().selNoSym("MatchFailure", synth=true), State.matchFailureClsSymbol)

/** Make a pattern looks like `runtime.MatchFailure.class`. */
/** Make a pattern that looks like `runtime.MatchFailure.class`. */
protected def matchFailurePattern(parameters: Opt[List[BlockLocalSymbol]]): Ctxl[Pattern.ClassLike] =
val (classRef, classSym) = matchResultClass
val classSel = Term.SynthSel(classRef, Ident("class"))(S(classSym))
Expand Down
4 changes: 3 additions & 1 deletion hkmc2/shared/src/test/mlscript-compile/Runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ let Runtime1;
return runtime.safeCall(globalThis.Array.prototype.slice.call(xs, i, tmp))
}
static get(xs1, i1) {
return globalThis.Array.prototype.at.call(xs1, i1)
let tmp;
tmp = globalThis.Array.prototype.at.call(xs1, i1);
return Runtime.safeCall(tmp)
}
static toString() { return "Tuple"; }
});
Expand Down
2 changes: 1 addition & 1 deletion hkmc2/shared/src/test/mlscript-compile/Runtime.mls
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module Tuple with

fun get(xs, i) =
// * Contrary to `xs.[i]`, this supports negative indices (Python-style)
globalThis.Array.prototype.at.call(xs, i)
safeCall of globalThis.Array.prototype.at.call(xs, i)

module Str with
fun startsWith(string, prefix) = string.startsWith(prefix)
Expand Down
35 changes: 35 additions & 0 deletions hkmc2/shared/src/test/mlscript/codegen/RuntimeUsage.mls
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,38 @@ print of checkCall(undefined)
//│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value.


print of Tuple.slice([1, 2, 3, 4], 1, -1)
//│ > [2, 3, 4]

print of Tuple.get([1, 2, 3, 4], 0)
//│ > 1

print of Tuple.get([1, 2, 3, 4], -1)
//│ > 4

print of Tuple.get([1, 2, 3, 4], 5)
//│ > ()

print of Str.drop("hello, world", 2)
//│ > llo, world

print of Str.drop("hello, world", -2)
//│ > ld

print of Str.get("hello, world", 2)
//│ > l

print of Str.get("hello, world", 22)
//│ > ()

print of Str.get("hello, world", -1)
//│ > d

print of Str.startsWith("hello, world", "hello")
//│ > true

print of Str.startsWith("hello, world", "")
//│ > true

print of Str.startsWith("hello, world", "farewell")
//│ > false
Loading