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,440 @@
|
|
|
1
|
+
// Static type checking for PlainScript contracts and v1.1 language depth.
|
|
2
|
+
//
|
|
3
|
+
// The checker is intentionally conservative: unknown expressions remain `any`,
|
|
4
|
+
// while declared contracts, typed bindings, known literals, calls, members,
|
|
5
|
+
// collections, and branch-local null checks receive deterministic diagnostics.
|
|
6
|
+
|
|
7
|
+
const { lowerToIR } = require('./ir');
|
|
8
|
+
|
|
9
|
+
const PRIMITIVES = new Set(['any', 'number', 'text', 'boolean', 'null', 'object', 'list', 'dictionary']);
|
|
10
|
+
|
|
11
|
+
function diagnostic(message, node, code = 'PLN-TYPE') {
|
|
12
|
+
const line = node && node.line ? node.line : 1;
|
|
13
|
+
const column = node && node.col ? node.col : 1;
|
|
14
|
+
return {
|
|
15
|
+
severity: 1,
|
|
16
|
+
source: 'plainscript',
|
|
17
|
+
code,
|
|
18
|
+
message,
|
|
19
|
+
...(node && node.sourceFile ? { file: node.sourceFile } : {}),
|
|
20
|
+
range: {
|
|
21
|
+
start: { line: line - 1, character: column - 1 },
|
|
22
|
+
end: { line: line - 1, character: column },
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function typeName(spec) {
|
|
28
|
+
if (!spec) return 'any';
|
|
29
|
+
if (typeof spec === 'string') return spec;
|
|
30
|
+
if (spec.kind === 'name') return spec.name;
|
|
31
|
+
if (spec.kind === 'optional') return `optional ${typeName(spec.value)}`;
|
|
32
|
+
if (spec.kind === 'list') return `list of ${typeName(spec.value)}`;
|
|
33
|
+
if (spec.kind === 'dictionary') return `dictionary of ${typeName(spec.value)}`;
|
|
34
|
+
if (spec.kind === 'union') return spec.values.map(typeName).join(' or ');
|
|
35
|
+
return 'any';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function nameSpec(name) { return { kind: 'name', name }; }
|
|
39
|
+
function listSpec(value = 'any') { return { kind: 'list', value }; }
|
|
40
|
+
function dictionarySpec(value = 'any') { return { kind: 'dictionary', value }; }
|
|
41
|
+
|
|
42
|
+
function isNullSpec(spec) {
|
|
43
|
+
return spec && ((spec.kind === 'name' && spec.name === 'null') || spec === 'null');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isOptionalSpec(spec) {
|
|
47
|
+
return spec && (spec.kind === 'optional' || (spec.kind === 'union' && spec.values.some(isNullSpec)));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function withoutNull(spec) {
|
|
51
|
+
if (!spec) return spec;
|
|
52
|
+
if (spec.kind === 'optional') return spec.value;
|
|
53
|
+
if (spec.kind === 'union') {
|
|
54
|
+
const values = spec.values.filter(value => !isNullSpec(value));
|
|
55
|
+
return values.length === 1 ? values[0] : { kind: 'union', values };
|
|
56
|
+
}
|
|
57
|
+
return spec;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function specMatches(actual, expected, schemas) {
|
|
61
|
+
if (!expected || expected === 'any' || (expected.kind === 'name' && expected.name === 'any') || !actual || actual === 'any') return true;
|
|
62
|
+
if (expected.kind === 'optional') return isNullSpec(actual) || specMatches(actual, expected.value, schemas);
|
|
63
|
+
if (expected.kind === 'union') return expected.values.some(item => specMatches(actual, item, schemas));
|
|
64
|
+
if (expected.kind === 'name') {
|
|
65
|
+
if (expected.name === 'any') return true;
|
|
66
|
+
if (expected.name === 'null') return isNullSpec(actual);
|
|
67
|
+
if (expected.name === 'object') return typeName(actual) === 'object' || typeName(actual) === 'dictionary';
|
|
68
|
+
if (expected.name === 'list' || expected.name === 'dictionary') return typeName(actual) === expected.name || actual.kind === expected.name;
|
|
69
|
+
if (typeof actual === 'string') return expected.name === actual;
|
|
70
|
+
if (actual.kind === 'name') return expected.name === actual.name;
|
|
71
|
+
return schemas.has(expected.name) && (typeName(actual) === 'object' || actual.kind === 'name');
|
|
72
|
+
}
|
|
73
|
+
if (expected.kind === 'list') {
|
|
74
|
+
if (actual === 'list') return true;
|
|
75
|
+
if (!actual || actual.kind !== 'list') return false;
|
|
76
|
+
return specMatches(actual.value, expected.value, schemas);
|
|
77
|
+
}
|
|
78
|
+
if (expected.kind === 'dictionary') {
|
|
79
|
+
if (actual === 'dictionary' || actual === 'object') return true;
|
|
80
|
+
if (!actual || actual.kind !== 'dictionary') return false;
|
|
81
|
+
return specMatches(actual.value, expected.value, schemas);
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function literalType(node) {
|
|
87
|
+
if (!node) return 'any';
|
|
88
|
+
switch (node.type) {
|
|
89
|
+
case 'NumberLiteral': return 'number';
|
|
90
|
+
case 'StringLiteral':
|
|
91
|
+
case 'TemplateLiteral': return 'text';
|
|
92
|
+
case 'BooleanLiteral': return 'boolean';
|
|
93
|
+
case 'NullLiteral': return 'null';
|
|
94
|
+
case 'ArrayLiteral': return 'list';
|
|
95
|
+
case 'InlineObjectLiteral':
|
|
96
|
+
case 'ObjectLiteral': return 'object';
|
|
97
|
+
default: return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function checkTypes(ast, options = {}) {
|
|
102
|
+
const ir = options.ir || lowerToIR(ast);
|
|
103
|
+
const diagnostics = [];
|
|
104
|
+
const schemas = new Map();
|
|
105
|
+
const functions = new Map();
|
|
106
|
+
const symbols = new Map();
|
|
107
|
+
|
|
108
|
+
function addTypeSpecErrors(spec, node) {
|
|
109
|
+
if (!spec) return;
|
|
110
|
+
if (spec.kind === 'name') {
|
|
111
|
+
if (!PRIMITIVES.has(spec.name) && !schemas.has(spec.name)) {
|
|
112
|
+
diagnostics.push(diagnostic(`Unknown type "${spec.name}". Declare it with type ${spec.name} ... done.`, node, 'PLN-TYPE-UNKNOWN'));
|
|
113
|
+
}
|
|
114
|
+
} else if (spec.kind === 'optional' || spec.kind === 'list' || spec.kind === 'dictionary') {
|
|
115
|
+
addTypeSpecErrors(spec.value, node);
|
|
116
|
+
} else if (spec.kind === 'union') {
|
|
117
|
+
spec.values.forEach(value => addTypeSpecErrors(value, node));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const node of ast.body || []) {
|
|
122
|
+
if (node.type === 'TypeDeclaration') {
|
|
123
|
+
if (schemas.has(node.name)) diagnostics.push(diagnostic(`Type "${node.name}" is declared more than once.`, node, 'PLN-TYPE-DUPLICATE'));
|
|
124
|
+
schemas.set(node.name, node);
|
|
125
|
+
symbols.set(node.name, { kind: 'type', node, type: nameSpec(node.name) });
|
|
126
|
+
}
|
|
127
|
+
if (node.type === 'FunctionDeclaration' || node.type === 'IntentDeclaration') {
|
|
128
|
+
functions.set(node.name, node);
|
|
129
|
+
symbols.set(node.name, { kind: node.type === 'IntentDeclaration' ? 'intent' : 'function', node, type: node.returnType || 'any' });
|
|
130
|
+
}
|
|
131
|
+
if (node.type === 'RememberStatement' && typeof node.name === 'string') symbols.set(node.name, { kind: 'variable', node, type: node.typeAnnotation || 'any' });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const node of ast.body || []) {
|
|
135
|
+
if (node.type === 'TypeDeclaration') {
|
|
136
|
+
for (const field of node.fields || []) addTypeSpecErrors(field.type, field);
|
|
137
|
+
}
|
|
138
|
+
if (node.type === 'FunctionDeclaration' || node.type === 'IntentDeclaration') {
|
|
139
|
+
for (const param of node.params || []) if (param.typeAnnotation) addTypeSpecErrors(param.typeAnnotation, param);
|
|
140
|
+
if (node.returnType) addTypeSpecErrors(node.returnType, node);
|
|
141
|
+
}
|
|
142
|
+
if (node.type === 'RememberStatement' && node.typeAnnotation) addTypeSpecErrors(node.typeAnnotation, node);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function fieldSpec(schemaName, fieldName) {
|
|
146
|
+
const schema = schemas.get(schemaName);
|
|
147
|
+
const field = schema && (schema.fields || []).find(item => item.key === fieldName);
|
|
148
|
+
return field ? field.type : null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function infer(node, env, origin = node) {
|
|
152
|
+
if (!node) return 'any';
|
|
153
|
+
if (node.type === 'Identifier') {
|
|
154
|
+
const entry = env.get(node.name) || symbols.get(node.name);
|
|
155
|
+
return entry && entry.type ? entry.type : 'any';
|
|
156
|
+
}
|
|
157
|
+
const literal = literalType(node);
|
|
158
|
+
if (literal === 'list') {
|
|
159
|
+
const elements = (node.elements || []).filter(item => item && item.type !== 'SpreadElement').map(item => infer(item, env, origin));
|
|
160
|
+
const first = elements[0] || 'any';
|
|
161
|
+
return listSpec(elements.every(item => specMatches(item, first, schemas)) ? first : 'any');
|
|
162
|
+
}
|
|
163
|
+
if (literal === 'object') return node.type === 'ObjectLiteral' ? dictionarySpec('any') : 'object';
|
|
164
|
+
if (literal) return literal;
|
|
165
|
+
if (node.type === 'MemberExpression') {
|
|
166
|
+
let objectType = infer(node.object, env, origin);
|
|
167
|
+
if (isOptionalSpec(objectType)) {
|
|
168
|
+
diagnostics.push(diagnostic(`Value may be null before accessing field "${node.property}". Check it first.`, origin, 'PLN-TYPE-OPTIONAL'));
|
|
169
|
+
objectType = withoutNull(objectType);
|
|
170
|
+
}
|
|
171
|
+
const schemaName = objectType && objectType.kind === 'name' ? objectType.name : objectType;
|
|
172
|
+
if (schemaName && schemas.has(schemaName)) {
|
|
173
|
+
const field = fieldSpec(schemaName, node.property);
|
|
174
|
+
if (!field) diagnostics.push(diagnostic(`Type "${schemaName}" has no field named "${node.property}".`, origin, 'PLN-TYPE-FIELD'));
|
|
175
|
+
return field || 'any';
|
|
176
|
+
}
|
|
177
|
+
if (objectType && objectType.kind === 'dictionary') return objectType.value;
|
|
178
|
+
return 'any';
|
|
179
|
+
}
|
|
180
|
+
if (node.type === 'IndexExpression') {
|
|
181
|
+
const objectType = infer(node.object, env, origin);
|
|
182
|
+
if (objectType && (objectType.kind === 'list' || objectType.kind === 'dictionary')) return objectType.value;
|
|
183
|
+
return 'any';
|
|
184
|
+
}
|
|
185
|
+
if (node.type === 'CallExpression') {
|
|
186
|
+
const fn = functions.get(node.name);
|
|
187
|
+
return fn && fn.returnType ? fn.returnType : 'any';
|
|
188
|
+
}
|
|
189
|
+
if (node.type === 'BinaryExpression') {
|
|
190
|
+
if (['===', '!==', '>', '<', '>=', '<=', 'in'].includes(node.operator)) return 'boolean';
|
|
191
|
+
const left = infer(node.left, env, origin);
|
|
192
|
+
const right = infer(node.right, env, origin);
|
|
193
|
+
if (node.operator === '+' && (typeName(left) === 'text' || typeName(right) === 'text')) return 'text';
|
|
194
|
+
if (['+', '-', '*', '/', '%'].includes(node.operator) && typeName(left) === 'number' && typeName(right) === 'number') return 'number';
|
|
195
|
+
return 'any';
|
|
196
|
+
}
|
|
197
|
+
if (node.type === 'ConditionalExpression') {
|
|
198
|
+
const left = infer(node.consequent, env, origin);
|
|
199
|
+
const right = infer(node.alternate, env, origin);
|
|
200
|
+
return typeName(left) === typeName(right) ? left : { kind: 'union', values: [left, right] };
|
|
201
|
+
}
|
|
202
|
+
if (node.type === 'ConditionExpression' || node.type === 'BinaryCondition' || node.type === 'UnaryCondition' ||
|
|
203
|
+
node.type === 'BetweenCondition' || node.type === 'StringCondition' || node.type === 'InCondition' ||
|
|
204
|
+
node.type === 'NotInCondition' || node.type === 'LogicalCondition') return 'boolean';
|
|
205
|
+
return 'any';
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function expectedMatchesNode(node, expected, env, origin, path, emit = true) {
|
|
209
|
+
if (!node || !expected) return true;
|
|
210
|
+
if (expected.kind === 'optional') {
|
|
211
|
+
if (node.type === 'NullLiteral') return true;
|
|
212
|
+
return expectedMatchesNode(node, expected.value, env, origin, path, emit);
|
|
213
|
+
}
|
|
214
|
+
if (expected.kind === 'union') {
|
|
215
|
+
const start = diagnostics.length;
|
|
216
|
+
for (const option of expected.values) {
|
|
217
|
+
diagnostics.length = start;
|
|
218
|
+
if (expectedMatchesNode(node, option, env, origin, path, false)) {
|
|
219
|
+
diagnostics.length = start;
|
|
220
|
+
return expectedMatchesNode(node, option, env, origin, path, emit);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
diagnostics.length = start;
|
|
224
|
+
if (emit) diagnostics.push(diagnostic(`${path ? `${path} ` : ''}expects ${typeName(expected)}, received ${typeName(infer(node, env, origin))}.`, origin, 'PLN-TYPE-ELEMENT'));
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
if (expected.kind === 'list') {
|
|
228
|
+
const actual = infer(node, env, origin);
|
|
229
|
+
if (node.type !== 'ArrayLiteral') {
|
|
230
|
+
const ok = specMatches(actual, expected, schemas);
|
|
231
|
+
if (!ok && emit) diagnostics.push(diagnostic(`${path ? `${path} ` : ''}expects ${typeName(expected)}, received ${typeName(actual)}.`, origin, 'PLN-TYPE-COLLECTION'));
|
|
232
|
+
return ok;
|
|
233
|
+
}
|
|
234
|
+
let ok = true;
|
|
235
|
+
(node.elements || []).forEach((element, index) => {
|
|
236
|
+
if (element && element.type === 'SpreadElement') return;
|
|
237
|
+
if (!expectedMatchesNode(element, expected.value, env, origin, `${path || 'Collection'} element ${index + 1}`, emit)) ok = false;
|
|
238
|
+
});
|
|
239
|
+
return ok;
|
|
240
|
+
}
|
|
241
|
+
if (expected.kind === 'dictionary') {
|
|
242
|
+
const actual = infer(node, env, origin);
|
|
243
|
+
if (node.type !== 'ObjectLiteral' && node.type !== 'InlineObjectLiteral') {
|
|
244
|
+
const ok = specMatches(actual, expected, schemas);
|
|
245
|
+
if (!ok && emit) diagnostics.push(diagnostic(`${path ? `${path} ` : ''}expects ${typeName(expected)}, received ${typeName(actual)}.`, origin, 'PLN-TYPE-COLLECTION'));
|
|
246
|
+
return ok;
|
|
247
|
+
}
|
|
248
|
+
let ok = true;
|
|
249
|
+
(node.properties || []).forEach(property => {
|
|
250
|
+
if (property.type === 'SpreadProperty') return;
|
|
251
|
+
if (!expectedMatchesNode(property.value, expected.value, env, origin, `${path || 'Dictionary'} value "${property.key}"`, emit)) ok = false;
|
|
252
|
+
});
|
|
253
|
+
return ok;
|
|
254
|
+
}
|
|
255
|
+
if (expected.kind === 'name' && schemas.has(expected.name) &&
|
|
256
|
+
(node.type === 'InlineObjectLiteral' || node.type === 'ObjectLiteral')) {
|
|
257
|
+
const schema = schemas.get(expected.name);
|
|
258
|
+
const properties = new Map((node.properties || []).map(property => [property.key, property]));
|
|
259
|
+
let ok = true;
|
|
260
|
+
for (const field of schema.fields || []) {
|
|
261
|
+
const property = properties.get(field.key);
|
|
262
|
+
if (!property) {
|
|
263
|
+
if (!isOptionalSpec(field.type)) {
|
|
264
|
+
if (emit) diagnostics.push(diagnostic(`${path ? `${path} ` : ''}is missing required field "${field.key}" for type "${expected.name}".`, origin, 'PLN-TYPE-FIELD'));
|
|
265
|
+
ok = false;
|
|
266
|
+
}
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const start = diagnostics.length;
|
|
270
|
+
if (!expectedMatchesNode(property.value, field.type, env, origin, `${path ? `${path} ` : ''}field "${field.key}"`, emit)) ok = false;
|
|
271
|
+
for (let index = start; index < diagnostics.length; index += 1) {
|
|
272
|
+
const item = diagnostics[index];
|
|
273
|
+
if (item.code === 'PLN-TYPE-ARG' || item.code === 'PLN-TYPE-ELEMENT' || item.code === 'PLN-TYPE-COLLECTION') item.code = 'PLN-TYPE-FIELD';
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
for (const property of node.properties || []) {
|
|
277
|
+
if (!(schema.fields || []).some(field => field.key === property.key)) {
|
|
278
|
+
if (emit) diagnostics.push(diagnostic(`${path ? `${path} ` : ''}type "${expected.name}" has no field named "${property.key}".`, origin, 'PLN-TYPE-FIELD'));
|
|
279
|
+
ok = false;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return ok;
|
|
283
|
+
}
|
|
284
|
+
const actual = infer(node, env, origin);
|
|
285
|
+
const ok = specMatches(actual, expected, schemas);
|
|
286
|
+
if (!ok && emit) diagnostics.push(diagnostic(`${path ? `${path} ` : ''}expects ${typeName(expected)}, received ${typeName(actual)}.`, origin, 'PLN-TYPE-ARG'));
|
|
287
|
+
return ok;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function checkExpression(node, env, origin = node) {
|
|
291
|
+
if (!node || typeof node !== 'object') return;
|
|
292
|
+
if (node.type === 'MemberExpression' || node.type === 'IndexExpression') {
|
|
293
|
+
infer(node, env, origin);
|
|
294
|
+
checkExpression(node.object, env, origin);
|
|
295
|
+
if (node.index) checkExpression(node.index, env, origin);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (node.type === 'CallExpression') {
|
|
299
|
+
const fn = functions.get(node.name);
|
|
300
|
+
if (fn) {
|
|
301
|
+
const params = (fn.params || []).filter(param => param.name);
|
|
302
|
+
if (node.args.length !== params.length && !params.some(param => param.type === 'RestElement')) {
|
|
303
|
+
diagnostics.push(diagnostic(`Intent/function "${node.name}" expects ${params.length} argument(s), received ${node.args.length}.`, origin, 'PLN-TYPE-ARITY'));
|
|
304
|
+
}
|
|
305
|
+
node.args.forEach((arg, index) => {
|
|
306
|
+
const param = params[index];
|
|
307
|
+
if (param && param.typeAnnotation) expectedMatchesNode(arg, param.typeAnnotation, env, origin, `Argument ${index + 1} of "${node.name}"`);
|
|
308
|
+
checkExpression(arg, env, origin);
|
|
309
|
+
});
|
|
310
|
+
} else {
|
|
311
|
+
node.args.forEach(arg => checkExpression(arg, env, origin));
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (node.type === 'BinaryExpression') {
|
|
316
|
+
checkExpression(node.left, env, origin);
|
|
317
|
+
checkExpression(node.right, env, origin);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (node.type === 'ConditionalExpression') {
|
|
321
|
+
checkExpression(node.condition, env, origin);
|
|
322
|
+
checkExpression(node.consequent, env, origin);
|
|
323
|
+
checkExpression(node.alternate, env, origin);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
for (const value of Object.values(node)) {
|
|
327
|
+
if (value && typeof value === 'object') {
|
|
328
|
+
if (Array.isArray(value)) value.forEach(item => checkExpression(item, env, origin));
|
|
329
|
+
else if (value.type) checkExpression(value, env, origin);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function narrowEnvironment(env, condition, truthy) {
|
|
335
|
+
const next = new Map(env);
|
|
336
|
+
function apply(left, operator, right) {
|
|
337
|
+
if (!left || left.type !== 'Identifier' || !right || right.type !== 'NullLiteral') return;
|
|
338
|
+
const entry = next.get(left.name);
|
|
339
|
+
if (!entry || !isOptionalSpec(entry.type)) return;
|
|
340
|
+
const isNonNull = operator === 'is not' || operator === '!==' || operator === 'different from';
|
|
341
|
+
if (truthy === isNonNull) next.set(left.name, { ...entry, type: withoutNull(entry.type) });
|
|
342
|
+
}
|
|
343
|
+
if (condition && condition.type === 'ConditionExpression') return narrowEnvironment(next, condition.value, truthy);
|
|
344
|
+
if (condition && condition.type === 'BinaryCondition') apply(condition.left, condition.op, condition.right);
|
|
345
|
+
if (condition && condition.type === 'LogicalCondition' && condition.op === 'and' && truthy) {
|
|
346
|
+
narrowEnvironment(narrowEnvironment(next, condition.left, true), condition.right, true).forEach((value, key) => next.set(key, value));
|
|
347
|
+
}
|
|
348
|
+
return next;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function checkStatements(statements, env, returnType = null, functionName = null) {
|
|
352
|
+
for (const node of statements || []) {
|
|
353
|
+
if (!node) continue;
|
|
354
|
+
if (node.type === 'RememberStatement' && typeof node.name === 'string') {
|
|
355
|
+
if (node.typeAnnotation) expectedMatchesNode(node.value, node.typeAnnotation, env, node, `Variable "${node.name}"`);
|
|
356
|
+
const inferred = node.typeAnnotation || infer(node.value, env, node);
|
|
357
|
+
env.set(node.name, { type: inferred || 'any', node });
|
|
358
|
+
symbols.set(node.name, { kind: 'variable', node, type: inferred || 'any' });
|
|
359
|
+
checkExpression(node.value, env, node);
|
|
360
|
+
} else if (node.type === 'FunctionDeclaration' || node.type === 'IntentDeclaration') {
|
|
361
|
+
const nested = new Map(env);
|
|
362
|
+
for (const param of node.params || []) if (param.name) nested.set(param.name, { type: param.typeAnnotation || 'any', node: param });
|
|
363
|
+
checkStatements(node.body, nested, node.returnType || null, node.name);
|
|
364
|
+
checkFunctionReturns(node, nested);
|
|
365
|
+
} else if (node.type === 'IfStatement') {
|
|
366
|
+
checkExpression(node.condition, env, node);
|
|
367
|
+
checkStatements(node.consequent, narrowEnvironment(env, node.condition, true), returnType, functionName);
|
|
368
|
+
if (node.alternate) checkStatements(node.alternate, narrowEnvironment(env, node.condition, false), returnType, functionName);
|
|
369
|
+
} else if (node.type === 'BecomeStatement') {
|
|
370
|
+
checkExpression(node.value, env, node);
|
|
371
|
+
const actual = infer(node.value, env, node);
|
|
372
|
+
if (node.target && node.target.type === 'Identifier') {
|
|
373
|
+
const entry = env.get(node.target.name);
|
|
374
|
+
if (entry && entry.type && !specMatches(actual, entry.type, schemas)) {
|
|
375
|
+
diagnostics.push(diagnostic(`Assignment to "${node.target.name}" expects ${typeName(entry.type)}, received ${typeName(actual)}.`, node, 'PLN-TYPE-ASSIGN'));
|
|
376
|
+
}
|
|
377
|
+
if (entry && node.op === '=') env.set(node.target.name, { ...entry, type: actual });
|
|
378
|
+
}
|
|
379
|
+
} else if (node.type === 'GiveStatement' || node.type === 'ReturnStatement') {
|
|
380
|
+
checkExpression(node.value, env, node);
|
|
381
|
+
if (returnType && node.value) {
|
|
382
|
+
const start = diagnostics.length;
|
|
383
|
+
expectedMatchesNode(node.value, returnType, env, node, `Function "${functionName}" return`);
|
|
384
|
+
for (let index = start; index < diagnostics.length; index += 1) {
|
|
385
|
+
const item = diagnostics[index];
|
|
386
|
+
if (item.code === 'PLN-TYPE-ARG' || item.code === 'PLN-TYPE-ELEMENT' || item.code === 'PLN-TYPE-COLLECTION') item.code = 'PLN-TYPE-RETURN';
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
for (const value of Object.values(node)) {
|
|
391
|
+
if (value && typeof value === 'object') {
|
|
392
|
+
if (Array.isArray(value)) value.forEach(item => item && (item.type ? checkExpression(item, env, node) : checkStatements([item], env)));
|
|
393
|
+
else if (value.type && value.type !== 'TypeDeclaration') checkExpression(value, env, node);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function collectReturns(statements, result = []) {
|
|
401
|
+
for (const node of statements || []) {
|
|
402
|
+
if (!node || typeof node !== 'object') continue;
|
|
403
|
+
if (node.type === 'GiveStatement' || node.type === 'ReturnStatement') result.push(node);
|
|
404
|
+
for (const key of ['body', 'consequent', 'alternate', 'handlers', 'finalizer']) {
|
|
405
|
+
const child = node[key];
|
|
406
|
+
if (Array.isArray(child)) collectReturns(child, result);
|
|
407
|
+
else if (child && typeof child === 'object') collectReturns([child], result);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function checkFunctionReturns(fn, env) {
|
|
414
|
+
if (!fn.returnType) return;
|
|
415
|
+
const returns = collectReturns(fn.body);
|
|
416
|
+
if (returns.length === 0) {
|
|
417
|
+
diagnostics.push(diagnostic(`Function "${fn.name}" declares ${typeName(fn.returnType)} but does not return a value.`, fn, 'PLN-TYPE-RETURN-MISSING'));
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (returns.some(statement => !statement.value) && !isOptionalSpec(fn.returnType)) {
|
|
421
|
+
diagnostics.push(diagnostic(`Function "${fn.name}" returns ${typeName(fn.returnType)} but this return has no value.`, fn, 'PLN-TYPE-RETURN'));
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Imports are resolved by the bundler before this checker receives a graph.
|
|
426
|
+
// A flattened graph therefore exposes declarations from every local module;
|
|
427
|
+
// report only names that are genuinely absent and ignore npm imports.
|
|
428
|
+
for (const node of ast.body || []) {
|
|
429
|
+
if (node.type !== 'ImportStatement' || !node.path || !node.path.endsWith('.pln') && !node.path.startsWith('.') && !node.path.startsWith('@/') && !node.path.startsWith('/')) continue;
|
|
430
|
+
const imported = node.names || (node.defaultImport ? [node.defaultImport] : []);
|
|
431
|
+
for (const name of imported) {
|
|
432
|
+
if (!symbols.has(name)) diagnostics.push(diagnostic(`Imported symbol "${name}" was not found in the resolved module graph.`, node, 'PLN-MODULE-UNKNOWN'));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
checkStatements(ast.body, new Map());
|
|
437
|
+
return { diagnostics, ir };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
module.exports = { checkTypes, typeName, specMatches };
|
package/compiler/version.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# PlainScript 1.0.
|
|
1
|
+
# PlainScript 1.0.363 Capability Audit
|
|
2
2
|
|
|
3
3
|
This audit is based on what the compiler actually supports right now.
|
|
4
4
|
If `plainscript check` validates it, it's listed here.
|
|
@@ -14,12 +14,23 @@ If `plainscript check` validates it, it's listed here.
|
|
|
14
14
|
| Records and record kinds | Implemented | `define a kind called "User" with ... done` |
|
|
15
15
|
| Conditions | Implemented | `if ... otherwise ... done` |
|
|
16
16
|
| Functions | Implemented | `make add(a, b) ... done` |
|
|
17
|
+
| Intent declarations | Implemented | `intend greet(name) ... done`; preserved in AST and IR |
|
|
18
|
+
| Runtime data contracts | Implemented | `type User ... done`, typed parameters, unions and collections |
|
|
19
|
+
| Static contract diagnostics | Implemented | AST/IR checker reports field, arity, literal-value, and unknown-type errors |
|
|
17
20
|
| Generators | Implemented | `yield value` |
|
|
18
21
|
| For and while loops | Implemented | `for each ... done`, `while ... done` |
|
|
19
22
|
| Match and switch | Implemented | `match value against ... done` |
|
|
20
23
|
| Loop control | Implemented | `break`, `continue` |
|
|
21
24
|
| String verbs | Implemented | `lowercase`, `uppercase`, `split`, `join`, `trim`, `capitalize words` |
|
|
22
25
|
| Classes and `new` | Implemented | JavaScript-compatible class forms |
|
|
26
|
+
| Compound assignment | Implemented | `+=`, `-=`, `*=`, `/=`, `%=`, `++=`, `or becomes`, `and becomes`, `nullish becomes` |
|
|
27
|
+
| `set` / `change` assignment | Implemented | `set x to 1`, `change x to 1`, `x set to 1`, `x change to 1` |
|
|
28
|
+
| Statistics | Implemented | `mean`, `median`, `variance`, `deviation` |
|
|
29
|
+
| Vector math | Implemented | `dotProduct`, `magnitude`, `normalize` |
|
|
30
|
+
| Randomness | Implemented | `randomInteger`, `randomChoice`, `weightedChoice`, `shuffle`, `sample` |
|
|
31
|
+
| Memoization | Implemented | `memoize(fn)`, `parseBoolean`, `characters` |
|
|
32
|
+
| Regex capture | Implemented | `match pattern "^(\\d+)$" in "4061" as digits` |
|
|
33
|
+
| Collection size | Implemented | `count of [1, 2, 3]`, `count of "hello"` |
|
|
23
34
|
|
|
24
35
|
## Modules and Runtime
|
|
25
36
|
|
|
@@ -32,9 +43,14 @@ If `plainscript check` validates it, it's listed here.
|
|
|
32
43
|
| Files and paths | Implemented | Read/write, bytes, folders, path helpers |
|
|
33
44
|
| Crypto and auth helpers | Implemented | Hashes, passwords, tokens |
|
|
34
45
|
| Async/await | Implemented | `wait for`, `await`, async propagation |
|
|
35
|
-
| Concurrency | Implemented | `
|
|
46
|
+
| Concurrency | Implemented | `all of`, `any of`, `settled of`, `withTimeout`, `run in parallel ... done as name` |
|
|
36
47
|
| Errors and retries | Implemented | `try`, `recover`, `finally`, `retry` |
|
|
37
48
|
| Native tests | Implemented | `test` and `check` assertions |
|
|
49
|
+
| Terminal I/O | Implemented | `confirm("...")`, `choose("...", [...])`, `stderr(...)` |
|
|
50
|
+
| Terminal query | Implemented | `clearTerminal()`, `terminalWidth()`, `terminalHeight()` |
|
|
51
|
+
| CLI flags | Implemented | `--quiet`, `--verbose` |
|
|
52
|
+
| Compiler IR inspection | Implemented | `plainscript ir file.pln` emits versioned JSON |
|
|
53
|
+
| Language-server protocol | Implemented | `plainscript-lsp` stdio server with diagnostics, hover, completion, definitions, symbols |
|
|
38
54
|
|
|
39
55
|
## Web and Services
|
|
40
56
|
|
|
@@ -126,6 +142,6 @@ find examples -name '*.pln' -exec node compiler/cli.js check {} \;
|
|
|
126
142
|
|
|
127
143
|
## Version Info
|
|
128
144
|
|
|
129
|
-
The release label for this repo is `1.0.
|
|
145
|
+
The release label for this repo is `1.0.363`. It covers the compiler,
|
|
130
146
|
package metadata, website, editor tooling, and browser/game documentation.
|
|
131
147
|
Third-party dependency versions are not tied to this label.
|
package/docs/GAME-PROMPT.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# PlainScript Game Development Prompt Guide (v1.0.
|
|
1
|
+
# PlainScript Game Development Prompt Guide (v1.0.363)
|
|
2
2
|
|
|
3
3
|
The standing instruction sheet for AI agents asked to build games and
|
|
4
4
|
interactive browser apps in PlainScript.
|
|
@@ -29,7 +29,7 @@ Every PlainScript block you show a user must compile - check it first.
|
|
|
29
29
|
|
|
30
30
|
PlainScript is intent-oriented: you describe *what* the game should do and the
|
|
31
31
|
compiler generates JavaScript. One hard rule - **game logic lives in
|
|
32
|
-
PlainScript**. v1.0.
|
|
32
|
+
PlainScript**. v1.0.363 reaches into any JS API from `.pln`, which makes
|
|
33
33
|
dropping to JS tempting; do not. Every browser capability below stays
|
|
34
34
|
reachable without leaving PlainScript.
|
|
35
35
|
|
|
@@ -237,7 +237,7 @@ done
|
|
|
237
237
|
requestAnimationFrame(loop)
|
|
238
238
|
```
|
|
239
239
|
|
|
240
|
-
v1.0.
|
|
240
|
+
v1.0.363 adds hex literals (`0xRRGGBB`): numeric colors use `0xffffff` instead
|
|
241
241
|
of magic decimals, and a `"#ffffff"` string works too - THREE accepts all
|
|
242
242
|
three.
|
|
243
243
|
|