plainscript-lang 1.0.361 → 1.0.363
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 +383 -55
- package/compiler/bundler.js +78 -3
- package/compiler/cli.js +224 -30
- package/compiler/formatter.js +21 -1
- package/compiler/generator.js +490 -114
- package/compiler/ir.js +61 -0
- package/compiler/lexer.js +42 -7
- package/compiler/lsp.js +234 -0
- package/compiler/parser.js +780 -110
- package/compiler/type-checker.js +246 -0
- package/compiler/version.js +1 -1
- package/docs/CAPABILITY_GAP_AUDIT.md +19 -3
- package/docs/GAME-PROMPT.md +3 -3
- package/docs/PLAINSCRIPT-GRAMMAR.md +308 -0
- package/docs/PLAINSCRIPT-SPEC.md +231 -5
- package/docs/index.html +5 -5
- package/docs/website/script.js +1 -1
- package/examples/ai-memory-cli.pln +221 -0
- package/examples/ai-memory.json +1 -0
- package/examples/ai-providers.pln +1 -1
- package/examples/arrays.pln +1 -1
- package/examples/async-errors.pln +1 -1
- package/examples/auth-api.pln +1 -1
- package/examples/auth-sessions.pln +1 -1
- package/examples/basics.pln +1 -1
- package/examples/bots.pln +1 -1
- package/examples/browser-interactive/README.md +1 -1
- package/examples/browser-interactive/src/interactive.pln +1 -1
- package/examples/cache-and-jobs.pln +1 -1
- package/examples/cache-schedule.pln +1 -1
- package/examples/canvas-game/README.md +1 -1
- package/examples/canvas-game/src/game.pln +1 -1
- package/examples/collections.pln +1 -1
- package/examples/compound-assignments.pln +24 -0
- package/examples/concurrency.pln +2 -2
- package/examples/conditions.pln +1 -1
- package/examples/database.pln +1 -1
- package/examples/day2.pln +1 -1
- package/examples/day3.pln +1 -1
- package/examples/deployment.pln +1 -1
- package/examples/email-send.pln +1 -1
- package/examples/error-handling.pln +1 -1
- package/examples/expressions.pln +1 -1
- package/examples/file-manager-cli.pln +51 -0
- package/examples/file-uploads.pln +1 -1
- package/examples/files-and-config.pln +1 -1
- package/examples/files.pln +1 -1
- package/examples/football-backend/app.pln +1 -1
- package/examples/functions.pln +1 -1
- package/examples/hello.pln +1 -1
- package/examples/http-client.pln +1 -1
- package/examples/http.pln +1 -1
- package/examples/id-verification/app.pln +1 -1
- package/examples/interactive-cli.pln +34 -0
- package/examples/javascript-library/README.md +1 -1
- package/examples/javascript-library/src/interop.pln +1 -1
- package/examples/json.pln +1 -1
- package/examples/loops.pln +1 -1
- package/examples/memoize-demo.pln +31 -0
- package/examples/modules/app.pln +1 -1
- package/examples/modules/main.pln +1 -1
- package/examples/modules/math.pln +1 -1
- package/examples/mongo.pln +1 -1
- package/examples/normalization-forms.pln +22 -0
- package/examples/objects.pln +1 -1
- package/examples/ocr.pln +1 -1
- package/examples/packages.pln +1 -1
- package/examples/random-cli.pln +32 -0
- package/examples/record-kinds.pln +1 -1
- package/examples/records.pln +1 -1
- package/examples/rest-api-fetch.pln +1 -1
- package/examples/rest-api.pln +1 -1
- package/examples/run-in-parallel.pln +25 -0
- package/examples/scheduled-tasks.pln +1 -1
- package/examples/search-and-filter.pln +1 -1
- package/examples/server.pln +1 -1
- package/examples/sessions-and-cookies.pln +1 -1
- package/examples/sqlite.pln +1 -1
- package/examples/start.pln +1 -1
- package/examples/stats-cli.pln +29 -0
- package/examples/stdlib.pln +1 -1
- package/examples/string-helpers.pln +23 -0
- package/examples/string-verbs.pln +1 -1
- package/examples/telegram-ai-bot.pln +1 -1
- package/examples/telegram-bot.pln +1 -1
- package/examples/terminal-cli.pln +11 -0
- package/examples/testing.pln +1 -1
- package/examples/three-dimensional/README.md +1 -1
- package/examples/three-dimensional/src/scene.pln +1 -1
- package/examples/todo-cli.pln +98 -0
- package/examples/web-api.pln +1 -1
- package/examples/web-app.pln +1 -1
- package/examples/websocket-server.pln +1 -1
- package/examples/websocket.pln +1 -1
- package/examples/whatsapp-bot/pairing.pln +1 -1
- package/examples/whatsapp-bot/qr.pln +1 -1
- package/package.json +16 -6
package/docs/PLAINSCRIPT-SPEC.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# PlainScript 1.0.
|
|
1
|
+
# PlainScript 1.0.363 language specification
|
|
2
2
|
|
|
3
3
|
This reference covers the syntax implemented by `compiler/lexer.js` and
|
|
4
4
|
`compiler/parser.js`. The runtime it generates lives in
|
|
@@ -9,7 +9,8 @@ repository's maintained example set.
|
|
|
9
9
|
|
|
10
10
|
PlainScript is a line-oriented, intent-oriented language made for Node.js.
|
|
11
11
|
Source files use `.pln`. Blocks end with `done` unless the syntax says
|
|
12
|
-
`together
|
|
12
|
+
`together`, and `end` is accepted as a synonym for `done` in every block
|
|
13
|
+
position. Whitespace does not matter.
|
|
13
14
|
|
|
14
15
|
The basic declaration forms are:
|
|
15
16
|
|
|
@@ -52,6 +53,47 @@ precedence. Word forms `plus`, `minus`, `times`, and `divided by` also work.
|
|
|
52
53
|
`??` plus unary `-`, `wait for`, `typeof`, `void`, and `delete` are
|
|
53
54
|
supported.
|
|
54
55
|
|
|
56
|
+
### Assignment
|
|
57
|
+
|
|
58
|
+
`becomes` is the ordinary assignment operator. Compound forms combine an
|
|
59
|
+
operator with assignment; the symbols `+=`, `-=`, `*=`, `/=`, and `%=` (plus
|
|
60
|
+
the legacy `++=` spelling of `+=`) reuse the same grammar:
|
|
61
|
+
|
|
62
|
+
```plainscript
|
|
63
|
+
remember score as 10
|
|
64
|
+
score += 5
|
|
65
|
+
score -= 3
|
|
66
|
+
score *= 2
|
|
67
|
+
score /= 4
|
|
68
|
+
score %= 7
|
|
69
|
+
let count be 0
|
|
70
|
+
count ++= 1
|
|
71
|
+
show score
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The word forms `or becomes`, `and becomes`, and `nullish becomes` map to
|
|
75
|
+
the logical assignments `||=`, `&&=`, and `??=`:
|
|
76
|
+
|
|
77
|
+
```plainscript
|
|
78
|
+
let name be null
|
|
79
|
+
name or becomes "Grace"
|
|
80
|
+
name and becomes name
|
|
81
|
+
name nullish becomes "default"
|
|
82
|
+
show name
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`set <expr> to <value>` and `change <expr> to <value>` are prefix synonyms
|
|
86
|
+
for `becomes`, and the same words work postfixed after the expression:
|
|
87
|
+
|
|
88
|
+
```plainscript
|
|
89
|
+
let age be 16
|
|
90
|
+
set age to 17
|
|
91
|
+
change age to 18
|
|
92
|
+
age set to 19
|
|
93
|
+
age change to 20
|
|
94
|
+
show age
|
|
95
|
+
```
|
|
96
|
+
|
|
55
97
|
## Conditions
|
|
56
98
|
|
|
57
99
|
```plainscript
|
|
@@ -71,7 +113,7 @@ Operators:
|
|
|
71
113
|
| `more than`, `is greater than`, `is above` | `>` |
|
|
72
114
|
| `fewer than`, `is less than`, `is below` | `<` |
|
|
73
115
|
| `is at least` | `>=` |
|
|
74
|
-
| `is most` | `<=` |
|
|
116
|
+
| `is at most` | `<=` |
|
|
75
117
|
| `contains`, `starts with`, `ends with`, `made of` | string predicates |
|
|
76
118
|
| `between low and high` | inclusive range |
|
|
77
119
|
| `has field field` | property existence |
|
|
@@ -112,6 +154,63 @@ done
|
|
|
112
154
|
`yield` turns a function into a generator. Functions can use `wait for` and
|
|
113
155
|
other async operations.
|
|
114
156
|
|
|
157
|
+
`intend name(params)` is a callable declaration equivalent to `make` at
|
|
158
|
+
runtime, but it remains an `IntentDeclaration` in the AST and compiler IR. This
|
|
159
|
+
metadata is reserved for future contract analysis, documentation generation,
|
|
160
|
+
and semantic diagnostics.
|
|
161
|
+
|
|
162
|
+
```plainscript
|
|
163
|
+
intend greet(name)
|
|
164
|
+
give `Hello ${name}`
|
|
165
|
+
done
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Tooling can inspect the backend-neutral representation with:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
plainscript ir path/to/program.pln
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
The command emits versioned JSON and preserves statement kinds, source spans,
|
|
175
|
+
function parameters, and intent metadata. The JavaScript backend continues to
|
|
176
|
+
consume the existing AST while alternate backends are developed against this
|
|
177
|
+
stable boundary.
|
|
178
|
+
|
|
179
|
+
## Typed data contracts
|
|
180
|
+
|
|
181
|
+
Contextual `type` declarations describe the required shape of a record. A
|
|
182
|
+
function parameter can refer to a declared type after `as`; unlike a default
|
|
183
|
+
literal such as `name as "friend"`, a type name is an identifier such as
|
|
184
|
+
`User`.
|
|
185
|
+
|
|
186
|
+
```plainscript
|
|
187
|
+
type User
|
|
188
|
+
id is number
|
|
189
|
+
name is text
|
|
190
|
+
active is optional boolean
|
|
191
|
+
tags is list of text
|
|
192
|
+
metadata is dictionary of text
|
|
193
|
+
done
|
|
194
|
+
|
|
195
|
+
make greet(user as User)
|
|
196
|
+
give `Hello, ${user.name}`
|
|
197
|
+
done
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
The current implementation emits the contract into the generated program and
|
|
201
|
+
validates typed arguments at function entry. Required fields must be present;
|
|
202
|
+
primitive values must match their declared type. `optional T` accepts a missing
|
|
203
|
+
or null value, while `T or null` accepts null in addition to `T`. A declared
|
|
204
|
+
contract can also be used inside `list of` and `dictionary of`. The static
|
|
205
|
+
checker consumes the same AST and IR and reports unknown contract names, arity
|
|
206
|
+
errors, invalid literal fields, missing required fields, and invalid member
|
|
207
|
+
access. Generic types, whole-program inference, and compile-time exhaustiveness
|
|
208
|
+
checking are not yet part of this slice.
|
|
209
|
+
|
|
210
|
+
The checker powers the stdio language server started with `plainscript-lsp`.
|
|
211
|
+
The server publishes these diagnostics and provides hover, completion,
|
|
212
|
+
definition, and document-symbol responses using standard LSP JSON-RPC framing.
|
|
213
|
+
|
|
115
214
|
## Control flow
|
|
116
215
|
|
|
117
216
|
```plainscript
|
|
@@ -132,6 +231,33 @@ Use `break` and `continue` inside loops. `match value against` uses `->`
|
|
|
132
231
|
case arrows and `otherwise`. `switch value against` works the same way and
|
|
133
232
|
is just another way to write it.
|
|
134
233
|
|
|
234
|
+
Regex capture uses `match pattern "..." in text as result`:
|
|
235
|
+
```plainscript
|
|
236
|
+
match pattern "^(\\d+)$" in "4061" as digits
|
|
237
|
+
show digits // → "4061"
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Collection size uses `count of`:
|
|
241
|
+
```plainscript
|
|
242
|
+
show count of [1, 2, 3] // → 3
|
|
243
|
+
show count of "hello" // → 5
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
`run in parallel ... done as <name>` executes each statement in the block as
|
|
247
|
+
its own concurrent async job and awaits them together with `Promise.all`,
|
|
248
|
+
collecting each statement's value in body order. It is not a worker-thread
|
|
249
|
+
API:
|
|
250
|
+
|
|
251
|
+
```plainscript
|
|
252
|
+
run in parallel
|
|
253
|
+
makeJob(1)
|
|
254
|
+
makeJob(2)
|
|
255
|
+
done as results
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
An expression statement (typically a call) contributes its return value; any
|
|
259
|
+
other statement runs as its own async job and contributes `undefined`.
|
|
260
|
+
|
|
135
261
|
## Record kinds
|
|
136
262
|
|
|
137
263
|
```plainscript
|
|
@@ -237,10 +363,106 @@ retry 3 times every 1 second
|
|
|
237
363
|
done
|
|
238
364
|
```
|
|
239
365
|
|
|
240
|
-
Concurrency helpers are `
|
|
366
|
+
Concurrency helpers are `all of`, `any of`, `settled of`, and `withTimeout`.
|
|
241
367
|
Recurring blocks use `every 5 minutes ... done` and
|
|
242
368
|
`schedule "0 * * * *" ... done`.
|
|
243
369
|
|
|
370
|
+
## Interactive terminal
|
|
371
|
+
|
|
372
|
+
Simple CLI prompts read from stdin and write to stdout. `confirm("question")`
|
|
373
|
+
asks a yes/no question and resolves to a boolean; `choose("question",
|
|
374
|
+
options)` prints a numbered menu and resolves to the picked option (or `null`
|
|
375
|
+
when cancelled):
|
|
376
|
+
|
|
377
|
+
```plainscript
|
|
378
|
+
remember ok as confirm("Delete this file?")
|
|
379
|
+
remember color as choose("Pick a color", ["red", "green", "blue"])
|
|
380
|
+
show color
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
`clearTerminal()` clears the screen, `terminalWidth()` and
|
|
384
|
+
`terminalHeight()` report the terminal size, and `stderr(...)` writes to
|
|
385
|
+
standard error so diagnostics never pollute stdout:
|
|
386
|
+
|
|
387
|
+
```plainscript
|
|
388
|
+
stderr("starting")
|
|
389
|
+
clearTerminal()
|
|
390
|
+
show "width " + terminalWidth() + " height " + terminalHeight()
|
|
391
|
+
stderr("done")
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## Statistics, vectors, and randomness
|
|
395
|
+
|
|
396
|
+
Numeric statistics over an array:
|
|
397
|
+
|
|
398
|
+
| Function | Result |
|
|
399
|
+
| --- | --- |
|
|
400
|
+
| `mean(values)` | Arithmetic mean, or `NaN` for an empty array. |
|
|
401
|
+
| `median(values)` | Middle value; average of the two middle values for even-length arrays. |
|
|
402
|
+
| `variance(values)` | Sample variance (`n - 1` denominator). |
|
|
403
|
+
| `deviation(values)` | Sample standard deviation. |
|
|
404
|
+
|
|
405
|
+
```plainscript
|
|
406
|
+
remember data as [2, 4, 4, 4, 5, 5, 7, 9]
|
|
407
|
+
show mean(data)
|
|
408
|
+
show median(data)
|
|
409
|
+
show variance(data)
|
|
410
|
+
show deviation(data)
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
Vector primitives operate on numeric arrays:
|
|
414
|
+
|
|
415
|
+
```plainscript
|
|
416
|
+
remember a as [1, 2, 3]
|
|
417
|
+
remember b as [4, 5, 6]
|
|
418
|
+
show dotProduct(a, b)
|
|
419
|
+
show magnitude(a)
|
|
420
|
+
show normalize(a)
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
`dotProduct(a, b)` sums the element-wise products, `magnitude(v)` is the
|
|
424
|
+
Euclidean length, and `normalize(v)` is the unit vector (a zero vector maps
|
|
425
|
+
to the same-length zero vector).
|
|
426
|
+
|
|
427
|
+
Randomness helpers (all inclusive where a range applies):
|
|
428
|
+
|
|
429
|
+
```plainscript
|
|
430
|
+
remember die as randomInteger(1, 6)
|
|
431
|
+
remember coin as randomChoice(["heads", "tails"])
|
|
432
|
+
remember loot as weightedChoice(["common", "rare", "legendary"], [0.7, 0.25, 0.05])
|
|
433
|
+
remember deck as shuffle(["A", "2", "3", "4", "5"])
|
|
434
|
+
remember hand as sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
`randomInteger(low, high)` is inclusive on both ends, `randomChoice(items)`
|
|
438
|
+
picks one element, `weightedChoice(items, weights)` picks an element weighted
|
|
439
|
+
by its parallel weight, `shuffle(items)` returns a new shuffled copy, and
|
|
440
|
+
`sample(items, n)` returns a shuffled copy cut to `n` elements.
|
|
441
|
+
|
|
442
|
+
## Memoization and helpers
|
|
443
|
+
|
|
444
|
+
`memoize(function)` wraps a callable so repeated calls with the same argument
|
|
445
|
+
list hit a cache instead of re-running:
|
|
446
|
+
|
|
447
|
+
```plainscript
|
|
448
|
+
make slowSquare(n)
|
|
449
|
+
give n * n
|
|
450
|
+
done
|
|
451
|
+
|
|
452
|
+
remember fastSquare as memoize(slowSquare)
|
|
453
|
+
show fastSquare(9)
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
`parseBoolean(text)` treats `"true"`, `"yes"`, `"1"`, and `"on"` (ignoring
|
|
457
|
+
case and surrounding whitespace) as `true` and everything else as `false`.
|
|
458
|
+
`characters(text)` splits a string into an array of its characters:
|
|
459
|
+
|
|
460
|
+
```plainscript
|
|
461
|
+
remember enabled as parseBoolean("yes")
|
|
462
|
+
remember letters as characters("hello")
|
|
463
|
+
show count of letters
|
|
464
|
+
```
|
|
465
|
+
|
|
244
466
|
## AI providers
|
|
245
467
|
|
|
246
468
|
`chat(model, messages, options)` and `embedText(model, text, options)` talk
|
|
@@ -532,6 +754,10 @@ remember engine as matter.Engine.create()
|
|
|
532
754
|
remember scene as new THREE.Scene()
|
|
533
755
|
```
|
|
534
756
|
|
|
757
|
+
> Removed: a `javascript ... done` raw block existed briefly in 1.0.363 but
|
|
758
|
+
> is no longer part of the language. Interop is through imports (`use` /
|
|
759
|
+
> `bring`) and normal member calls, which are always available.
|
|
760
|
+
|
|
535
761
|
Call anything - member chains need no special syntax, and there is no wrapper
|
|
536
762
|
around values:
|
|
537
763
|
|
|
@@ -596,4 +822,4 @@ plainscript version
|
|
|
596
822
|
```
|
|
597
823
|
|
|
598
824
|
`check` parses imports, generates JavaScript, and validates that output with
|
|
599
|
-
Node's parser without writing files.
|
|
825
|
+
Node's parser without writing files.
|
package/docs/index.html
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="utf-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
-
<meta name="description" content="PlainScript 1.0.
|
|
6
|
+
<meta name="description" content="PlainScript 1.0.363 language reference and examples">
|
|
7
7
|
<meta name="theme-color" content="#070c16">
|
|
8
|
-
<title>PlainScript 1.0.
|
|
8
|
+
<title>PlainScript 1.0.363</title>
|
|
9
9
|
|
|
10
10
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
11
11
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
<a class="nav-logo" href="#">
|
|
22
22
|
<img class="nav-logo-image" src="logo-pln.jpg" alt="">
|
|
23
23
|
<span>PlainScript</span>
|
|
24
|
-
<span class="logo-ver">1.0.
|
|
24
|
+
<span class="logo-ver">1.0.363</span>
|
|
25
25
|
</a>
|
|
26
26
|
<button class="nav-toggle" id="nav-toggle" aria-label="Toggle navigation" aria-expanded="false" aria-controls="nav-links">
|
|
27
27
|
<span class="nav-toggle-bar"></span>
|
|
@@ -117,7 +117,7 @@ npm start</code></pre>
|
|
|
117
117
|
<div class="container">
|
|
118
118
|
<p class="section-tag" data-reveal>Feature library</p>
|
|
119
119
|
<h2 class="section-title" data-reveal>Examples you can try today.</h2>
|
|
120
|
-
<p class="section-lead">These snippets use the PlainScript 1.0.
|
|
120
|
+
<p class="section-lead">These snippets use the PlainScript 1.0.363 grammar.</p>
|
|
121
121
|
<div class="library-toolbar" data-reveal>
|
|
122
122
|
<label class="sr-only" for="lib-search">Search examples</label>
|
|
123
123
|
<input id="lib-search" class="lib-search-input" type="search" placeholder="Search examples">
|
|
@@ -146,7 +146,7 @@ npm start</code></pre>
|
|
|
146
146
|
</main>
|
|
147
147
|
|
|
148
148
|
<footer class="site-footer">
|
|
149
|
-
<div class="container footer-bottom"><span>PlainScript</span><span>1.0.
|
|
149
|
+
<div class="container footer-bottom"><span>PlainScript</span><span>1.0.363</span></div>
|
|
150
150
|
</footer>
|
|
151
151
|
|
|
152
152
|
<script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
|
package/docs/website/script.js
CHANGED
|
@@ -289,7 +289,7 @@
|
|
|
289
289
|
{ category: "web", name: "HTTP client", description: "Fetch data from a URL.", source: 'remember response as get "https://example.com/data" timeout 5000\nshow response.status' },
|
|
290
290
|
{ category: "web", name: "Auth and sessions", description: "Protect routes and track visitors.", source: 'web app\nenable sessions "session-secret"\nrequire api key from env("API_KEY")\nroute get "/me"\n reply json\n user is user of session of request\n done\ndone' },
|
|
291
291
|
{ category: "runtime", name: "Async errors", description: "Handle errors gracefully.", source: 'try\n remember result as get "https://example.com" timeout 5000\nrecover as error\n show message of error\nfinally\n show "finished"\ndone' },
|
|
292
|
-
{ category: "runtime", name: "Concurrency", description: "Run several tasks at once.", source: 'remember results as
|
|
292
|
+
{ category: "runtime", name: "Concurrency", description: "Run several tasks at once.", source: 'remember results as all of (list with firstJob(), secondJob())\nremember quick as withTimeout(firstJob(), 1000)\nshow results' },
|
|
293
293
|
{ category: "runtime", name: "WebSockets", description: "Chat and push updates live.", source: 'websocket server on 8080\n when socket connects\n send socket "connected"\n done\n when socket sends message\n broadcast message\n done\ndone' },
|
|
294
294
|
{ category: "runtime", name: "Cache", description: "Keep values ready with a TTL.", source: 'cache env("REDIS_URL")\ncacheSet("status", "ready", 60)\nremember status as cacheGet("status")' },
|
|
295
295
|
{ category: "integrations", name: "Telegram", description: "Build a Telegram bot.", source: 'bot env("TELEGRAM_BOT_TOKEN")\nwhen someone sends "/start"\n reply "Welcome"\ndone\nstart telegram bot' },
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// PlainScript 1.0.363 — persistent-memory AI-style CLI.
|
|
2
|
+
//
|
|
3
|
+
// A tiny "assistant" with durable memory stored as JSON. No LLM, no API keys:
|
|
4
|
+
// retrieval is deterministic cosine similarity over a bag-of-words vector
|
|
5
|
+
// built with the new vector primitives (dotProduct / magnitude / normalize).
|
|
6
|
+
// A small synonym table broadens queries so "food" also matches "toast".
|
|
7
|
+
//
|
|
8
|
+
// Usage:
|
|
9
|
+
// plainscript run examples/ai-memory-cli.pln note "Ada loves math and jazz"
|
|
10
|
+
// plainscript run examples/ai-memory-cli.pln query "who likes math"
|
|
11
|
+
// plainscript run examples/ai-memory-cli.pln list
|
|
12
|
+
// plainscript run examples/ai-memory-cli.pln forget 0
|
|
13
|
+
// plainscript run examples/ai-memory-cli.pln clear
|
|
14
|
+
|
|
15
|
+
remember memoryFile as "ai-memory.json"
|
|
16
|
+
|
|
17
|
+
make loadMemory()
|
|
18
|
+
if fileExists(memoryFile) is true
|
|
19
|
+
give jsonDecode(readFile(memoryFile))
|
|
20
|
+
otherwise
|
|
21
|
+
give []
|
|
22
|
+
done
|
|
23
|
+
done
|
|
24
|
+
|
|
25
|
+
make saveMemory(facts)
|
|
26
|
+
writeFile(memoryFile, jsonEncode(facts))
|
|
27
|
+
done
|
|
28
|
+
|
|
29
|
+
make words(text)
|
|
30
|
+
remember cleaned as toLowerCase(text)
|
|
31
|
+
remember pieces as split(cleaned, " ")
|
|
32
|
+
remember result as []
|
|
33
|
+
for each piece in pieces
|
|
34
|
+
if piece different from ""
|
|
35
|
+
push(result, piece)
|
|
36
|
+
done
|
|
37
|
+
done
|
|
38
|
+
give result
|
|
39
|
+
done
|
|
40
|
+
|
|
41
|
+
remember synonyms as [
|
|
42
|
+
["food", "toast"],
|
|
43
|
+
["food", "tasty"],
|
|
44
|
+
["food", "snack"],
|
|
45
|
+
["food", "meal"],
|
|
46
|
+
["drink", "tea"],
|
|
47
|
+
["drink", "coffee"],
|
|
48
|
+
["drink", "juice"],
|
|
49
|
+
["color", "red"],
|
|
50
|
+
["color", "green"],
|
|
51
|
+
["color", "blue"],
|
|
52
|
+
["color", "yellow"]
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
make expand(queryWord)
|
|
56
|
+
remember out as []
|
|
57
|
+
for each row in synonyms
|
|
58
|
+
if row at position 0 is queryWord
|
|
59
|
+
push(out, row at position 1)
|
|
60
|
+
done
|
|
61
|
+
done
|
|
62
|
+
push(out, queryWord)
|
|
63
|
+
give out
|
|
64
|
+
done
|
|
65
|
+
|
|
66
|
+
make vectorize(vocab, ws)
|
|
67
|
+
remember v as []
|
|
68
|
+
for each term in vocab
|
|
69
|
+
remember hits as 0
|
|
70
|
+
for each w in ws
|
|
71
|
+
if w is term
|
|
72
|
+
hits becomes hits + 1
|
|
73
|
+
done
|
|
74
|
+
done
|
|
75
|
+
push(v, hits)
|
|
76
|
+
done
|
|
77
|
+
give v
|
|
78
|
+
done
|
|
79
|
+
|
|
80
|
+
make cosine(vocab, aWords, bWords)
|
|
81
|
+
remember a as vectorize(vocab, aWords)
|
|
82
|
+
remember b as vectorize(vocab, bWords)
|
|
83
|
+
remember lengthA as magnitude(a)
|
|
84
|
+
remember lengthB as magnitude(b)
|
|
85
|
+
if lengthA is 0 or lengthB is 0
|
|
86
|
+
give 0
|
|
87
|
+
done
|
|
88
|
+
give dotProduct(a, b) / (lengthA * lengthB)
|
|
89
|
+
done
|
|
90
|
+
|
|
91
|
+
make buildVocab(facts, qwords)
|
|
92
|
+
remember vocab as []
|
|
93
|
+
for each fact in facts
|
|
94
|
+
for each w in words of fact
|
|
95
|
+
if not (vocab contains w)
|
|
96
|
+
push(vocab, w)
|
|
97
|
+
done
|
|
98
|
+
done
|
|
99
|
+
done
|
|
100
|
+
for each w in qwords
|
|
101
|
+
if not (vocab contains w)
|
|
102
|
+
push(vocab, w)
|
|
103
|
+
done
|
|
104
|
+
done
|
|
105
|
+
give vocab
|
|
106
|
+
done
|
|
107
|
+
|
|
108
|
+
make bestMatch(qwords)
|
|
109
|
+
remember facts as loadMemory()
|
|
110
|
+
if count of facts is 0
|
|
111
|
+
give "no facts stored yet"
|
|
112
|
+
done
|
|
113
|
+
remember vocab as buildVocab(facts, qwords)
|
|
114
|
+
remember top as ""
|
|
115
|
+
remember topScore as 0
|
|
116
|
+
for each fact in facts
|
|
117
|
+
remember score as cosine(vocab, qwords, words of fact)
|
|
118
|
+
if score is more than topScore
|
|
119
|
+
topScore becomes score
|
|
120
|
+
top becomes text of fact
|
|
121
|
+
done
|
|
122
|
+
done
|
|
123
|
+
if topScore is 0
|
|
124
|
+
give "no fact matches that query"
|
|
125
|
+
done
|
|
126
|
+
give top + " (score " + round(topScore * 100) + "%)"
|
|
127
|
+
done
|
|
128
|
+
|
|
129
|
+
make addFact(text)
|
|
130
|
+
remember facts as loadMemory()
|
|
131
|
+
push(facts, { text: text, words: words(text) })
|
|
132
|
+
saveMemory(facts)
|
|
133
|
+
done
|
|
134
|
+
|
|
135
|
+
make listFacts()
|
|
136
|
+
remember facts as loadMemory()
|
|
137
|
+
if count of facts is 0
|
|
138
|
+
show "memory is empty"
|
|
139
|
+
otherwise
|
|
140
|
+
remember i as 0
|
|
141
|
+
for each fact in facts
|
|
142
|
+
show "[" + i + "] " + text of fact
|
|
143
|
+
i becomes i + 1
|
|
144
|
+
done
|
|
145
|
+
done
|
|
146
|
+
done
|
|
147
|
+
|
|
148
|
+
make forgetFact(index)
|
|
149
|
+
remember facts as loadMemory()
|
|
150
|
+
remember kept as []
|
|
151
|
+
remember i as 0
|
|
152
|
+
for each fact in facts
|
|
153
|
+
if i different from index
|
|
154
|
+
push(kept, fact)
|
|
155
|
+
done
|
|
156
|
+
i becomes i + 1
|
|
157
|
+
done
|
|
158
|
+
saveMemory(kept)
|
|
159
|
+
show "forgot fact " + index
|
|
160
|
+
done
|
|
161
|
+
|
|
162
|
+
make clearMemory()
|
|
163
|
+
saveMemory([])
|
|
164
|
+
show "memory cleared"
|
|
165
|
+
done
|
|
166
|
+
|
|
167
|
+
make showHelp()
|
|
168
|
+
show "Persistent-memory assistant"
|
|
169
|
+
show " note <text> add a fact (remember works too)"
|
|
170
|
+
show " query <words> retrieve the best-matching fact"
|
|
171
|
+
show " list show all stored facts"
|
|
172
|
+
show " forget <index> delete a fact by its list index"
|
|
173
|
+
show " clear delete everything"
|
|
174
|
+
done
|
|
175
|
+
|
|
176
|
+
make main()
|
|
177
|
+
remember argv as args()
|
|
178
|
+
if count of argv is 0
|
|
179
|
+
showHelp()
|
|
180
|
+
exit(0)
|
|
181
|
+
done
|
|
182
|
+
remember command as argv at position 0
|
|
183
|
+
if command is "remember" or command is "note"
|
|
184
|
+
if count of argv is at least 2
|
|
185
|
+
addFact(argv at position 1)
|
|
186
|
+
show "remembered: " + argv at position 1
|
|
187
|
+
otherwise
|
|
188
|
+
show "usage: ... note <text>"
|
|
189
|
+
done
|
|
190
|
+
otherwise if command is "query"
|
|
191
|
+
remember qwords as []
|
|
192
|
+
remember skippingFirst as true
|
|
193
|
+
for each current in argv
|
|
194
|
+
if skippingFirst is false
|
|
195
|
+
push(qwords, current)
|
|
196
|
+
done
|
|
197
|
+
skippingFirst becomes false
|
|
198
|
+
done
|
|
199
|
+
remember queryWords as []
|
|
200
|
+
for each qword in qwords
|
|
201
|
+
for each term in expand(qword)
|
|
202
|
+
push(queryWords, term)
|
|
203
|
+
done
|
|
204
|
+
done
|
|
205
|
+
show bestMatch(words(join(queryWords, " ")))
|
|
206
|
+
otherwise if command is "list"
|
|
207
|
+
listFacts()
|
|
208
|
+
otherwise if command is "forget"
|
|
209
|
+
if count of argv is at least 2
|
|
210
|
+
forgetFact(parseInt(argv at position 1, 10))
|
|
211
|
+
otherwise
|
|
212
|
+
show "usage: ... forget <index>"
|
|
213
|
+
done
|
|
214
|
+
otherwise if command is "clear"
|
|
215
|
+
clearMemory()
|
|
216
|
+
otherwise
|
|
217
|
+
showHelp()
|
|
218
|
+
done
|
|
219
|
+
done
|
|
220
|
+
|
|
221
|
+
main()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[]
|
package/examples/arrays.pln
CHANGED
package/examples/auth-api.pln
CHANGED
package/examples/basics.pln
CHANGED
package/examples/bots.pln
CHANGED
|
@@ -5,7 +5,7 @@ no canvas, no library, no bundler. Click **+10 points** and the score span
|
|
|
5
5
|
updates; click **Log entry** to append a line to the run log; press **R** to
|
|
6
6
|
reset both.
|
|
7
7
|
|
|
8
|
-
It demonstrates the v1.0.
|
|
8
|
+
It demonstrates the v1.0.363 browser API:
|
|
9
9
|
|
|
10
10
|
- `document.querySelector` + `.innerHTML becomes` / `.textContent becomes`.
|
|
11
11
|
- `when clickButton "click" happens` DOM event handlers.
|
|
@@ -5,7 +5,7 @@ arrow keys while red blocks fall from the top. Score a point for every block
|
|
|
5
5
|
that drifts past you; lose a life when one collides with you. Run out of lives
|
|
6
6
|
and the game freezes until you press Restart.
|
|
7
7
|
|
|
8
|
-
It demonstrates the v1.0.
|
|
8
|
+
It demonstrates the v1.0.363 browser API:
|
|
9
9
|
|
|
10
10
|
- `when document "keydown"/"keyup" happens` handlers that set flags on a
|
|
11
11
|
`keys{}` map - the game loop reads the map, the event handlers never move the
|
package/examples/collections.pln
CHANGED