fez-lisp 1.5.32 → 1.5.33

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.32",
5
+ "version": "1.5.33",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/check.js CHANGED
@@ -61,7 +61,6 @@ export const typeCheck = (ast) => {
61
61
  [DEBUG.SET_THEME]: {
62
62
  [STATS]: { type: APPLY, [ARGS_COUNT]: VARIADIC, [RETURNS]: UNKNOWN }
63
63
  },
64
- [SCOPE_NAME]: performance.now().toString().replace('.', 0),
65
64
  [KEYWORDS.BLOCK]: {
66
65
  [STATS]: { type: APPLY, [ARGS_COUNT]: VARIADIC, [RETURNS]: UNKNOWN }
67
66
  },
@@ -376,7 +375,29 @@ export const typeCheck = (ast) => {
376
375
  }
377
376
  }
378
377
  const errorStack = new Map()
379
- const withScope = (name, scope) => `${scope[SCOPE_NAME]}_${name}`
378
+ // const isDefinitionOfAFunction = (head, tail) =>
379
+ // head[TYPE] === APPLY &&
380
+ // head[VALUE] === KEYWORDS.DEFINE_VARIABLE &&
381
+ // tail.at(-1)[0][TYPE] === APPLY &&
382
+ // tail.at(-1)[0][VALUE] === KEYWORDS.ANONYMOUS_FUNCTION
383
+ const getScopeNames = (scope) => {
384
+ const scopeNames = []
385
+ let current = scope
386
+
387
+ while (current) {
388
+ if (current[SCOPE_NAME]) {
389
+ scopeNames.push(current[SCOPE_NAME])
390
+ }
391
+ current = Object.getPrototypeOf(current)
392
+ }
393
+ return scopeNames.reverse()
394
+ }
395
+ const withScope = (name, scope) => {
396
+ const chain = getScopeNames(scope)
397
+ const str = `${chain.join('_')}_${name}`
398
+ // console.log({ str })
399
+ return { str, chain }
400
+ }
380
401
 
381
402
  const stack = []
382
403
  const check = (exp, env, scope) => {
@@ -385,14 +406,15 @@ export const typeCheck = (ast) => {
385
406
  switch (first[TYPE]) {
386
407
  case WORD:
387
408
  {
388
- const key = withScope(first[VALUE], scope)
389
- if (env[first[VALUE]] === undefined) {
390
- errorStack.set(
391
- key,
392
- `Trying to access undefined variable ${first[VALUE]} (check #11)`
393
- )
394
- }
395
- // else if (errorStack.has(key)) errorStack.delete(key)
409
+ stack.push(() => {
410
+ const key = withScope(first[VALUE], scope)
411
+ if (env[first[VALUE]] === undefined) {
412
+ errorStack.set(
413
+ key.str,
414
+ `Trying to access undefined variable ${first[VALUE]} (check #11)`
415
+ )
416
+ }
417
+ })
396
418
  }
397
419
  break
398
420
  case ATOM:
@@ -410,12 +432,11 @@ export const typeCheck = (ast) => {
410
432
  )}) (check #10)`
411
433
  )
412
434
  } else {
435
+ const name = rest[0][VALUE]
413
436
  if (
414
- rest.at(-1).length &&
415
437
  rest.at(-1)[0][TYPE] === APPLY &&
416
438
  rest.at(-1)[0][VALUE] === KEYWORDS.ANONYMOUS_FUNCTION
417
439
  ) {
418
- const name = rest[0][VALUE]
419
440
  const n = rest.at(-1).length
420
441
  env[name] = {
421
442
  [STATS]: {
@@ -426,11 +447,8 @@ export const typeCheck = (ast) => {
426
447
  }
427
448
  if (name[name.length - 1] === PREDICATE_SUFFIX)
428
449
  env[name][STATS][SUBTYPE] = PREDICATE
429
- const key = withScope(name, scope)
430
- if (errorStack.has(key)) errorStack.delete(key)
431
- scope = exp
450
+ check(rest.at(-1), env, exp)
432
451
  } else {
433
- const name = rest[0][VALUE]
434
452
  if (!(name in env)) {
435
453
  if (rest[1][TYPE] === WORD)
436
454
  env[name] = env[rest[1][VALUE]]
@@ -445,16 +463,8 @@ export const typeCheck = (ast) => {
445
463
  }
446
464
  }
447
465
  }
448
- // if (name === 'math:decimal-scaling') {
449
- // const key = withScope(name, scope)
450
- // if (errorStack.has(key)) errorStack.delete(key)
451
- // }
452
- // if (scope[SCOPE_NAME]) {
453
- // const key = withScope(name, scope)
454
- // if (errorStack.has(key)) errorStack.delete(key)
455
- // }
466
+ check(rest.at(-1), env, scope)
456
467
  }
457
- check(rest.at(-1), env, scope)
458
468
  }
459
469
  }
460
470
  break
@@ -471,7 +481,7 @@ export const typeCheck = (ast) => {
471
481
  }
472
482
  const params = exp.slice(1, -1)
473
483
  const copy = Object.create(env)
474
- if (isLeaf(scope[1])) {
484
+ if (Array.isArray(scope[1]) && scope[1][TYPE] === WORD) {
475
485
  copy[SCOPE_NAME] = scope[1][VALUE]
476
486
  } else {
477
487
  copy[SCOPE_NAME] = performance
@@ -479,13 +489,12 @@ export const typeCheck = (ast) => {
479
489
  .toString()
480
490
  .replace('.', 0)
481
491
  }
482
-
483
492
  for (const param of params) {
484
493
  copy[param[VALUE]] = { [STATS]: { type: UNKNOWN } }
485
494
  if (env[copy[SCOPE_NAME]])
486
495
  env[copy[SCOPE_NAME]][STATS][ARGS].push(copy[param[VALUE]])
487
496
  }
488
- check(rest.at(-1), copy, scope)
497
+ check(rest.at(-1), copy, copy)
489
498
  }
490
499
  break
491
500
  default:
@@ -493,7 +502,7 @@ export const typeCheck = (ast) => {
493
502
  const key = withScope(first[VALUE], scope)
494
503
  if (env[first[VALUE]] === undefined)
495
504
  errorStack.set(
496
- key,
505
+ key.str,
497
506
  `Trying to call undefined (lambda) ${first[VALUE]} (check #9)`
498
507
  )
499
508
  else if (
@@ -502,7 +511,7 @@ export const typeCheck = (ast) => {
502
511
  env[first[VALUE]][STATS][ARGS_COUNT] !== rest.length
503
512
  ) {
504
513
  errorStack.set(
505
- key,
514
+ key.str,
506
515
  `Incorrect number of arguments for (${
507
516
  first[VALUE]
508
517
  }). Expected (= ${
@@ -517,10 +526,10 @@ export const typeCheck = (ast) => {
517
526
  if (first[TYPE] === APPLY && !isSpecial) {
518
527
  if (env[first[VALUE]][STATS].type === ATOM) {
519
528
  errorStack.set(
520
- key,
529
+ key.str,
521
530
  `(${first[VALUE]}) is not a (lambda) (${stringifyArgs(
522
531
  exp
523
- )})`
532
+ )}) (check #12)`
524
533
  )
525
534
  } else if (!env[first[VALUE]][STATS][ARGS_COUNT]) {
526
535
  env[first[VALUE]][STATS][RETURNS] = UNKNOWN
@@ -546,7 +555,7 @@ export const typeCheck = (ast) => {
546
555
  env[rest[i][VALUE]][STATS][ARGS_COUNT]
547
556
  ) {
548
557
  errorStack.set(
549
- key,
558
+ key.str,
550
559
  `Incorrect number of arguments for (${
551
560
  first[VALUE]
552
561
  }). Expected (= ${
@@ -565,7 +574,7 @@ export const typeCheck = (ast) => {
565
574
  ) {
566
575
  if (args[i][STATS][ARGS_COUNT] !== rest[i].length - 2)
567
576
  errorStack.set(
568
- key,
577
+ key.str,
569
578
  `Incorrect number of arguments for (${
570
579
  first[VALUE]
571
580
  }). Expected (= ${
@@ -595,7 +604,7 @@ export const typeCheck = (ast) => {
595
604
  ) {
596
605
  // console.log(env[CAR][STATS], expectedArgs[i][TYPE])
597
606
  errorStack.set(
598
- key,
607
+ key.str,
599
608
  `Incorrect type of arguments for special form (${
600
609
  first[VALUE]
601
610
  }). Expected (${toTypeNames(
@@ -629,7 +638,7 @@ export const typeCheck = (ast) => {
629
638
  env[TT][STATS][RETURNS]
630
639
  )
631
640
  errorStack.set(
632
- key,
641
+ key.str,
633
642
  `Incorrect type of arguments for special form (${
634
643
  first[VALUE]
635
644
  }). Expected (${toTypeNames(
@@ -644,7 +653,7 @@ export const typeCheck = (ast) => {
644
653
  expectedArgs[i][TYPE] !== T
645
654
  ) {
646
655
  errorStack.set(
647
- key,
656
+ key.str,
648
657
  `Incorrect type of arguments for special form (${
649
658
  first[VALUE]
650
659
  }). Expected (${toTypeNames(
@@ -661,7 +670,7 @@ export const typeCheck = (ast) => {
661
670
  case APPLY:
662
671
  case ATOM:
663
672
  errorStack.set(
664
- key,
673
+ key.str,
665
674
  `Incorrect type of arguments for (${
666
675
  first[VALUE]
667
676
  }). Expected (${toTypeNames(
@@ -697,7 +706,7 @@ export const typeCheck = (ast) => {
697
706
  args[i][STATS].type)
698
707
  ) {
699
708
  errorStack.set(
700
- key,
709
+ key.str,
701
710
  `Incorrect type of arguments ${i} for (${
702
711
  first[VALUE]
703
712
  }). Expected (${toTypeNames(
@@ -715,8 +724,6 @@ export const typeCheck = (ast) => {
715
724
  args[i][STATS].type === UNKNOWN
716
725
  ) {
717
726
  retry.retried = true
718
- if (!scope[SCOPE_NAME])
719
- scope[SCOPE_NAME] = scope[1][VALUE]
720
727
  stack.unshift(() => check(exp, env, scope))
721
728
  }
722
729
  // console.log(
@@ -736,7 +743,7 @@ export const typeCheck = (ast) => {
736
743
  args[i][STATS].type
737
744
  ) {
738
745
  errorStack.set(
739
- key,
746
+ key.str,
740
747
  `Incorrect type of arguments ${i} for (${
741
748
  first[VALUE]
742
749
  }). Expected (${toTypeNames(
@@ -771,6 +778,7 @@ export const typeCheck = (ast) => {
771
778
  }
772
779
  }
773
780
  const copy = JSON.parse(JSON.stringify(ast))
781
+ copy[SCOPE_NAME] = 'root'
774
782
  check(copy, root, copy)
775
783
  while (stack.length) stack.pop()()
776
784
  if (errorStack.size)