plainscript-lang 1.0.362 → 1.1.0
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 +394 -54
- package/compiler/bundler.js +78 -3
- package/compiler/cli.js +239 -30
- package/compiler/formatter.js +21 -1
- package/compiler/generator.js +453 -107
- package/compiler/ir.js +62 -0
- package/compiler/lexer.js +42 -7
- package/compiler/lsp.js +290 -0
- package/compiler/parser.js +820 -113
- package/compiler/type-checker.js +440 -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 +317 -0
- package/docs/PLAINSCRIPT-SPEC.md +264 -5
- package/docs/V1.1-TYPED-COLLECTIONS-DESIGN.md +220 -0
- 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/typed-collections.pln +21 -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 +13 -4
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# PlainScript v1.1.0: Typed Collections Design
|
|
2
|
+
|
|
3
|
+
**Status:** Design proposal
|
|
4
|
+
**Milestone:** v1.1.0 · Language Depth
|
|
5
|
+
**Predecessor:** Function return contracts
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
Typed collections are the next increment after return contracts. Return contracts
|
|
10
|
+
make a function's result visible to the checker. Typed collections make the
|
|
11
|
+
shape of that result useful when it contains repeated or nested values.
|
|
12
|
+
|
|
13
|
+
The feature must solve one narrow problem well: when PlainScript has a declared
|
|
14
|
+
collection type and a collection literal is available, the checker must verify
|
|
15
|
+
every element against the declared element type. This includes nested lists,
|
|
16
|
+
dictionaries, and declared record contracts. The implementation must not become
|
|
17
|
+
a general-purpose whole-program inference system in this branch.
|
|
18
|
+
|
|
19
|
+
## Proposed surface syntax
|
|
20
|
+
|
|
21
|
+
PlainScript already uses `list of T` and `dictionary of T` in record fields and
|
|
22
|
+
function parameter contracts. The same type expressions will be valid for local
|
|
23
|
+
mutable bindings by placing the annotation between the variable name and its
|
|
24
|
+
initializer:
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
type User
|
|
28
|
+
id is number
|
|
29
|
+
name is text
|
|
30
|
+
done
|
|
31
|
+
|
|
32
|
+
let users as list of User is [
|
|
33
|
+
{ id: 1, name: "David" },
|
|
34
|
+
{ id: 2, name: "Samuel" }
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
let usersById as dictionary of User is {
|
|
38
|
+
first: { id: 1, name: "David" },
|
|
39
|
+
second: { id: 2, name: "Samuel" }
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The existing `let name is value` form remains unchanged. The existing
|
|
44
|
+
`remember name as value` form also remains unchanged in this milestone because
|
|
45
|
+
`as` is already its initializer separator. Typed immutable bindings are not
|
|
46
|
+
needed to establish collection checking and can be designed separately without
|
|
47
|
+
making `remember` ambiguous.
|
|
48
|
+
|
|
49
|
+
The feature does not introduce a new collection literal syntax. Square brackets
|
|
50
|
+
remain list literals, and braces remain object or dictionary literals according
|
|
51
|
+
to their existing parsing context.
|
|
52
|
+
|
|
53
|
+
## Type model
|
|
54
|
+
|
|
55
|
+
The parser will continue to produce the existing recursive type-specification
|
|
56
|
+
shape:
|
|
57
|
+
|
|
58
|
+
```text
|
|
59
|
+
name -> { kind: "name", name }
|
|
60
|
+
optional T -> { kind: "optional", value: T }
|
|
61
|
+
list of T -> { kind: "list", value: T }
|
|
62
|
+
dictionary of T -> { kind: "dictionary", value: T }
|
|
63
|
+
T or U -> { kind: "union", values: [T, U] }
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The checker should treat these specifications as recursive values rather than
|
|
67
|
+
flattening collection types to the single string `list` or `dictionary`. A
|
|
68
|
+
small internal `TypeRef` normalization layer may be introduced if the current
|
|
69
|
+
string-based inference prevents recursive comparisons. That refactoring belongs
|
|
70
|
+
inside the checker and must preserve existing diagnostic codes and messages for
|
|
71
|
+
primitive contracts.
|
|
72
|
+
|
|
73
|
+
A declared record name remains a named type. Its value is valid only when the
|
|
74
|
+
checker can verify an object literal against the corresponding `type`
|
|
75
|
+
declaration, or when the value's known type is that declared record. A plain
|
|
76
|
+
`object` remains intentionally less specific and accepts any object value.
|
|
77
|
+
|
|
78
|
+
## Static checking rules
|
|
79
|
+
|
|
80
|
+
When a typed binding has an initializer, the checker performs these checks:
|
|
81
|
+
|
|
82
|
+
1. `list of T` requires a list literal or a known list value. Every element is
|
|
83
|
+
checked recursively against `T`.
|
|
84
|
+
2. `dictionary of T` requires an object or known dictionary value. Every value is
|
|
85
|
+
checked recursively against `T`; dictionary keys remain unconstrained text
|
|
86
|
+
keys.
|
|
87
|
+
3. `optional T` accepts a missing value or `null`, and otherwise checks the
|
|
88
|
+
value against `T`.
|
|
89
|
+
4. `T or U` accepts a value matching at least one branch. Literal diagnostics
|
|
90
|
+
should report the complete expected union when no branch matches.
|
|
91
|
+
5. A named record type checks required fields, optional fields, unknown fields,
|
|
92
|
+
and each field value recursively. If a field itself has type `list of User`,
|
|
93
|
+
every list element is checked as a `User` record.
|
|
94
|
+
6. A literal with an expression-valued element is not rejected merely because
|
|
95
|
+
the expression is not yet inferable. The checker reports a mismatch only
|
|
96
|
+
when the expression has a known incompatible type. Expression propagation is
|
|
97
|
+
the following v1.1.0 milestone.
|
|
98
|
+
|
|
99
|
+
For example, this program must produce a field diagnostic for the second list
|
|
100
|
+
element:
|
|
101
|
+
|
|
102
|
+
```text
|
|
103
|
+
type User
|
|
104
|
+
id is number
|
|
105
|
+
name is text
|
|
106
|
+
done
|
|
107
|
+
|
|
108
|
+
make loadUsers() returns list of User
|
|
109
|
+
give [
|
|
110
|
+
{ id: 1, name: "David" },
|
|
111
|
+
{ id: "wrong", name: "Samuel" }
|
|
112
|
+
]
|
|
113
|
+
done
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The diagnostic should identify the collection context and element path, such as
|
|
117
|
+
`Return value element 2 field "id" expects number, received text.` The exact
|
|
118
|
+
wording may evolve, but the diagnostic must retain the existing
|
|
119
|
+
`PLN-TYPE-FIELD` or `PLN-TYPE-RETURN` family and point to the collection's source
|
|
120
|
+
range until element-level source spans are available.
|
|
121
|
+
|
|
122
|
+
## Contract propagation within this milestone
|
|
123
|
+
|
|
124
|
+
The checker should record a typed local binding's declared type in its
|
|
125
|
+
environment. A call to a function returning `list of User` or `dictionary of
|
|
126
|
+
User` should likewise retain that recursive return type rather than reducing it
|
|
127
|
+
to `list` or `dictionary`.
|
|
128
|
+
|
|
129
|
+
This milestone only requires enough propagation to validate collection values at
|
|
130
|
+
contract boundaries and to validate indexed access when the type is already
|
|
131
|
+
known. Indexing a known `list of T` may produce `T`, and reading a known
|
|
132
|
+
`dictionary of T` may produce `T`. Rich propagation through arbitrary operators,
|
|
133
|
+
branches, aliases, and callbacks belongs to the next expression-typing branch.
|
|
134
|
+
|
|
135
|
+
## Runtime behavior
|
|
136
|
+
|
|
137
|
+
Typed collections do not add runtime behavior. Existing generated JavaScript
|
|
138
|
+
already represents lists as arrays and dictionaries as objects. Runtime record
|
|
139
|
+
validation remains the responsibility of the existing function-entry contract
|
|
140
|
+
checks. The new work is static validation of literal collection contents and
|
|
141
|
+
preservation of recursive type information in the checker.
|
|
142
|
+
|
|
143
|
+
If a typed collection is passed to a function at runtime, the generated program
|
|
144
|
+
must continue to use the same contract validation path as an equivalent record
|
|
145
|
+
field. The design must not introduce a second incompatible runtime validator.
|
|
146
|
+
|
|
147
|
+
## Diagnostics
|
|
148
|
+
|
|
149
|
+
The implementation should add only diagnostics needed to explain collection
|
|
150
|
+
mismatches:
|
|
151
|
+
|
|
152
|
+
| Code | Meaning |
|
|
153
|
+
| --- | --- |
|
|
154
|
+
| `PLN-TYPE-COLLECTION` | A value is not a list or dictionary where the contract requires one. |
|
|
155
|
+
| `PLN-TYPE-ELEMENT` | A list element or dictionary value does not match its element type. |
|
|
156
|
+
| `PLN-TYPE-FIELD` | A nested declared record has a missing, invalid, or unknown field. |
|
|
157
|
+
| `PLN-TYPE-ARG` | A function argument does not match a collection parameter contract. |
|
|
158
|
+
| `PLN-TYPE-RETURN` | A return value does not match a collection return contract. |
|
|
159
|
+
|
|
160
|
+
Existing field and argument diagnostics should be reused where they already
|
|
161
|
+
explain the failure. New codes are justified only when they distinguish the
|
|
162
|
+
collection boundary from the nested value. Diagnostics should include the
|
|
163
|
+
expected recursive type using the existing `typeName` rendering, for example
|
|
164
|
+
`list of User` or `dictionary of optional text`.
|
|
165
|
+
|
|
166
|
+
## Parser and checker changes
|
|
167
|
+
|
|
168
|
+
The implementation branch should make the following bounded changes:
|
|
169
|
+
|
|
170
|
+
- Extend `let` declarations to accept `let name as Type is value` and preserve
|
|
171
|
+
the parsed type on the `RememberStatement` node as `typeAnnotation`.
|
|
172
|
+
- Validate the annotation through the same unknown-type path used by parameter
|
|
173
|
+
and return contracts.
|
|
174
|
+
- Replace literal-only contract validation with a recursive validator that can
|
|
175
|
+
descend through `ArrayLiteral`, `InlineObjectLiteral`, and `ObjectLiteral`
|
|
176
|
+
nodes.
|
|
177
|
+
- Store the declared recursive type on the local checker environment.
|
|
178
|
+
- Preserve recursive return types from function calls.
|
|
179
|
+
- Add focused tests for lists, dictionaries, nested records, unions, optional
|
|
180
|
+
elements, invalid element values, and unknown nested contract names.
|
|
181
|
+
- Update the language specification and README examples only after the grammar
|
|
182
|
+
and checker tests pass.
|
|
183
|
+
|
|
184
|
+
No generator changes are expected unless the existing runtime contract emitter
|
|
185
|
+
cannot consume recursive collection specifications. No LSP feature work belongs
|
|
186
|
+
in this branch.
|
|
187
|
+
|
|
188
|
+
## Explicit non-goals
|
|
189
|
+
|
|
190
|
+
This branch will not implement flow-sensitive narrowing, full operator typing,
|
|
191
|
+
collection mutation checking, generic type parameters, tuple types, inferred
|
|
192
|
+
annotations for every variable, cross-file type resolution, or advanced LSP
|
|
193
|
+
features. It will not attempt to prove that every possible runtime collection
|
|
194
|
+
value is valid. Static guarantees apply to values and expressions whose types
|
|
195
|
+
are known at the checking point.
|
|
196
|
+
|
|
197
|
+
## Acceptance criteria
|
|
198
|
+
|
|
199
|
+
The branch is ready for review when all of the following hold:
|
|
200
|
+
|
|
201
|
+
- A valid `list of User` literal passes static checking.
|
|
202
|
+
- An invalid nested record inside a list produces a deterministic diagnostic.
|
|
203
|
+
- A valid `dictionary of User` literal passes static checking.
|
|
204
|
+
- An invalid dictionary value is reported without masking unrelated diagnostics.
|
|
205
|
+
- Nested lists and dictionaries recurse through at least two levels.
|
|
206
|
+
- Optional and union element types behave consistently with existing contracts.
|
|
207
|
+
- A function returning `list of User` preserves that return type for a caller.
|
|
208
|
+
- Existing untyped programs and existing parameter contracts remain unchanged.
|
|
209
|
+
- Focused type tests, LSP tests, documentation checks, and the full test suite
|
|
210
|
+
pass before the branch is opened.
|
|
211
|
+
|
|
212
|
+
## References
|
|
213
|
+
|
|
214
|
+
[1]: https://github.com/ayoistooslick/plainscript/blob/main/docs/PLAINSCRIPT-SPEC.md "PlainScript language specification"
|
|
215
|
+
[2]: https://github.com/ayoistooslick/plainscript/blob/main/compiler/type-checker.js "PlainScript static type checker"
|
|
216
|
+
[3]: https://github.com/ayoistooslick/plainscript/blob/main/compiler/parser.js "PlainScript parser"
|
|
217
|
+
[4]: https://github.com/ayoistooslick/plainscript/blob/main/tests/types.test.js "PlainScript type tests"
|
|
218
|
+
|
|
219
|
+
The proposal is grounded in the existing contract syntax and checker behavior
|
|
220
|
+
specified in [1] and implemented in [2]–[4].
|
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
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// PlainScript 1.0.363 — compound arithmetic assignments.
|
|
2
|
+
// The symbols `+= -= *= /= %=` map to the same assignment machinery as the
|
|
3
|
+
// word forms; the legacy `++=` spelling is still accepted as `+=`.
|
|
4
|
+
|
|
5
|
+
let score be 10
|
|
6
|
+
score += 5 // 15
|
|
7
|
+
score -= 3 // 12
|
|
8
|
+
score *= 2 // 24
|
|
9
|
+
score /= 4 // 6
|
|
10
|
+
score %= 7 // 6 (remainder of 6 / 7)
|
|
11
|
+
show "score is " + score
|
|
12
|
+
|
|
13
|
+
let word be "ver"
|
|
14
|
+
word += "nacular"
|
|
15
|
+
show word
|
|
16
|
+
|
|
17
|
+
let counter be 0
|
|
18
|
+
counter ++= 1
|
|
19
|
+
counter ++= 1
|
|
20
|
+
counter ++= 1
|
|
21
|
+
show "legacy ++= count is " + counter
|
|
22
|
+
|
|
23
|
+
let items be list with "a", "b"
|
|
24
|
+
show count of items
|
package/examples/concurrency.pln
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// PlainScript 1.0.
|
|
1
|
+
// PlainScript 1.0.363: parallel operations and timeouts.
|
|
2
2
|
|
|
3
3
|
make firstJob()
|
|
4
4
|
wait for sleep(10)
|
|
@@ -10,7 +10,7 @@ make secondJob()
|
|
|
10
10
|
give "second"
|
|
11
11
|
done
|
|
12
12
|
|
|
13
|
-
remember results as
|
|
13
|
+
remember results as all of [firstJob(), secondJob()]
|
|
14
14
|
show results
|
|
15
15
|
|
|
16
16
|
remember quick as withTimeout(firstJob(), 1000)
|
package/examples/conditions.pln
CHANGED
package/examples/database.pln
CHANGED
package/examples/day2.pln
CHANGED
package/examples/day3.pln
CHANGED