fez-lisp 1.5.89 → 1.5.91

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 +216 -14
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.89",
5
+ "version": "1.5.91",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/check.js CHANGED
@@ -197,6 +197,8 @@ export const typeCheck = (ast) => {
197
197
  root[ORDER] = 0
198
198
  const errorStack = new Set()
199
199
  const warningStack = new Set()
200
+ // TODO delete this
201
+ const tempStack = new Set()
200
202
  const Types = new Map()
201
203
  const stack = []
202
204
  const check = (exp, env, scope) => {
@@ -619,16 +621,18 @@ export const typeCheck = (ast) => {
619
621
  )
620
622
  } else if (!env[first[VALUE]][STATS][ARG_COUNT]) {
621
623
  // TODO recursively take return type of applicaion
622
- if (env[first[VALUE]][STATS][RETURNS][0] === APPLY) {
623
- env[first[VALUE]][STATS][RETURNS] = [UNKNOWN]
624
- }
624
+ // if (env[first[VALUE]][STATS][RETURNS][0] === APPLY) {
625
+ // env[first[VALUE]][STATS][RETURNS] = [UNKNOWN]
626
+ // }
625
627
  // FN ASSIGMENT
628
+
629
+ // ASSIGMENT of paramaters of lambda that are a lambda
630
+ // minus one to remove the body
626
631
  env[first[VALUE]][STATS][TYPE_PROP] = [APPLY]
627
632
  env[first[VALUE]][STATS][ARG_COUNT] = rest.length
628
633
  env[first[VALUE]][STATS][ARGUMENTS] = fillUknownArgs(
629
634
  rest.length
630
635
  )
631
- // ASSIGMENT of paramaters of lambda that are a lambda
632
636
  for (let i = 0; i < rest.length; ++i) {
633
637
  const arg = env[first[VALUE]][STATS][ARGUMENTS]
634
638
  arg[i] = {
@@ -641,13 +645,23 @@ export const typeCheck = (ast) => {
641
645
  [ARG_COUNT]: 0
642
646
  }
643
647
  }
644
- switch (rest[i][TYPE]) {
645
- case ATOM:
646
- arg[i][STATS][TYPE_PROP][0] = ATOM
648
+ const ARG = isLeaf(rest[i]) ? rest[i] : rest[i][0]
649
+ if (!env[ARG[VALUE]]) continue
650
+ // console.log(stringifyArgs(exp), ARG)
651
+ switch (ARG[TYPE]) {
652
+ // THERE ARE NO ATOM ARGUMENTS
653
+ // case ATOM:
654
+ // break
655
+ case APPLY:
656
+ // passing arg asA APPLICATION
657
+ if (arg[i][STATS][TYPE_PROP][0] === UNKNOWN)
658
+ arg[i][STATS][TYPE_PROP] =
659
+ env[ARG[VALUE]][STATS][RETURNS]
647
660
  break
648
661
  case WORD:
649
- case APPLY:
650
- arg[i][STATS] = env[rest[i][VALUE]][STATS]
662
+ // if param is a word we assosiate them by referanc
663
+ if (arg[i][STATS][TYPE_PROP][0] === UNKNOWN)
664
+ arg[i][STATS] = env[ARG[VALUE]][STATS]
651
665
  break
652
666
  }
653
667
  }
@@ -661,16 +675,12 @@ export const typeCheck = (ast) => {
661
675
  // TODO get rof pred type
662
676
  // const PRED_TYPE = args[i][STATS][TYPE_PROP][1]
663
677
  const MAIN_TYPE = args[i][STATS][TYPE_PROP][0]
678
+
664
679
  if (first[TYPE] === APPLY && isSpecial) {
665
680
  if (
666
681
  MAIN_TYPE === ATOM &&
667
682
  PREDICATES_INPUT_SET.has(first[VALUE])
668
683
  ) {
669
- // if (isRestILeaf && rest[i][TYPE] === ATOM && rest[i][VALUE] !== TRUE && rest[i][TYPE] !== FALSE) throw some rror
670
- // else if (
671
- // isRestILeaf && est[i][TYPE] === ATOM && (rest[i][VALUE] === TRUE || rest[i][VALUE] === FALSE)
672
- // ) continue
673
- // else
674
684
  if (
675
685
  !isRestILeaf &&
676
686
  rest[i][0][TYPE] === APPLY &&
@@ -750,6 +760,28 @@ export const typeCheck = (ast) => {
750
760
  // env[CAR][STATS][RETURNS][1] !== PRED_TYPE
751
761
  // ) {
752
762
  // }
763
+ } else if (!isKnown && !isCast && env[CAR]) {
764
+ if (
765
+ env[CAR][STATS][TYPE_PROP][0] === APPLY &&
766
+ env[CAR][STATS][RETURNS][0] !== UNKNOWN
767
+ ) {
768
+ switch (first[VALUE]) {
769
+ case KEYWORDS.IF:
770
+ break
771
+ case KEYWORDS.CALL_FUNCTION:
772
+ break
773
+ default:
774
+ // console.log(stringifyArgs(exp))
775
+ // TODO fix this assigment
776
+ // It turns out it's not possible to determine return type of function here
777
+ // what if it's a global function used elsewhere where the return type mwould be different/
778
+ // env[CAR][STATS][RETURNS][0] =
779
+ // root[first[VALUE]][STATS][RETURNS][0]
780
+ break
781
+ }
782
+ }
783
+
784
+ // TODO also handle casting
753
785
  }
754
786
  } else {
755
787
  switch (rest[i][TYPE]) {
@@ -847,6 +879,79 @@ export const typeCheck = (ast) => {
847
879
  env[rest[i][VALUE]][STATS][ARG_COUNT]
848
880
  } (${stringifyArgs(exp)}) (check #778)`
849
881
  )
882
+ } else {
883
+ // DEFINED LAMBDAS TYPE CHECKING
884
+ // TODO delete this maybe
885
+ // It will not be possilbe to know return type
886
+ // const match1 = () => {
887
+ // const actual = env[rest[i][VALUE]]
888
+ // const expected = args[i]
889
+ // if (
890
+ // expected[STATS][RETURNS][0] !== UNKNOWN &&
891
+ // actual[STATS][RETURNS][0] !== UNKNOWN &&
892
+ // expected[STATS][RETURNS][0] !==
893
+ // actual[STATS][RETURNS][0]
894
+ // ) {
895
+ // errorStack.add(
896
+ // `Incorrect return type for (${
897
+ // expected[STATS][SIGNATURE]
898
+ // }) the (lambda) argument of (${
899
+ // first[VALUE]
900
+ // }) at position (${i}). Expected (${toTypeNames(
901
+ // expected[STATS][RETURNS][0]
902
+ // )}) but got (${toTypeNames(
903
+ // actual[STATS][RETURNS][0]
904
+ // )}) (${stringifyArgs(exp)}) (check #782)`
905
+ // )
906
+ // } else if (
907
+ // actual[STATS].retried < MAX_RETRY_DEFINITION
908
+ // ) {
909
+ // actual[STATS].retried += 1
910
+ // stack.unshift(() => match1())
911
+ // }
912
+ // }
913
+ // match1()
914
+ for (
915
+ let j = 0;
916
+ j < args[i][STATS][ARGUMENTS].length;
917
+ ++j
918
+ ) {
919
+ const match2 = () => {
920
+ const actual =
921
+ env[rest[i][VALUE]][STATS][ARGUMENTS][j]
922
+ const expected = args[i][STATS][ARGUMENTS][j]
923
+ if (
924
+ actual[STATS][TYPE_PROP][0] !== UNKNOWN &&
925
+ expected[STATS][TYPE_PROP][0] !== UNKNOWN &&
926
+ actual[STATS][TYPE_PROP][0] !==
927
+ expected[STATS][TYPE_PROP][0]
928
+ )
929
+ errorStack.add(
930
+ `Incorrect type for (lambda) (${
931
+ args[i][STATS][SIGNATURE]
932
+ }) argument at position (${j}) named as (${
933
+ env[rest[i][VALUE]][STATS][ARGUMENTS][j][
934
+ STATS
935
+ ][SIGNATURE]
936
+ }). Expected (${toTypeNames(
937
+ args[i][STATS][ARGUMENTS][j][STATS][
938
+ TYPE_PROP
939
+ ][0]
940
+ )}) but got (${toTypeNames(
941
+ env[rest[i][VALUE]][STATS][ARGUMENTS][j][
942
+ STATS
943
+ ][TYPE_PROP][0]
944
+ )}) (${stringifyArgs(exp)}) (check #781)`
945
+ )
946
+ else if (
947
+ actual[STATS].retried < MAX_RETRY_DEFINITION
948
+ ) {
949
+ actual[STATS].retried += 1
950
+ stack.unshift(() => match2())
951
+ }
952
+ }
953
+ match2()
954
+ }
850
955
  }
851
956
  }
852
957
  if (
@@ -927,6 +1032,103 @@ export const typeCheck = (ast) => {
927
1032
  exp
928
1033
  )}) (check #777)`
929
1034
  )
1035
+ } else {
1036
+ // ANONYMOUS LAMBDAS TYPE CHECKING
1037
+ const local = Object.create(env)
1038
+ const lambdaName = `lambda::annonymous::${i}::${performance
1039
+ .now()
1040
+ .toString()
1041
+ .replace('.', 0)}*`
1042
+ check(
1043
+ [
1044
+ [APPLY, KEYWORDS.DEFINE_VARIABLE],
1045
+ [WORD, lambdaName],
1046
+ rest[i]
1047
+ ],
1048
+ local,
1049
+ scope
1050
+ )
1051
+ // TODO delete this maybe
1052
+ // It will not be possilbe to know return type
1053
+ // const match1 = () => {
1054
+ // const actual = local[lambdaName]
1055
+ // const expected = args[i]
1056
+ // if (
1057
+ // expected[STATS][RETURNS][0] !==
1058
+ // UNKNOWN &&
1059
+ // actual[STATS][RETURNS][0] !== UNKNOWN &&
1060
+ // expected[STATS][RETURNS][0] !==
1061
+ // actual[STATS][RETURNS][0]
1062
+ // ) {
1063
+ // errorStack.add(
1064
+ // `Incorrect return type for (${
1065
+ // expected[STATS][SIGNATURE]
1066
+ // }) the (lambda) argument of (${
1067
+ // first[VALUE]
1068
+ // }) at position (${i}). Expected (${toTypeNames(
1069
+ // expected[STATS][RETURNS][0]
1070
+ // )}) but got (${toTypeNames(
1071
+ // actual[STATS][RETURNS][0]
1072
+ // )}) (${stringifyArgs(
1073
+ // exp
1074
+ // )}) (check #779)`
1075
+ // )
1076
+ // } else if (
1077
+ // actual[STATS].retried <
1078
+ // MAX_RETRY_DEFINITION
1079
+ // ) {
1080
+ // actual[STATS].retried += 1
1081
+ // stack.unshift(() => match1())
1082
+ // }
1083
+ // }
1084
+ // match1()
1085
+ for (
1086
+ let j = 0;
1087
+ j < args[i][STATS][ARGUMENTS].length;
1088
+ ++j
1089
+ ) {
1090
+ const match2 = () => {
1091
+ const actual =
1092
+ local[lambdaName][STATS][ARGUMENTS][j]
1093
+ const expected =
1094
+ args[i][STATS][ARGUMENTS][j]
1095
+ if (
1096
+ actual[STATS][TYPE_PROP][0] !==
1097
+ UNKNOWN &&
1098
+ expected[STATS][TYPE_PROP][0] !==
1099
+ UNKNOWN &&
1100
+ actual[STATS][TYPE_PROP][0] !==
1101
+ expected[STATS][TYPE_PROP][0]
1102
+ ) {
1103
+ errorStack.add(
1104
+ `Incorrect type for (lambda) (${
1105
+ args[i][STATS][SIGNATURE]
1106
+ }) argument at position (${j}) named as (${
1107
+ local[lambdaName][STATS][
1108
+ ARGUMENTS
1109
+ ][j][STATS][SIGNATURE]
1110
+ }). Expected (${toTypeNames(
1111
+ args[i][STATS][ARGUMENTS][j][
1112
+ STATS
1113
+ ][TYPE_PROP][0]
1114
+ )}) but got (${toTypeNames(
1115
+ local[lambdaName][STATS][
1116
+ ARGUMENTS
1117
+ ][j][STATS][TYPE_PROP][0]
1118
+ )}) (${stringifyArgs(
1119
+ exp
1120
+ )}) (check #780)`
1121
+ )
1122
+ } else if (
1123
+ actual[STATS].retried <
1124
+ MAX_RETRY_DEFINITION
1125
+ ) {
1126
+ actual[STATS].retried += 1
1127
+ stack.unshift(() => match2())
1128
+ }
1129
+ }
1130
+ match2()
1131
+ }
930
1132
  }
931
1133
  } else {
932
1134
  // TODO fix curry: lambdas enter here as undefined