fez-lisp 1.5.48 → 1.5.50

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "fez-lisp",
3
3
  "description": "Lisp interpreted & compiled to JavaScript",
4
4
  "author": "AT290690",
5
- "version": "1.5.48",
5
+ "version": "1.5.50",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/check.js CHANGED
@@ -70,22 +70,25 @@ const deepLambdaReturn = (rest, condition) => {
70
70
  const rem = hasBlock(body) ? body.at(-1) : body
71
71
  return condition(rem) ? rem : deepLambdaReturn(rem, condition)
72
72
  }
73
- const getScopeNames = (scope) => {
74
- const scopeNames = []
75
- let current = scope
76
- while (current) {
77
- if (current[SCOPE_NAME]) {
78
- scopeNames.push(current[SCOPE_NAME])
79
- }
80
- current = Object.getPrototypeOf(current)
81
- }
82
- return scopeNames.reverse()
83
- }
84
- const withScope = (name, scope) => {
85
- const chain = getScopeNames(scope)
86
- const str = `${chain.join('_')}_${name}`
87
- return { str, chain }
88
- }
73
+ // const getScopeNames = (scope) => {
74
+ // const scopeNames = []
75
+ // let current = scope
76
+ // while (current) {
77
+ // if (current[SCOPE_NAME]) {
78
+ // scopeNames.push(current[SCOPE_NAME])
79
+ // }
80
+ // current = Object.getPrototypeOf(current)
81
+ // }
82
+ // return scopeNames.reverse()
83
+ // }
84
+ // const withScope = (name, scope) => {
85
+ // const chain = getScopeNames(scope)
86
+ // const str = `${chain.join('_')}_${name}::${performance
87
+ // .now()
88
+ // .toString()
89
+ // .replace('.', 0)}`
90
+ // return { str, chain }
91
+ // }
89
92
  export const typeCheck = (ast) => {
90
93
  const root = {
91
94
  [toTypeNames(APPLY)]: {
@@ -536,7 +539,7 @@ export const typeCheck = (ast) => {
536
539
  }
537
540
  }
538
541
  }
539
- const errorStack = new Map()
542
+ const errorStack = new Set()
540
543
  const warningStack = new Set()
541
544
 
542
545
  // const isDefinitionOfAFunction = (head, tail) =>
@@ -557,10 +560,8 @@ export const typeCheck = (ast) => {
557
560
  case WORD:
558
561
  {
559
562
  stack.push(() => {
560
- const key = withScope(first[VALUE], scope)
561
563
  if (env[first[VALUE]] === undefined) {
562
- errorStack.set(
563
- key.str,
564
+ errorStack.add(
564
565
  `Trying to access undefined variable ${first[VALUE]} (check #11)`
565
566
 
566
567
  // `Trying to access undefined variable ${
@@ -1011,10 +1012,8 @@ export const typeCheck = (ast) => {
1011
1012
  break
1012
1013
  default:
1013
1014
  stack.push(() => {
1014
- const key = withScope(first[VALUE], scope)
1015
1015
  if (env[first[VALUE]] === undefined)
1016
- errorStack.set(
1017
- key.str,
1016
+ errorStack.add(
1018
1017
  `Trying to call undefined (lambda) ${first[VALUE]} (check #9)`
1019
1018
  )
1020
1019
  else {
@@ -1023,8 +1022,7 @@ export const typeCheck = (ast) => {
1023
1022
  env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC &&
1024
1023
  env[first[VALUE]][STATS][ARGS_COUNT] !== rest.length
1025
1024
  ) {
1026
- errorStack.set(
1027
- key.str,
1025
+ errorStack.add(
1028
1026
  `Incorrect number of arguments for (${
1029
1027
  first[VALUE]
1030
1028
  }). Expected (= ${
@@ -1037,8 +1035,7 @@ export const typeCheck = (ast) => {
1037
1035
  const isSpecial = SPECIAL_FORMS_SET.has(first[VALUE])
1038
1036
  if (first[TYPE] === APPLY && !isSpecial) {
1039
1037
  if (env[first[VALUE]][STATS][TYPE_PROP][0] === ATOM) {
1040
- errorStack.set(
1041
- key.str,
1038
+ errorStack.add(
1042
1039
  `(${first[VALUE]}) is not a (lambda) (${stringifyArgs(
1043
1040
  exp
1044
1041
  )}) (check #12)`
@@ -1073,9 +1070,8 @@ export const typeCheck = (ast) => {
1073
1070
  args[i][SUB] !==
1074
1071
  env[rest[i][VALUE]][STATS][RETURNS][1]
1075
1072
  ) {
1076
- errorStack.set(
1077
- key.str,
1078
- `Incorrect type of arguments for (${
1073
+ errorStack.add(
1074
+ `Incorrect type of argument (${i}) for (${
1079
1075
  first[VALUE]
1080
1076
  }). Expected (${toTypeNames(
1081
1077
  args[i][SUB]
@@ -1091,8 +1087,7 @@ export const typeCheck = (ast) => {
1091
1087
  rest[i][VALUE] !== TRUE &&
1092
1088
  rest[i][VALUE] !== FALSE
1093
1089
  ) {
1094
- errorStack.set(
1095
- key.str,
1090
+ errorStack.add(
1096
1091
  `Incorrect type of arguments for (${
1097
1092
  first[VALUE]
1098
1093
  }). Expected (${toTypeNames(
@@ -1116,9 +1111,8 @@ export const typeCheck = (ast) => {
1116
1111
  fn &&
1117
1112
  fn[STATS][RETURNS][0] !== args[i][TYPE]
1118
1113
  ) {
1119
- errorStack.set(
1120
- key.str,
1121
- `Incorrect type of arguments for (${
1114
+ errorStack.add(
1115
+ `Incorrect type of argument (${i}) for (${
1122
1116
  first[VALUE]
1123
1117
  }). Expected (${toTypeNames(
1124
1118
  args[i][TYPE]
@@ -1131,14 +1125,14 @@ export const typeCheck = (ast) => {
1131
1125
  fn &&
1132
1126
  fn[STATS][RETURNS][1] !== args[i][SUB]
1133
1127
  ) {
1134
- errorStack.set(
1135
- key.str,
1136
- `Incorrect type of arguments for (${
1128
+ errorStack.add(
1129
+ `Incorrect type of argument (${i}) for (${
1137
1130
  first[VALUE]
1138
1131
  }). Expected (${toTypeNames(
1139
1132
  args[i][SUB]
1140
1133
  )}) but got an (${toTypeNames(
1141
- fn[STATS][RETURNS][1]
1134
+ fn[STATS][RETURNS][1] ??
1135
+ fn[STATS][RETURNS][0]
1142
1136
  )}) which is neither ${TRUE} or ${FALSE} (${stringifyArgs(
1143
1137
  exp
1144
1138
  )}) (check #27)`
@@ -1150,8 +1144,7 @@ export const typeCheck = (ast) => {
1150
1144
  const returns = isLeaf(rem) ? rem : rem[0]
1151
1145
  if (returns[TYPE] === ATOM) {
1152
1146
  if (args[i][TYPE] !== ATOM) {
1153
- errorStack.set(
1154
- key.str,
1147
+ errorStack.add(
1155
1148
  `Incorrect type of argument ${i} for (${
1156
1149
  first[VALUE]
1157
1150
  }). Expected (${toTypeNames(
@@ -1167,8 +1160,7 @@ export const typeCheck = (ast) => {
1167
1160
  returns[VALUE] !== TRUE &&
1168
1161
  returns[VALUE] !== FALSE
1169
1162
  ) {
1170
- errorStack.set(
1171
- key.str,
1163
+ errorStack.add(
1172
1164
  `Incorrect type of argument ${i} for (${
1173
1165
  first[VALUE]
1174
1166
  }). Expected (${toTypeNames(
@@ -1185,8 +1177,7 @@ export const typeCheck = (ast) => {
1185
1177
  args[i][TYPE] !==
1186
1178
  env[returns[VALUE]][STATS][RETURNS][0]
1187
1179
  ) {
1188
- errorStack.set(
1189
- key.str,
1180
+ errorStack.add(
1190
1181
  `Incorrect type of argument ${i} for (${
1191
1182
  first[VALUE]
1192
1183
  }). Expected (${toTypeNames(
@@ -1201,8 +1192,7 @@ export const typeCheck = (ast) => {
1201
1192
  args[i][SUB] !==
1202
1193
  env[returns[VALUE]][STATS][RETURNS][1]
1203
1194
  ) {
1204
- errorStack.set(
1205
- key.str,
1195
+ errorStack.add(
1206
1196
  `Incorrect type of argument ${i} for (${
1207
1197
  first[VALUE]
1208
1198
  }). Expected (${toTypeNames(
@@ -1220,8 +1210,7 @@ export const typeCheck = (ast) => {
1220
1210
  env[current[VALUE]][STATS][RETURNS][1] !==
1221
1211
  args[i][SUB]
1222
1212
  ) {
1223
- errorStack.set(
1224
- key.str,
1213
+ errorStack.add(
1225
1214
  `Incorrect type of arguments (${i}) for (${
1226
1215
  first[VALUE]
1227
1216
  }). Expected (${toTypeNames(
@@ -1236,12 +1225,7 @@ export const typeCheck = (ast) => {
1236
1225
  }
1237
1226
  }
1238
1227
 
1239
- if (
1240
- first[TYPE] === APPLY &&
1241
- isSpecial
1242
- // &&
1243
- // env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC
1244
- ) {
1228
+ if (first[TYPE] === APPLY && isSpecial) {
1245
1229
  const expectedArgs = env[first[VALUE]][STATS][ARGS]
1246
1230
  for (let i = 0; i < rest.length; ++i) {
1247
1231
  if (expectedArgs[i][TYPE] === UNKNOWN) continue
@@ -1250,88 +1234,154 @@ export const typeCheck = (ast) => {
1250
1234
  const isKnown =
1251
1235
  env[CAR] &&
1252
1236
  env[CAR][STATS][RETURNS][0] !== UNKNOWN
1253
- if (
1254
- isKnown &&
1255
- env[CAR][STATS][RETURNS][0] !==
1237
+ if (isKnown) {
1238
+ if (
1239
+ env[CAR][STATS][RETURNS][0] !==
1256
1240
  expectedArgs[i][TYPE]
1257
- ) {
1258
- errorStack.set(
1259
- key.str,
1260
- `Incorrect type of argument (${i}) for special form (${
1261
- first[VALUE]
1262
- }). Expected (${toTypeNames(
1263
- expectedArgs[i][TYPE]
1264
- )}) but got (${toTypeNames(
1265
- env[CAR][STATS][RETURNS][0]
1266
- )}) (${stringifyArgs(exp)}) (check #1)`
1267
- )
1268
- } else if (
1269
- isKnown &&
1270
- expectedArgs[i][SUB] &&
1271
- env[CAR][STATS][RETURNS][1] !==
1272
- expectedArgs[i][SUB]
1273
- ) {
1274
- errorStack.set(
1275
- key.str,
1276
- `Incorrect type of arguments for special form (${
1277
- first[VALUE]
1278
- }). Expected (${toTypeNames(
1279
- expectedArgs[i][SUB]
1280
- )}) but got (${toTypeNames(
1281
- env[CAR][STATS][RETURNS][1] ??
1241
+ ) {
1242
+ errorStack.add(
1243
+ `Incorrect type of argument (${i}) for special form (${
1244
+ first[VALUE]
1245
+ }). Expected (${toTypeNames(
1246
+ expectedArgs[i][TYPE]
1247
+ )}) but got (${toTypeNames(
1282
1248
  env[CAR][STATS][RETURNS][0]
1283
- )}) (${stringifyArgs(exp)}) (check #13)`
1284
- )
1249
+ )}) (${stringifyArgs(exp)}) (check #1)`
1250
+ )
1251
+ } else if (
1252
+ expectedArgs[i][SUB] &&
1253
+ env[CAR][STATS][RETURNS][1] !==
1254
+ expectedArgs[i][SUB]
1255
+ ) {
1256
+ errorStack.add(
1257
+ `Incorrect type of arguments for special form (${
1258
+ first[VALUE]
1259
+ }). Expected (${toTypeNames(
1260
+ expectedArgs[i][SUB]
1261
+ )}) but got (${toTypeNames(
1262
+ env[CAR][STATS][RETURNS][1] ??
1263
+ env[CAR][STATS][RETURNS][0]
1264
+ )}) (${stringifyArgs(exp)}) (check #13)`
1265
+ )
1266
+ }
1285
1267
  }
1286
- }
1287
- if (
1288
- env[rest[i][VALUE]] &&
1289
- expectedArgs[i][TYPE] !== rest[i][TYPE]
1290
- ) {
1268
+ } else {
1291
1269
  switch (rest[i][TYPE]) {
1292
- // case UNKNOWN:
1293
- // env[first[VALUE]][STATS][TYPE_PROP][0] =
1294
- // expectedArgs[i][TYPE]
1295
- // break
1296
1270
  case WORD:
1297
- const T =
1298
- env[rest[i][VALUE]][STATS][TYPE_PROP][0]
1299
- if (
1300
- T !== UNKNOWN &&
1301
- expectedArgs[i][TYPE] !== UNKNOWN &&
1302
- expectedArgs[i][TYPE] !== T
1303
- ) {
1304
- errorStack.set(
1305
- key.str,
1306
- `Incorrect type of arguments for special form (${
1271
+ {
1272
+ const CAR = rest[i][VALUE]
1273
+ const isKnown =
1274
+ env[CAR] &&
1275
+ env[CAR][STATS][TYPE_PROP][0] !== UNKNOWN
1276
+ if (isKnown) {
1277
+ if (
1278
+ expectedArgs[i][TYPE] !==
1279
+ env[CAR][STATS][TYPE_PROP][0]
1280
+ ) {
1281
+ errorStack.add(
1282
+ `Incorrect type of argument (${i}) for special form (${
1283
+ first[VALUE]
1284
+ }). Expected (${toTypeNames(
1285
+ expectedArgs[i][TYPE]
1286
+ )}) but got (${toTypeNames(
1287
+ env[CAR][STATS][TYPE_PROP][0]
1288
+ )}) (${stringifyArgs(exp)}) (check #3)`
1289
+ )
1290
+ } else if (
1291
+ expectedArgs[i][SUB] &&
1292
+ env[CAR][STATS][RETURNS][1] !==
1293
+ expectedArgs[i][SUB]
1294
+ )
1295
+ errorStack.add(
1296
+ `Incorrect type of argument (${i}) for special form (${
1297
+ first[VALUE]
1298
+ }). Expected (${toTypeNames(
1299
+ expectedArgs[i][SUB]
1300
+ )}) but got (${toTypeNames(
1301
+ env[CAR][STATS][RETURNS][1] ??
1302
+ env[CAR][STATS][TYPE_PROP][0]
1303
+ )}) (${stringifyArgs(exp)}) (check #6)`
1304
+ )
1305
+ } else if (env[rest[i][VALUE]]) {
1306
+ env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
1307
+ expectedArgs[i][TYPE]
1308
+ }
1309
+ }
1310
+ break
1311
+ // case APPLY:
1312
+ // errorStack.add(
1313
+ // `Incorrect type of arguments for (${
1314
+ // first[VALUE]
1315
+ // }). Expected (${toTypeNames(
1316
+ // expectedArgs[i][TYPE]
1317
+ // )}) but got (${toTypeNames(
1318
+ // rest[i][TYPE]
1319
+ // )}) (${stringifyArgs(exp)}) (check #5)`
1320
+ // )
1321
+ // break
1322
+ case ATOM: {
1323
+ if (rest[i][TYPE] !== expectedArgs[i][TYPE]) {
1324
+ errorStack.add(
1325
+ `Incorrect type of argument (${i}) for special form (${
1307
1326
  first[VALUE]
1308
1327
  }). Expected (${toTypeNames(
1309
1328
  expectedArgs[i][TYPE]
1310
1329
  )}) but got (${toTypeNames(
1311
- T
1312
- )}) (${stringifyArgs(exp)}) (check #3)`
1330
+ rest[i][TYPE]
1331
+ )}) (${stringifyArgs(exp)}) (check #2)`
1313
1332
  )
1314
- } else {
1315
- env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
1316
- expectedArgs[i][TYPE]
1317
1333
  }
1318
1334
  break
1319
- case APPLY:
1320
- case ATOM:
1321
- errorStack.set(
1322
- key.str,
1323
- `Incorrect type of arguments for (${
1324
- first[VALUE]
1325
- }). Expected (${toTypeNames(
1326
- expectedArgs[i][TYPE]
1327
- )}) but got (${toTypeNames(
1328
- rest[i][TYPE]
1329
- )}) (${stringifyArgs(exp)}) (check #5)`
1330
- )
1331
- break
1335
+ }
1332
1336
  }
1333
- } else {
1334
1337
  }
1338
+ // if (
1339
+ // env[rest[i][VALUE]] &&
1340
+ // expectedArgs[i][TYPE] !== rest[i][TYPE]
1341
+ // ) {
1342
+ // switch (rest[i][TYPE]) {
1343
+ // // case UNKNOWN:
1344
+ // // env[first[VALUE]][STATS][TYPE_PROP][0] =
1345
+ // // expectedArgs[i][TYPE]
1346
+ // // break
1347
+ // case WORD:
1348
+ // const T =
1349
+ // env[rest[i][VALUE]][STATS][TYPE_PROP][0]
1350
+ // if (
1351
+ // T !== UNKNOWN &&
1352
+ // expectedArgs[i][TYPE] !== UNKNOWN &&
1353
+ // expectedArgs[i][TYPE] !== T
1354
+ // ) {
1355
+ // errorStack.add(
1356
+ // `Incorrect type of argument (${i}) for special form (${
1357
+ // first[VALUE]
1358
+ // }). Expected (${toTypeNames(
1359
+ // expectedArgs[i][TYPE]
1360
+ // )}) but got (${toTypeNames(
1361
+ // T
1362
+ // )}) (${stringifyArgs(exp)}) (check #3.1)`
1363
+ // )
1364
+ // } else {
1365
+ // env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
1366
+ // expectedArgs[i][TYPE]
1367
+ // }
1368
+ // break
1369
+ // case APPLY:
1370
+ // case ATOM:
1371
+ // errorStack.add(
1372
+ // `Incorrect type of arguments for (${
1373
+ // first[VALUE]
1374
+ // }). Expected (${toTypeNames(
1375
+ // expectedArgs[i][TYPE]
1376
+ // )}) but got (${toTypeNames(
1377
+ // rest[i][TYPE]
1378
+ // )}) (${stringifyArgs(exp)}) (check #5)`
1379
+ // )
1380
+ // break
1381
+ // }
1382
+ // } else {
1383
+ // // TIDI fugyre iyt wgat ti di gere
1384
+ // }
1335
1385
  }
1336
1386
  }
1337
1387
  // type checking
@@ -1356,8 +1406,7 @@ export const typeCheck = (ast) => {
1356
1406
  env[rest[i][VALUE]][STATS][TYPE_PROP][0] !==
1357
1407
  args[i][STATS][TYPE_PROP][0])
1358
1408
  ) {
1359
- errorStack.set(
1360
- key.str,
1409
+ errorStack.add(
1361
1410
  `Incorrect type of arguments ${i} for (${
1362
1411
  first[VALUE]
1363
1412
  }). Expected (${toTypeNames(
@@ -1393,8 +1442,7 @@ export const typeCheck = (ast) => {
1393
1442
  env[rest[i][0][VALUE]][STATS][RETURNS][0] !==
1394
1443
  args[i][STATS][TYPE_PROP][0]
1395
1444
  ) {
1396
- errorStack.set(
1397
- key.str,
1445
+ errorStack.add(
1398
1446
  `Incorrect type of arguments ${i} for (${
1399
1447
  first[VALUE]
1400
1448
  }). Expected (${toTypeNames(
@@ -1403,19 +1451,21 @@ export const typeCheck = (ast) => {
1403
1451
  env[rest[i][0][VALUE]][STATS][RETURNS][0]
1404
1452
  )}) (${stringifyArgs(exp)}) (check #4)`
1405
1453
  )
1406
- } else {
1407
- if (
1408
- rest[i].length &&
1409
- env[rest[i][0][VALUE]] &&
1410
- args[i][STATS][TYPE_PROP][0] === UNKNOWN &&
1411
- env[rest[i][0][VALUE]][STATS].retried < RETRY_COUNT
1412
- ) {
1413
- env[rest[i][0][VALUE]][STATS].retried += 1
1414
- if (!scope[SCOPE_NAME])
1415
- scope[SCOPE_NAME] = scope[1][VALUE]
1416
- stack.unshift(() => check(exp, env, scope))
1417
- }
1418
1454
  }
1455
+ // TODO figure out why we don't need this anymore
1456
+ // else {
1457
+ // if (
1458
+ // rest[i].length &&
1459
+ // env[rest[i][0][VALUE]] &&
1460
+ // args[i][STATS][TYPE_PROP][0] === UNKNOWN &&
1461
+ // env[rest[i][0][VALUE]][STATS].retried < RETRY_COUNT
1462
+ // ) {
1463
+ // env[rest[i][0][VALUE]][STATS].retried += 1
1464
+ // if (!scope[SCOPE_NAME])
1465
+ // scope[SCOPE_NAME] = scope[1][VALUE]
1466
+ // stack.unshift(() => check(exp, env, scope))
1467
+ // }
1468
+ // }
1419
1469
  }
1420
1470
  }
1421
1471
  }
@@ -1432,7 +1482,7 @@ export const typeCheck = (ast) => {
1432
1482
  copy[SCOPE_NAME] = 'root'
1433
1483
  check(copy, root, copy)
1434
1484
  while (stack.length) stack.pop()()
1435
- const issues = [...new Set(errorStack.values()), ...warningStack]
1485
+ const issues = [...errorStack, ...warningStack]
1436
1486
  if (issues.length) throw new TypeError(issues.join('\n'))
1437
1487
  return ast
1438
1488
  }
package/src/keywords.js CHANGED
@@ -60,7 +60,8 @@ export const TYPES = {
60
60
  }
61
61
  export const RUNTIME_TYPES = {
62
62
  NUMBER: 'number',
63
- ARRAY: 'array'
63
+ ARRAY: 'array',
64
+ LAMBDA: 'lambda'
64
65
  }
65
66
  export const DEBUG = {
66
67
  STRING: 'string',