fez-lisp 1.5.54 → 1.5.57

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.54",
5
+ "version": "1.5.57",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/check.js CHANGED
@@ -8,6 +8,8 @@ import {
8
8
  PLACEHOLDER,
9
9
  PREDICATE_SUFFIX,
10
10
  SPECIAL_FORMS_SET,
11
+ STATIC_TYPES,
12
+ STATIC_TYPES_SET,
11
13
  TRUE,
12
14
  TYPE,
13
15
  VALUE,
@@ -37,7 +39,7 @@ const DEFINITON_RETRY_COUNT = 1
37
39
  const toTypeNames = (type) => {
38
40
  switch (type) {
39
41
  case APPLY:
40
- return 'Application'
42
+ return 'Abstraction'
41
43
  case ATOM:
42
44
  return 'Atom'
43
45
  case UNKNOWN:
@@ -79,12 +81,20 @@ const formatType = (name, env) => {
79
81
  ? `${name} (${(stats[ARGUMENTS] ?? [])
80
82
  .map(
81
83
  (x) =>
82
- `${x[STATS][SIGNATURE]} ${toTypeNames(x[STATS][TYPE_PROP][0])}`
84
+ `${x[STATS][SIGNATURE]} ${x[STATS][TYPE_PROP].map(
85
+ toTypeNames
86
+ ).join(' ')}${
87
+ x[STATS][TYPE_PROP][0] === APPLY
88
+ ? ` ${toTypeNames(
89
+ x[STATS][RETURNS][1] ?? x[STATS][RETURNS][0]
90
+ )}`
91
+ : ''
92
+ }`
83
93
  )
84
94
  .join(' ')}) -> ${toTypeNames(
85
95
  stats[RETURNS][1] ?? stats[RETURNS][0]
86
96
  )}`
87
- : `${name} ${toTypeNames(stats[TYPE_PROP][0])}`
97
+ : `${name} ${stats[TYPE_PROP].map(toTypeNames).join(' ')}`
88
98
  : name
89
99
  }
90
100
  const formatTypes = (env) => {
@@ -107,7 +117,9 @@ const getScopeNames = (scope) => {
107
117
  }
108
118
  const withScope = (name, scope) => {
109
119
  const chain = getScopeNames(scope)
110
- return `${chain.join(' ')} ${name}`
120
+ return `${chain
121
+ .map((x) => (Number.isInteger(+x) ? '~' : x))
122
+ .join(' ')} ${name}`
111
123
  }
112
124
  export const typeCheck = (ast) => {
113
125
  const root = {
@@ -122,8 +134,8 @@ export const typeCheck = (ast) => {
122
134
  [STATS]: {
123
135
  retried: RETRY_COUNT,
124
136
  [SIGNATURE]: PLACEHOLDER,
125
- [TYPE_PROP]: [APPLY],
126
- [RETURNS]: [APPLY],
137
+ [TYPE_PROP]: [UNKNOWN],
138
+ [RETURNS]: [UNKNOWN],
127
139
  [ARGS_COUNT]: [],
128
140
  [ARGUMENTS]: [],
129
141
  [ARGS_COUNT]: 0
@@ -144,8 +156,8 @@ export const typeCheck = (ast) => {
144
156
  [STATS]: {
145
157
  retried: RETRY_COUNT,
146
158
  [SIGNATURE]: PLACEHOLDER,
147
- [TYPE_PROP]: [ATOM],
148
- [RETURNS]: [ATOM],
159
+ [TYPE_PROP]: [UNKNOWN],
160
+ [RETURNS]: [UNKNOWN],
149
161
  [ARGS_COUNT]: [],
150
162
  [ARGUMENTS]: [],
151
163
  [ARGS_COUNT]: 0
@@ -166,8 +178,8 @@ export const typeCheck = (ast) => {
166
178
  [STATS]: {
167
179
  retried: RETRY_COUNT,
168
180
  [SIGNATURE]: PLACEHOLDER,
169
- [TYPE_PROP]: [APPLY, PREDICATE],
170
- [RETURNS]: [APPLY, PREDICATE],
181
+ [TYPE_PROP]: [UNKNOWN],
182
+ [RETURNS]: [UNKNOWN],
171
183
  [ARGS_COUNT]: [],
172
184
  [ARGUMENTS]: [],
173
185
  [ARGS_COUNT]: 0
@@ -188,8 +200,8 @@ export const typeCheck = (ast) => {
188
200
  [STATS]: {
189
201
  retried: RETRY_COUNT,
190
202
  [SIGNATURE]: PLACEHOLDER,
191
- [TYPE_PROP]: [COLLECTION],
192
- [RETURNS]: [COLLECTION],
203
+ [TYPE_PROP]: [UNKNOWN],
204
+ [RETURNS]: [UNKNOWN],
193
205
  [ARGS_COUNT]: [],
194
206
  [ARGUMENTS]: [],
195
207
  [ARGS_COUNT]: 0
@@ -1233,7 +1245,7 @@ export const typeCheck = (ast) => {
1233
1245
  const [first, ...rest] = isLeaf(exp) ? [exp] : exp
1234
1246
  if (first === undefined) {
1235
1247
  throw new TypeError(
1236
- `(lambda) invocation with missing (Application) name () Provide an (Application) name as the (1) argument.`
1248
+ `(lambda) invocation with missing (Abstraction) name () Provide an (Abstraction) name as the (1) argument.`
1237
1249
  )
1238
1250
  }
1239
1251
  switch (first[TYPE]) {
@@ -1244,6 +1256,32 @@ export const typeCheck = (ast) => {
1244
1256
  errorStack.add(
1245
1257
  `Trying to access undefined variable ${first[VALUE]} (check #11)`
1246
1258
  )
1259
+ } else {
1260
+ const T = env[first[VALUE]][STATS]
1261
+ const isKnown = T[TYPE_PROP][0] !== UNKNOWN
1262
+ switch (first[VALUE]) {
1263
+ case 'xs':
1264
+ if (isKnown && T[TYPE_PROP][0] !== COLLECTION) {
1265
+ warningStack.add(
1266
+ `A variable named xs must be of type (${
1267
+ STATIC_TYPES.COLLECTION
1268
+ }) but got type (${toTypeNames(
1269
+ T[TYPE_PROP][0]
1270
+ )}) (check #32)`
1271
+ )
1272
+ } else T[TYPE_PROP] = [COLLECTION]
1273
+ break
1274
+ default:
1275
+ {
1276
+ const isPredicate =
1277
+ getSuffix(first[VALUE]) === PREDICATE_SUFFIX
1278
+ if (isPredicate) {
1279
+ if (isKnown) T[TYPE_PROP][1] = PREDICATE
1280
+ T[RETURNS] = [ATOM, PREDICATE]
1281
+ }
1282
+ }
1283
+ break
1284
+ }
1247
1285
  }
1248
1286
  })
1249
1287
  }
@@ -1742,6 +1780,7 @@ export const typeCheck = (ast) => {
1742
1780
  if (isLeaf(rest[i])) {
1743
1781
  if (rest[i][TYPE] === WORD) {
1744
1782
  if (
1783
+ !isSpecial &&
1745
1784
  env[rest[i][VALUE]] &&
1746
1785
  PRED_TYPE !==
1747
1786
  env[rest[i][VALUE]][STATS][RETURNS][1]
@@ -1896,17 +1935,18 @@ export const typeCheck = (ast) => {
1896
1935
  }
1897
1936
 
1898
1937
  if (first[TYPE] === APPLY && isSpecial) {
1938
+ const isCast = STATIC_TYPES_SET.has(first[VALUE])
1899
1939
  const expectedArgs = env[first[VALUE]][STATS][ARGUMENTS]
1900
1940
  for (let i = 0; i < rest.length; ++i) {
1901
1941
  const PRED_TYPE = args[i][STATS][TYPE_PROP][1]
1902
1942
  const MAIN_TYPE = expectedArgs[i][STATS][TYPE_PROP][0]
1903
- if (MAIN_TYPE === UNKNOWN) continue
1943
+ if (MAIN_TYPE === UNKNOWN && !isCast) continue
1904
1944
  if (!isLeaf(rest[i])) {
1905
1945
  const CAR = rest[i][0][VALUE]
1906
1946
  const isKnown =
1907
1947
  env[CAR] &&
1908
1948
  env[CAR][STATS][RETURNS][0] !== UNKNOWN
1909
- if (isKnown) {
1949
+ if (isKnown && !isCast) {
1910
1950
  if (env[CAR][STATS][RETURNS][0] !== MAIN_TYPE) {
1911
1951
  errorStack.add(
1912
1952
  `Incorrect type of argument (${i}) for special form (${
@@ -1941,7 +1981,7 @@ export const typeCheck = (ast) => {
1941
1981
  const isKnown =
1942
1982
  env[CAR] &&
1943
1983
  env[CAR][STATS][TYPE_PROP][0] !== UNKNOWN
1944
- if (isKnown) {
1984
+ if (isKnown && !isCast) {
1945
1985
  if (
1946
1986
  MAIN_TYPE !==
1947
1987
  env[CAR][STATS][TYPE_PROP][0]
@@ -1957,7 +1997,9 @@ export const typeCheck = (ast) => {
1957
1997
  )
1958
1998
  } else if (
1959
1999
  PRED_TYPE &&
1960
- env[CAR][STATS][RETURNS][1] !== PRED_TYPE
2000
+ env[CAR][STATS][RETURNS][1] !==
2001
+ PRED_TYPE &&
2002
+ !isCast
1961
2003
  )
1962
2004
  errorStack.add(
1963
2005
  `Incorrect type of argument (${i}) for special form (${
@@ -1970,8 +2012,15 @@ export const typeCheck = (ast) => {
1970
2012
  )}) (${stringifyArgs(exp)}) (check #6)`
1971
2013
  )
1972
2014
  } else if (env[rest[i][VALUE]]) {
1973
- env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
1974
- MAIN_TYPE
2015
+ if (isCast) {
2016
+ env[rest[i][VALUE]][STATS][TYPE_PROP] =
2017
+ root[first[VALUE]][STATS][RETURNS]
2018
+ root[first[VALUE]][STATS][RETURNS] =
2019
+ root[first[VALUE]][STATS][RETURNS]
2020
+ } else {
2021
+ env[rest[i][VALUE]][STATS][TYPE_PROP][0] =
2022
+ MAIN_TYPE
2023
+ }
1975
2024
  }
1976
2025
  }
1977
2026
  break
package/src/compiler.js CHANGED
@@ -264,7 +264,7 @@ const comp = (tree, Drill) => {
264
264
  return `__error(${compile(tail[0], Drill)})`
265
265
  }
266
266
 
267
- case STATIC_TYPES.APPLICATION:
267
+ case STATIC_TYPES.ABSTRACTION:
268
268
  case STATIC_TYPES.COLLECTION:
269
269
  case STATIC_TYPES.UNKNOWN:
270
270
  case STATIC_TYPES.ATOM:
@@ -833,7 +833,7 @@ export const keywords = {
833
833
  throw new Error(expression.map((x) => String.fromCharCode(x)).join(''))
834
834
  },
835
835
 
836
- [STATIC_TYPES.APPLICATION]: (args, env) => evaluate(args[0], env),
836
+ [STATIC_TYPES.ABSTRACTION]: (args, env) => evaluate(args[0], env),
837
837
  [STATIC_TYPES.ATOM]: (args, env) => evaluate(args[0], env),
838
838
  [STATIC_TYPES.COLLECTION]: (args, env) => evaluate(args[0], env),
839
839
  [STATIC_TYPES.PREDICATE]: (args, env) => evaluate(args[0], env),
package/src/keywords.js CHANGED
@@ -9,12 +9,13 @@ export const PLACEHOLDER = '.'
9
9
  export const MUTATION_SUFFIX = '!'
10
10
  export const PREDICATE_SUFFIX = '?'
11
11
  export const STATIC_TYPES = {
12
- APPLICATION: 'Application',
12
+ ABSTRACTION: 'Abstraction',
13
13
  UNKNOWN: 'Unknown',
14
14
  ATOM: 'Atom',
15
15
  COLLECTION: 'Collection',
16
16
  PREDICATE: 'Predicate'
17
17
  }
18
+ export const STATIC_TYPES_SET = new Set(Object.values(STATIC_TYPES))
18
19
  export const KEYWORDS = {
19
20
  LOOP: 'loop',
20
21
  CREATE_ARRAY: 'array',
@@ -70,7 +71,7 @@ export const DEBUG = {
70
71
  SIGNATURE: '?',
71
72
  LIST_THEMES: 'theme?',
72
73
  SET_THEME: 'theme!',
73
- TYPE_SIGNATURE: 'type?'
74
+ TYPE_SIGNATURE: 't?'
74
75
  }
75
76
 
76
77
  export const SPECIAL_FORMS_SET = new Set(
package/src/macros.js CHANGED
@@ -863,6 +863,11 @@ const iron = (scope) => {
863
863
  for (let i = 0; i < copy.length; ++i) scope[i] = copy[i]
864
864
  }
865
865
  }
866
+ export const seeIfProgramIsInvalid = (source) => {
867
+ if (source.includes('()') || source.includes(',') || source.includes(';'))
868
+ throw new SyntaxError(`Failed to parse due to invalid lisp programm near`)
869
+ return source
870
+ }
866
871
  export const replaceQuotes = (source) =>
867
872
  source
868
873
  .replaceAll(/\[/g, `(${KEYWORDS.CREATE_ARRAY} `)
package/src/parser.js CHANGED
@@ -11,10 +11,6 @@ export const LISP = {
11
11
  const cursor = source[i]
12
12
  if (cursor === '(') {
13
13
  const temp = []
14
- if (head == undefined)
15
- throw new SyntaxError(
16
- `Failed to parse due to invalid lisp programm near ${acc}`
17
- )
18
14
  head.push(temp)
19
15
  stack.push(head)
20
16
  head = temp
package/src/utils.js CHANGED
@@ -321,7 +321,7 @@ export const addTypeIdentities = (ast) => {
321
321
  const temp = block.shift()
322
322
  block.unshift(
323
323
  temp,
324
- identity(STATIC_TYPES.APPLICATION),
324
+ identity(STATIC_TYPES.ABSTRACTION),
325
325
  identity(STATIC_TYPES.ATOM),
326
326
  identity(STATIC_TYPES.COLLECTION),
327
327
  identity(STATIC_TYPES.PREDICATE),