fez-lisp 1.6.77 → 1.6.80

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/types.js CHANGED
@@ -58,6 +58,9 @@ export class SubType {
58
58
  has(type) {
59
59
  return this.types[0] === type
60
60
  }
61
+ nestedLevels() {
62
+ return this.types.length - 1
63
+ }
61
64
  isMatching(b) {
62
65
  for (let i = 0; i < this.types.length; ++i) {
63
66
  if (
@@ -66,7 +69,9 @@ export class SubType {
66
69
  this.types[i] === UNKNOWN ||
67
70
  b.types[i] === UNKNOWN ||
68
71
  this.types[i] === ANY ||
69
- b.types[i] === ANY
72
+ b.types[i] === ANY ||
73
+ this.types[i] === GENERIC ||
74
+ b.types[i] === GENERIC
70
75
  )
71
76
  return true
72
77
  if (this.types[i] !== b.types[i]) return false
@@ -83,6 +88,7 @@ export class SubType {
83
88
  export const BOOLEAN_SUBTYPE = () => new SubType([BOOLEAN])
84
89
  export const COLLECTION_SUBTYPE = () => new SubType([COLLECTION])
85
90
  export const NUMBER_SUBTYPE = () => new SubType([NUMBER])
91
+ export const GENERIC_SUBTYPE = () => new SubType([GENERIC])
86
92
  export const ABSTRACTION_SUBTYPE = () => new SubType([APPLY])
87
93
  export const UNKNOWN_SUBTYPE = () => new SubType([UNKNOWN])
88
94
 
@@ -106,6 +112,8 @@ export const toTypeNames = (type) => {
106
112
  return 'Unknown[]'
107
113
  case ANY:
108
114
  return 'Any'
115
+ case GENERIC:
116
+ // return 'T'
109
117
  default:
110
118
  return 'Unknown'
111
119
  }
@@ -113,10 +121,10 @@ export const toTypeNames = (type) => {
113
121
 
114
122
  export const extractArrayType = (type) => {
115
123
  const arr = [...type].filter((x) => x === '[')
116
- return [type.split('[')[0], arr.length]
124
+ return [type.replaceAll('[]', ''), arr.length]
117
125
  }
118
126
  const fillArrayType = (n) => Array.from({ length: n - 1 }).fill(COLLECTION)
119
- export const toTypeCodes = (type) => {
127
+ export const toTypeCodes = (type, i) => {
120
128
  const [t, n] = extractArrayType(type)
121
129
  switch (t) {
122
130
  case 'Abstraction':
@@ -136,8 +144,16 @@ export const toTypeCodes = (type) => {
136
144
  return [UNKNOWN]
137
145
  case 'Any':
138
146
  return [ANY]
139
- default:
140
- break
147
+ default: {
148
+ const d = type[0] === '[' ? -1 : 1
149
+ if (n)
150
+ return [
151
+ COLLECTION,
152
+ new SubType(fillArrayType(n).concat(UNKNOWN)),
153
+ [i, d]
154
+ ]
155
+ return [UNKNOWN, undefined, [i, d]]
156
+ }
141
157
  }
142
158
  }
143
159
  export const toTypeNamesAnyToUknown = (type) => {
@@ -1492,7 +1508,7 @@ export const toArgType = (A, i) => {
1492
1508
  [IS_ARGUMENT]: true,
1493
1509
  [SIGNATURE]: PLACEHOLDER,
1494
1510
  [TYPE_PROP]: [APPLY],
1495
- [RETURNS]: toTypeCodes(returns[VALUE]),
1511
+ [RETURNS]: toTypeCodes(returns[VALUE], i),
1496
1512
  [ARGUMENTS]: args.map(toArgType).flat(1),
1497
1513
  [ARG_COUNT]: args.length
1498
1514
  }
@@ -1504,8 +1520,8 @@ export const toArgType = (A, i) => {
1504
1520
  retried: Infinity,
1505
1521
  [IS_ARGUMENT]: true,
1506
1522
  [SIGNATURE]: PLACEHOLDER,
1507
- [TYPE_PROP]: toTypeCodes(arg[VALUE]),
1508
- [RETURNS]: toTypeCodes(arg[VALUE]),
1523
+ [TYPE_PROP]: toTypeCodes(arg[VALUE], i),
1524
+ [RETURNS]: toTypeCodes(arg[VALUE], i),
1509
1525
  [ARGUMENTS]: [],
1510
1526
  [ARG_COUNT]: 0
1511
1527
  }
@@ -1525,7 +1541,15 @@ export const fromSourceToType = (T) => {
1525
1541
  [SIGNATURE]: name,
1526
1542
  [ARG_COUNT]: args.length,
1527
1543
  [ARGUMENTS]: args.map(toArgType).flat(1),
1528
- [RETURNS]: toTypeCodes(returns[VALUE])
1544
+ [RETURNS]: toTypeCodes(
1545
+ returns[VALUE],
1546
+ args.findIndex(
1547
+ (x) =>
1548
+ !Array.isArray(x[VALUE]) &&
1549
+ x[VALUE] ===
1550
+ returns[VALUE].replaceAll('[', '').replaceAll(']', '')
1551
+ )
1552
+ )
1529
1553
  }
1530
1554
  }
1531
1555
  }
@@ -1541,9 +1565,9 @@ export const withStdDefinedTypes = (ast) =>
1541
1565
  withCtxTypes(definedTypes(filteredDefinedTypes(ast, std, stdT)))
1542
1566
 
1543
1567
  export const extractTypes = (source) => {
1544
- let types
1568
+ let types = ''
1545
1569
  const src = source.replaceAll(/\(the.+\)/g, (match, token) => {
1546
- types = match
1570
+ types += match + '\n'
1547
1571
  return ''
1548
1572
  })
1549
1573
  return [src, types]
package/src/utils.js CHANGED
@@ -389,42 +389,63 @@ export const init = () => {
389
389
  // console.log('Added file types.lisp in src')
390
390
  writeFileSync(
391
391
  'index.js',
392
- `import { compile, enhance, parse, LISP, UTILS } from "fez-lisp";
393
- import { readFileSync, writeFileSync } from "fs";
394
- export const dev = (source, types) => {
395
- try {
396
- const parsed = parse(source);
397
- const { evaluated, type, error } = UTILS.debug(
398
- parsed,
399
- true,
400
- types ? types : undefined
401
- );
402
- if (error == null) {
403
- if (type) {
404
- UTILS.logType(type);
392
+ `import { compile, enhance, parse, LISP, UTILS, verify } from '../index.js'
393
+ import { readFileSync, writeFileSync } from 'fs'
394
+ export const dev = (source, types) => {
395
+ try {
396
+ const parsed = parse(source)
397
+ const { evaluated, type, error } = UTILS.debug(parsed, true, types)
398
+ if (error == null) {
399
+ if (type) {
400
+ UTILS.logType(type)
401
+ }
402
+ UTILS.logResult(LISP.serialise(evaluated))
403
+ } else UTILS.logError(error.message)
404
+ } catch (error) {
405
+ UTILS.logError(error.message)
406
+ }
405
407
  }
406
- UTILS.logResult(LISP.serialise(evaluated));
407
- } else UTILS.logError(error.message);
408
- } catch (error) {
409
- UTILS.logError(error.message);
410
- }
411
- };
412
- export const comp = (source) => compile(enhance(parse(source)));
413
- const file = readFileSync("./src/main.lisp", "utf-8");
414
- const [src, typ] = UTILS.extractTypes(file);
415
- switch (process.argv[2]) {
416
- case "comp":
417
- writeFileSync(
418
- "./src/main.js",
419
- "var _ = " + comp(src) + "; console.log(_)"
420
- );
421
- break;
422
- case "dev":
423
- default:
424
- dev(src, typ);
425
- break;
426
- }
427
- `
408
+ export const run = (source) => {
409
+ try {
410
+ const parsed = parse(source)
411
+ const { evaluated, error } = UTILS.debug(parsed, false)
412
+ if (error == null) {
413
+ UTILS.logResult(LISP.serialise(evaluated))
414
+ } else UTILS.logError(error.message)
415
+ } catch (error) {
416
+ UTILS.logError(error.message)
417
+ }
418
+ }
419
+ export const check = (source, types) => {
420
+ try {
421
+ const parsed = parse(source)
422
+ const error = verify(parsed, types)
423
+ if (error != null) UTILS.logError(error)
424
+ } catch (error) {
425
+ UTILS.logError(error.message)
426
+ }
427
+ }
428
+ export const comp = (source) => compile(enhance(parse(source)))
429
+ const file = readFileSync('./src/main.lisp', 'utf-8')
430
+ const [src, typ] = UTILS.extractTypes(file)
431
+ switch (process.argv[2]) {
432
+ case 'check':
433
+ check(src, typ)
434
+ break
435
+ case 'run':
436
+ run(src, typ)
437
+ break
438
+ case 'comp':
439
+ writeFileSync(
440
+ './src/main.js',
441
+ 'var _ = ' + comp(src) + '; console.log(_)'
442
+ )
443
+ break
444
+ case 'dev':
445
+ default:
446
+ dev(src, typ)
447
+ break
448
+ }`
428
449
  )
429
450
  console.log('Added file index.js in root')
430
451
  console.log(
@@ -432,6 +453,8 @@ switch (process.argv[2]) {
432
453
 
433
454
  Write code in main.lisp
434
455
  Run node index.js with the following flags:
456
+ - check (static type check)
457
+ - run (run time validations)
435
458
  - dev (static type check and run time validations)
436
459
  - comp (compile Fez to JavaScript file main.js)
437
460
 
@@ -645,3 +668,23 @@ export const toTypedAst = (ast, userDefinedTypes) => {
645
668
  }
646
669
  }
647
670
  export const atst = (ast, ctx) => toTypedAst(ast, ctx)[0]
671
+
672
+ export const verify = (ast, userDefinedTypes) => {
673
+ try {
674
+ typeCheck(
675
+ ast,
676
+ withCtxTypes(
677
+ userDefinedTypes
678
+ ? {
679
+ ...definedTypes(filteredDefinedTypes(ast, std, stdT)),
680
+ ...definedTypes(LISP.parse(removeNoCode(userDefinedTypes)))
681
+ }
682
+ : definedTypes(filteredDefinedTypes(ast, std, stdT))
683
+ )
684
+ )
685
+ return null
686
+ } catch (error) {
687
+ // console.log(error)
688
+ return error.message
689
+ }
690
+ }