plainscript-lang 1.0.1 → 1.0.2
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 +154 -147
- package/compiler/bundler.js +2 -2
- package/compiler/cli.js +58 -58
- package/compiler/formatter.js +1 -1
- package/compiler/generator.js +753 -67
- package/compiler/lexer.js +127 -53
- package/compiler/parser.js +953 -151
- package/compiler/version.js +1 -1
- package/docs/CAPABILITY_GAP_AUDIT.md +130 -29
- package/docs/PLAINSCRIPT-SPEC.md +584 -0
- package/docs/index.html +33 -42
- package/docs/website/script.js +110 -24
- package/docs/website/style.css +4 -4
- package/examples/arrays.pln +12 -0
- package/examples/{auth-api.ps → auth-api.pln} +3 -3
- package/examples/{cache-and-jobs.ps → cache-and-jobs.pln} +1 -1
- package/examples/{concurrency.ps → concurrency.pln} +8 -8
- package/examples/{database.ps → database.pln} +1 -1
- package/examples/{deployment.ps → deployment.pln} +2 -2
- package/examples/{email-send.ps → email-send.pln} +1 -1
- package/examples/{error-handling.ps → error-handling.pln} +8 -7
- package/examples/{expressions.ps → expressions.pln} +1 -1
- package/examples/{file-uploads.ps → file-uploads.pln} +4 -4
- package/examples/{files-and-config.ps → files-and-config.pln} +4 -4
- package/examples/football-backend/{app.ps → app.pln} +2 -2
- package/examples/{http-client.ps → http-client.pln} +18 -18
- package/examples/id-verification/{app.ps → app.pln} +1 -1
- package/examples/{loops.ps → loops.pln} +2 -3
- package/examples/modules/{app.ps → app.pln} +4 -4
- package/examples/modules/{math.ps → math.pln} +1 -1
- package/examples/{record-kinds.ps → record-kinds.pln} +4 -4
- package/examples/{rest-api-fetch.ps → rest-api-fetch.pln} +1 -1
- package/examples/{rest-api.ps → rest-api.pln} +5 -5
- package/examples/{scheduled-tasks.ps → scheduled-tasks.pln} +2 -2
- package/examples/{search-and-filter.ps → search-and-filter.pln} +5 -5
- package/examples/{sessions-and-cookies.ps → sessions-and-cookies.pln} +2 -2
- package/examples/start.pln +2 -0
- package/examples/{stdlib.ps → stdlib.pln} +2 -2
- package/examples/{telegram-bot.ps → telegram-bot.pln} +2 -2
- package/examples/{websocket-server.ps → websocket-server.pln} +1 -1
- package/package.json +1 -1
- package/docs/PLINJS_SPEC.md +0 -1151
- package/examples/arrays.ps +0 -13
- package/examples/start.ps +0 -2
- /package/examples/{day2.ps → day2.pln} +0 -0
- /package/examples/{day3.ps → day3.pln} +0 -0
- /package/examples/{hello.ps → hello.pln} +0 -0
- /package/examples/{objects.ps → objects.pln} +0 -0
- /package/examples/{server.ps → server.pln} +0 -0
- /package/examples/{web-app.ps → web-app.pln} +0 -0
- /package/examples/whatsapp-bot/{pairing.ps → pairing.pln} +0 -0
- /package/examples/whatsapp-bot/{qr.ps → qr.pln} +0 -0
package/compiler/generator.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// Generator: converts a PlainScript AST into JavaScript source code.
|
|
2
2
|
|
|
3
|
-
const vm = require('vm');
|
|
4
3
|
const { splitPackageSpec } = require('./dependency-detector');
|
|
5
4
|
|
|
6
5
|
// Known runtime packages and their require() statements.
|
|
@@ -1095,15 +1094,33 @@ const BUILTIN_DECLARATIONS = {
|
|
|
1095
1094
|
};
|
|
1096
1095
|
|
|
1097
1096
|
// Built-in stdlib functions: PlainScript name → JS code generator.
|
|
1098
|
-
// v0.1–v0.4 original stdlib
|
|
1099
|
-
const STDLIB = {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1097
|
+
// v0.1–v0.4 original stdlib
|
|
1098
|
+
const STDLIB = {
|
|
1099
|
+
length: (args, context) => `(${generateExpr(args[0], context)}).length`,
|
|
1100
|
+
uppercase: (args, context) => `(${generateExpr(args[0], context)}).toUpperCase()`,
|
|
1101
|
+
lowercase: (args, context) => `(${generateExpr(args[0], context)}).toLowerCase()`,
|
|
1102
|
+
random: (_args) => `Math.random()`,
|
|
1103
|
+
round: (args, context) => `Math.round(${generateExpr(args[0], context)})`,
|
|
1104
|
+
// Symbol creation
|
|
1105
|
+
symbol: (args, context) => `Symbol(${args.length > 0 ? generateExpr(args[0], context) : 'undefined'})`,
|
|
1106
|
+
symbolFor: (args, context) => `Symbol.for(${generateExpr(args[0], context)})`,
|
|
1107
|
+
symbolKeyFor: (args, context) => `Symbol.keyFor(${generateExpr(args[0], context)})`,
|
|
1108
|
+
// Well-known symbols
|
|
1109
|
+
symbolIterator: (_args) => `Symbol.iterator`,
|
|
1110
|
+
symbolAsyncIterator: (_args) => `Symbol.asyncIterator`,
|
|
1111
|
+
symbolMatch: (_args) => `Symbol.match`,
|
|
1112
|
+
symbolMatchAll: (_args) => `Symbol.matchAll`,
|
|
1113
|
+
symbolReplace: (_args) => `Symbol.replace`,
|
|
1114
|
+
symbolSearch: (_args) => `Symbol.search`,
|
|
1115
|
+
symbolSplit: (_args) => `Symbol.split`,
|
|
1116
|
+
symbolHasInstance: (_args) => `Symbol.hasInstance`,
|
|
1117
|
+
symbolIsConcatSpreadable: (_args) => `Symbol.isConcatSpreadable`,
|
|
1118
|
+
symbolToPrimitive: (_args) => `Symbol.toPrimitive`,
|
|
1119
|
+
symbolToStringTag: (_args) => `Symbol.toStringTag`,
|
|
1120
|
+
symbolUnscopables: (_args) => `Symbol.unscopables`,
|
|
1121
|
+
symbolSpecies: (_args) => `Symbol.species`,
|
|
1122
|
+
// Runtime constructors
|
|
1123
|
+
sqlite: (args, context) => `new Database(${args.map(arg => generateExpr(arg, context)).join(', ')})`,
|
|
1107
1124
|
// v0.6 — runtime standard library
|
|
1108
1125
|
print: (args, context) => `console.log(${args.map(arg => generateExpr(arg, context)).join(', ')})`,
|
|
1109
1126
|
readFile: (args, context) => `fs.readFileSync(${generateExpr(args[0], context)}, 'utf8')`,
|
|
@@ -1261,6 +1278,26 @@ const STDLIB = {
|
|
|
1261
1278
|
padStart: (args, context) => `String(${generateExpr(args[0], context)}).padStart(${generateExpr(args[1], context)}, String(${args.length > 2 ? generateExpr(args[2], context) : '" "'}))`,
|
|
1262
1279
|
padEnd: (args, context) => `String(${generateExpr(args[0], context)}).padEnd(${generateExpr(args[1], context)}, String(${args.length > 2 ? generateExpr(args[2], context) : '" "'}))`,
|
|
1263
1280
|
|
|
1281
|
+
// ── v2.3.0 — Proxy and Reflect (IOPL-native).
|
|
1282
|
+
// Proxy creation: proxy(target, handler)
|
|
1283
|
+
proxy: (args, context) => `new Proxy(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1284
|
+
// Proxy revocable: proxyRevocable(target, handler) -> { proxy, revoke }
|
|
1285
|
+
proxyRevocable: (args, context) => `Proxy.revocable(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1286
|
+
// Reflect methods
|
|
1287
|
+
reflectApply: (args, context) => `Reflect.apply(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1288
|
+
reflectConstruct: (args, context) => `Reflect.construct(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1289
|
+
reflectDefineProperty: (args, context) => `Reflect.defineProperty(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1290
|
+
reflectDeleteProperty: (args, context) => `Reflect.deleteProperty(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1291
|
+
reflectGet: (args, context) => `Reflect.get(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1292
|
+
reflectGetOwnPropertyDescriptor: (args, context) => `Reflect.getOwnPropertyDescriptor(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1293
|
+
reflectGetPrototypeOf: (args, context) => `Reflect.getPrototypeOf(${generateExpr(args[0], context)})`,
|
|
1294
|
+
reflectHas: (args, context) => `Reflect.has(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1295
|
+
reflectIsExtensible: (args, context) => `Reflect.isExtensible(${generateExpr(args[0], context)})`,
|
|
1296
|
+
reflectOwnKeys: (args, context) => `Reflect.ownKeys(${generateExpr(args[0], context)})`,
|
|
1297
|
+
reflectPreventExtensions: (args, context) => `Reflect.preventExtensions(${generateExpr(args[0], context)})`,
|
|
1298
|
+
reflectSet: (args, context) => `Reflect.set(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1299
|
+
reflectSetPrototypeOf: (args, context) => `Reflect.setPrototypeOf(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1300
|
+
|
|
1264
1301
|
// ── v1.0.1 — nullable / regex / date helpers (IOPL-native).
|
|
1265
1302
|
// first non-null, non-undefined argument — IOPL null-coalescing.
|
|
1266
1303
|
coalesce: (args, context) =>
|
|
@@ -1278,6 +1315,144 @@ const STDLIB = {
|
|
|
1278
1315
|
return `__formatDate(${value}, ${pattern})`;
|
|
1279
1316
|
},
|
|
1280
1317
|
|
|
1318
|
+
// ── v1.0.2 — Native Date/DateTime/Regex support (IOPL-native, no JS gateway needed).
|
|
1319
|
+
// Create a new Date object. With no args: now. With 1 arg: parse ISO string or timestamp.
|
|
1320
|
+
// With multiple args: year, month (1-12), day, hour, min, sec, ms.
|
|
1321
|
+
newDate: (args, context) => {
|
|
1322
|
+
if (!args || args.length === 0) return `new Date()`;
|
|
1323
|
+
if (args.length === 1) return `new Date(${generateExpr(args[0], context)})`;
|
|
1324
|
+
return `new Date(${args.map(a => generateExpr(a, context)).join(', ')})`;
|
|
1325
|
+
},
|
|
1326
|
+
// Current timestamp in milliseconds.
|
|
1327
|
+
now: (_args) => `Date.now()`,
|
|
1328
|
+
// Current timestamp in seconds (Unix epoch).
|
|
1329
|
+
nowSec: (_args) => `Math.floor(Date.now() / 1000)`,
|
|
1330
|
+
// Date → ISO string (e.g., "2026-08-28T12:34:56.789Z").
|
|
1331
|
+
toISO: (args, context) => `${generateExpr(args[0], context)}.toISOString()`,
|
|
1332
|
+
// Date → locale date string.
|
|
1333
|
+
toLocaleDate: (args, context) => `${generateExpr(args[0], context)}.toLocaleDateString()`,
|
|
1334
|
+
// Date → locale time string.
|
|
1335
|
+
toLocaleTime: (args, context) => `${generateExpr(args[0], context)}.toLocaleTimeString()`,
|
|
1336
|
+
// Date → locale string.
|
|
1337
|
+
toLocale: (args, context) => `${generateExpr(args[0], context)}.toLocaleString()`,
|
|
1338
|
+
// Date → UTC string.
|
|
1339
|
+
toUTC: (args, context) => `${generateExpr(args[0], context)}.toUTCString()`,
|
|
1340
|
+
// Get timestamp (milliseconds) from a Date.
|
|
1341
|
+
timestamp: (args, context) => `${generateExpr(args[0], context)}.getTime()`,
|
|
1342
|
+
// Get year (full, e.g., 2026).
|
|
1343
|
+
year: (args, context) => `${generateExpr(args[0], context)}.getFullYear()`,
|
|
1344
|
+
// Get month (1-12).
|
|
1345
|
+
month: (args, context) => `${generateExpr(args[0], context)}.getMonth() + 1`,
|
|
1346
|
+
// Get day of month (1-31).
|
|
1347
|
+
day: (args, context) => `${generateExpr(args[0], context)}.getDate()`,
|
|
1348
|
+
// Get day of week (0-6, Sun=0).
|
|
1349
|
+
dayOfWeek: (args, context) => `${generateExpr(args[0], context)}.getDay()`,
|
|
1350
|
+
// Get hours (0-23).
|
|
1351
|
+
hours: (args, context) => `${generateExpr(args[0], context)}.getHours()`,
|
|
1352
|
+
// Get minutes (0-59).
|
|
1353
|
+
minutes: (args, context) => `${generateExpr(args[0], context)}.getMinutes()`,
|
|
1354
|
+
// Get seconds (0-59).
|
|
1355
|
+
seconds: (args, context) => `${generateExpr(args[0], context)}.getSeconds()`,
|
|
1356
|
+
// Get milliseconds (0-999).
|
|
1357
|
+
milliseconds: (args, context) => `${generateExpr(args[0], context)}.getMilliseconds()`,
|
|
1358
|
+
// Get timezone offset in minutes.
|
|
1359
|
+
tzOffset: (args, context) => `${generateExpr(args[0], context)}.getTimezoneOffset()`,
|
|
1360
|
+
// Add time to a Date (returns new Date). Unit: ms, s, m, h, d, w, mo, y.
|
|
1361
|
+
addTime: (args, context) => {
|
|
1362
|
+
if (args.length < 3) throw new Error('addTime(date, amount, unit) requires 3 arguments');
|
|
1363
|
+
const date = generateExpr(args[0], context);
|
|
1364
|
+
const amount = generateExpr(args[1], context);
|
|
1365
|
+
const unit = generateExpr(args[2], context);
|
|
1366
|
+
return `(() => { const base = new Date(${date}.getTime()); const u = String(${unit}).toLowerCase(); const mult = { ms:1, s:1000, m:60000, h:3600000, d:86400000, w:604800000, mo:2592000000, y:31536000000 }; const result = new Date(base.getTime()); result.setTime(result.getTime() + ${amount} * (mult[u] || 1)); return result; })()`;
|
|
1367
|
+
},
|
|
1368
|
+
// Subtract time from a Date (returns new Date).
|
|
1369
|
+
subTime: (args, context) => {
|
|
1370
|
+
if (args.length < 3) throw new Error('subTime(date, amount, unit) requires 3 arguments');
|
|
1371
|
+
const date = generateExpr(args[0], context);
|
|
1372
|
+
const amount = generateExpr(args[1], context);
|
|
1373
|
+
const unit = generateExpr(args[2], context);
|
|
1374
|
+
return `(() => { const base = new Date(${date}.getTime()); const u = String(${unit}).toLowerCase(); const mult = { ms:1, s:1000, m:60000, h:3600000, d:86400000, w:604800000, mo:2592000000, y:31536000000 }; const result = new Date(base.getTime()); result.setTime(result.getTime() - ${amount} * (mult[u] || 1)); return result; })()`;
|
|
1375
|
+
},
|
|
1376
|
+
// Difference between two dates in given unit (ms, s, m, h, d, w).
|
|
1377
|
+
diffTime: (args, context) => {
|
|
1378
|
+
if (args.length < 3) throw new Error('diffTime(date1, date2, unit) requires 3 arguments');
|
|
1379
|
+
const d1 = generateExpr(args[0], context);
|
|
1380
|
+
const d2 = generateExpr(args[1], context);
|
|
1381
|
+
const unit = generateExpr(args[2], context);
|
|
1382
|
+
return `(() => { const u = String(${unit}).toLowerCase(); const mult = { ms:1, s:1000, m:60000, h:3600000, d:86400000, w:604800000 }; return Math.floor((${d1}.getTime() - ${d2}.getTime()) / (mult[u] || 1)); })()`;
|
|
1383
|
+
},
|
|
1384
|
+
// Start of day (00:00:00.000) for a Date.
|
|
1385
|
+
startOfDay: (args, context) => `(() => { const dt = new Date(${generateExpr(args[0], context)}); dt.setHours(0,0,0,0); return dt; })()`,
|
|
1386
|
+
// End of day (23:59:59.999) for a Date.
|
|
1387
|
+
endOfDay: (args, context) => `(() => { const dt = new Date(${generateExpr(args[0], context)}); dt.setHours(23,59,59,999); return dt; })()`,
|
|
1388
|
+
// Start of month (1st, 00:00:00.000).
|
|
1389
|
+
startOfMonth: (args, context) => `(() => { const dt = new Date(${generateExpr(args[0], context)}); dt.setDate(1); dt.setHours(0,0,0,0); return dt; })()`,
|
|
1390
|
+
// End of month (last day, 23:59:59.999).
|
|
1391
|
+
endOfMonth: (args, context) => `(() => { const dt = new Date(${generateExpr(args[0], context)}); dt.setMonth(dt.getMonth()+1, 0); dt.setHours(23,59,59,999); return dt; })()`,
|
|
1392
|
+
// Start of year (Jan 1, 00:00:00.000).
|
|
1393
|
+
startOfYear: (args, context) => `(() => { const dt = new Date(${generateExpr(args[0], context)}); dt.setMonth(0,1); dt.setHours(0,0,0,0); return dt; })()`,
|
|
1394
|
+
// End of year (Dec 31, 23:59:59.999).
|
|
1395
|
+
endOfYear: (args, context) => `(() => { const dt = new Date(${generateExpr(args[0], context)}); dt.setMonth(11,31); dt.setHours(23,59,59,999); return dt; })()`,
|
|
1396
|
+
// Check if a Date is valid (not NaN).
|
|
1397
|
+
isValidDate: (args, context) => `!isNaN(${generateExpr(args[0], context)}.getTime())`,
|
|
1398
|
+
// Format a Date with a custom pattern (YYYY, MM, DD, HH, mm, ss, SSS).
|
|
1399
|
+
format: (args, context) => {
|
|
1400
|
+
ensureBuiltin(context, 'core');
|
|
1401
|
+
const value = generateExpr(args[0], context);
|
|
1402
|
+
const pattern = args.length > 1 ? generateExpr(args[1], context) : '"YYYY-MM-DD HH:mm:ss"';
|
|
1403
|
+
return `__formatDate(${value}, ${pattern})`;
|
|
1404
|
+
},
|
|
1405
|
+
|
|
1406
|
+
// Regex creation and matching.
|
|
1407
|
+
// Create a RegExp from a pattern string. Optional flags (e.g., "gi").
|
|
1408
|
+
regex: (args, context) => {
|
|
1409
|
+
if (!args || args.length === 0) throw new Error('regex(pattern, flags?) requires at least a pattern');
|
|
1410
|
+
const pattern = generateExpr(args[0], context);
|
|
1411
|
+
const flags = args.length > 1 ? generateExpr(args[1], context) : 'undefined';
|
|
1412
|
+
return `new RegExp(${pattern}, ${flags})`;
|
|
1413
|
+
},
|
|
1414
|
+
// Test if a string matches a regex pattern.
|
|
1415
|
+
matches: (args, context) => {
|
|
1416
|
+
if (args.length < 2) throw new Error('matches(text, pattern, flags?) requires at least text and pattern');
|
|
1417
|
+
const text = generateExpr(args[0], context);
|
|
1418
|
+
const pattern = generateExpr(args[1], context);
|
|
1419
|
+
const flags = args.length > 2 ? generateExpr(args[2], context) : 'undefined';
|
|
1420
|
+
return `new RegExp(${pattern}, ${flags}).test(${text})`;
|
|
1421
|
+
},
|
|
1422
|
+
// Extract first match of regex in string (returns array or null).
|
|
1423
|
+
regexMatch: (args, context) => {
|
|
1424
|
+
if (args.length < 2) throw new Error('regexMatch(text, pattern, flags?) requires at least text and pattern');
|
|
1425
|
+
const text = generateExpr(args[0], context);
|
|
1426
|
+
const pattern = generateExpr(args[1], context);
|
|
1427
|
+
const flags = args.length > 2 ? generateExpr(args[2], context) : 'undefined';
|
|
1428
|
+
return `${text}.match(new RegExp(${pattern}, ${flags}))`;
|
|
1429
|
+
},
|
|
1430
|
+
// Extract all matches of regex in string (returns array).
|
|
1431
|
+
regexMatchAll: (args, context) => {
|
|
1432
|
+
if (args.length < 2) throw new Error('regexMatchAll(text, pattern, flags?) requires at least text and pattern');
|
|
1433
|
+
const text = generateExpr(args[0], context);
|
|
1434
|
+
const pattern = generateExpr(args[1], context);
|
|
1435
|
+
const flags = args.length > 2 ? generateExpr(args[2], context) : 'undefined';
|
|
1436
|
+
return `((${flags}) === undefined ? Array.from(${text}.matchAll(new RegExp(${pattern}, 'g'))) : Array.from(${text}.matchAll(new RegExp(${pattern}, ${flags} + 'g'))))`;
|
|
1437
|
+
},
|
|
1438
|
+
// Replace using regex (with capture groups support).
|
|
1439
|
+
replaceRegex: (args, context) => {
|
|
1440
|
+
if (args.length < 3) throw new Error('replaceRegex(text, pattern, replacement, flags?) requires at least 3 arguments');
|
|
1441
|
+
const text = generateExpr(args[0], context);
|
|
1442
|
+
const pattern = generateExpr(args[1], context);
|
|
1443
|
+
const replacement = generateExpr(args[2], context);
|
|
1444
|
+
const flags = args.length > 3 ? generateExpr(args[3], context) : 'undefined';
|
|
1445
|
+
return `${text}.replace(new RegExp(${pattern}, ${flags}), ${replacement})`;
|
|
1446
|
+
},
|
|
1447
|
+
// Split string by regex pattern.
|
|
1448
|
+
splitRegex: (args, context) => {
|
|
1449
|
+
if (args.length < 2) throw new Error('splitRegex(text, pattern, flags?) requires at least text and pattern');
|
|
1450
|
+
const text = generateExpr(args[0], context);
|
|
1451
|
+
const pattern = generateExpr(args[1], context);
|
|
1452
|
+
const flags = args.length > 2 ? generateExpr(args[2], context) : 'undefined';
|
|
1453
|
+
return `${text}.split(new RegExp(${pattern}, ${flags}))`;
|
|
1454
|
+
},
|
|
1455
|
+
|
|
1281
1456
|
// ── v2.1.0 — cache (Redis) accessors. All async.
|
|
1282
1457
|
|
|
1283
1458
|
cacheGet: (args, context) => {
|
|
@@ -1412,7 +1587,7 @@ const STDLIB = {
|
|
|
1412
1587
|
// Generators / iterables
|
|
1413
1588
|
spread: (args, context) => `[...${generateExpr(args[0], context)}]`,
|
|
1414
1589
|
|
|
1415
|
-
// v1.0.1 — dynamic module loading (the runtime companion to `import "./x.
|
|
1590
|
+
// v1.0.1 — dynamic module loading (the runtime companion to `import "./x.pt"`).
|
|
1416
1591
|
// Resolves relative to the bundler's CWD so `loadModule("./m")` behaves like
|
|
1417
1592
|
// `require.resolve` from the program root.
|
|
1418
1593
|
loadModule: (args, context) => {
|
|
@@ -1498,6 +1673,385 @@ const STDLIB = {
|
|
|
1498
1673
|
ensureBuiltin(context, 'fs');
|
|
1499
1674
|
return `fs.appendFileSync(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)} + '\\n', 'utf8')`;
|
|
1500
1675
|
},
|
|
1676
|
+
|
|
1677
|
+
// ── v1.0.2 — Extended Math functions (IOPL-native).
|
|
1678
|
+
abs: (args, context) => `Math.abs(${generateExpr(args[0], context)})`,
|
|
1679
|
+
min: (args, context) => `Math.min(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1680
|
+
max: (args, context) => `Math.max(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1681
|
+
sqrt: (args, context) => `Math.sqrt(${generateExpr(args[0], context)})`,
|
|
1682
|
+
pow: (args, context) => `Math.pow(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1683
|
+
floor: (args, context) => `Math.floor(${generateExpr(args[0], context)})`,
|
|
1684
|
+
ceil: (args, context) => `Math.ceil(${generateExpr(args[0], context)})`,
|
|
1685
|
+
round: (args, context) => `Math.round(${generateExpr(args[0], context)})`,
|
|
1686
|
+
trunc: (args, context) => `Math.trunc(${generateExpr(args[0], context)})`,
|
|
1687
|
+
sign: (args, context) => `Math.sign(${generateExpr(args[0], context)})`,
|
|
1688
|
+
random: (_args) => `Math.random()`,
|
|
1689
|
+
randomInt: (args, context) => `Math.floor(Math.random() * (${generateExpr(args[1], context)} - ${generateExpr(args[0], context)} + 1)) + ${generateExpr(args[0], context)}`,
|
|
1690
|
+
clamp: (args, context) => `Math.min(Math.max(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)}), ${generateExpr(args[2], context)})`,
|
|
1691
|
+
lerp: (args, context) => `${generateExpr(args[0], context)} + (${generateExpr(args[1], context)} - ${generateExpr(args[0], context)}) * ${generateExpr(args[2], context)}`,
|
|
1692
|
+
PI: (_args) => `Math.PI`,
|
|
1693
|
+
E: (_args) => `Math.E`,
|
|
1694
|
+
ln2: (_args) => `Math.LN2`,
|
|
1695
|
+
ln10: (_args) => `Math.LN10`,
|
|
1696
|
+
log2e: (_args) => `Math.LOG2E`,
|
|
1697
|
+
log10e: (_args) => `Math.LOG10E`,
|
|
1698
|
+
sin: (args, context) => `Math.sin(${generateExpr(args[0], context)})`,
|
|
1699
|
+
cos: (args, context) => `Math.cos(${generateExpr(args[0], context)})`,
|
|
1700
|
+
tan: (args, context) => `Math.tan(${generateExpr(args[0], context)})`,
|
|
1701
|
+
asin: (args, context) => `Math.asin(${generateExpr(args[0], context)})`,
|
|
1702
|
+
acos: (args, context) => `Math.acos(${generateExpr(args[0], context)})`,
|
|
1703
|
+
atan: (args, context) => `Math.atan(${generateExpr(args[0], context)})`,
|
|
1704
|
+
atan2: (args, context) => `Math.atan2(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1705
|
+
sinh: (args, context) => `Math.sinh(${generateExpr(args[0], context)})`,
|
|
1706
|
+
cosh: (args, context) => `Math.cosh(${generateExpr(args[0], context)})`,
|
|
1707
|
+
tanh: (args, context) => `Math.tanh(${generateExpr(args[0], context)})`,
|
|
1708
|
+
exp: (args, context) => `Math.exp(${generateExpr(args[0], context)})`,
|
|
1709
|
+
expm1: (args, context) => `Math.expm1(${generateExpr(args[0], context)})`,
|
|
1710
|
+
log: (args, context) => `Math.log(${generateExpr(args[0], context)})`,
|
|
1711
|
+
log1p: (args, context) => `Math.log1p(${generateExpr(args[0], context)})`,
|
|
1712
|
+
log2: (args, context) => `Math.log2(${generateExpr(args[0], context)})`,
|
|
1713
|
+
log10: (args, context) => `Math.log10(${generateExpr(args[0], context)})`,
|
|
1714
|
+
hypot: (args, context) => `Math.hypot(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1715
|
+
cbrt: (args, context) => `Math.cbrt(${generateExpr(args[0], context)})`,
|
|
1716
|
+
|
|
1717
|
+
// ── v1.0.2 — Extended String functions (IOPL-native).
|
|
1718
|
+
trim: (args, context) => `String(${generateExpr(args[0], context)}).trim()`,
|
|
1719
|
+
trimStart: (args, context) => `String(${generateExpr(args[0], context)}).trimStart()`,
|
|
1720
|
+
trimEnd: (args, context) => `String(${generateExpr(args[0], context)}).trimEnd()`,
|
|
1721
|
+
toUpperCase: (args, context) => `String(${generateExpr(args[0], context)}).toUpperCase()`,
|
|
1722
|
+
toLowerCase: (args, context) => `String(${generateExpr(args[0], context)}).toLowerCase()`,
|
|
1723
|
+
charAt: (args, context) => `String(${generateExpr(args[0], context)}).charAt(${generateExpr(args[1], context)})`,
|
|
1724
|
+
charCodeAt: (args, context) => `String(${generateExpr(args[0], context)}).charCodeAt(${generateExpr(args[1], context)})`,
|
|
1725
|
+
codePointAt: (args, context) => `String(${generateExpr(args[0], context)}).codePointAt(${generateExpr(args[1], context)})`,
|
|
1726
|
+
slice: (args, context) => `String(${generateExpr(args[0], context)}).slice(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1727
|
+
substring: (args, context) => `String(${generateExpr(args[0], context)}).substring(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1728
|
+
substr: (args, context) => `String(${generateExpr(args[0], context)}).substr(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1729
|
+
indexOf: (args, context) => `String(${generateExpr(args[0], context)}).indexOf(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1730
|
+
lastIndexOf: (args, context) => `String(${generateExpr(args[0], context)}).lastIndexOf(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1731
|
+
includes: (args, context) => `String(${generateExpr(args[0], context)}).includes(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1732
|
+
startsWith: (args, context) => `String(${generateExpr(args[0], context)}).startsWith(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1733
|
+
endsWith: (args, context) => `String(${generateExpr(args[0], context)}).endsWith(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1734
|
+
repeat: (args, context) => `String(${generateExpr(args[0], context)}).repeat(${generateExpr(args[1], context)})`,
|
|
1735
|
+
padStart: (args, context) => `String(${generateExpr(args[0], context)}).padStart(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1736
|
+
padEnd: (args, context) => `String(${generateExpr(args[0], context)}).padEnd(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1737
|
+
split: (args, context) => {
|
|
1738
|
+
const sep = args.length > 1 ? generateExpr(args[1], context) : '","';
|
|
1739
|
+
return `String(${generateExpr(args[0], context)}).split(${sep})`;
|
|
1740
|
+
},
|
|
1741
|
+
join: (args, context) => `(${generateExpr(args[0], context)}).join(${args.length > 1 ? generateExpr(args[1], context) : '","'})`,
|
|
1742
|
+
replaceAll: (args, context) => `String(${generateExpr(args[0], context)}).replaceAll(${generateExpr(args[1], context)}, ${generateExpr(args[2], context)})`,
|
|
1743
|
+
match: (args, context) => `String(${generateExpr(args[0], context)}).match(${generateExpr(args[1], context)})`,
|
|
1744
|
+
matchAll: (args, context) => `Array.from(String(${generateExpr(args[0], context)}).matchAll(${generateExpr(args[1], context)}))`,
|
|
1745
|
+
search: (args, context) => `String(${generateExpr(args[0], context)}).search(${generateExpr(args[1], context)})`,
|
|
1746
|
+
toString: (args, context) => `String(${generateExpr(args[0], context)}).toString()`,
|
|
1747
|
+
stringValueOf: (args, context) => `String(${generateExpr(args[0], context)}).valueOf()`,
|
|
1748
|
+
|
|
1749
|
+
// ── v1.0.2 — Extended Array/Collection functions (IOPL-native).
|
|
1750
|
+
push: (args, context) => `(${generateExpr(args[0], context)}).push(${args.slice(1).map(a => generateExpr(a, context)).join(', ')})`,
|
|
1751
|
+
pop: (args, context) => `(${generateExpr(args[0], context)}).pop()`,
|
|
1752
|
+
shift: (args, context) => `(${generateExpr(args[0], context)}).shift()`,
|
|
1753
|
+
unshift: (args, context) => `(${generateExpr(args[0], context)}).unshift(${args.slice(1).map(a => generateExpr(a, context)).join(', ')})`,
|
|
1754
|
+
splice: (args, context) => `(${generateExpr(args[0], context)}).splice(${args.slice(1).map(a => generateExpr(a, context)).join(', ')})`,
|
|
1755
|
+
concat: (args, context) => `(${generateExpr(args[0], context)}).concat(${args.slice(1).map(a => generateExpr(a, context)).join(', ')})`,
|
|
1756
|
+
slice: (args, context) => `(${generateExpr(args[0], context)}).slice(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1757
|
+
copyWithin: (args, context) => `(${generateExpr(args[0], context)}).copyWithin(${args.slice(1).map(a => generateExpr(a, context)).join(', ')})`,
|
|
1758
|
+
fill: (args, context) => `(${generateExpr(args[0], context)}).fill(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''}${args.length > 3 ? ', ' + generateExpr(args[3], context) : ''})`,
|
|
1759
|
+
find: (args, context) => `(${generateExpr(args[0], context)}).find(${generateExpr(args[1], context)})`,
|
|
1760
|
+
findIndex: (args, context) => `(${generateExpr(args[0], context)}).findIndex(${generateExpr(args[1], context)})`,
|
|
1761
|
+
findLast: (args, context) => `(${generateExpr(args[0], context)}).findLast(${generateExpr(args[1], context)})`,
|
|
1762
|
+
findLastIndex: (args, context) => `(${generateExpr(args[0], context)}).findLastIndex(${generateExpr(args[1], context)})`,
|
|
1763
|
+
indexOf: (args, context) => `(${generateExpr(args[0], context)}).indexOf(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1764
|
+
lastIndexOf: (args, context) => `(${generateExpr(args[0], context)}).lastIndexOf(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1765
|
+
includes: (args, context) => `(${generateExpr(args[0], context)}).includes(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1766
|
+
join: (args, context) => `(${generateExpr(args[0], context)}).join(${args.length > 1 ? generateExpr(args[1], context) : '","'})`,
|
|
1767
|
+
reverse: (args, context) => `[...${generateExpr(args[0], context)}].reverse()`,
|
|
1768
|
+
sort: (args, context) => `[...${generateExpr(args[0], context)}].sort(${args.length > 1 ? generateExpr(args[1], context) : '(a, b) => (a < b ? -1 : a > b ? 1 : 0)'})`,
|
|
1769
|
+
flat: (args, context) => `(${generateExpr(args[0], context)}).flat(${args.length > 1 ? generateExpr(args[1], context) : ''})`,
|
|
1770
|
+
flatMap: (args, context) => `(${generateExpr(args[0], context)}).flatMap(${generateExpr(args[1], context)})`,
|
|
1771
|
+
map: (args, context) => `(${generateExpr(args[0], context)}).map(${generateExpr(args[1], context)})`,
|
|
1772
|
+
filter: (args, context) => `(${generateExpr(args[0], context)}).filter(${generateExpr(args[1], context)})`,
|
|
1773
|
+
reduce: (args, context) => `(${generateExpr(args[0], context)}).reduce(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1774
|
+
reduceRight: (args, context) => `(${generateExpr(args[0], context)}).reduceRight(${generateExpr(args[1], context)}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1775
|
+
forEach: (args, context) => `(${generateExpr(args[0], context)}).forEach(${generateExpr(args[1], context)})`,
|
|
1776
|
+
some: (args, context) => `(${generateExpr(args[0], context)}).some(${generateExpr(args[1], context)})`,
|
|
1777
|
+
every: (args, context) => `(${generateExpr(args[0], context)}).every(${generateExpr(args[1], context)})`,
|
|
1778
|
+
at: (args, context) => `(${generateExpr(args[0], context)}).at(${generateExpr(args[1], context)})`,
|
|
1779
|
+
with: (args, context) => `(${generateExpr(args[0], context)}).with(${generateExpr(args[1], context)}, ${generateExpr(args[2], context)})`,
|
|
1780
|
+
toSorted: (args, context) => `(${generateExpr(args[0], context)}).toSorted(${args.length > 1 ? generateExpr(args[1], context) : '(a, b) => (a < b ? -1 : a > b ? 1 : 0)'})`,
|
|
1781
|
+
toReversed: (args, context) => `(${generateExpr(args[0], context)}).toReversed()`,
|
|
1782
|
+
toSpliced: (args, context) => `(${generateExpr(args[0], context)}).toSpliced(${args.slice(1).map(a => generateExpr(a, context)).join(', ')})`,
|
|
1783
|
+
keys: (args, context) => `Object.keys(${generateExpr(args[0], context)})`,
|
|
1784
|
+
values: (args, context) => `Object.values(${generateExpr(args[0], context)})`,
|
|
1785
|
+
entries: (args, context) => `Object.entries(${generateExpr(args[0], context)})`,
|
|
1786
|
+
|
|
1787
|
+
// ── v1.0.2 — Extended Object functions (IOPL-native).
|
|
1788
|
+
assign: (args, context) => `Object.assign(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1789
|
+
create: (args, context) => `Object.create(${generateExpr(args[0], context)}${args.length > 1 ? ', ' + generateExpr(args[1], context) : ''})`,
|
|
1790
|
+
defineProperty: (args, context) => `Object.defineProperty(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1791
|
+
defineProperties: (args, context) => `Object.defineProperties(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1792
|
+
getOwnPropertyDescriptor: (args, context) => `Object.getOwnPropertyDescriptor(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1793
|
+
getOwnPropertyDescriptors: (args, context) => `Object.getOwnPropertyDescriptors(${generateExpr(args[0], context)})`,
|
|
1794
|
+
getOwnPropertyNames: (args, context) => `Object.getOwnPropertyNames(${generateExpr(args[0], context)})`,
|
|
1795
|
+
getOwnPropertySymbols: (args, context) => `Object.getOwnPropertySymbols(${generateExpr(args[0], context)})`,
|
|
1796
|
+
getPrototypeOf: (args, context) => `Object.getPrototypeOf(${generateExpr(args[0], context)})`,
|
|
1797
|
+
setPrototypeOf: (args, context) => `Object.setPrototypeOf(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1798
|
+
objectIs: (args, context) => `Object.is(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1799
|
+
isExtensible: (args, context) => `Object.isExtensible(${generateExpr(args[0], context)})`,
|
|
1800
|
+
isFrozen: (args, context) => `Object.isFrozen(${generateExpr(args[0], context)})`,
|
|
1801
|
+
isSealed: (args, context) => `Object.isSealed(${generateExpr(args[0], context)})`,
|
|
1802
|
+
preventExtensions: (args, context) => `Object.preventExtensions(${generateExpr(args[0], context)})`,
|
|
1803
|
+
freeze: (args, context) => `Object.freeze(${generateExpr(args[0], context)})`,
|
|
1804
|
+
seal: (args, context) => `Object.seal(${generateExpr(args[0], context)})`,
|
|
1805
|
+
hasOwn: (args, context) => `Object.hasOwn(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1806
|
+
|
|
1807
|
+
// ── v1.0.2 — Promise/Async utilities (IOPL-native).
|
|
1808
|
+
Promise: {
|
|
1809
|
+
resolve: (args, context) => `Promise.resolve(${generateExpr(args[0], context)})`,
|
|
1810
|
+
reject: (args, context) => `Promise.reject(${generateExpr(args[0], context)})`,
|
|
1811
|
+
all: (args, context) => `Promise.all(${generateExpr(args[0], context)})`,
|
|
1812
|
+
allSettled: (args, context) => `Promise.allSettled(${generateExpr(args[0], context)})`,
|
|
1813
|
+
race: (args, context) => `Promise.race(${generateExpr(args[0], context)})`,
|
|
1814
|
+
any: (args, context) => `Promise.any(${generateExpr(args[0], context)})`,
|
|
1815
|
+
},
|
|
1816
|
+
// Note: These are accessed as Promise.resolve, Promise.all, etc.
|
|
1817
|
+
// We'll add helper functions for them.
|
|
1818
|
+
promiseResolve: (args, context) => `Promise.resolve(${generateExpr(args[0], context)})`,
|
|
1819
|
+
promiseReject: (args, context) => `Promise.reject(${generateExpr(args[0], context)})`,
|
|
1820
|
+
promiseAll: (args, context) => `Promise.all(${generateExpr(args[0], context)})`,
|
|
1821
|
+
promiseAllSettled: (args, context) => `Promise.allSettled(${generateExpr(args[0], context)})`,
|
|
1822
|
+
promiseRace: (args, context) => `Promise.race(${generateExpr(args[0], context)})`,
|
|
1823
|
+
promiseAny: (args, context) => `Promise.any(${generateExpr(args[0], context)})`,
|
|
1824
|
+
promiseWithResolvers: (args, context) => `Promise.withResolvers()`,
|
|
1825
|
+
await: (args, context) => {
|
|
1826
|
+
markAsync(context);
|
|
1827
|
+
return `await ${generateExpr(args[0], context)}`;
|
|
1828
|
+
},
|
|
1829
|
+
// Async sleep (returns a promise that resolves after ms).
|
|
1830
|
+
sleepAsync: (args, context) => {
|
|
1831
|
+
markAsync(context);
|
|
1832
|
+
return `new Promise(r => setTimeout(r, ${generateExpr(args[0], context)}))`;
|
|
1833
|
+
},
|
|
1834
|
+
// Timeout wrapper for promises.
|
|
1835
|
+
timeout: (args, context) => {
|
|
1836
|
+
markAsync(context);
|
|
1837
|
+
const ms = args.length > 1 ? generateExpr(args[1], context) : '30000';
|
|
1838
|
+
return `Promise.race([${generateExpr(args[0], context)}, new Promise((_, r) => setTimeout(() => r(new Error('Timed out')), ${ms}))])`;
|
|
1839
|
+
},
|
|
1840
|
+
|
|
1841
|
+
// ── v1.0.2 — JSON utilities (IOPL-native).
|
|
1842
|
+
jsonParse: (args, context) => `JSON.parse(${generateExpr(args[0], context)})`,
|
|
1843
|
+
stringify: (args, context) => `JSON.stringify(${generateExpr(args[0], context)}${args.length > 1 ? ', ' + generateExpr(args[1], context) : ''}${args.length > 2 ? ', ' + generateExpr(args[2], context) : ''})`,
|
|
1844
|
+
|
|
1845
|
+
// ── v1.0.2 — Type checking utilities (IOPL-native).
|
|
1846
|
+
isArray: (args, context) => `Array.isArray(${generateExpr(args[0], context)})`,
|
|
1847
|
+
isInteger: (args, context) => `Number.isInteger(${generateExpr(args[0], context)})`,
|
|
1848
|
+
isNaN: (args, context) => `Number.isNaN(${generateExpr(args[0], context)})`,
|
|
1849
|
+
isFinite: (args, context) => `Number.isFinite(${generateExpr(args[0], context)})`,
|
|
1850
|
+
isSafeInteger: (args, context) => `Number.isSafeInteger(${generateExpr(args[0], context)})`,
|
|
1851
|
+
parseInt: (args, context) => `parseInt(${generateExpr(args[0], context)}${args.length > 1 ? ', ' + generateExpr(args[1], context) : ''})`,
|
|
1852
|
+
parseFloat: (args, context) => `parseFloat(${generateExpr(args[0], context)})`,
|
|
1853
|
+
|
|
1854
|
+
// ── v1.0.2 — Array static methods (IOPL-native).
|
|
1855
|
+
arrayFrom: (args, context) => `Array.from(${generateExpr(args[0], context)}${args.length > 1 ? ', ' + generateExpr(args[1], context) : ''})`,
|
|
1856
|
+
arrayOf: (args, context) => `Array.of(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1857
|
+
arrayIsArray: (args, context) => `Array.isArray(${generateExpr(args[0], context)})`,
|
|
1858
|
+
|
|
1859
|
+
// ── v1.0.2 — Object static methods (IOPL-native).
|
|
1860
|
+
objectFromEntries: (args, context) => `Object.fromEntries(${generateExpr(args[0], context)})`,
|
|
1861
|
+
objectGetOwnPropertySymbols: (args, context) => `Object.getOwnPropertySymbols(${generateExpr(args[0], context)})`,
|
|
1862
|
+
objectGetOwnPropertyDescriptors: (args, context) => `Object.getOwnPropertyDescriptors(${generateExpr(args[0], context)})`,
|
|
1863
|
+
objectGetPrototypeOf: (args, context) => `Object.getPrototypeOf(${generateExpr(args[0], context)})`,
|
|
1864
|
+
objectSetPrototypeOf: (args, context) => `Object.setPrototypeOf(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1865
|
+
objectIs: (args, context) => `Object.is(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1866
|
+
objectCreate: (args, context) => `Object.create(${generateExpr(args[0], context)}${args.length > 1 ? ', ' + generateExpr(args[1], context) : ''})`,
|
|
1867
|
+
objectAssign: (args, context) => `Object.assign(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1868
|
+
objectKeys: (args, context) => `Object.keys(${generateExpr(args[0], context)})`,
|
|
1869
|
+
objectValues: (args, context) => `Object.values(${generateExpr(args[0], context)})`,
|
|
1870
|
+
objectEntries: (args, context) => `Object.entries(${generateExpr(args[0], context)})`,
|
|
1871
|
+
objectHasOwn: (args, context) => `Object.hasOwn(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1872
|
+
objectDefineProperty: (args, context) => `Object.defineProperty(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1873
|
+
objectDefineProperties: (args, context) => `Object.defineProperties(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1874
|
+
objectGetOwnPropertyDescriptor: (args, context) => `Object.getOwnPropertyDescriptor(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1875
|
+
objectGetOwnPropertyNames: (args, context) => `Object.getOwnPropertyNames(${generateExpr(args[0], context)})`,
|
|
1876
|
+
objectIsExtensible: (args, context) => `Object.isExtensible(${generateExpr(args[0], context)})`,
|
|
1877
|
+
objectIsFrozen: (args, context) => `Object.isFrozen(${generateExpr(args[0], context)})`,
|
|
1878
|
+
objectIsSealed: (args, context) => `Object.isSealed(${generateExpr(args[0], context)})`,
|
|
1879
|
+
objectPreventExtensions: (args, context) => `Object.preventExtensions(${generateExpr(args[0], context)})`,
|
|
1880
|
+
objectFreeze: (args, context) => `Object.freeze(${generateExpr(args[0], context)})`,
|
|
1881
|
+
objectSeal: (args, context) => `Object.seal(${generateExpr(args[0], context)})`,
|
|
1882
|
+
|
|
1883
|
+
// ── v1.0.2 — Symbol support (IOPL-native).
|
|
1884
|
+
symbol: (args, context) => `Symbol(${args.length > 0 ? generateExpr(args[0], context) : 'undefined'})`,
|
|
1885
|
+
symbolFor: (args, context) => `Symbol.for(${generateExpr(args[0], context)})`,
|
|
1886
|
+
symbolKeyFor: (args, context) => `Symbol.keyFor(${generateExpr(args[0], context)})`,
|
|
1887
|
+
symbolIterator: (_args) => `Symbol.iterator`,
|
|
1888
|
+
symbolAsyncIterator: (_args) => `Symbol.asyncIterator`,
|
|
1889
|
+
symbolToStringTag: (_args) => `Symbol.toStringTag`,
|
|
1890
|
+
symbolHasInstance: (_args) => `Symbol.hasInstance`,
|
|
1891
|
+
symbolIsConcatSpreadable: (_args) => `Symbol.isConcatSpreadable`,
|
|
1892
|
+
symbolMatch: (_args) => `Symbol.match`,
|
|
1893
|
+
symbolMatchAll: (_args) => `Symbol.matchAll`,
|
|
1894
|
+
symbolReplace: (_args) => `Symbol.replace`,
|
|
1895
|
+
symbolSearch: (_args) => `Symbol.search`,
|
|
1896
|
+
symbolSplit: (_args) => `Symbol.split`,
|
|
1897
|
+
symbolToPrimitive: (_args) => `Symbol.toPrimitive`,
|
|
1898
|
+
symbolUnscopables: (_args) => `Symbol.unscopables`,
|
|
1899
|
+
|
|
1900
|
+
// ── v1.0.2 — Math static methods (IOPL-native).
|
|
1901
|
+
mathAbs: (args, context) => `Math.abs(${generateExpr(args[0], context)})`,
|
|
1902
|
+
mathMin: (args, context) => `Math.min(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1903
|
+
mathMax: (args, context) => `Math.max(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1904
|
+
mathSqrt: (args, context) => `Math.sqrt(${generateExpr(args[0], context)})`,
|
|
1905
|
+
mathPow: (args, context) => `Math.pow(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1906
|
+
mathFloor: (args, context) => `Math.floor(${generateExpr(args[0], context)})`,
|
|
1907
|
+
mathCeil: (args, context) => `Math.ceil(${generateExpr(args[0], context)})`,
|
|
1908
|
+
mathRound: (args, context) => `Math.round(${generateExpr(args[0], context)})`,
|
|
1909
|
+
mathTrunc: (args, context) => `Math.trunc(${generateExpr(args[0], context)})`,
|
|
1910
|
+
mathSign: (args, context) => `Math.sign(${generateExpr(args[0], context)})`,
|
|
1911
|
+
mathRandom: (_args) => `Math.random()`,
|
|
1912
|
+
mathClamp: (args, context) => `Math.min(Math.max(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)}), ${generateExpr(args[2], context)})`,
|
|
1913
|
+
mathPI: (_args) => `Math.PI`,
|
|
1914
|
+
mathE: (_args) => `Math.E`,
|
|
1915
|
+
mathLN2: (_args) => `Math.LN2`,
|
|
1916
|
+
mathLN10: (_args) => `Math.LN10`,
|
|
1917
|
+
mathLOG2E: (_args) => `Math.LOG2E`,
|
|
1918
|
+
mathLOG10E: (_args) => `Math.LOG10E`,
|
|
1919
|
+
mathSin: (args, context) => `Math.sin(${generateExpr(args[0], context)})`,
|
|
1920
|
+
mathCos: (args, context) => `Math.cos(${generateExpr(args[0], context)})`,
|
|
1921
|
+
mathTan: (args, context) => `Math.tan(${generateExpr(args[0], context)})`,
|
|
1922
|
+
mathAsin: (args, context) => `Math.asin(${generateExpr(args[0], context)})`,
|
|
1923
|
+
mathAcos: (args, context) => `Math.acos(${generateExpr(args[0], context)})`,
|
|
1924
|
+
mathAtan: (args, context) => `Math.atan(${generateExpr(args[0], context)})`,
|
|
1925
|
+
mathAtan2: (args, context) => `Math.atan2(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1926
|
+
mathSinh: (args, context) => `Math.sinh(${generateExpr(args[0], context)})`,
|
|
1927
|
+
mathCosh: (args, context) => `Math.cosh(${generateExpr(args[0], context)})`,
|
|
1928
|
+
mathTanh: (args, context) => `Math.tanh(${generateExpr(args[0], context)})`,
|
|
1929
|
+
mathExp: (args, context) => `Math.exp(${generateExpr(args[0], context)})`,
|
|
1930
|
+
mathExpm1: (args, context) => `Math.expm1(${generateExpr(args[0], context)})`,
|
|
1931
|
+
mathLog: (args, context) => `Math.log(${generateExpr(args[0], context)})`,
|
|
1932
|
+
mathLog1p: (args, context) => `Math.log1p(${generateExpr(args[0], context)})`,
|
|
1933
|
+
mathLog2: (args, context) => `Math.log2(${generateExpr(args[0], context)})`,
|
|
1934
|
+
mathLog10: (args, context) => `Math.log10(${generateExpr(args[0], context)})`,
|
|
1935
|
+
mathHypot: (args, context) => `Math.hypot(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1936
|
+
mathCbrt: (args, context) => `Math.cbrt(${generateExpr(args[0], context)})`,
|
|
1937
|
+
|
|
1938
|
+
// ── v1.0.2 — TypedArray support (IOPL-native).
|
|
1939
|
+
int8Array: (args, context) => `new Int8Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1940
|
+
uint8Array: (args, context) => `new Uint8Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1941
|
+
uint8ClampedArray: (args, context) => `new Uint8ClampedArray(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1942
|
+
int16Array: (args, context) => `new Int16Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1943
|
+
uint16Array: (args, context) => `new Uint16Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1944
|
+
int32Array: (args, context) => `new Int32Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1945
|
+
uint32Array: (args, context) => `new Uint32Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1946
|
+
float32Array: (args, context) => `new Float32Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1947
|
+
float64Array: (args, context) => `new Float64Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1948
|
+
bigInt64Array: (args, context) => `new BigInt64Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1949
|
+
bigUint64Array: (args, context) => `new BigUint64Array(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1950
|
+
dataView: (args, context) => `new DataView(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1951
|
+
arrayBuffer: (args, context) => `new ArrayBuffer(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1952
|
+
|
|
1953
|
+
// ── v1.0.2 — Global objects access (IOPL-native).
|
|
1954
|
+
globalThis: (_args) => `globalThis`,
|
|
1955
|
+
console: (_args) => `console`,
|
|
1956
|
+
process: (_args) => `process`,
|
|
1957
|
+
Buffer: (_args) => `Buffer`,
|
|
1958
|
+
|
|
1959
|
+
// ── v2.3.0 — EventSource / Server-Sent Events (IOPL-native).
|
|
1960
|
+
eventSource: (args, context) => `new EventSource(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1961
|
+
|
|
1962
|
+
// ── v2.2.0 — URL and URLSearchParams (IOPL-native).
|
|
1963
|
+
url: (args, context) => `new URL(${generateExpr(args[0], context)}${args[1] ? `, ${generateExpr(args[1], context)}` : ''})`,
|
|
1964
|
+
urlSearchParams: (args, context) => `new URLSearchParams(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
1965
|
+
|
|
1966
|
+
// ── v2.2.0 — Intl / Internationalization (IOPL-native).
|
|
1967
|
+
dateTimeFormat: (args, context) => `new Intl.DateTimeFormat(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1968
|
+
numberFormat: (args, context) => `new Intl.NumberFormat(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1969
|
+
collator: (args, context) => `new Intl.Collator(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1970
|
+
|
|
1971
|
+
// ── v2.2.0 — Crypto / Web Crypto API (IOPL-native).
|
|
1972
|
+
cryptoRandomUUID: (_args) => `crypto.randomUUID()`,
|
|
1973
|
+
cryptoGetRandomValues: (args, context) => `crypto.getRandomValues(${generateExpr(args[0], context)})`,
|
|
1974
|
+
cryptoSubtleDigest: (args, context) => `crypto.subtle.digest(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1975
|
+
cryptoSubtleEncrypt: (args, context) => `crypto.subtle.encrypt(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1976
|
+
cryptoSubtleDecrypt: (args, context) => `crypto.subtle.decrypt(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1977
|
+
cryptoSubtleSign: (args, context) => `crypto.subtle.sign(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1978
|
+
cryptoSubtleVerify: (args, context) => `crypto.subtle.verify(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1979
|
+
cryptoSubtleGenerateKey: (args, context) => `crypto.subtle.generateKey(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1980
|
+
cryptoSubtleDeriveKey: (args, context) => `crypto.subtle.deriveKey(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1981
|
+
cryptoSubtleImportKey: (args, context) => `crypto.subtle.importKey(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1982
|
+
cryptoSubtleExportKey: (args, context) => `crypto.subtle.exportKey(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1983
|
+
cryptoSubtleWrapKey: (args, context) => `crypto.subtle.wrapKey(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1984
|
+
cryptoSubtleUnwrapKey: (args, context) => `crypto.subtle.unwrapKey(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1985
|
+
|
|
1986
|
+
// ── v2.2.0 — WeakMap / WeakSet / WeakRef / FinalizationRegistry (IOPL-native).
|
|
1987
|
+
weakMap: (_args) => `new WeakMap()`,
|
|
1988
|
+
weakSet: (_args) => `new WeakSet()`,
|
|
1989
|
+
weakRef: (args, context) => `new WeakRef(${generateExpr(args[0], context)})`,
|
|
1990
|
+
finalizationRegistry: (args, context) => `new FinalizationRegistry(${generateExpr(args[0], context)})`,
|
|
1991
|
+
|
|
1992
|
+
// ── v2.2.0 — Proxy / Reflect (IOPL-native).
|
|
1993
|
+
proxy: (args, context) => `new Proxy(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1994
|
+
reflectGet: (args, context) => `Reflect.get(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1995
|
+
reflectSet: (args, context) => `Reflect.set(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)}, ${generateExpr(args[2], context)})`,
|
|
1996
|
+
reflectHas: (args, context) => `Reflect.has(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1997
|
+
reflectDeleteProperty: (args, context) => `Reflect.deleteProperty(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
1998
|
+
reflectConstruct: (args, context) => `Reflect.construct(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
1999
|
+
reflectApply: (args, context) => `Reflect.apply(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2000
|
+
reflectOwnKeys: (args, context) => `Reflect.ownKeys(${generateExpr(args[0], context)})`,
|
|
2001
|
+
reflectDefineProperty: (args, context) => `Reflect.defineProperty(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2002
|
+
reflectGetOwnPropertyDescriptor: (args, context) => `Reflect.getOwnPropertyDescriptor(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
2003
|
+
|
|
2004
|
+
// ── v2.2.0 — Symbol (IOPL-native).
|
|
2005
|
+
symbol: (args, context) => `Symbol(${args.length ? generateExpr(args[0], context) : 'undefined'})`,
|
|
2006
|
+
symbolFor: (args, context) => `Symbol.for(${generateExpr(args[0], context)})`,
|
|
2007
|
+
symbolKeyFor: (args, context) => `Symbol.keyFor(${generateExpr(args[0], context)})`,
|
|
2008
|
+
symbolIterator: (_args) => `Symbol.iterator`,
|
|
2009
|
+
symbolAsyncIterator: (_args) => `Symbol.asyncIterator`,
|
|
2010
|
+
symbolToStringTag: (_args) => `Symbol.toStringTag`,
|
|
2011
|
+
symbolHasInstance: (_args) => `Symbol.hasInstance`,
|
|
2012
|
+
symbolIsConcatSpreadable: (_args) => `Symbol.isConcatSpreadable`,
|
|
2013
|
+
symbolMatch: (_args) => `Symbol.match`,
|
|
2014
|
+
symbolMatchAll: (_args) => `Symbol.matchAll`,
|
|
2015
|
+
symbolReplace: (_args) => `Symbol.replace`,
|
|
2016
|
+
symbolSearch: (_args) => `Symbol.search`,
|
|
2017
|
+
symbolSplit: (_args) => `Symbol.split`,
|
|
2018
|
+
symbolToPrimitive: (_args) => `Symbol.toPrimitive`,
|
|
2019
|
+
symbolUnscopables: (_args) => `Symbol.unscopables`,
|
|
2020
|
+
|
|
2021
|
+
// ── v2.2.0 — BigInt (IOPL-native).
|
|
2022
|
+
bigInt: (args, context) => `BigInt(${generateExpr(args[0], context)})`,
|
|
2023
|
+
bigIntAsIntN: (args, context) => `BigInt.asIntN(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
2024
|
+
bigIntAsUintN: (args, context) => `BigInt.asUintN(${generateExpr(args[0], context)}, ${generateExpr(args[1], context)})`,
|
|
2025
|
+
|
|
2026
|
+
// ── v2.2.0 — Atomics / SharedArrayBuffer (IOPL-native).
|
|
2027
|
+
atomicsAdd: (args, context) => `Atomics.add(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2028
|
+
atomicsSub: (args, context) => `Atomics.sub(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2029
|
+
atomicsAnd: (args, context) => `Atomics.and(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2030
|
+
atomicsOr: (args, context) => `Atomics.or(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2031
|
+
atomicsXor: (args, context) => `Atomics.xor(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2032
|
+
atomicsExchange: (args, context) => `Atomics.exchange(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2033
|
+
atomicsCompareExchange: (args, context) => `Atomics.compareExchange(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2034
|
+
atomicsLoad: (args, context) => `Atomics.load(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2035
|
+
atomicsStore: (args, context) => `Atomics.store(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2036
|
+
atomicsWait: (args, context) => `Atomics.wait(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2037
|
+
atomicsNotify: (args, context) => `Atomics.notify(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2038
|
+
atomicsIsLockFree: (args, context) => `Atomics.isLockFree(${generateExpr(args[0], context)})`,
|
|
2039
|
+
sharedArrayBuffer: (args, context) => `new SharedArrayBuffer(${generateExpr(args[0], context)})`,
|
|
2040
|
+
|
|
2041
|
+
// ── v2.2.0 — Error subclasses (IOPL-native).
|
|
2042
|
+
error: (args, context) => `new Error(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2043
|
+
typeError: (args, context) => `new TypeError(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2044
|
+
rangeError: (args, context) => `new RangeError(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2045
|
+
referenceError: (args, context) => `new ReferenceError(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2046
|
+
syntaxError: (args, context) => `new SyntaxError(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2047
|
+
evalError: (args, context) => `new EvalError(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2048
|
+
uriError: (args, context) => `new URIError(${args.length ? generateExpr(args[0], context) : ''})`,
|
|
2049
|
+
aggregateError: (args, context) => `new AggregateError(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2050
|
+
|
|
2051
|
+
// ── v2.2.0 — Blob / File / FormData (IOPL-native, browser-compatible).
|
|
2052
|
+
blob: (args, context) => `new Blob(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2053
|
+
file: (args, context) => `new File(${args.map(a => generateExpr(a, context)).join(', ')})`,
|
|
2054
|
+
formData: (_args) => `new FormData()`,
|
|
1501
2055
|
};
|
|
1502
2056
|
|
|
1503
2057
|
// Mark the enclosing program async when a call awaits at the top level, and
|
|
@@ -1806,8 +2360,16 @@ function generateCondition(cond, context) {
|
|
|
1806
2360
|
|
|
1807
2361
|
function generateStatement(node, indent = '', context = createGenerationContext()) {
|
|
1808
2362
|
switch (node.type) {
|
|
1809
|
-
case 'RememberStatement':
|
|
2363
|
+
case 'RememberStatement': {
|
|
2364
|
+
// Handle destructuring: remember [a, b] as arr / remember {x, y} as obj
|
|
2365
|
+
if (node.name && typeof node.name === 'object' && (node.name.type === 'ArrayPattern' || node.name.type === 'ObjectPattern')) {
|
|
2366
|
+
const pattern = node.name.type === 'ArrayPattern'
|
|
2367
|
+
? `[${node.name.elements.map(e => generateLValue(e, context)).join(', ')}]`
|
|
2368
|
+
: `{ ${node.name.properties.map(p => p.key).join(', ')} }`;
|
|
2369
|
+
return `${indent}let ${pattern} = ${generateExpr(node.value, context)};`;
|
|
2370
|
+
}
|
|
1810
2371
|
return `${indent}let ${node.name} = ${generateExpr(node.value, context)};`;
|
|
2372
|
+
}
|
|
1811
2373
|
|
|
1812
2374
|
case 'ShowStatement':
|
|
1813
2375
|
return `${indent}console.log(${generateExpr(node.value, context)});`;
|
|
@@ -1815,18 +2377,49 @@ function generateStatement(node, indent = '', context = createGenerationContext(
|
|
|
1815
2377
|
case 'GiveStatement':
|
|
1816
2378
|
return `${indent}return ${generateExpr(node.value, context)};`;
|
|
1817
2379
|
|
|
1818
|
-
case 'BecomeStatement':
|
|
1819
|
-
|
|
2380
|
+
case 'BecomeStatement': {
|
|
2381
|
+
// Handle destructuring: [a, b] = arr / {x, y} = obj
|
|
2382
|
+
if (node.target && typeof node.target === 'object' && (node.target.type === 'ArrayPattern' || node.target.type === 'ObjectPattern')) {
|
|
2383
|
+
const pattern = node.target.type === 'ArrayPattern'
|
|
2384
|
+
? `[${node.target.elements.map(e => generateLValue(e, context)).join(', ')}]`
|
|
2385
|
+
: `{ ${node.target.properties.map(p => generateLValue(p, context)).join(', ')} }`;
|
|
2386
|
+
return `${indent}let ${pattern} = ${generateExpr(node.value, context)};`;
|
|
2387
|
+
}
|
|
2388
|
+
const target = generateLValue(node.target, context);
|
|
2389
|
+
const value = generateExpr(node.value, context);
|
|
2390
|
+
const op = node.op || '=';
|
|
2391
|
+
if (op === '=') {
|
|
2392
|
+
return `${indent}${target} = ${value};`;
|
|
2393
|
+
}
|
|
2394
|
+
// Logical assignment: x ||= y, x &&= y, x ??= y
|
|
2395
|
+
// In JS: x ||= y -> x || (x = y)
|
|
2396
|
+
// We need to handle this carefully for member expressions
|
|
2397
|
+
const opMap = { '||=': '||', '&&=': '&&', '??=': '??' };
|
|
2398
|
+
const jsOp = opMap[op];
|
|
2399
|
+
if (jsOp) {
|
|
2400
|
+
return `${indent}${target} ${jsOp} (${target} = ${value});`;
|
|
2401
|
+
}
|
|
2402
|
+
return `${indent}${target} ${op} ${value};`;
|
|
2403
|
+
}
|
|
1820
2404
|
|
|
1821
2405
|
// v1.0.1 — record kinds: `define a kind called "Person" with ... done`.
|
|
1822
2406
|
// Names are emitted as a JS factory that returns a fresh plain object with
|
|
1823
2407
|
// declared defaults. Constructors prompt for required fields at compile
|
|
1824
2408
|
// time via `create a Person with ...` (see GenerateExpr CreateKind).
|
|
2409
|
+
// v1.0.2 — supports `extends` for inheritance.
|
|
1825
2410
|
case 'DefineKindStatement': {
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
2411
|
+
let defaults = '';
|
|
2412
|
+
if (node.extends) {
|
|
2413
|
+
// Inherit from parent kind by spreading its defaults
|
|
2414
|
+
defaults = `{ ...__makeKind_${node.extends}({}), ${node.fields.map(f =>
|
|
2415
|
+
`${JSON.stringify(f.key)}: ${f.value ? generateExpr(f.value, context) : 'undefined'}`
|
|
2416
|
+
).join(', ')} }`;
|
|
2417
|
+
} else {
|
|
2418
|
+
const members = node.fields.map(f =>
|
|
2419
|
+
`${JSON.stringify(f.key)}: ${f.value ? generateExpr(f.value, context) : 'undefined'}`
|
|
2420
|
+
);
|
|
2421
|
+
defaults = `{ ${members.join(', ')} }`;
|
|
2422
|
+
}
|
|
1830
2423
|
return [
|
|
1831
2424
|
`${indent}function __makeKind_${node.name}(${node.name}Fields) {`,
|
|
1832
2425
|
`${indent} const __rec = ${defaults};`,
|
|
@@ -1901,47 +2494,6 @@ function generateStatement(node, indent = '', context = createGenerationContext(
|
|
|
1901
2494
|
return pkg ? `${indent}${pkg}` : '';
|
|
1902
2495
|
}
|
|
1903
2496
|
|
|
1904
|
-
// v1.1.1 — JavaScript Gateway (RFC-0011)
|
|
1905
|
-
|
|
1906
|
-
// remember <name> as javascript … done (named — always produces a value)
|
|
1907
|
-
// javascript … done (statement-level block)
|
|
1908
|
-
case 'JavaScriptBlock': {
|
|
1909
|
-
// Validate the raw JavaScript at compile time so JS syntax errors are
|
|
1910
|
-
// reported as such, with the PlainScript context that produced them.
|
|
1911
|
-
try {
|
|
1912
|
-
new vm.Script(`(async () => {\n${node.body}\n})`);
|
|
1913
|
-
} catch (e) {
|
|
1914
|
-
const label = node.name ? `assigned to "${node.name}"` : 'block';
|
|
1915
|
-
throw new Error(
|
|
1916
|
-
`JavaScript error inside the "javascript" ${label}: ${e.message}`
|
|
1917
|
-
);
|
|
1918
|
-
}
|
|
1919
|
-
// Does the body use top-level `await`? Top-level await only parses
|
|
1920
|
-
// inside an async function, so a plain Script rejects it. Synchronous
|
|
1921
|
-
// statement-level blocks need no wrapper; async ones do.
|
|
1922
|
-
let hasTopLevelAwait = false;
|
|
1923
|
-
try {
|
|
1924
|
-
new vm.Script(node.body);
|
|
1925
|
-
} catch (_) {
|
|
1926
|
-
hasTopLevelAwait = true;
|
|
1927
|
-
}
|
|
1928
|
-
// Named blocks always bind a value, so they keep the async IIFE. The
|
|
1929
|
-
// body is emitted verbatim: JavaScript indentation, template literals,
|
|
1930
|
-
// and line structure are preserved as written (RFC-0011 §31).
|
|
1931
|
-
if (node.name) {
|
|
1932
|
-
markAsync(context);
|
|
1933
|
-
return `${indent}let ${node.name} = await (async () => {\n${node.body}\n${indent}})();`;
|
|
1934
|
-
}
|
|
1935
|
-
// Statement-level blocks inside functions/routes/loops, or that need
|
|
1936
|
-
// top-level await, retain the async-context wrapper.
|
|
1937
|
-
if (context.inFunction || hasTopLevelAwait) {
|
|
1938
|
-
markAsync(context);
|
|
1939
|
-
return `${indent}await (async () => {\n${node.body}\n${indent}})();`;
|
|
1940
|
-
}
|
|
1941
|
-
// Synchronous statement-level block: emit its body directly/verbatim.
|
|
1942
|
-
return `${indent}${node.body}`;
|
|
1943
|
-
}
|
|
1944
|
-
|
|
1945
2497
|
// ask name / ask "<prompt>" as name
|
|
1946
2498
|
case 'AskStatement': {
|
|
1947
2499
|
ensureBuiltin(context, 'ask');
|
|
@@ -1967,6 +2519,21 @@ function generateStatement(node, indent = '', context = createGenerationContext(
|
|
|
1967
2519
|
const isAsync = block.emitted ? 'async ' : '';
|
|
1968
2520
|
const isGen = containsYield(node.body) ? '*' : '';
|
|
1969
2521
|
const paramStr = node.params.map(p => {
|
|
2522
|
+
// Rest parameter: ...args
|
|
2523
|
+
if (p && p.type === 'RestElement') {
|
|
2524
|
+
return `...${p.name}`;
|
|
2525
|
+
}
|
|
2526
|
+
// Destructuring: [a, b] or {x, y}
|
|
2527
|
+
if (p && (p.type === 'ArrayPattern' || p.type === 'ObjectPattern')) {
|
|
2528
|
+
const pattern = p.type === 'ArrayPattern'
|
|
2529
|
+
? `[${p.elements.map(e => generateLValue(e, context)).join(', ')}]`
|
|
2530
|
+
: `{ ${p.properties.map(prop => generateLValue(prop, context)).join(', ')} }`;
|
|
2531
|
+
if (p.defaultValue) {
|
|
2532
|
+
return `${pattern} = ${generateExpr(p.defaultValue, context)}`;
|
|
2533
|
+
}
|
|
2534
|
+
return pattern;
|
|
2535
|
+
}
|
|
2536
|
+
// Regular parameter
|
|
1970
2537
|
if (typeof p === 'object') {
|
|
1971
2538
|
if (p.defaultValue) return `${p.name} = ${generateExpr(p.defaultValue, context)}`;
|
|
1972
2539
|
return p.name;
|
|
@@ -2037,9 +2604,51 @@ function generateStatement(node, indent = '', context = createGenerationContext(
|
|
|
2037
2604
|
return `${indent}continue;`;
|
|
2038
2605
|
}
|
|
2039
2606
|
|
|
2607
|
+
case 'DebuggerStatement':
|
|
2608
|
+
return `${indent}debugger;`;
|
|
2609
|
+
|
|
2040
2610
|
case 'ThrowStatement':
|
|
2041
2611
|
return `${indent}throw ${generateExpr(node.value, context)};`;
|
|
2042
2612
|
|
|
2613
|
+
case 'ReturnStatement':
|
|
2614
|
+
return `${indent}return${node.value ? ' ' + generateExpr(node.value, context) : ''};`;
|
|
2615
|
+
|
|
2616
|
+
case 'BreakStatement': {
|
|
2617
|
+
if (context.loopDepth <= 0) {
|
|
2618
|
+
throw new Error('"break" can only be used inside a loop (for each, for index, or while).\n\nExample:\n for each item in list\n if done(item)\n break\n done\n done');
|
|
2619
|
+
}
|
|
2620
|
+
return `${indent}break;`;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
case 'ContinueStatement': {
|
|
2624
|
+
if (context.loopDepth <= 0) {
|
|
2625
|
+
throw new Error('"continue" can only be used inside a loop (for each, for index, or while).\n\nExample:\n for each item in list\n if skip(item)\n continue\n done\n done');
|
|
2626
|
+
}
|
|
2627
|
+
return `${indent}continue;`;
|
|
2628
|
+
}
|
|
2629
|
+
|
|
2630
|
+
case 'SwitchStatement': {
|
|
2631
|
+
const discriminant = generateExpr(node.discriminant, context);
|
|
2632
|
+
const cases = node.cases.map(c => {
|
|
2633
|
+
const test = generateExpr(c.test, context);
|
|
2634
|
+
const body = c.body.map(s => generateStatement(s, indent + ' ', context)).join('\n');
|
|
2635
|
+
return `${indent} case ${test}:\n${body}\n${indent} break;`;
|
|
2636
|
+
}).join('\n');
|
|
2637
|
+
let out = `${indent}switch (${discriminant}) {\n${cases}`;
|
|
2638
|
+
if (node.defaultCase) {
|
|
2639
|
+
const defaultBody = node.defaultCase.map(s => generateStatement(s, indent + ' ', context)).join('\n');
|
|
2640
|
+
out += `\n${indent} default:\n${defaultBody}\n${indent} break;`;
|
|
2641
|
+
}
|
|
2642
|
+
out += `\n${indent}}`;
|
|
2643
|
+
return out;
|
|
2644
|
+
}
|
|
2645
|
+
|
|
2646
|
+
case 'ClassDeclaration': {
|
|
2647
|
+
const superClass = node.superClass ? ` extends ${generateExpr(node.superClass, context)}` : '';
|
|
2648
|
+
const body = node.body.map(s => generateStatement(s, indent + ' ', context)).join('\n');
|
|
2649
|
+
return `${indent}class ${node.name}${superClass} {\n${body}\n${indent}}`;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2043
2652
|
// v0.3 — Express runtime
|
|
2044
2653
|
|
|
2045
2654
|
case 'ListenStatement': {
|
|
@@ -2265,8 +2874,8 @@ function generateStatement(node, indent = '', context = createGenerationContext(
|
|
|
2265
2874
|
case 'TryStatement': {
|
|
2266
2875
|
const tryBody = (node.tryBody || node.body || []).map(s => generateStatement(s, indent + ' ', context)).join('\n');
|
|
2267
2876
|
const tryBlock = `${indent}try {\n${tryBody}\n${indent}`;
|
|
2877
|
+
const lines = [tryBlock];
|
|
2268
2878
|
if (node.catches && node.catches.length > 0) {
|
|
2269
|
-
const lines = [tryBlock];
|
|
2270
2879
|
for (let i = 0; i < node.catches.length; i++) {
|
|
2271
2880
|
const c = node.catches[i];
|
|
2272
2881
|
const errorName = c.name || c.param || '__plainError';
|
|
@@ -2294,17 +2903,22 @@ function generateStatement(node, indent = '', context = createGenerationContext(
|
|
|
2294
2903
|
}
|
|
2295
2904
|
}
|
|
2296
2905
|
}
|
|
2297
|
-
|
|
2298
|
-
}
|
|
2299
|
-
let out = tryBlock;
|
|
2300
|
-
if (node.recoverBody) {
|
|
2906
|
+
} else if (node.recoverBody) {
|
|
2301
2907
|
const errorName = node.catchName || '__plainError';
|
|
2302
2908
|
const recoverBody = node.recoverBody.map(s => generateStatement(s, indent + ' ', context)).join('\n');
|
|
2303
|
-
|
|
2909
|
+
lines.push(`${indent}} catch (${errorName}) {`);
|
|
2910
|
+
lines.push(recoverBody);
|
|
2911
|
+
lines.push(`${indent}}`);
|
|
2304
2912
|
} else {
|
|
2305
|
-
|
|
2913
|
+
lines.push(`${indent}} catch (__plainError) {}`);
|
|
2306
2914
|
}
|
|
2307
|
-
|
|
2915
|
+
if (node.finallyBody) {
|
|
2916
|
+
const finallyBody = node.finallyBody.map(s => generateStatement(s, indent + ' ', context)).join('\n');
|
|
2917
|
+
lines.push(`${indent}finally {`);
|
|
2918
|
+
lines.push(finallyBody);
|
|
2919
|
+
lines.push(`${indent}}`);
|
|
2920
|
+
}
|
|
2921
|
+
return lines.join('\n');
|
|
2308
2922
|
}
|
|
2309
2923
|
|
|
2310
2924
|
case 'RetryStatement': {
|
|
@@ -2695,6 +3309,9 @@ function generateLValue(node, context) {
|
|
|
2695
3309
|
if (node.type === 'FirstItem') return `${generateExpr(node.collection, context)}[0]`;
|
|
2696
3310
|
if (node.type === 'NumberedItem') return `${generateExpr(node.collection, context)}[${node.index}]`;
|
|
2697
3311
|
if (node.type === 'LastItem') return `${generateExpr(node.collection, context)}[${generateExpr(node.collection, context)}.length - 1]`;
|
|
3312
|
+
// Destructuring patterns
|
|
3313
|
+
if (node.type === 'ArrayPattern') return `[${node.elements.map(e => generateLValue(e, context)).join(', ')}]`;
|
|
3314
|
+
if (node.type === 'ObjectPattern') return `{ ${node.properties.map(p => p.key).join(', ')} }`;
|
|
2698
3315
|
throw new Error(`Invalid assignment target "${node.type}".`);
|
|
2699
3316
|
}
|
|
2700
3317
|
|
|
@@ -2777,12 +3394,24 @@ function generateExpr(node, context = createGenerationContext()) {
|
|
|
2777
3394
|
return `{ ${props} }`;
|
|
2778
3395
|
}
|
|
2779
3396
|
|
|
3397
|
+
case 'ConditionalExpression':
|
|
3398
|
+
return `(${generateExpr(node.condition, context)} ? ${generateExpr(node.consequent, context)} : ${generateExpr(node.alternate, context)})`;
|
|
3399
|
+
|
|
3400
|
+
case 'NullishCoalesceExpression':
|
|
3401
|
+
return `(${generateExpr(node.left, context)} ?? ${generateExpr(node.right, context)})`;
|
|
3402
|
+
|
|
2780
3403
|
case 'IndexExpression':
|
|
2781
3404
|
return `${generateExpr(node.object, context)}[${generateExpr(node.index, context)}]`;
|
|
2782
3405
|
|
|
2783
3406
|
case 'MemberExpression':
|
|
2784
3407
|
return `${generateExpr(node.object, context)}.${node.property}`;
|
|
2785
3408
|
|
|
3409
|
+
case 'OptionalChainExpression':
|
|
3410
|
+
return `${generateExpr(node.object, context)}?.${node.property}`;
|
|
3411
|
+
|
|
3412
|
+
case 'OptionalCallExpression':
|
|
3413
|
+
return `${generateExpr(node.callee.object, context)}?.${node.callee.property}(${node.args.map(arg => generateExpr(arg, context)).join(', ')})`;
|
|
3414
|
+
|
|
2786
3415
|
case 'CallExpression': {
|
|
2787
3416
|
// Method call: receiver.method(args). Member access invoked with parens —
|
|
2788
3417
|
// e.g. path.join("a", "b"), mrz.parse(line), fs.existsSync(("x")).
|
|
@@ -2869,6 +3498,63 @@ function generateExpr(node, context = createGenerationContext()) {
|
|
|
2869
3498
|
case 'SpreadExpression':
|
|
2870
3499
|
return `[...${generateExpr(node.collection, context)}]`;
|
|
2871
3500
|
|
|
3501
|
+
case 'SpreadElement':
|
|
3502
|
+
return `...${generateExpr(node.argument, context)}`;
|
|
3503
|
+
|
|
3504
|
+
case 'SpreadProperty':
|
|
3505
|
+
return `...${generateExpr(node.argument, context)}`;
|
|
3506
|
+
|
|
3507
|
+
case 'ArrayPattern':
|
|
3508
|
+
// Array destructuring pattern: [a, b] = value
|
|
3509
|
+
return `[${node.elements.map(e => generateExpr(e, context)).join(', ')}]`;
|
|
3510
|
+
|
|
3511
|
+
case 'ObjectPattern':
|
|
3512
|
+
// Object destructuring pattern: {x, y} = value
|
|
3513
|
+
return `{ ${node.properties.map(p => generateExpr(p, context)).join(', ')} }`;
|
|
3514
|
+
|
|
3515
|
+
case 'RestElement':
|
|
3516
|
+
return `...${node.name}`;
|
|
3517
|
+
|
|
3518
|
+
case 'BigIntLiteral':
|
|
3519
|
+
return `${node.value}n`;
|
|
3520
|
+
|
|
3521
|
+
case 'UndefinedLiteral':
|
|
3522
|
+
return 'undefined';
|
|
3523
|
+
|
|
3524
|
+
case 'ThisExpression':
|
|
3525
|
+
return 'this';
|
|
3526
|
+
|
|
3527
|
+
case 'ImportMetaExpression':
|
|
3528
|
+
return 'import.meta';
|
|
3529
|
+
|
|
3530
|
+
case 'SuperExpression':
|
|
3531
|
+
return 'super';
|
|
3532
|
+
|
|
3533
|
+
case 'SuperProperty':
|
|
3534
|
+
return `super.${node.property}`;
|
|
3535
|
+
|
|
3536
|
+
case 'NewExpression':
|
|
3537
|
+
return `new ${generateExpr(node.callee, context)}(${node.args.map(arg => generateExpr(arg, context)).join(', ')})`;
|
|
3538
|
+
|
|
3539
|
+
case 'AwaitExpression': {
|
|
3540
|
+
markAsync(context);
|
|
3541
|
+
return `(await ${generateExpr(node.value, context)})`;
|
|
3542
|
+
}
|
|
3543
|
+
|
|
3544
|
+
case 'UnaryExpression': {
|
|
3545
|
+
// Handle typeof, void, delete operators
|
|
3546
|
+
if (['typeof', 'void', 'delete'].includes(node.operator)) {
|
|
3547
|
+
const operand = node.operand.type === 'BinaryExpression'
|
|
3548
|
+
? `(${generateExpr(node.operand, context)})`
|
|
3549
|
+
: generateExpr(node.operand, context);
|
|
3550
|
+
return `${node.operator} ${operand}`;
|
|
3551
|
+
}
|
|
3552
|
+
// Regular unary minus
|
|
3553
|
+
return `${node.operator}${node.operand.type === 'BinaryExpression'
|
|
3554
|
+
? `(${generateExpr(node.operand, context)})`
|
|
3555
|
+
: generateExpr(node.operand, context)}`;
|
|
3556
|
+
}
|
|
3557
|
+
|
|
2872
3558
|
default:
|
|
2873
3559
|
throw new Error(`Unknown expression type "${node.type}".`);
|
|
2874
3560
|
}
|