fez-lisp 1.5.72 → 1.5.74

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/src/check.js CHANGED
@@ -32,6 +32,7 @@ import {
32
32
  PREDICATE,
33
33
  COLLECTION,
34
34
  MAX_RETRY_DEFINITION,
35
+ MAX_ARGUMENT_RETRY,
35
36
  ORDER,
36
37
  VARIABLE_ORDER_INDEX
37
38
  } from './types.js'
@@ -390,6 +391,7 @@ export const typeCheck = (ast) => {
390
391
  [STATS]: {
391
392
  [TYPE_PROP]: [APPLY],
392
393
  retried: 0,
394
+ counter: 0,
393
395
  [VARIABLE_ORDER_INDEX]: env[ORDER],
394
396
  [ARGS_COUNT]: n - 2,
395
397
  [ARGUMENTS]: [],
@@ -422,7 +424,9 @@ export const typeCheck = (ast) => {
422
424
  )
423
425
  }
424
426
  // FULL REFF ASSIGMENT
425
- env[name] = env[rest[1][VALUE]]
427
+ env[name] = SPECIAL_FORMS_SET.has(rest[1][VALUE])
428
+ ? structuredClone(env[rest[1][VALUE]])
429
+ : env[rest[1][VALUE]]
426
430
 
427
431
  if (
428
432
  getSuffix(rest[1][VALUE]) === PREDICATE_SUFFIX &&
@@ -451,6 +455,7 @@ export const typeCheck = (ast) => {
451
455
  env[name] = {
452
456
  [STATS]: {
453
457
  retried: 0,
458
+ counter: 0,
454
459
  [VARIABLE_ORDER_INDEX]: env[ORDER],
455
460
  [TYPE_PROP]: [ATOM],
456
461
  [RETURNS]: [ATOM]
@@ -485,6 +490,7 @@ export const typeCheck = (ast) => {
485
490
  env[name] = {
486
491
  [STATS]: {
487
492
  retried: 0,
493
+ counter: 0,
488
494
  [VARIABLE_ORDER_INDEX]: env[ORDER],
489
495
  [TYPE_PROP]: [
490
496
  isL
@@ -597,7 +603,8 @@ export const typeCheck = (ast) => {
597
603
  [TYPE_PROP]: [UNKNOWN],
598
604
  [RETURNS]: [UNKNOWN],
599
605
  [ARGUMENTS]: [],
600
- retried: 0
606
+ retried: 0,
607
+ counter: 0
601
608
  }
602
609
  }
603
610
  const ref = env[copy[SCOPE_NAME]]
@@ -678,484 +685,403 @@ export const typeCheck = (ast) => {
678
685
  errorStack.add(
679
686
  `Trying to call undefined (lambda) ${first[VALUE]} (check #9)`
680
687
  )
681
- else {
682
- if (
683
- env[first[VALUE]][STATS][TYPE_PROP][0] === APPLY &&
684
- env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC &&
685
- env[first[VALUE]][STATS][ARGS_COUNT] !== rest.length
686
- ) {
687
- errorStack.add(
688
- `Incorrect number of arguments for (${
689
- first[VALUE]
690
- }). Expected (= ${
691
- env[first[VALUE]][STATS][ARGS_COUNT]
692
- }) but got ${rest.length} (${stringifyArgs(
693
- exp
694
- )}) (check #15)`
695
- )
696
- } else {
697
- if (first[TYPE] === APPLY && !isSpecial) {
698
- if (env[first[VALUE]][STATS][TYPE_PROP][0] === ATOM) {
699
- errorStack.add(
700
- `(${first[VALUE]}) is not a (lambda) (${stringifyArgs(
701
- exp
702
- )}) (check #12)`
703
- )
704
- } else if (!env[first[VALUE]][STATS][ARGS_COUNT]) {
705
- // TODO recursively take return type of applicaion
706
- if (env[first[VALUE]][STATS][RETURNS][0] === APPLY) {
707
- env[first[VALUE]][STATS][RETURNS] = [UNKNOWN]
708
- }
709
- // FN ASSIGMENT
710
- env[first[VALUE]][STATS][TYPE_PROP] = [APPLY]
711
- env[first[VALUE]][STATS][ARGS_COUNT] = rest.length
688
+ else if (
689
+ env[first[VALUE]][STATS][TYPE_PROP][0] === APPLY &&
690
+ env[first[VALUE]][STATS][ARGS_COUNT] !== VARIADIC &&
691
+ env[first[VALUE]][STATS][ARGS_COUNT] !== rest.length
692
+ ) {
693
+ errorStack.add(
694
+ `Incorrect number of arguments for (${
695
+ first[VALUE]
696
+ }). Expected (= ${
697
+ env[first[VALUE]][STATS][ARGS_COUNT]
698
+ }) but got ${rest.length} (${stringifyArgs(exp)}) (check #15)`
699
+ )
700
+ } else {
701
+ if (first[TYPE] === APPLY && !isSpecial) {
702
+ if (env[first[VALUE]][STATS][TYPE_PROP][0] === ATOM) {
703
+ errorStack.add(
704
+ `(${first[VALUE]}) is not a (lambda) (${stringifyArgs(
705
+ exp
706
+ )}) (check #12)`
707
+ )
708
+ } else if (!env[first[VALUE]][STATS][ARGS_COUNT]) {
709
+ // TODO recursively take return type of applicaion
710
+ if (env[first[VALUE]][STATS][RETURNS][0] === APPLY) {
711
+ env[first[VALUE]][STATS][RETURNS] = [UNKNOWN]
712
712
  }
713
+ // FN ASSIGMENT
714
+ env[first[VALUE]][STATS][TYPE_PROP] = [APPLY]
715
+ env[first[VALUE]][STATS][ARGS_COUNT] = rest.length
713
716
  }
717
+ }
714
718
 
715
- // also type of arg
716
- const args = env[first[VALUE]][STATS][ARGUMENTS]
717
- if (args) {
718
- for (let i = 0; i < args.length; ++i) {
719
- // type check
720
- const PRED_TYPE = args[i][STATS][TYPE_PROP][1]
721
- const MAIN_TYPE = args[i][STATS][TYPE_PROP][0]
722
- if (PRED_TYPE != undefined && !isLeaf(rest[i])) {
723
- const current = rest[i][0]
724
- if (current[TYPE] === APPLY) {
725
- if (current[VALUE] == KEYWORDS.CALL_FUNCTION) {
726
- if (isLeaf(rest[i].at(-1))) {
727
- const fnName = rest[i].at(-1)[VALUE]
728
- const fn = env[fnName]
729
- if (fn && fn[STATS][RETURNS][0] !== MAIN_TYPE) {
730
- errorStack.add(
731
- `Incorrect type of argument (${i}) for (${
732
- first[VALUE]
733
- }). Expected (${toTypeNames(
734
- MAIN_TYPE
735
- )}) but got an (${toTypeNames(
736
- fn[STATS][RETURNS][0]
737
- )}) (${stringifyArgs(exp)}) (check #26)`
738
- )
739
- }
740
- if (fn && fn[STATS][RETURNS][1] !== PRED_TYPE) {
741
- errorStack.add(
742
- `Incorrect type of argument (${i}) for (${
743
- first[VALUE]
744
- }). Expected (${toTypeNames(
745
- PRED_TYPE
746
- )}) but got an (${toTypeNames(
747
- fn[STATS][RETURNS][1] ??
748
- fn[STATS][RETURNS][0]
749
- )}) which is neither ${TRUE} or ${FALSE} (${stringifyArgs(
750
- exp
751
- )}) (check #27)`
752
- )
753
- }
754
- } else {
755
- const body = rest[i].at(-1).at(-1)
756
- const rem = hasBlock(body) ? body.at(-1) : body
757
- const returns = isLeaf(rem) ? rem : rem[0]
758
- if (returns[TYPE] === ATOM) {
759
- if (MAIN_TYPE !== ATOM) {
760
- errorStack.add(
761
- `Incorrect type of argument ${i} for (${
762
- first[VALUE]
763
- }). Expected (${toTypeNames(
764
- MAIN_TYPE
765
- )}) but got an (${toTypeNames(
766
- ATOM
767
- )}) (${stringifyArgs(exp)}) (check #27)`
768
- )
769
- }
770
- if (
771
- PRED_TYPE &&
772
- PRED_TYPE === PREDICATE &&
773
- returns[VALUE] !== TRUE &&
774
- returns[VALUE] !== FALSE
775
- ) {
776
- errorStack.add(
777
- `Incorrect type of argument ${i} for (${
778
- first[VALUE]
779
- }). Expected (${toTypeNames(
780
- PRED_TYPE
781
- )}) but got an (${toTypeNames(
782
- ATOM
783
- )}) which is neither ${TRUE} or ${FALSE} (${stringifyArgs(
784
- exp
785
- )}) (check #27)`
786
- )
787
- }
788
- } else if (env[returns[VALUE]]) {
789
- if (
790
- MAIN_TYPE !==
791
- env[returns[VALUE]][STATS][RETURNS][0]
792
- ) {
793
- errorStack.add(
794
- `Incorrect type of argument ${i} for (${
795
- first[VALUE]
796
- }). Expected (${toTypeNames(
797
- MAIN_TYPE
798
- )}) but got (${toTypeNames(
799
- env[returns[VALUE]][STATS][TYPE_PROP]
800
- )}) (${stringifyArgs(exp)}) (check #29)`
801
- )
802
- }
803
- // Never happens because there is only 1 sub type at the moment
804
- // if (
805
- // PRED_TYPE &&
806
- // PRED_TYPE !==
807
- // env[returns[VALUE]][STATS][RETURNS][1]
808
- // ) {
809
- // }
810
- }
719
+ // also type of arg
720
+ const args = env[first[VALUE]][STATS][ARGUMENTS] ?? []
721
+ for (let i = 0; i < args.length; ++i) {
722
+ // type check
723
+ const PRED_TYPE = args[i][STATS][TYPE_PROP][1]
724
+ const MAIN_TYPE = args[i][STATS][TYPE_PROP][0]
725
+ if (PRED_TYPE != undefined && !isLeaf(rest[i])) {
726
+ const current = rest[i][0]
727
+ if (current[TYPE] === APPLY) {
728
+ if (current[VALUE] == KEYWORDS.CALL_FUNCTION) {
729
+ if (isLeaf(rest[i].at(-1))) {
730
+ const fnName = rest[i].at(-1)[VALUE]
731
+ const fn = env[fnName]
732
+ if (fn && fn[STATS][RETURNS][0] !== MAIN_TYPE) {
733
+ errorStack.add(
734
+ `Incorrect type of argument (${i}) for (${
735
+ first[VALUE]
736
+ }). Expected (${toTypeNames(
737
+ MAIN_TYPE
738
+ )}) but got an (${toTypeNames(
739
+ fn[STATS][RETURNS][0]
740
+ )}) (${stringifyArgs(exp)}) (check #26)`
741
+ )
742
+ }
743
+ if (fn && fn[STATS][RETURNS][1] !== PRED_TYPE) {
744
+ errorStack.add(
745
+ `Incorrect type of argument (${i}) for (${
746
+ first[VALUE]
747
+ }). Expected (${toTypeNames(
748
+ PRED_TYPE
749
+ )}) but got an (${toTypeNames(
750
+ fn[STATS][RETURNS][1] ?? fn[STATS][RETURNS][0]
751
+ )}) which is neither ${TRUE} or ${FALSE} (${stringifyArgs(
752
+ exp
753
+ )}) (check #27)`
754
+ )
755
+ }
756
+ } else {
757
+ const body = rest[i].at(-1).at(-1)
758
+ const rem = hasBlock(body) ? body.at(-1) : body
759
+ const returns = isLeaf(rem) ? rem : rem[0]
760
+ if (returns[TYPE] === ATOM) {
761
+ if (MAIN_TYPE !== ATOM) {
762
+ errorStack.add(
763
+ `Incorrect type of argument ${i} for (${
764
+ first[VALUE]
765
+ }). Expected (${toTypeNames(
766
+ MAIN_TYPE
767
+ )}) but got an (${toTypeNames(
768
+ ATOM
769
+ )}) (${stringifyArgs(exp)}) (check #27)`
770
+ )
811
771
  }
812
- } else if (
813
- PRED_TYPE &&
814
- env[current[VALUE]] &&
815
- env[current[VALUE]][STATS][RETURNS][1] !== PRED_TYPE
816
- ) {
817
772
  if (
818
- current[VALUE] === KEYWORDS.ANONYMOUS_FUNCTION
773
+ PRED_TYPE &&
774
+ PRED_TYPE === PREDICATE &&
775
+ returns[VALUE] !== TRUE &&
776
+ returns[VALUE] !== FALSE
819
777
  ) {
820
- const body = rest[i].at(-1)
821
- const rem = hasBlock(body) ? body.at(-1) : body
822
- const returns = isLeaf(rem) ? rem : rem[0]
823
- if (
824
- env[returns[VALUE]] &&
825
- root[returns[VALUE]][STATS][RETURNS][1] ===
826
- PREDICATE
827
- ) {
828
- // TODO invert this logic
829
- } else {
830
- errorStack.add(
831
- `Incorrect type of arguments (${i}) for (${
832
- first[VALUE]
833
- }). Expected (${toTypeNames(
834
- PRED_TYPE
835
- )}) but got (${toTypeNames(
836
- env[current[VALUE]][STATS][RETURNS][1] ??
837
- env[current[VALUE]][STATS][RETURNS][0]
838
- )}) (${stringifyArgs(exp)}) (check #21)`
839
- )
840
- }
778
+ errorStack.add(
779
+ `Incorrect type of argument ${i} for (${
780
+ first[VALUE]
781
+ }). Expected (${toTypeNames(
782
+ PRED_TYPE
783
+ )}) but got an (${toTypeNames(
784
+ ATOM
785
+ )}) which is neither ${TRUE} or ${FALSE} (${stringifyArgs(
786
+ exp
787
+ )}) (check #27)`
788
+ )
841
789
  }
790
+ } else if (env[returns[VALUE]]) {
791
+ if (
792
+ MAIN_TYPE !==
793
+ env[returns[VALUE]][STATS][RETURNS][0]
794
+ ) {
795
+ errorStack.add(
796
+ `Incorrect type of argument ${i} for (${
797
+ first[VALUE]
798
+ }). Expected (${toTypeNames(
799
+ MAIN_TYPE
800
+ )}) but got (${toTypeNames(
801
+ env[returns[VALUE]][STATS][TYPE_PROP]
802
+ )}) (${stringifyArgs(exp)}) (check #29)`
803
+ )
804
+ }
805
+ // Never happens because there is only 1 sub type at the moment
806
+ // if (
807
+ // PRED_TYPE &&
808
+ // PRED_TYPE !==
809
+ // env[returns[VALUE]][STATS][RETURNS][1]
810
+ // ) {
811
+ // }
842
812
  }
843
813
  }
844
- }
845
-
846
- // if (PRED_TYPE != undefined) {
847
- // if (isLeaf(rest[i])) {
848
- // // if (rest[i][TYPE] === WORD) {
849
- // // Never reached because there is only 1 subtype
850
- // // if (
851
- // // !isSpecial &&
852
- // // env[rest[i][VALUE]] &&
853
- // // PRED_TYPE !==
854
- // // env[rest[i][VALUE]][STATS][RETURNS][1]
855
- // // ) {
856
- // // }
857
- // // }
858
- // // never happens
859
- // // else if (rest[i][TYPE] === ATOM) {
860
- // // if (
861
- // // PRED_TYPE === PREDICATE &&
862
- // // rest[i][VALUE] !== TRUE &&
863
- // // rest[i][VALUE] !== FALSE
864
- // // ) {
865
- // // }
866
- // // }
867
- // } else {
868
- // // above code used to be ere
869
- // }
870
- // }
871
-
872
- if (first[TYPE] === APPLY && isSpecial) {
873
- const isCast = STATIC_TYPES_SET.has(first[VALUE])
874
- const expectedArgs = env[first[VALUE]][STATS][ARGUMENTS]
875
- for (let i = 0; i < rest.length; ++i) {
876
- const PRED_TYPE = args[i][STATS][TYPE_PROP][1]
877
- const MAIN_TYPE = expectedArgs[i][STATS][TYPE_PROP][0]
878
- if (MAIN_TYPE === UNKNOWN && !isCast) continue
879
- if (!isLeaf(rest[i])) {
880
- const CAR = rest[i][0][VALUE]
881
- const isKnown =
882
- env[CAR] &&
883
- env[CAR][STATS][RETURNS][0] !== UNKNOWN
884
- if (isKnown && !isCast) {
885
- if (env[CAR][STATS][RETURNS][0] !== MAIN_TYPE) {
886
- errorStack.add(
887
- `Incorrect type of argument (${i}) for special form (${
888
- first[VALUE]
889
- }). Expected (${toTypeNames(
890
- MAIN_TYPE
891
- )}) but got (${toTypeNames(
892
- env[CAR][STATS][RETURNS][0]
893
- )}) (${stringifyArgs(exp)}) (check #1)`
894
- )
895
- }
896
- // never reached because there is only 1 subtype at the moment
897
- // else if (
898
- // PRED_TYPE &&
899
- // env[CAR][STATS][RETURNS][1] !== PRED_TYPE
900
- // ) {
901
- // }
902
- }
814
+ } else if (
815
+ PRED_TYPE &&
816
+ env[current[VALUE]] &&
817
+ env[current[VALUE]][STATS][RETURNS][1] !== PRED_TYPE
818
+ ) {
819
+ if (current[VALUE] === KEYWORDS.ANONYMOUS_FUNCTION) {
820
+ const body = rest[i].at(-1)
821
+ const rem = hasBlock(body) ? body.at(-1) : body
822
+ const returns = isLeaf(rem) ? rem : rem[0]
823
+ if (
824
+ env[returns[VALUE]] &&
825
+ root[returns[VALUE]][STATS][RETURNS][1] ===
826
+ PREDICATE
827
+ ) {
828
+ // TODO invert this logic
903
829
  } else {
904
- switch (rest[i][TYPE]) {
905
- case WORD:
906
- {
907
- const CAR = rest[i][VALUE]
908
- const isKnown =
909
- env[CAR] &&
910
- env[CAR][STATS][TYPE_PROP][0] !== UNKNOWN
911
- if (isKnown && !isCast) {
912
- if (
913
- MAIN_TYPE !==
914
- env[CAR][STATS][TYPE_PROP][0]
915
- ) {
916
- errorStack.add(
917
- `Incorrect type of argument (${i}) for special form (${
918
- first[VALUE]
919
- }). Expected (${toTypeNames(
920
- MAIN_TYPE
921
- )}) but got (${toTypeNames(
922
- env[CAR][STATS][TYPE_PROP][0]
923
- )}) (${stringifyArgs(exp)}) (check #3)`
924
- )
925
- } else if (
926
- PRED_TYPE === PREDICATE &&
927
- env[CAR][STATS][RETURNS][1] !==
928
- PRED_TYPE &&
929
- !isCast
930
- )
931
- errorStack.add(
932
- `Incorrect type of argument (${i}) for special form (${
933
- first[VALUE]
934
- }). Expected (${toTypeNames(
935
- PRED_TYPE
936
- )}) but got (${toTypeNames(
937
- env[CAR][STATS][RETURNS][1] ??
938
- env[CAR][STATS][TYPE_PROP][0]
939
- )}) (${stringifyArgs(exp)}) (check #6)`
940
- )
941
- } else if (env[rest[i][VALUE]]) {
942
- if (isCast) {
943
- // CAST assigment
944
- env[rest[i][VALUE]][STATS][TYPE_PROP] =
945
- root[first[VALUE]][STATS][RETURNS]
946
- root[first[VALUE]][STATS][RETURNS] =
947
- root[first[VALUE]][STATS][RETURNS]
948
- } else {
949
- // VALUE assigment
950
- env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
951
- MAIN_TYPE
952
- }
953
- }
954
- }
955
- break
956
- case ATOM: {
957
- if (rest[i][TYPE] !== MAIN_TYPE) {
830
+ errorStack.add(
831
+ `Incorrect type of arguments (${i}) for (${
832
+ first[VALUE]
833
+ }). Expected (${toTypeNames(
834
+ PRED_TYPE
835
+ )}) but got (${toTypeNames(
836
+ env[current[VALUE]][STATS][RETURNS][1] ??
837
+ env[current[VALUE]][STATS][RETURNS][0]
838
+ )}) (${stringifyArgs(exp)}) (check #21)`
839
+ )
840
+ }
841
+ }
842
+ }
843
+ }
844
+ }
845
+ if (first[TYPE] === APPLY && isSpecial) {
846
+ const isCast = STATIC_TYPES_SET.has(first[VALUE])
847
+ const expectedArgs = env[first[VALUE]][STATS][ARGUMENTS]
848
+ for (let i = 0; i < rest.length; ++i) {
849
+ const PRED_TYPE = args[i][STATS][TYPE_PROP][1]
850
+ const MAIN_TYPE = expectedArgs[i][STATS][TYPE_PROP][0]
851
+ if (MAIN_TYPE === UNKNOWN && !isCast) continue
852
+ if (!isLeaf(rest[i])) {
853
+ const CAR = rest[i][0][VALUE]
854
+ const isKnown =
855
+ env[CAR] && env[CAR][STATS][RETURNS][0] !== UNKNOWN
856
+ if (isKnown && !isCast) {
857
+ if (env[CAR][STATS][RETURNS][0] !== MAIN_TYPE) {
858
+ errorStack.add(
859
+ `Incorrect type of argument (${i}) for special form (${
860
+ first[VALUE]
861
+ }). Expected (${toTypeNames(
862
+ MAIN_TYPE
863
+ )}) but got (${toTypeNames(
864
+ env[CAR][STATS][RETURNS][0]
865
+ )}) (${stringifyArgs(exp)}) (check #1)`
866
+ )
867
+ }
868
+ // never reached because there is only 1 subtype at the moment
869
+ // else if (
870
+ // PRED_TYPE &&
871
+ // env[CAR][STATS][RETURNS][1] !== PRED_TYPE
872
+ // ) {
873
+ // }
874
+ }
875
+ } else {
876
+ switch (rest[i][TYPE]) {
877
+ case WORD:
878
+ {
879
+ const CAR = rest[i][VALUE]
880
+ const isKnown =
881
+ env[CAR] &&
882
+ env[CAR][STATS][TYPE_PROP][0] !== UNKNOWN
883
+ if (isKnown && !isCast) {
884
+ if (
885
+ MAIN_TYPE !== env[CAR][STATS][TYPE_PROP][0]
886
+ ) {
958
887
  errorStack.add(
959
888
  `Incorrect type of argument (${i}) for special form (${
960
889
  first[VALUE]
961
890
  }). Expected (${toTypeNames(
962
891
  MAIN_TYPE
963
892
  )}) but got (${toTypeNames(
964
- rest[i][TYPE]
965
- )}) (${stringifyArgs(exp)}) (check #2)`
893
+ env[CAR][STATS][TYPE_PROP][0]
894
+ )}) (${stringifyArgs(exp)}) (check #3)`
966
895
  )
967
- }
968
- if (
896
+ } else if (
969
897
  PRED_TYPE === PREDICATE &&
970
- rest[i][VALUE] !== TRUE &&
971
- rest[i][VALUE] !== FALSE
972
- ) {
898
+ env[CAR][STATS][RETURNS][1] !== PRED_TYPE &&
899
+ !isCast
900
+ )
973
901
  errorStack.add(
974
902
  `Incorrect type of argument (${i}) for special form (${
975
903
  first[VALUE]
976
904
  }). Expected (${toTypeNames(
977
905
  PRED_TYPE
978
906
  )}) but got (${toTypeNames(
979
- rest[i][VALUE]
980
- )}) (${stringifyArgs(exp)}) (check #5)`
907
+ env[CAR][STATS][RETURNS][1] ??
908
+ env[CAR][STATS][TYPE_PROP][0]
909
+ )}) (${stringifyArgs(exp)}) (check #6)`
981
910
  )
911
+ } else if (env[rest[i][VALUE]]) {
912
+ if (isCast) {
913
+ // CAST assigment
914
+ env[rest[i][VALUE]][STATS][TYPE_PROP] =
915
+ root[first[VALUE]][STATS][RETURNS]
916
+ root[first[VALUE]][STATS][RETURNS] =
917
+ root[first[VALUE]][STATS][RETURNS]
918
+ } else {
919
+ // VALUE assigment
920
+ env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
921
+ MAIN_TYPE
982
922
  }
983
-
984
- break
985
923
  }
986
924
  }
987
- }
988
- }
989
- }
990
- // type checking
991
- else if (
992
- rest[i] &&
993
- args[i][STATS] &&
994
- rest[i][TYPE] !== args[i][STATS][TYPE_PROP][0]
995
- ) {
996
- if (isLeaf(rest[i])) {
997
- const T =
998
- rest[i][TYPE] === WORD && env[rest[i][VALUE]]
999
- ? env[rest[i][VALUE]][STATS][TYPE_PROP][0]
1000
- : rest[i][TYPE]
1001
- if (T === ATOM) {
1002
- if (
1003
- args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
1004
- args[i][STATS][TYPE_PROP][0] !== ATOM
1005
- ) {
925
+ break
926
+ case ATOM: {
927
+ if (rest[i][TYPE] !== MAIN_TYPE) {
1006
928
  errorStack.add(
1007
- `Incorrect type of arguments ${i} for (${
929
+ `Incorrect type of argument (${i}) for special form (${
1008
930
  first[VALUE]
1009
931
  }). Expected (${toTypeNames(
1010
- args[i][STATS][TYPE_PROP][0]
932
+ MAIN_TYPE
1011
933
  )}) but got (${toTypeNames(
1012
- T
1013
- )}) (${stringifyArgs(exp)}) (check #10)`
934
+ rest[i][TYPE]
935
+ )}) (${stringifyArgs(exp)}) (check #2)`
1014
936
  )
1015
- } else if (
1016
- args[i][STATS][RETURNS][0] === ATOM &&
1017
- args[i][STATS][RETURNS][1] === PREDICATE &&
937
+ }
938
+ if (
939
+ PRED_TYPE === PREDICATE &&
1018
940
  rest[i][VALUE] !== TRUE &&
1019
941
  rest[i][VALUE] !== FALSE
1020
942
  ) {
1021
943
  errorStack.add(
1022
- `Incorrect type of arguments ${i} for (${
944
+ `Incorrect type of argument (${i}) for special form (${
1023
945
  first[VALUE]
1024
946
  }). Expected (${toTypeNames(
1025
- args[i][STATS][RETURNS][1]
947
+ PRED_TYPE
1026
948
  )}) but got (${toTypeNames(
1027
- T
1028
- )}) (${stringifyArgs(exp)}) (check #13)`
949
+ rest[i][VALUE]
950
+ )}) (${stringifyArgs(exp)}) (check #5)`
1029
951
  )
1030
952
  }
1031
- }
1032
953
 
1033
- if (
1034
- env[rest[i][VALUE]] &&
1035
- env[rest[i][VALUE]][STATS][TYPE_PROP][0] !==
1036
- UNKNOWN &&
1037
- args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
1038
- env[rest[i][VALUE]][STATS][TYPE_PROP][0] !==
1039
- args[i][STATS][TYPE_PROP][0]
1040
- ) {
1041
- errorStack.add(
1042
- `Incorrect type of arguments ${i} for (${
1043
- first[VALUE]
1044
- }). Expected (${toTypeNames(
1045
- args[i][STATS][TYPE_PROP][0]
1046
- )}) but got (${toTypeNames(T)}) (${stringifyArgs(
1047
- exp
1048
- )}) (check #30)`
1049
- )
1050
- } else {
1051
- // const retry = env[rest[i][VALUE]]
1052
- // if (
1053
- // retry &&
1054
- // retry[STATS].retried < RETRY_COUNT &&
1055
- // args[i][STATS][TYPE_PROP][0] === UNKNOWN
1056
- // ) {
1057
- // retry[STATS].retried += 1
1058
- // stack.unshift(() => check(exp, env, scope))
1059
- // } else
1060
- if (
1061
- args[i][STATS][TYPE_PROP][0] === UNKNOWN &&
1062
- args[i][STATS].retried < MAX_RETRY_DEFINITION
1063
- ) {
1064
- args[i][STATS].retried += 1
1065
- stack.unshift(() => check(exp, env, scope))
1066
- } else {
1067
- if (
1068
- env[rest[i][VALUE]] &&
1069
- args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
1070
- env[rest[i][VALUE]][STATS][TYPE_PROP][0] ===
1071
- UNKNOWN &&
1072
- args[i][STATS][TYPE_PROP][0] !== APPLY
1073
- ) {
1074
- // REFF ASSIGMENT
1075
- env[rest[i][VALUE]][STATS][TYPE_PROP] =
1076
- args[i][STATS][TYPE_PROP]
1077
- env[rest[i][VALUE]][STATS][RETURNS] =
1078
- args[i][STATS][RETURNS]
1079
- }
1080
- }
1081
- }
1082
- } else if (rest[i].length) {
1083
- // TODO figure out what cann we do in this else ?
1084
- // Check arg types
1085
- // errorStack.add(
1086
- // `Incorrect type of arguments ${i} for (${
1087
- // first[VALUE]
1088
- // }). Expected (${toTypeNames(
1089
- // args[i][STATS][TYPE_PROP][0]
1090
- // )}) but got (${toTypeNames(
1091
- // env[rest[i][0][VALUE]][STATS][RETURNS][0]
1092
- // )}) (${stringifyArgs(exp)}) (check #4)`
1093
- // )
1094
- // TODO figure out if we need this
1095
- if (
1096
- SPECIAL_FORMS_SET.has(rest[i][0][VALUE]) &&
1097
- env[rest[i][0][VALUE]] &&
1098
- env[rest[i][0][VALUE]][STATS][RETURNS][0] !==
1099
- UNKNOWN &&
1100
- args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
1101
- env[rest[i][0][VALUE]][STATS][RETURNS][0] !==
1102
- args[i][STATS][TYPE_PROP][0]
1103
- )
1104
- errorStack.add(
1105
- `Incorrect type of arguments ${i} for (${
1106
- first[VALUE]
1107
- }). Expected (${toTypeNames(
1108
- args[i][STATS][TYPE_PROP][0]
1109
- )}) but got (${toTypeNames(
1110
- env[rest[i][0][VALUE]][STATS][RETURNS][0]
1111
- )}) (${stringifyArgs(exp)}) (check #4)`
1112
- )
1113
- else if (
1114
- rest[i][0][TYPE] === APPLY &&
1115
- env[rest[i][0][VALUE]]
1116
- ) {
1117
- const actual =
1118
- env[rest[i][0][VALUE]][STATS][RETURNS]
1119
- const expected = args[i][STATS][TYPE_PROP]
1120
- if (
1121
- expected[0] !== UNKNOWN &&
1122
- actual[0] !== UNKNOWN
1123
- ) {
1124
- if (expected[0] !== actual[0])
1125
- errorStack.add(
1126
- `Incorrect type of arguments ${i} for (${
1127
- first[VALUE]
1128
- }). Expected (${toTypeNames(
1129
- expected[0]
1130
- )}) but got (${toTypeNames(
1131
- actual[0]
1132
- )}) (${stringifyArgs(exp)}) (check #16)`
1133
- )
1134
- } else {
1135
- if (
1136
- args[i][STATS].retried < MAX_RETRY_DEFINITION
1137
- ) {
1138
- args[i][STATS].retried += 1
1139
- stack.unshift(() => check(exp, env, scope))
1140
- }
1141
- }
954
+ break
1142
955
  }
1143
956
  }
1144
957
  }
1145
958
  }
1146
959
  }
960
+ // type checking
961
+ else if (isLeaf(rest[i])) {
962
+ const T =
963
+ rest[i][TYPE] === WORD && env[rest[i][VALUE]]
964
+ ? env[rest[i][VALUE]][STATS][TYPE_PROP][0]
965
+ : rest[i][TYPE]
966
+ if (
967
+ T === ATOM &&
968
+ args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
969
+ args[i][STATS][TYPE_PROP][0] !== ATOM
970
+ ) {
971
+ errorStack.add(
972
+ `Incorrect type of arguments ${i} for (${
973
+ first[VALUE]
974
+ }). Expected (${toTypeNames(
975
+ args[i][STATS][TYPE_PROP][0]
976
+ )}) but got (${toTypeNames(T)}) (${stringifyArgs(
977
+ exp
978
+ )}) (check #10)`
979
+ )
980
+ } else if (
981
+ T === ATOM &&
982
+ args[i][STATS][RETURNS][0] === ATOM &&
983
+ args[i][STATS][RETURNS][1] === PREDICATE &&
984
+ rest[i][VALUE] !== TRUE &&
985
+ rest[i][VALUE] !== FALSE
986
+ ) {
987
+ errorStack.add(
988
+ `Incorrect type of arguments ${i} for (${
989
+ first[VALUE]
990
+ }). Expected (${toTypeNames(
991
+ args[i][STATS][RETURNS][1]
992
+ )}) but got (${toTypeNames(T)}) (${stringifyArgs(
993
+ exp
994
+ )}) (check #13)`
995
+ )
996
+ }
997
+
998
+ if (
999
+ env[rest[i][VALUE]] &&
1000
+ env[rest[i][VALUE]][STATS][TYPE_PROP][0] !== UNKNOWN &&
1001
+ args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
1002
+ env[rest[i][VALUE]][STATS][TYPE_PROP][0] !==
1003
+ args[i][STATS][TYPE_PROP][0]
1004
+ ) {
1005
+ errorStack.add(
1006
+ `Incorrect type of arguments ${i} for (${
1007
+ first[VALUE]
1008
+ }). Expected (${toTypeNames(
1009
+ args[i][STATS][TYPE_PROP][0]
1010
+ )}) but got (${toTypeNames(T)}) (${stringifyArgs(
1011
+ exp
1012
+ )}) (check #30)`
1013
+ )
1014
+ } else if (
1015
+ args[i][STATS][TYPE_PROP][0] === UNKNOWN &&
1016
+ args[i][STATS].retried < MAX_RETRY_DEFINITION
1017
+ ) {
1018
+ args[i][STATS].retried += 1
1019
+ stack.unshift(() => check(exp, env, scope))
1020
+ } else {
1021
+ if (
1022
+ env[rest[i][VALUE]] &&
1023
+ args[i][STATS][TYPE_PROP][0] !== UNKNOWN &&
1024
+ env[rest[i][VALUE]][STATS][TYPE_PROP][0] === UNKNOWN &&
1025
+ args[i][STATS][TYPE_PROP][0] !== APPLY
1026
+ ) {
1027
+ // REFF ASSIGMENT
1028
+ env[rest[i][VALUE]][STATS][TYPE_PROP] =
1029
+ args[i][STATS][TYPE_PROP]
1030
+ env[rest[i][VALUE]][STATS][RETURNS] =
1031
+ args[i][STATS][RETURNS]
1032
+ }
1033
+ }
1034
+ } else if (
1035
+ rest[i].length &&
1036
+ rest[i][0][TYPE] === APPLY &&
1037
+ env[rest[i][0][VALUE]]
1038
+ ) {
1039
+ const match = () => {
1040
+ const actual = env[rest[i][0][VALUE]][STATS][RETURNS]
1041
+ const expected = args[i][STATS][TYPE_PROP]
1042
+ if (args[i][STATS].counter < MAX_ARGUMENT_RETRY) {
1043
+ args[i][STATS].counter++
1044
+ stack.unshift(() => match())
1045
+ }
1046
+ if (expected[0] !== UNKNOWN && actual[0] !== UNKNOWN) {
1047
+ if (expected[0] !== actual[0])
1048
+ errorStack.add(
1049
+ `Incorrect type of arguments ${i} for (${
1050
+ first[VALUE]
1051
+ }). Expected (${toTypeNames(
1052
+ expected[0]
1053
+ )}) but got (${toTypeNames(
1054
+ actual[0]
1055
+ )}) (${stringifyArgs(exp)}) (check #16)`
1056
+ )
1057
+ } else if (
1058
+ expected[0] === UNKNOWN &&
1059
+ args[i][STATS].retried < MAX_RETRY_DEFINITION
1060
+ ) {
1061
+ args[i][STATS].retried += 1
1062
+ stack.unshift(() => match())
1063
+ }
1064
+ }
1065
+
1066
+ match()
1067
+ }
1147
1068
  }
1148
1069
  }
1149
1070
  })
1150
1071
  for (let i = 0; i < rest.length; ++i) {
1151
1072
  const r = rest[i]
1152
- if (isLeaf(r) && r[TYPE] !== ATOM && env[r[VALUE]] == undefined) {
1153
- errorStack.add(
1154
- `(${first[VALUE]}) is trying to access undefined variable (${
1155
- r[VALUE]
1156
- }) at argument (${i}) (${stringifyArgs(exp)}) (check #20)`
1157
- )
1073
+ if (isLeaf(r) && r[TYPE] !== ATOM) {
1074
+ if (env[r[VALUE]] == undefined) {
1075
+ errorStack.add(
1076
+ `(${
1077
+ first[VALUE]
1078
+ }) is trying to access undefined variable (${
1079
+ r[VALUE]
1080
+ }) at argument (${i}) (${stringifyArgs(exp)}) (check #20)`
1081
+ )
1082
+ }
1158
1083
  }
1084
+
1159
1085
  check(r, env, scope)
1160
1086
  }
1161
1087
  break