@plylang/plylang 1.0.18 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -31
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -6,14 +6,15 @@ The compiler translates a module into JavaScript. There is no separate virtual m
|
|
|
6
6
|
|
|
7
7
|
This text walks through the language once, with a short example of each feature.
|
|
8
8
|
|
|
9
|
-
The repository contains the `plylang` compiler and the `plylang-ls` server. Words that work on ready-made values are not built into the compiler: they are imported. The neighboring package `@
|
|
9
|
+
The repository contains the `plylang` compiler and the `plylang-ls` server. Words that work on ready-made values are not built into the compiler: they are imported. The neighboring package `@plylang/prelude` is one such place, and it has its own documentation. The import lines below name that package. They check in a package that depends on it.
|
|
10
10
|
|
|
11
11
|
## Stack
|
|
12
12
|
|
|
13
13
|
A signature says what a word takes and what it puts. The tail under that prefix stays in place.
|
|
14
14
|
|
|
15
15
|
```ply
|
|
16
|
-
~ "
|
|
16
|
+
~ "@plylang/prelude/Classes/Arithmetic" add
|
|
17
|
+
~ "@plylang/prelude/Data/Num"
|
|
17
18
|
|
|
18
19
|
: double ( Num -- Num )
|
|
19
20
|
dup add
|
|
@@ -95,15 +96,18 @@ The same permutations as in Factor follow, with names made of letters: `dupd`, `
|
|
|
95
96
|
`cons` pushes a value and then runs a quotation. The value sits under the quotation, so `5 [ 1 add ] cons` is a quotation of type `(-- Num)` that pushes five and then adds one. The call site does not name that value.
|
|
96
97
|
|
|
97
98
|
```ply
|
|
98
|
-
~ "
|
|
99
|
+
~ "@plylang/prelude/Classes/Comparable" equal
|
|
100
|
+
~ "@plylang/prelude/Classes/Subtractable" subtract
|
|
101
|
+
~ "@plylang/prelude/Classes/Arithmetic" multiply
|
|
102
|
+
~ "@plylang/prelude/Data/Num"
|
|
99
103
|
|
|
100
104
|
: fact ( Num -- Num )
|
|
101
|
-
dup 0
|
|
105
|
+
dup 0 equal [ drop 1 ] [ dup 1 subtract fact multiply ] if
|
|
102
106
|
```
|
|
103
107
|
|
|
104
|
-
Zero is replaced by one. Otherwise `n` and `n - 1` remain on the stack, the recursive `fact` computes the second, and `
|
|
108
|
+
Zero is replaced by one. Otherwise `n` and `n - 1` remain on the stack, the recursive `fact` computes the second, and `multiply` multiplies.
|
|
105
109
|
|
|
106
|
-
Names before `->` name stack values. The left name is the deeper value, the right name is the top. A quotation sees these names, and they are not part of the input.
|
|
110
|
+
Names before `->` name stack values. The left name is the deeper value, the right name is the top. A quotation sees these names, and they are not part of the input. A clause that only renames the stack, and whose names do not appear in a quotation, is ordinary stack code written without `->`.
|
|
107
111
|
|
|
108
112
|
```ply
|
|
109
113
|
: curry ( a (a b -- c) -- (b -- c) )
|
|
@@ -127,7 +131,7 @@ When the first line of a body is patterns and `->`, the whole body is equations.
|
|
|
127
131
|
|
|
128
132
|
`true` together with `false` covers `Bool`. `Num`, `Int`, and `Str` need a final branch with a name or `_`.
|
|
129
133
|
|
|
130
|
-
A bare constructor
|
|
134
|
+
A bare constructor checks the tag and leaves the value. `(Ctor ..)` spills every field, last on top. `(Just x)` names the one field and does not put it until the name is mentioned. `(Person n _)` is positional: the first field is kept under `n` and the second is discarded. `(Person age name)` binds by name, in either order, when every name is a field. `(Person)` is an error. `(Some (Array Num) items)` checks that the field is an array of numbers and names that array. The check runs when the field's type is still open. `(Some (Array Num))` without a name is an error. `(Some (Num) ..)` checks and spills that number. `(Some (Num) _)` only checks.
|
|
131
135
|
|
|
132
136
|
An array in a pattern has a fixed length. `<>` matches only the empty array. One length does not cover every `Array`.
|
|
133
137
|
|
|
@@ -145,15 +149,15 @@ An array in a pattern has a fixed length. `<>` matches only the empty array. One
|
|
|
145
149
|
; Maybe a = Nothing | Just a
|
|
146
150
|
|
|
147
151
|
: fromMaybe ( a (Maybe a) -- a )
|
|
148
|
-
Nothing ->
|
|
149
|
-
Just -> nip
|
|
152
|
+
(Nothing ..) ->
|
|
153
|
+
(Just ..) -> nip
|
|
150
154
|
|
|
151
155
|
: mapMaybe ( (Maybe a) (a -- b) -- (Maybe b) )
|
|
152
|
-
Nothing q -> q drop Nothing
|
|
156
|
+
(Nothing ..) q -> q drop Nothing
|
|
153
157
|
(Just x) q -> x q call Just
|
|
154
158
|
```
|
|
155
159
|
|
|
156
|
-
The empty body of the `Nothing` branch leaves the fallback value. For `Just` the field
|
|
160
|
+
The empty body of the `(Nothing ..)` branch leaves the fallback value. For `Just`, `(Just ..)` puts the field above it, and `nip` drops what is underneath. In `mapMaybe` the quotation is the top: `q` names it, and `(Just x)` names the field. A bare `Just` would leave the `Maybe` in place.
|
|
157
161
|
|
|
158
162
|
A list with its own structure is a separate type. The head sits in the constructor, and the tail is another value of that type.
|
|
159
163
|
|
|
@@ -161,6 +165,8 @@ A list with its own structure is a separate type. The head sits in the construct
|
|
|
161
165
|
; List a = Nil | Cons head : a, tail : (List a)
|
|
162
166
|
```
|
|
163
167
|
|
|
168
|
+
A `|` may stand before the first constructor, so every arm starts in the same column.
|
|
169
|
+
|
|
164
170
|
Zero fields and one field have no names. `Nothing` stores nothing. `Just a` stores one value, and the equation names it. Two or more fields are `name : Type`, separated by commas. A second positional type is [PL0117](docs/Errors.md#pl0117). One named field is [PL0118](docs/Errors.md#pl0118).
|
|
165
171
|
|
|
166
172
|
```ply
|
|
@@ -173,7 +179,7 @@ Zero fields and one field have no names. `Nothing` stores nothing. `Just a` stor
|
|
|
173
179
|
>>snd
|
|
174
180
|
```
|
|
175
181
|
|
|
176
|
-
`Pair` the word takes the fields in that order, last on top. `frs>>` consumes the pair and leaves the field. When the pair is still needed, copy it first: `dup frs>>`. `>>snd` takes the new field above the pair and returns a new pair. The previous pair is a separate value. A
|
|
182
|
+
`Pair` the word takes the fields in that order, last on top. `frs>>` consumes the pair and leaves the field. When the pair is still needed, copy it first: `dup frs>>`. `>>snd` takes the new field above the pair and returns a new pair. The previous pair is a separate value. A field on every constructor, with the same type, is `name>>`. A field of one constructor is read after that constructor's check: `Sent -> body>>`. `name>>` on `Maybe` is [PL0119](docs/Errors.md#pl0119). `frs>>` on `Int`, on a type variable, or on a synonym of `Num` is [PL0063](docs/Errors.md#pl0063). A missing field is [PL0120](docs/Errors.md#pl0120). `(Page)` is [PL0136](docs/Errors.md#pl0136).
|
|
177
183
|
|
|
178
184
|
In JavaScript a constructor with no fields is `{ tag: "Nothing" }`. One field is `(value) => ({ tag: "Just", value })`. Named fields are properties: `(v0, v1) => ({ tag: "Pair", frs: v0, snd: v1 })`.
|
|
179
185
|
|
|
@@ -184,15 +190,16 @@ In JavaScript a constructor with no fields is `{ tag: "Nothing" }`. One field is
|
|
|
184
190
|
A word that walks an array is imported. In the example the quotation sits on top. The name comes from one module, and the instance for the array comes from another.
|
|
185
191
|
|
|
186
192
|
```ply
|
|
187
|
-
~ "
|
|
188
|
-
~ "
|
|
189
|
-
~ "
|
|
193
|
+
~ "@plylang/prelude/Classes/Mappable" map
|
|
194
|
+
~ "@plylang/prelude/Classes/Arithmetic" add
|
|
195
|
+
~ "@plylang/prelude/Data/Num"
|
|
196
|
+
~ "@plylang/prelude/Data/Array"
|
|
190
197
|
|
|
191
198
|
: shifted ( -- (Array Num) )
|
|
192
199
|
< 1 2 3 > [ 1 add ] map
|
|
193
200
|
```
|
|
194
201
|
|
|
195
|
-
The bare import of `
|
|
202
|
+
The bare import of `Data/Array` brings the instances of that module and brings no names. The result is `2`, `3`, `4`. `dup` of an array shares the JavaScript reference. A word that writes returns a new array, and the input stays.
|
|
196
203
|
|
|
197
204
|
## A name for a type
|
|
198
205
|
|
|
@@ -213,12 +220,13 @@ Parameters are written as they are on a type: `- Twin a (Pair a a)`. In a slot a
|
|
|
213
220
|
`;` with `==` is a distinct type with one constructor and one field. It does not unify with the field. In JavaScript the constructor returns its argument as it is.
|
|
214
221
|
|
|
215
222
|
```ply
|
|
216
|
-
~ "
|
|
223
|
+
~ "@plylang/prelude/Classes/Arithmetic" add
|
|
224
|
+
~ "@plylang/prelude/Data/Num"
|
|
217
225
|
|
|
218
226
|
; Age == Age Num
|
|
219
227
|
|
|
220
228
|
: older ( Age -- Age )
|
|
221
|
-
Age -> 1 add Age
|
|
229
|
+
(Age ..) -> 1 add Age
|
|
222
230
|
```
|
|
223
231
|
|
|
224
232
|
Zero fields, two fields, or two constructors do not fit `==`. A type with `=` is for those.
|
|
@@ -228,10 +236,10 @@ Zero fields, two fields, or two constructors do not fit `==`. A type with `=` is
|
|
|
228
236
|
`(StrMap a)` is a dictionary with key `Str`. The key becomes known at run time and is not named in the type ahead of time. There is no literal. An empty dictionary in JavaScript is `Object.create(null)`.
|
|
229
237
|
|
|
230
238
|
```ply
|
|
231
|
-
~ "
|
|
239
|
+
~ "@plylang/prelude/Data/StrMap" emptyStrMap, insertStrMap
|
|
232
240
|
|
|
233
241
|
: ages ( -- (StrMap Num) )
|
|
234
|
-
|
|
242
|
+
emptyStrMap "Ada" 36 insertStrMap
|
|
235
243
|
```
|
|
236
244
|
|
|
237
245
|
The order in the example is the dictionary, the key, and the value on top.
|
|
@@ -242,35 +250,35 @@ The order in the example is the dictionary, the key, and the value on top.
|
|
|
242
250
|
|
|
243
251
|
```ply
|
|
244
252
|
& Equal a
|
|
245
|
-
|
|
253
|
+
equal ( a a -- Bool )
|
|
246
254
|
|
|
247
255
|
& Equal a => Ordered a
|
|
248
256
|
before ( a a -- Bool )
|
|
249
257
|
```
|
|
250
258
|
|
|
251
259
|
```ply
|
|
252
|
-
~ "
|
|
253
|
-
~ "Num"
|
|
260
|
+
~ "@plylang/prelude/Classes/Comparable" Comparable, equal
|
|
261
|
+
~ "@plylang/prelude/Data/Num"
|
|
254
262
|
|
|
255
|
-
: same (
|
|
256
|
-
|
|
263
|
+
: same ( Comparable a => a a -- Bool )
|
|
264
|
+
equal
|
|
257
265
|
```
|
|
258
266
|
|
|
259
|
-
The instance for numbers is declared in
|
|
267
|
+
The instance for numbers is declared in `@plylang/prelude/Data/Num`. The bare import brings the instances of that module and brings no names. That import gives `same` that instance.
|
|
260
268
|
|
|
261
269
|
A hand-written instance is a word for each method. A module may write one when it defines the class, the type, or the wrapper in the head, or when the head contains a built-in constructor `Int`, `Num`, `Bool`, `Str`, `Unit`, `Array`, or `StrMap`.
|
|
262
270
|
|
|
263
271
|
```ply
|
|
264
272
|
& Equal a
|
|
265
|
-
|
|
273
|
+
equal ( a a -- Bool )
|
|
266
274
|
|
|
267
275
|
; Box a = Mk a
|
|
268
276
|
|
|
269
277
|
: eqBox ( Equal a => (Box a) (Box a) -- Bool )
|
|
270
|
-
(Mk x) (Mk y) -> x y
|
|
278
|
+
(Mk x) (Mk y) -> x y equal
|
|
271
279
|
|
|
272
280
|
# Equal (Box a)
|
|
273
|
-
|
|
281
|
+
equal = eqBox
|
|
274
282
|
```
|
|
275
283
|
|
|
276
284
|
A parameter of kind `Type -> Type` writes the constructor in the head.
|
|
@@ -325,7 +333,7 @@ A package is described by `package.json`.
|
|
|
325
333
|
}
|
|
326
334
|
```
|
|
327
335
|
|
|
328
|
-
A `foreign` word has no body. Its implementation lives in the foreign directory, and the number of JavaScript parameters matches the input length. Leave it out of the `@` list when it stays in this file.
|
|
336
|
+
A `foreign` word has no body. Its implementation lives in the foreign directory, and the number of JavaScript parameters matches the input length. Leave it out of the `@` list when it stays in this file. A foreign word that receives an array or a `StrMap` does not change that value. It copies, then writes. `dup` shares the reference. `Cell` is the type of a shared cell.
|
|
329
337
|
|
|
330
338
|
```ply
|
|
331
339
|
: foreign length ( (Array a) -- Num )
|
|
@@ -349,7 +357,7 @@ plylang check --js <file>
|
|
|
349
357
|
|
|
350
358
|
A word named `main` whose scheme is `( -- Unit )` is the program of that module. It may stay off the `@` list. `build` calls it at the bottom of that module's `.js`, and only when Node started that file. `node dist/App.js` runs the program. An import of the module does not. A `package.json` script can name that file. Several modules may each have a `main`. A module without one is a library. Any other scheme on the name `main` is an error. `plylang compile` also writes those modules, then `script.js` beside them. The file is the program of `ply.entry`. A binding is inlined when `main` can call it. The modules themselves still carry the Node guard. `compile` does not rewrite `exports`.
|
|
351
359
|
|
|
352
|
-
`plylang docs` writes `DOCS.md` at the package root. `--out` names another file. A module heading is the string used to import it: the package name and the path, or only the path when there is no name. The prose comes from `..`. Inside a module the sections are `Types`, `Classes`, `Instances`, and `Words`. An empty section is left out. The name of a type, a class, or a word is a heading under its section. `Instances` is a list of `#` heads, with no comment and no method bindings.
|
|
360
|
+
`plylang docs` writes `DOCS.md` at the package root. `--out` names another file. A module heading is the string used to import it: the package name and the path, or only the path when there is no name. The prose comes from `..`. Inside a module the sections are `Types`, `Classes`, `Instances`, and `Words`. An empty section is left out. The name of a type, a class, or a word is a heading under its section. `Instances` is a list of `#` heads, with no comment and no method bindings. A head whose type is defined in another module of this package is listed there too.
|
|
353
361
|
|
|
354
362
|
`plylang check --parse` prints how one file was parsed, and it does not check types. `plylang check --js` prints the JavaScript that `build` would write for that file. Outside a package that is the JavaScript of the one file, and imports are not resolved.
|
|
355
363
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plylang/plylang",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.21",
|
|
4
4
|
"description": "Ply compiler and language server",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Algosail",
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"bin/native.js"
|
|
34
34
|
],
|
|
35
35
|
"optionalDependencies": {
|
|
36
|
-
"@plylang/plylang-darwin-arm64": "1.0.
|
|
37
|
-
"@plylang/plylang-darwin-x64": "1.0.
|
|
38
|
-
"@plylang/plylang-linux-arm64": "1.0.
|
|
39
|
-
"@plylang/plylang-linux-x64": "1.0.
|
|
40
|
-
"@plylang/plylang-win32-arm64": "1.0.
|
|
41
|
-
"@plylang/plylang-win32-x64": "1.0.
|
|
36
|
+
"@plylang/plylang-darwin-arm64": "1.0.21",
|
|
37
|
+
"@plylang/plylang-darwin-x64": "1.0.21",
|
|
38
|
+
"@plylang/plylang-linux-arm64": "1.0.21",
|
|
39
|
+
"@plylang/plylang-linux-x64": "1.0.21",
|
|
40
|
+
"@plylang/plylang-win32-arm64": "1.0.21",
|
|
41
|
+
"@plylang/plylang-win32-x64": "1.0.21"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "cargo build --release",
|