Skip to content

Commit 4280ea7

Browse files
committed
Rename WrappedMap to MutMap and use functions rather than methods
1 parent f2dec9e commit 4280ea7

25 files changed

+532
-358
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import "./Option.mls"
2+
import "./Predef.mls"
3+
4+
open Predef
5+
open Option { Some, None }
6+
7+
8+
module MutMap with ...
9+
10+
class MutMap(val underlying) with
11+
12+
set this.(Symbol.iterator) =
13+
() => underlying.(Symbol.iterator)()
14+
15+
fun get(key)(m) =
16+
if m.underlying.has(key)
17+
then Some of m.underlying.get(key)
18+
else None
19+
20+
fun insert(key, value)(m) =
21+
m.underlying.set(key, value)
22+
()
23+
24+
fun updateWith(key)(op)(m) =
25+
if op(m |> get(key)) is
26+
Some(value) then m.underlying.set(key, value)
27+
None then m.underlying.delete(key)
28+
29+
fun keysIterator(m) = m.underlying.keys()
30+
31+
fun valuesIterator(m) = m.underlying.values()
32+
33+
fun values(m) = Array.from of m.underlying.values()
34+
35+
fun toMap(entries) =
36+
let
37+
m = empty
38+
i = 0
39+
length = entries.length
40+
while i < length do
41+
m |> insert of ...entries.at(i)
42+
set i += 1
43+
m
44+
45+
fun empty = new MutMap(new Map())
46+
47+

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

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,49 @@ let Predef1;
3535
static pipeFrom(f2, x3) {
3636
return runtime.safeCall(f2(x3))
3737
}
38-
static tap(x4, f3) {
38+
static pipeIntoHi(x4, f3) {
39+
return runtime.safeCall(f3(x4))
40+
}
41+
static pipeFromHi(f4, x5) {
42+
return runtime.safeCall(f4(x5))
43+
}
44+
static tap(x6, f5) {
3945
let tmp;
40-
tmp = runtime.safeCall(f3(x4));
41-
return (tmp , x4)
46+
tmp = runtime.safeCall(f5(x6));
47+
return (tmp , x6)
4248
}
43-
static pat(f4, x5) {
49+
static pat(f6, x7) {
4450
let tmp;
45-
tmp = runtime.safeCall(f4(x5));
46-
return (tmp , x5)
51+
tmp = runtime.safeCall(f6(x7));
52+
return (tmp , x7)
4753
}
48-
static andThen(f5, g) {
49-
return (x6) => {
54+
static andThen(f7, g) {
55+
return (x8) => {
5056
let tmp;
51-
tmp = runtime.safeCall(f5(x6));
57+
tmp = runtime.safeCall(f7(x8));
5258
return runtime.safeCall(g(tmp))
5359
}
5460
}
55-
static compose(f6, g1) {
56-
return (x6) => {
61+
static compose(f8, g1) {
62+
return (x8) => {
5763
let tmp;
58-
tmp = runtime.safeCall(g1(x6));
59-
return runtime.safeCall(f6(tmp))
64+
tmp = runtime.safeCall(g1(x8));
65+
return runtime.safeCall(f8(tmp))
66+
}
67+
}
68+
static passTo(receiver, f9) {
69+
return (...args1) => {
70+
return runtime.safeCall(f9(receiver, ...args1))
6071
}
6172
}
62-
static passTo(receiver, f7) {
73+
static passToLo(receiver1, f10) {
6374
return (...args1) => {
64-
return runtime.safeCall(f7(receiver, ...args1))
75+
return runtime.safeCall(f10(receiver1, ...args1))
6576
}
6677
}
67-
static call(receiver1, f8) {
78+
static call(receiver2, f11) {
6879
return (...args1) => {
69-
return f8.call(receiver1, ...args1)
80+
return f11.call(receiver2, ...args1)
7081
}
7182
}
7283
static print(...xs) {
@@ -93,7 +104,7 @@ let Predef1;
93104
static tuple(...xs1) {
94105
return xs1
95106
}
96-
static foldr(f9) {
107+
static foldr(f12) {
97108
return (first, ...rest) => {
98109
let len, i, init, scrut, scrut1, tmp, tmp1, tmp2, tmp3, tmp4, tmp5;
99110
len = rest.length;
@@ -111,7 +122,7 @@ let Predef1;
111122
tmp2 = i - 1;
112123
i = tmp2;
113124
tmp3 = runtime.safeCall(rest.at(i));
114-
tmp4 = runtime.safeCall(f9(tmp3, init));
125+
tmp4 = runtime.safeCall(f12(tmp3, init));
115126
init = tmp4;
116127
tmp5 = runtime.Unit;
117128
continue tmp6;
@@ -120,21 +131,21 @@ let Predef1;
120131
}
121132
break;
122133
}
123-
return runtime.safeCall(f9(first, init))
134+
return runtime.safeCall(f12(first, init))
124135
}
125136
}
126137
}
127138
static mkStr(...xs2) {
128139
let tmp, tmp1, lambda;
129-
lambda = (undefined, function (acc, x6) {
140+
lambda = (undefined, function (acc, x8) {
130141
let tmp2, tmp3, tmp4;
131-
if (typeof x6 === 'string') {
142+
if (typeof x8 === 'string') {
132143
tmp2 = true;
133144
} else {
134145
tmp2 = false;
135146
}
136147
tmp3 = runtime.safeCall(Predef.assert(tmp2));
137-
tmp4 = acc + x6;
148+
tmp4 = acc + x8;
138149
return (tmp3 , tmp4)
139150
});
140151
tmp = lambda;

hkmc2/shared/src/test/mlscript-compile/Predef.mls

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ fun (@) apply(f, ...args) = f(...args)
1414
fun (|>) pipeInto(x, f) = f(x)
1515
fun (<|) pipeFrom(f, x) = f(x)
1616

17+
// Same as above but with high precedence
18+
fun (.>) pipeIntoHi(x, f) = f(x)
19+
fun (<.) pipeFromHi(f, x) = f(x)
20+
1721
fun (!>) tap(x, f) = f(x); x
1822
fun (<!) pat(f, x) = f(x); x
1923

2024
fun (>>) andThen(f, g)(x) = g(f(x))
2125
fun (<<) compose(f, g)(x) = f(g(x))
2226

27+
// x . foo(a, b, c) == foo(x, a, b, c)
2328
fun (.) passTo(receiver, f)(...args) = f(receiver, ...args)
2429

30+
// Same as above but with low precedence
31+
fun (|.) passToLo(receiver, f)(...args) = f(receiver, ...args)
32+
33+
// x . foo(a, b, c) == foo.call(x, a, b, c)
2534
fun (|>.) call(receiver, f)(...args) = f.call(receiver, ...args)
2635

2736
val pass1 = Rendering.pass1

hkmc2/shared/src/test/mlscript-compile/WrappedMap.mls

Lines changed: 0 additions & 43 deletions
This file was deleted.

hkmc2/shared/src/test/mlscript-compile/apps/parsing-web-demo/Examples.mls

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import "../../WrappedMap.mls"
1+
import "../../MutMap.mls"
2+
import "../../Predef.mls"
23

4+
open Predef
35
module Examples with...
46

5-
val examples = WrappedMap.empty
7+
val examples = Map.empty
68

7-
examples.insert of
9+
examples |> Map.insert of
810
"hanoi"
911
name: "Hanoi from Caml Light"
1012
source: """let spaces n = make_string n " ";;
@@ -33,7 +35,7 @@ let rec join_lines l1 l2 l3 =
3335
| _ -> failwith "join_lines";;
3436
"""
3537

36-
examples.insert of
38+
examples |> Map.insert of
3739
"extensible"
3840
name: "Extensible Syntax"
3941
source: """#newKeyword ("hello", Some 3, Some 3)

hkmc2/shared/src/test/mlscript-compile/apps/parsing/Extension.mls

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import "../../Stack.mls"
77
import "../../Option.mls"
88
import "../../Predef.mls"
99
import "../../Iter.mls"
10+
import "../../MutMap.mls"
1011

12+
open Predef
1113
open Stack
1214
open Option { Some, None }
13-
open Predef { print, tuple }
1415
open Token { LiteralKind }
1516
open ParseRule { Choice }
1617

@@ -39,10 +40,10 @@ fun extendKeyword(tree: TreeT) =
3940
// * Add an empty parse rule to the map associated with the given name.
4041
fun newCategory(tree: TreeT) =
4142
if tree is Tree.Literal(Token.LiteralKind.String, name) and
42-
Rules.syntaxKinds.get(name) is
43+
Rules.syntaxKinds |> MutMap.get(name) is
4344
Some(rule) then print of "Category already exists: " + rule.display
4445
None then
45-
Rules.syntaxKinds.insert(name, ParseRule.ParseRule(name, Nil))
46+
Rules.syntaxKinds |> MutMap.insert(name, ParseRule.ParseRule(name, Nil))
4647
Rules.extendedKinds.add(name)
4748
else
4849
print of "expect a string literal but found " + tree
@@ -52,14 +53,14 @@ pattern ClosedCategory = "ident" | "typevar"
5253

5354
fun extendCategory(choiceBodyTree: TreeT) =
5455
if parseChoiceTree(choiceBodyTree) is
55-
Some([kindName, choice]) and Rules.syntaxKinds.get(kindName) is
56+
Some([kindName, choice]) and Rules.syntaxKinds |> MutMap.get(kindName) is
5657
Some(rule) and kindName is
5758
// If the `kindName` is built-in and extensible, we check if the choice
5859
// refers to another category in the beginning. If so, we inline the
5960
// referenced category's choices into the current choice.
6061
OpenCategory and choice is
6162
Choice.Ref(refKindName, process, outerPrec, innerPrec, rest) and
62-
Rules.syntaxKinds.get(refKindName) is Some(refRule) then
63+
Rules.syntaxKinds |> MutMap.get(refKindName) is Some(refRule) then
6364
// TODO: The `process` function should be applied to the referenced rule.
6465
// TODO: How to handle `outerPrec` and `innerPrec`?
6566
rule.extendChoices of refRule.andThen(rest, process).choices
@@ -79,7 +80,7 @@ fun parseChoiceTree(tree: TreeT) =
7980
fun go(trees: StackT[TreeT]): Choice.Choice =
8081
let res = if trees is
8182
Tree.App(Tree.Ident("keyword", _), Tree.Literal(LiteralKind.String, name)) :: rest and
82-
Keywords.all.get(name) is Some(keyword) then
83+
Keywords.all |> MutMap.get(name) is Some(keyword) then
8384
Choice.keyword(keyword)(go(rest))
8485
Tree.Literal(LiteralKind.String, name) :: rest then
8586
Choice.reference(name)(process: Cons, name: "unnamed", choices: tuple of go(rest))

hkmc2/shared/src/test/mlscript-compile/apps/parsing/Keywords.mls

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import "../../Option.mls"
22
import "../../Iter.mls"
33
import "../../Predef.mls"
4-
import "../../WrappedMap.mls"
4+
import "../../MutMap.mls"
55
import "./Keyword.mls"
66

7+
open Predef
78
open Option { Some, None }
8-
open Predef { tuple }
99
open Keyword { INT_MIN, INT_MAX }
1010

1111
module Keywords with ...
1212

1313
fun makePrecMap(startPrec: Int, ...ops) =
1414
let
15-
m = WrappedMap.empty
15+
m = MutMap.empty
1616
i = 0
1717
while i < ops.length do
1818
ops.at(i).split(" ").forEach of (op, _, _) =>
1919
if op.length > 0 do
20-
m.insert of op, i + startPrec
20+
m |> MutMap.insert of op, i + startPrec
2121
set i += 1
2222
m
2323

24-
val all = WrappedMap.empty
24+
val all = MutMap.empty
2525
fun keyword(name, leftPrec, rightPrec) =
2626
let result = Keyword.Keyword(name, leftPrec, rightPrec)
27-
all.insert(name, result)
27+
all |> MutMap.insert(name, result)
2828
result
2929
let prec = 0
3030
fun currPrec = Some(prec)
@@ -89,12 +89,12 @@ let precMap = makePrecMap of
8989
"+ -"
9090
"* %"
9191
"~"
92-
"" // perfix operators
92+
"" // prefix operators
9393
"" // applications
9494
"."
9595

9696
// The largest precedence value of all operators.
97-
val periodPrec = precMap.get(".") Option.unsafe.get()
97+
val periodPrec = precMap |> MutMap.get(".") |> Option.unsafe.get
9898

9999
val _period = keyword of ".", Some(periodPrec), Some(periodPrec)
100100

@@ -103,9 +103,9 @@ val appPrec = maxOperatorPrec - 1
103103
val prefixPrec = appPrec - 1
104104

105105
// Get the precedence of a single character operator.
106-
fun charPrec(op) = precMap.get(op) Option.unsafe.get()
106+
fun charPrec(op) = precMap |> MutMap.get(op) |> Option.unsafe.get
107107

108-
fun charPrecOpt(op) = precMap.get(op)
108+
fun charPrecOpt(op) = precMap |> MutMap.get(op)
109109

110110
val _asterisk = keyword of "*", charPrecOpt("*"), charPrecOpt("*")
111111
val _equalequal = keyword of "==", charPrecOpt("="), charPrecOpt("=")
@@ -121,11 +121,11 @@ val _rightCurly = keyword of "}", basePrec, None
121121
val _begin = keyword of "begin", bracketPrec, basePrec
122122
val _end = keyword of "end", basePrec, None
123123

124-
let builtinKeywords = new Set(all.keysIterator)
124+
let builtinKeywords = new Set(all |> MutMap.keysIterator)
125125
fun extended = all
126126
Iter.filtering of case [k, _] then builtinKeywords.has(k) is false
127127
Iter.toArray()
128-
WrappedMap.toMap()
128+
MutMap.toMap()
129129

130130
pattern Letter = "a" ..= "z" | "A" ..= "Z"
131131

hkmc2/shared/src/test/mlscript-compile/apps/parsing/ParseRule.mls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "../../WrappedMap.mls"
1+
import "../../MutMap.mls"
22
import "../../Iter.mls"
33
import "../../Option.mls"
44
import "../../Stack.mls"
@@ -216,7 +216,7 @@ data class ParseRule[A](name: Str, choices: StackT[ChoiceT[A]]) with
216216
else []
217217
Iter.flattening()
218218
Iter.toArray()
219-
WrappedMap.toMap()
219+
MutMap.toMap()
220220

221221
// Collect all keyword choices in this rule.
222222
fun keywordChoices = _keywordChoices.get()

0 commit comments

Comments
 (0)