fez-lisp 1.5.33 → 1.5.34

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/check.js +181 -221
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.33",
5
+ "version": "1.5.34",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/check.js CHANGED
@@ -505,138 +505,111 @@ export const typeCheck = (ast) => {
505
505
  key.str,
506
506
  `Trying to call undefined (lambda) ${first[VALUE]} (check #9)`
507
507
  )
508
- else if (
509
- env[first[VALUE]][STATS].type === APPLY &&
510
- env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC &&
511
- env[first[VALUE]][STATS][ARGS_COUNT] !== rest.length
512
- ) {
513
- errorStack.set(
514
- key.str,
515
- `Incorrect number of arguments for (${
516
- first[VALUE]
517
- }). Expected (= ${
518
- env[first[VALUE]][STATS][ARGS_COUNT]
519
- }) but got ${rest.length} (${stringifyArgs(
520
- exp
521
- )}) (check #8)`
522
- )
523
- } else {
524
- const isSpecial = SPECIAL_FORMS_SET.has(first[VALUE])
508
+ else {
509
+ if (
510
+ env[first[VALUE]][STATS].type === APPLY &&
511
+ env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC &&
512
+ env[first[VALUE]][STATS][ARGS_COUNT] !== rest.length
513
+ ) {
514
+ errorStack.set(
515
+ key.str,
516
+ `Incorrect number of arguments for (${
517
+ first[VALUE]
518
+ }). Expected (= ${
519
+ env[first[VALUE]][STATS][ARGS_COUNT]
520
+ }) but got ${rest.length} (${stringifyArgs(
521
+ exp
522
+ )}) (check #8)`
523
+ )
524
+ } else {
525
+ const isSpecial = SPECIAL_FORMS_SET.has(first[VALUE])
525
526
 
526
- if (first[TYPE] === APPLY && !isSpecial) {
527
- if (env[first[VALUE]][STATS].type === ATOM) {
528
- errorStack.set(
529
- key.str,
530
- `(${first[VALUE]}) is not a (lambda) (${stringifyArgs(
531
- exp
532
- )}) (check #12)`
533
- )
534
- } else if (!env[first[VALUE]][STATS][ARGS_COUNT]) {
535
- env[first[VALUE]][STATS][RETURNS] = UNKNOWN
536
- env[first[VALUE]][STATS].type = APPLY
537
- env[first[VALUE]][STATS][ARGS_COUNT] = rest.length
527
+ if (first[TYPE] === APPLY && !isSpecial) {
528
+ if (env[first[VALUE]][STATS].type === ATOM) {
529
+ errorStack.set(
530
+ key.str,
531
+ `(${first[VALUE]}) is not a (lambda) (${stringifyArgs(
532
+ exp
533
+ )}) (check #12)`
534
+ )
535
+ } else if (!env[first[VALUE]][STATS][ARGS_COUNT]) {
536
+ env[first[VALUE]][STATS][RETURNS] = UNKNOWN
537
+ env[first[VALUE]][STATS].type = APPLY
538
+ env[first[VALUE]][STATS][ARGS_COUNT] = rest.length
539
+ }
538
540
  }
539
- }
540
- // also type of arg
541
- const args = env[first[VALUE]][STATS][ARGS]
542
- if (args) {
543
- for (let i = 0; i < args.length; ++i) {
544
- if (
545
- args[i][STATS] &&
546
- args[i][STATS].type === APPLY &&
547
- env[rest[i][VALUE]] &&
548
- env[rest[i][VALUE]][STATS] &&
549
- env[rest[i][VALUE]][STATS][ARGS_COUNT] &&
550
- args[i][STATS][ARGS_COUNT] !== VARIADIC &&
551
- env[rest[i][VALUE]][STATS][ARGS_COUNT] !== VARIADIC
552
- ) {
541
+ // also type of arg
542
+ const args = env[first[VALUE]][STATS][ARGS]
543
+ if (args) {
544
+ for (let i = 0; i < args.length; ++i) {
545
+ // type check
553
546
  if (
554
- args[i][STATS][ARGS_COUNT] !==
555
- env[rest[i][VALUE]][STATS][ARGS_COUNT]
547
+ first[TYPE] === APPLY &&
548
+ isSpecial &&
549
+ env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC
556
550
  ) {
557
- errorStack.set(
558
- key.str,
559
- `Incorrect number of arguments for (${
560
- first[VALUE]
561
- }). Expected (= ${
562
- args[i][STATS][ARGS_COUNT]
563
- }) but got ${rest.length} (${stringifyArgs(
564
- exp
565
- )}) (check #7)`
566
- )
567
- }
568
- } else if (
569
- args[i][STATS] &&
570
- args[i][STATS].type === APPLY &&
571
- !isLeaf(rest[i]) &&
572
- rest[i][0][TYPE] === APPLY &&
573
- rest[i][0][VALUE] === KEYWORDS.ANONYMOUS_FUNCTION
574
- ) {
575
- if (args[i][STATS][ARGS_COUNT] !== rest[i].length - 2)
576
- errorStack.set(
577
- key.str,
578
- `Incorrect number of arguments for (${
579
- first[VALUE]
580
- }). Expected (= ${
581
- args[i][STATS][ARGS_COUNT]
582
- }) but got ${rest.length} (${stringifyArgs(
583
- exp
584
- )}) (check #6)`
585
- )
586
- }
587
-
588
- // type check
589
- if (
590
- first[TYPE] === APPLY &&
591
- isSpecial &&
592
- env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC
593
- ) {
594
- const expectedArgs = env[first[VALUE]][STATS][ARGS]
595
- for (let i = 0; i < rest.length; ++i) {
596
- if (expectedArgs[i][TYPE] === UNKNOWN) continue
597
- if (!isLeaf(rest[i])) {
598
- const CAR = rest[i][0][VALUE]
599
- if (
600
- env[CAR] &&
601
- env[CAR][STATS][RETURNS] != undefined &&
602
- env[CAR][STATS][RETURNS] !== UNKNOWN &&
603
- env[CAR][STATS][RETURNS] !== expectedArgs[i][TYPE]
604
- ) {
605
- // console.log(env[CAR][STATS], expectedArgs[i][TYPE])
606
- errorStack.set(
607
- key.str,
608
- `Incorrect type of arguments for special form (${
609
- first[VALUE]
610
- }). Expected (${toTypeNames(
551
+ const expectedArgs = env[first[VALUE]][STATS][ARGS]
552
+ for (let i = 0; i < rest.length; ++i) {
553
+ if (expectedArgs[i][TYPE] === UNKNOWN) continue
554
+ if (!isLeaf(rest[i])) {
555
+ const CAR = rest[i][0][VALUE]
556
+ if (
557
+ env[CAR] &&
558
+ env[CAR][STATS][RETURNS] != undefined &&
559
+ env[CAR][STATS][RETURNS] !== UNKNOWN &&
560
+ env[CAR][STATS][RETURNS] !==
611
561
  expectedArgs[i][TYPE]
612
- )}) but got (${toTypeNames(
613
- env[CAR][STATS][RETURNS]
614
- )}) (${stringifyArgs(exp)}) (check #1)`
615
- )
562
+ ) {
563
+ // console.log(env[CAR][STATS], expectedArgs[i][TYPE])
564
+ errorStack.set(
565
+ key.str,
566
+ `Incorrect type of arguments for special form (${
567
+ first[VALUE]
568
+ }). Expected (${toTypeNames(
569
+ expectedArgs[i][TYPE]
570
+ )}) but got (${toTypeNames(
571
+ env[CAR][STATS][RETURNS]
572
+ )}) (${stringifyArgs(exp)}) (check #1)`
573
+ )
574
+ }
575
+ // else {
576
+ // console.log(env[CAR])
577
+ // }
616
578
  }
617
- // else {
618
- // console.log(env[CAR])
619
- // }
620
- }
621
- if (
622
- env[rest[i][VALUE]] &&
623
- expectedArgs[i][TYPE] !== rest[i][TYPE]
624
- ) {
625
- switch (rest[i][TYPE]) {
626
- case UNKNOWN:
627
- env[first[VALUE]][STATS].type =
628
- expectedArgs[i][TYPE]
629
- break
630
- case WORD:
631
- const T = env[rest[i][VALUE]][STATS].type
632
- if (Array.isArray(T)) {
633
- const TT = T[VALUE]
634
- if (
635
- env[TT][STATS][RETURNS] &&
636
- env[TT][STATS][RETURNS] !== UNKNOWN &&
637
- expectedArgs[i][TYPE] !==
638
- env[TT][STATS][RETURNS]
639
- )
579
+ if (
580
+ env[rest[i][VALUE]] &&
581
+ expectedArgs[i][TYPE] !== rest[i][TYPE]
582
+ ) {
583
+ switch (rest[i][TYPE]) {
584
+ case UNKNOWN:
585
+ env[first[VALUE]][STATS].type =
586
+ expectedArgs[i][TYPE]
587
+ break
588
+ case WORD:
589
+ const T = env[rest[i][VALUE]][STATS].type
590
+ if (Array.isArray(T)) {
591
+ const TT = T[VALUE]
592
+ if (
593
+ env[TT][STATS][RETURNS] &&
594
+ env[TT][STATS][RETURNS] !== UNKNOWN &&
595
+ expectedArgs[i][TYPE] !==
596
+ env[TT][STATS][RETURNS]
597
+ )
598
+ errorStack.set(
599
+ key.str,
600
+ `Incorrect type of arguments for special form (${
601
+ first[VALUE]
602
+ }). Expected (${toTypeNames(
603
+ expectedArgs[i][TYPE]
604
+ )}) but got (${toTypeNames(
605
+ rest[i][TYPE]
606
+ )}) (${stringifyArgs(exp)}) (check #2)`
607
+ )
608
+ } else if (
609
+ T !== UNKNOWN &&
610
+ expectedArgs[i][TYPE] !== UNKNOWN &&
611
+ expectedArgs[i][TYPE] !== T
612
+ ) {
640
613
  errorStack.set(
641
614
  key.str,
642
615
  `Incorrect type of arguments for special form (${
@@ -644,66 +617,89 @@ export const typeCheck = (ast) => {
644
617
  }). Expected (${toTypeNames(
645
618
  expectedArgs[i][TYPE]
646
619
  )}) but got (${toTypeNames(
647
- rest[i][TYPE]
648
- )}) (${stringifyArgs(exp)}) (check #2)`
620
+ T
621
+ )}) (${stringifyArgs(exp)}) (check #3)`
649
622
  )
650
- } else if (
651
- T !== UNKNOWN &&
652
- expectedArgs[i][TYPE] !== UNKNOWN &&
653
- expectedArgs[i][TYPE] !== T
654
- ) {
623
+ } else {
624
+ env[rest[i][VALUE]][STATS].type =
625
+ expectedArgs[i][TYPE]
626
+ }
627
+ break
628
+ case APPLY:
629
+ case ATOM:
655
630
  errorStack.set(
656
631
  key.str,
657
- `Incorrect type of arguments for special form (${
632
+ `Incorrect type of arguments for (${
658
633
  first[VALUE]
659
634
  }). Expected (${toTypeNames(
660
635
  expectedArgs[i][TYPE]
661
636
  )}) but got (${toTypeNames(
662
- T
663
- )}) (${stringifyArgs(exp)}) (check #3)`
637
+ rest[i][TYPE]
638
+ )}) (${stringifyArgs(exp)}) (check #5)`
664
639
  )
665
- } else {
666
- env[rest[i][VALUE]][STATS].type =
667
- expectedArgs[i][TYPE]
668
- }
669
- break
670
- case APPLY:
671
- case ATOM:
672
- errorStack.set(
673
- key.str,
674
- `Incorrect type of arguments for (${
675
- first[VALUE]
676
- }). Expected (${toTypeNames(
677
- expectedArgs[i][TYPE]
678
- )}) but got (${toTypeNames(
679
- rest[i][TYPE]
680
- )}) (${stringifyArgs(exp)}) (check #5)`
681
- )
682
- break
640
+ break
641
+ }
683
642
  }
684
643
  }
685
644
  }
686
- }
687
- // type checking
688
- else if (
689
- rest[i] &&
690
- args[i][STATS] &&
691
- rest[i][TYPE] !== args[i][STATS].type
692
- ) {
693
- if (isLeaf(rest[i])) {
694
- const T =
695
- rest[i][TYPE] === WORD && env[rest[i][VALUE]]
696
- ? env[rest[i][VALUE]][STATS].type
697
- : rest[i][TYPE]
698
- if (
699
- (args[i][STATS].type !== UNKNOWN &&
700
- T === ATOM &&
701
- args[i][STATS].type !== ATOM) ||
702
- (env[rest[i][VALUE]] &&
703
- env[rest[i][VALUE]][STATS].type !== UNKNOWN &&
704
- args[i][STATS].type !== UNKNOWN &&
705
- env[rest[i][VALUE]][STATS].type !==
706
- args[i][STATS].type)
645
+ // type checking
646
+ else if (
647
+ rest[i] &&
648
+ args[i][STATS] &&
649
+ rest[i][TYPE] !== args[i][STATS].type
650
+ ) {
651
+ if (isLeaf(rest[i])) {
652
+ const T =
653
+ rest[i][TYPE] === WORD && env[rest[i][VALUE]]
654
+ ? env[rest[i][VALUE]][STATS].type
655
+ : rest[i][TYPE]
656
+ if (
657
+ (args[i][STATS].type !== UNKNOWN &&
658
+ T === ATOM &&
659
+ args[i][STATS].type !== ATOM) ||
660
+ (env[rest[i][VALUE]] &&
661
+ env[rest[i][VALUE]][STATS].type !== UNKNOWN &&
662
+ args[i][STATS].type !== UNKNOWN &&
663
+ env[rest[i][VALUE]][STATS].type !==
664
+ args[i][STATS].type)
665
+ ) {
666
+ errorStack.set(
667
+ key.str,
668
+ `Incorrect type of arguments ${i} for (${
669
+ first[VALUE]
670
+ }). Expected (${toTypeNames(
671
+ args[i][STATS].type
672
+ )}) but got (${toTypeNames(
673
+ T
674
+ )}) (${stringifyArgs(exp)})`
675
+ )
676
+ } else {
677
+ // env[rest[i][VALUE]][STATS] THiss SHOULD BE
678
+ const retry = env[rest[i][VALUE]]
679
+ if (
680
+ retry &&
681
+ !retry.retried &&
682
+ args[i][STATS].type === UNKNOWN
683
+ ) {
684
+ retry.retried = true
685
+ stack.unshift(() => check(exp, env, scope))
686
+ }
687
+ // console.log(
688
+ // first[VALUE],
689
+ // env[first[VALUE]][STATS],
690
+ // rest[i][TYPE],
691
+ // args[i][STATS].type
692
+ // )
693
+ }
694
+ } else if (
695
+ rest[i].length &&
696
+ SPECIAL_FORMS_SET.has(rest[i][0][VALUE]) &&
697
+ env[rest[i][0][VALUE]] &&
698
+ env[rest[i][0][VALUE]][STATS][RETURNS] !==
699
+ UNKNOWN &&
700
+ args[i][STATS].type !== UNKNOWN &&
701
+ env[rest[i][0][VALUE]][STATS][RETURNS] !==
702
+ args[i][STATS].type
707
703
  ) {
708
704
  errorStack.set(
709
705
  key.str,
@@ -711,58 +707,22 @@ export const typeCheck = (ast) => {
711
707
  first[VALUE]
712
708
  }). Expected (${toTypeNames(
713
709
  args[i][STATS].type
714
- )}) but got (${toTypeNames(T)}) (${stringifyArgs(
715
- exp
716
- )})`
710
+ )}) but got (${toTypeNames(
711
+ env[rest[i][0][VALUE]][STATS][RETURNS]
712
+ )}) (${stringifyArgs(exp)}) (check #4)`
717
713
  )
718
714
  } else {
719
- // env[rest[i][VALUE]][STATS] THiss SHOULD BE
720
- const retry = env[rest[i][VALUE]]
721
715
  if (
722
- retry &&
723
- !retry.retried &&
724
- args[i][STATS].type === UNKNOWN
716
+ rest[i].length &&
717
+ env[rest[i][0][VALUE]] &&
718
+ args[i][STATS].type === UNKNOWN &&
719
+ !env[rest[i][0][VALUE]].retried
725
720
  ) {
726
- retry.retried = true
721
+ env[rest[i][0][VALUE]].retried = true
722
+ if (!scope[SCOPE_NAME])
723
+ scope[SCOPE_NAME] = scope[1][VALUE]
727
724
  stack.unshift(() => check(exp, env, scope))
728
725
  }
729
- // console.log(
730
- // first[VALUE],
731
- // env[first[VALUE]][STATS],
732
- // rest[i][TYPE],
733
- // args[i][STATS].type
734
- // )
735
- }
736
- } else if (
737
- rest[i].length &&
738
- SPECIAL_FORMS_SET.has(rest[i][0][VALUE]) &&
739
- env[rest[i][0][VALUE]] &&
740
- env[rest[i][0][VALUE]][STATS][RETURNS] !== UNKNOWN &&
741
- args[i][STATS].type !== UNKNOWN &&
742
- env[rest[i][0][VALUE]][STATS][RETURNS] !==
743
- args[i][STATS].type
744
- ) {
745
- errorStack.set(
746
- key.str,
747
- `Incorrect type of arguments ${i} for (${
748
- first[VALUE]
749
- }). Expected (${toTypeNames(
750
- args[i][STATS].type
751
- )}) but got (${toTypeNames(
752
- env[rest[i][0][VALUE]][STATS][RETURNS]
753
- )}) (${stringifyArgs(exp)})`
754
- )
755
- } else {
756
- if (
757
- rest[i].length &&
758
- env[rest[i][0][VALUE]] &&
759
- args[i][STATS].type === UNKNOWN &&
760
- !env[rest[i][0][VALUE]].retried
761
- ) {
762
- env[rest[i][0][VALUE]].retried = true
763
- if (!scope[SCOPE_NAME])
764
- scope[SCOPE_NAME] = scope[1][VALUE]
765
- stack.unshift(() => check(exp, env, scope))
766
726
  }
767
727
  }
768
728
  }