fez-lisp 1.2.50 → 1.2.54

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.2.50",
5
+ "version": "1.2.54",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/compiler.js CHANGED
@@ -313,16 +313,6 @@ const compile = (tree, Drill) => {
313
313
  Drill
314
314
  )}:${Arguments.length === 3 ? compile(Arguments[2], Drill) : 0});`
315
315
  }
316
- case KEYWORDS.CONDITION: {
317
- let out = '('
318
- for (let i = 0; i < Arguments.length; i += 2)
319
- out += `${compile(Arguments[i], Drill)}?${compile(
320
- Arguments[i + 1],
321
- Drill
322
- )}:`
323
- out += '0);'
324
- return out
325
- }
326
316
  case KEYWORDS.THROW: {
327
317
  Drill.Helpers.add('__error')
328
318
  return `__error(${compile(Arguments[0], Drill)})`
@@ -171,37 +171,6 @@ export const keywords = {
171
171
  ? evaluate(args[2], env)
172
172
  : 0
173
173
  },
174
- [KEYWORDS.CONDITION]: (args, env) => {
175
- if (args.length < 2)
176
- throw new RangeError(
177
- `Invalid number of arguments for (${
178
- KEYWORDS.CONDITION
179
- }), expected (> 2 required) but got ${args.length} (${
180
- KEYWORDS.CONDITION
181
- } ${stringifyArgs(args)})`
182
- )
183
- if (args.length % 2 !== 0)
184
- throw new RangeError(
185
- `Invalid number of arguments for (${
186
- KEYWORDS.CONDITION
187
- }), expected even number of arguments but got ${args.length} (${
188
- KEYWORDS.CONDITION
189
- } ${stringifyArgs(args)})`
190
- )
191
- for (let i = 0; i < args.length; i += 2) {
192
- const condition = evaluate(args[i], env)
193
- if (condition !== FALSE && condition !== TRUE)
194
- throw new TypeError(
195
- `Condition of (${
196
- KEYWORDS.CONDITION
197
- }) must be ${TRUE} or ${FALSE} but got (${
198
- KEYWORDS.CONDITION
199
- } ${stringifyArgs(args)})`
200
- )
201
- if (condition) return evaluate(args[i + 1], env)
202
- }
203
- return 0
204
- },
205
174
  [KEYWORDS.ARRAY_TYPE]: (args, env) => {
206
175
  return args.length ? args.map((x) => evaluate(x, env)) : []
207
176
  },
package/src/keywords.js CHANGED
@@ -15,7 +15,7 @@ export const SUGGAR = {
15
15
  LIST_TYPE: 'list',
16
16
  POWER: '**',
17
17
  INTEGER_DEVISION: '//',
18
-
18
+ CONDITION: 'cond',
19
19
  RECURSION: 'recursive',
20
20
  CACHE: 'memoized'
21
21
  }
@@ -42,7 +42,6 @@ export const KEYWORDS = {
42
42
  BLOCK: 'do',
43
43
  ANONYMOUS_FUNCTION: 'lambda',
44
44
  IF: 'if',
45
- CONDITION: 'cond',
46
45
  NOT: 'not',
47
46
  EQUAL: '=',
48
47
  LESS_THAN: '<',
package/src/utils.js CHANGED
@@ -1,6 +1,16 @@
1
1
  import std from '../lib/baked/std.js'
2
2
  import { comp } from './compiler.js'
3
- import { APPLY, ATOM, KEYWORDS, SUGGAR, TYPE, VALUE, WORD } from './keywords.js'
3
+ import {
4
+ APPLY,
5
+ ATOM,
6
+ FALSE,
7
+ KEYWORDS,
8
+ SUGGAR,
9
+ TRUE,
10
+ TYPE,
11
+ VALUE,
12
+ WORD
13
+ } from './keywords.js'
4
14
  import { evaluate, run } from './evaluator.js'
5
15
  import { AST, isLeaf, LISP } from './parser.js'
6
16
  export const logError = (error) =>
@@ -385,8 +395,8 @@ export const deSuggar = (ast) => {
385
395
  prev[TYPE] === APPLY &&
386
396
  prev[VALUE] !== KEYWORDS.ANONYMOUS_FUNCTION)
387
397
  ) {
388
- exp[0][1] = KEYWORDS.CALL_FUNCTION
389
- exp[0][0] = APPLY
398
+ exp[0][VALUE] = KEYWORDS.CALL_FUNCTION
399
+ exp[0][TYPE] = APPLY
390
400
  exp.length = 1
391
401
  exp[1] = [
392
402
  [APPLY, KEYWORDS.ANONYMOUS_FUNCTION],
@@ -396,20 +406,6 @@ export const deSuggar = (ast) => {
396
406
  }
397
407
  }
398
408
  break
399
- // case KEYWORDS.DEFINE_VARIABLE:
400
- // {
401
- // if (
402
- // rest[1] &&
403
- // rest[1][0] &&
404
- // rest[1][0][TYPE] === APPLY &&
405
- // rest[1][0][VALUE] === KEYWORDS.BLOCK
406
- // ) {
407
- // throw new SyntaxError(
408
- // `Can't use (${KEYWORDS.BLOCK}) in (${KEYWORDS.DEFINE_VARIABLE})`
409
- // )
410
- // }
411
- // }
412
- // break
413
409
  case SUGGAR.PIPE:
414
410
  {
415
411
  if (rest.length < 1)
@@ -437,15 +433,46 @@ export const deSuggar = (ast) => {
437
433
  deSuggar(exp)
438
434
  }
439
435
  break
436
+ case SUGGAR.CONDITION:
437
+ {
438
+ if (rest.length < 2)
439
+ throw new RangeError(
440
+ `Invalid number of arguments for (${
441
+ SUGGAR.CONDITION
442
+ }), expected (> 2 required) but got ${rest.length} (${
443
+ SUGGAR.CONDITION
444
+ } ${stringifyArgs(rest)})`
445
+ )
446
+ if (rest.length % 2 !== 0)
447
+ throw new RangeError(
448
+ `Invalid number of arguments for (${
449
+ SUGGAR.CONDITION
450
+ }), expected even number of arguments but got ${
451
+ rest.length
452
+ } (${SUGGAR.CONDITION} ${stringifyArgs(rest)})`
453
+ )
454
+ exp.length = 0
455
+ let temp = exp
456
+ for (let i = 0; i < rest.length; i += 2) {
457
+ if (i === rest.length - 2) {
458
+ temp.push([APPLY, KEYWORDS.IF], rest[i], rest.at(-1))
459
+ } else {
460
+ temp.push([APPLY, KEYWORDS.IF], rest[i], rest[i + 1], [])
461
+ temp = temp.at(-1)
462
+ }
463
+ }
464
+ deSuggar(exp)
465
+ }
466
+ break
440
467
  case SUGGAR.LIST_TYPE:
441
468
  {
442
469
  exp.length = 0
443
470
  let temp = exp
444
471
  for (const item of rest) {
445
- temp.push([0, KEYWORDS.ARRAY_TYPE], item, [])
472
+ temp.push([APPLY, KEYWORDS.ARRAY_TYPE], item, [])
446
473
  temp = temp.at(-1)
447
474
  }
448
- temp.push([0, 'array'])
475
+ temp.push([APPLY, KEYWORDS.ARRAY_TYPE])
449
476
  deSuggar(exp)
450
477
  }
451
478
  break
@@ -465,7 +492,7 @@ export const deSuggar = (ast) => {
465
492
  exp.length = 1
466
493
  exp[0] = [APPLY, KEYWORDS.BITWISE_OR]
467
494
  exp.push([[APPLY, KEYWORDS.DIVISION], ...rest])
468
- exp.push([ATOM, 0])
495
+ exp.push([ATOM, FALSE])
469
496
  }
470
497
  }
471
498
  break
@@ -479,23 +506,28 @@ export const deSuggar = (ast) => {
479
506
  SUGGAR.POWER
480
507
  } ${stringifyArgs(rest)})`
481
508
  )
482
- if (
483
- (exp[2][TYPE] === ATOM || exp[2][TYPE] === WORD) &&
484
- (exp[1][TYPE] === WORD || exp[1][TYPE] === ATOM)
485
- ) {
509
+ const isExponentAtom = exp[1][TYPE] === ATOM
510
+ const isPowerAtom = exp[2][TYPE] === ATOM
511
+ const isExponentWord = exp[1][TYPE] === WORD
512
+ if ((isExponentWord || isExponentAtom) && isPowerAtom) {
486
513
  exp[0][VALUE] = KEYWORDS.MULTIPLICATION
487
514
  const exponent = exp[1]
488
515
  const power = exp[2][VALUE]
489
516
  exp.length = 1
490
- const right = Array.from({ length: power })
491
- .fill(0)
492
- .map(() => [exponent[TYPE], exponent[VALUE]])
493
- exp.push(...right)
517
+ if (isExponentAtom) {
518
+ exp.push(exponent, [ATOM, exponent[VALUE] ** (power - 1)])
519
+ } else if (isExponentWord) {
520
+ exp.push(
521
+ ...Array.from({ length: power })
522
+ .fill(0)
523
+ .map(() => [exponent[TYPE], exponent[VALUE]])
524
+ )
525
+ }
494
526
  } else
495
527
  throw new TypeError(
496
- `ArgumentS of (${
528
+ `Second Arguments of (${
497
529
  SUGGAR.POWER
498
- }), must be (or atom word) (hint use math:power instead) (${
530
+ }), must be (atom) (hint use math:power instead) (${
499
531
  SUGGAR.POWER
500
532
  } ${stringifyArgs(rest)})`
501
533
  )
@@ -503,15 +535,15 @@ export const deSuggar = (ast) => {
503
535
  break
504
536
  case KEYWORDS.MULTIPLICATION:
505
537
  if (!rest.length) {
506
- exp[0][0] = 2
507
- exp[0][1] = 1
538
+ exp[0][TYPE] = ATOM
539
+ exp[0][VALUE] = TRUE
508
540
  }
509
541
  break
510
542
  case KEYWORDS.ADDITION:
511
543
  case KEYWORDS.DIVISION:
512
544
  if (!rest.length) {
513
- exp[0][0] = 2
514
- exp[0][1] = 0
545
+ exp[0][TYPE] = ATOM
546
+ exp[0][VALUE] = FALSE
515
547
  }
516
548
  break
517
549
  case SUGGAR.UNLESS:
@@ -524,9 +556,9 @@ export const deSuggar = (ast) => {
524
556
  SUGGAR.UNLESS
525
557
  } ${stringifyArgs(rest)})`
526
558
  )
527
- exp[0][1] = KEYWORDS.IF
559
+ exp[0][VALUE] = KEYWORDS.IF
528
560
  const temp = exp[2]
529
- exp[2] = exp[3] ?? [ATOM, 0]
561
+ exp[2] = exp[3] ?? [ATOM, FALSE]
530
562
  exp[3] = temp
531
563
  }
532
564
  deSuggar(exp)
@@ -543,7 +575,7 @@ export const deSuggar = (ast) => {
543
575
  exp[0][1]
544
576
  } ${stringifyArgs(rest)})`
545
577
  )
546
- exp[0][1] = KEYWORDS.NOT
578
+ exp[0][VALUE] = KEYWORDS.NOT
547
579
  exp[1] = [[APPLY, KEYWORDS.EQUAL], exp[1], exp[2]]
548
580
  exp.length = 2
549
581
  deSuggar(exp)