Skip to content

Commit

Permalink
Add a couple of test cases + small change
Browse files Browse the repository at this point in the history
  • Loading branch information
LPTK committed Dec 4, 2024
1 parent 910b671 commit fe1085d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hkmc2/shared/src/main/scala/hkmc2/syntax/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ abstract class Parser(
case (NEWLINE, _) :: (KEYWORD(kw), _) :: _
if kw.canStartInfixOnNewLine && kw.leftPrecOrMin > prec
&& infixRules.kwAlts.contains(kw.name)
&& kw != Keyword.`do` // This is to avoid the following case:
&& (kw isnt Keyword.`do`) // This is to avoid the following case:
// ```
// 0 then "null"
// do console.log("non-null")
Expand Down
20 changes: 19 additions & 1 deletion hkmc2/shared/src/test/mlscript/codegen/While.mls
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ let i = 0 in while
//│ > 2
//│ > 3

let i = 0 in while
do log(i)
i < 3 do set i += 1
//│ > 0
//│ > 1
//│ > 2
//│ > 3


// * Note that the semantics of UCS-while is quite subtle.
// * Currently, we only treat the *top-level* `else` as terminating the loop;
Expand Down Expand Up @@ -232,6 +240,16 @@ f(Cons(1, Cons(2, Cons(3, 0))))
//│ > 3
//│ > Done!

fun f(ls) =
while
do print(ls)
ls is Cons(h, tl) do set ls = tl

f(Cons(1, Cons(2, Cons(3, 0))))
//│ > Cons(1, Cons(2, Cons(3, 0)))
//│ > Cons(2, Cons(3, 0))
//│ > Cons(3, 0)
//│ > 0


// ——— FIXME: ———
Expand All @@ -246,7 +264,7 @@ while log("Hello World"); false
then 0(0)
else 1
//│ ╔══[PARSE ERROR] Unexpected 'then' keyword here
//│ ║ l.246: then 0(0)
//│ ║ l.264: then 0(0)
//│ ╙── ^^^^
//│ ═══[ERROR] Unrecognized term split (false literal).
//│ ═══[RUNTIME ERROR] Error: match error
Expand Down
19 changes: 17 additions & 2 deletions hkmc2/shared/src/test/mlscript/ucs/syntax/Do.mls
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ if x do
x
//│ = false

if not(x) do
print("executed")
//│ > executed

:e
:re
if not of x do
print("executed")
//│ ╔══[ERROR] Unrecognized term split (application).
//│ ║ l.25: if not of x do
//│ ║ ^^^^^^^^^^^
//│ ║ l.26: print("executed")
//│ ╙── ^^^^^^^^^^^^^^^^^^^
//│ ═══[RUNTIME ERROR] Error: match error

if (not of x) do
print("executed")
set x = false
Expand Down Expand Up @@ -56,10 +71,10 @@ fun g(y) =
x
//│ ╔══[ERROR] Mixed use of `do` and `then` in the `if` expression.
//│ ╟── Keyword `then` is used here.
//│ ║ l.55: Some(v) and v % 2 == 0 then set x = Some(v / 2)
//│ ║ l.70: Some(v) and v % 2 == 0 then set x = Some(v / 2)
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//│ ╟── Keyword `do` is used here.
//│ ║ l.54: Some(0) do set x = None
//│ ║ l.69: Some(0) do set x = None
//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^

g(0)
Expand Down

0 comments on commit fe1085d

Please sign in to comment.