fez-lisp 1.5.0 → 1.5.2

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.0",
5
+ "version": "1.5.2",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/utils.js CHANGED
@@ -1,18 +1,5 @@
1
1
  import std from '../lib/baked/std.js'
2
- import debugStd from '../lib/debug/std.js'
3
- import { compile } from './compiler.js'
4
- import {
5
- APPLY,
6
- ATOM,
7
- DEBUG,
8
- FALSE,
9
- KEYWORDS,
10
- RUNTIME_TYPES,
11
- TRUE,
12
- TYPE,
13
- VALUE,
14
- WORD
15
- } from './keywords.js'
2
+ import { APPLY, ATOM, KEYWORDS, TYPE, VALUE, WORD } from './keywords.js'
16
3
  import { evaluate } from './evaluator.js'
17
4
  import { isLeaf, LISP } from './parser.js'
18
5
  import {
@@ -21,7 +8,6 @@ import {
21
8
  handleUnbalancedQuotes,
22
9
  OPTIMIZATIONS
23
10
  } from './macros.js'
24
- import { keywords } from './interpreter.js'
25
11
  export const logError = (error) =>
26
12
  console.log('\x1b[31m', `\n${error}\n`, '\x1b[0m')
27
13
  export const logSuccess = (output) => console.log('\x1b[32m', output, '\x1b[0m')
@@ -277,166 +263,3 @@ export const parse = (source) =>
277
263
  std
278
264
  )
279
265
  )
280
-
281
- const identity = (name) => [
282
- [0, 'let'],
283
- [1, name],
284
- [
285
- [0, 'lambda'],
286
- [1, 'x'],
287
- [1, 'x']
288
- ]
289
- ]
290
- export const debug = (ast, onSuccess = compile) => {
291
- const debugEnv = {
292
- ...keywords,
293
- [DEBUG.CALLSTACK]: [KEYWORDS.BLOCK],
294
- [DEBUG.SIGNATURE]: (args, env) => {
295
- const signatures =
296
- args.length === 0
297
- ? debugStd[0][1][1].slice(1)
298
- : debugStd[0][1][1].filter(
299
- (x) =>
300
- x[0][TYPE] === APPLY &&
301
- x[0][VALUE] === KEYWORDS.DEFINE_VARIABLE &&
302
- x[1][TYPE] === WORD &&
303
- x[1][VALUE].toString().includes(args[0][VALUE])
304
- )
305
- return signatures.length === 0
306
- ? 'Not defined in library'
307
- : signatures.map(LISP.source).join('\n\n')
308
- },
309
- [DEBUG.LOG]: (args, env) => {
310
- if (args.length !== 1 && args.length !== 2)
311
- throw new RangeError(
312
- `Invalid number of arguments to (${DEBUG.LOG}) (or (= 1) (= 2)) (${
313
- DEBUG.LOG
314
- } ${stringifyArgs(args)})`
315
- )
316
- const expression = evaluate(args[0], env)
317
- if (args.length === 2) {
318
- const option = evaluate(args[1], env)
319
- if (!Array.isArray(option)) {
320
- throw new TypeError(
321
- `Second argument of (${DEBUG.LOG}) must be an (${
322
- RUNTIME_TYPES.ARRAY
323
- }) but got (${expression}) (${DEBUG.LOG} ${stringifyArgs(args)})`
324
- )
325
- }
326
- const type = option.map((x) => String.fromCharCode(x)).join('')
327
- switch (type) {
328
- case 'string':
329
- case 'str':
330
- {
331
- if (!Array.isArray(expression))
332
- throw new TypeError(
333
- `Argument of (${DEBUG.LOG}) must be an (${
334
- RUNTIME_TYPES.ARRAY
335
- }) in the case ${type} but got (${expression}) (${
336
- DEBUG.LOG
337
- } ${stringifyArgs(args)})`
338
- )
339
- console.log(
340
- expression.map((x) => String.fromCharCode(x)).join('')
341
- )
342
- }
343
- break
344
- case 'char':
345
- case 'ch':
346
- {
347
- if (typeof expression !== 'number')
348
- throw new TypeError(
349
- `Argument argument of (${DEBUG.LOG}) must be a (${
350
- RUNTIME_TYPES.NUMBER
351
- }) in the case ${type} but got (${expression}) (${
352
- DEBUG.LOG
353
- } ${stringifyArgs(args)})`
354
- )
355
- console.log(String.fromCharCode(expression))
356
- }
357
-
358
- break
359
- case '*':
360
- console.log(expression)
361
- break
362
- default:
363
- throw new TypeError(
364
- `Invalid number of option to (${
365
- DEBUG.LOG
366
- }) got ${option} ${stringifyArgs(args)})`
367
- )
368
- }
369
- } else console.log(expression)
370
- return expression
371
- },
372
- [DEBUG.ASSERT]: (args, env) => {
373
- if (args.length < 2)
374
- throw new RangeError(
375
- `Invalid number of arguments for (${
376
- DEBUG.ASSERT
377
- }), expected (> 2 required) but got ${args.length} (${
378
- DEBUG.ASSERT
379
- } ${stringifyArgs(args)})`
380
- )
381
- if (args.length % 2 !== 0)
382
- throw new RangeError(
383
- `Invalid number of arguments for (${
384
- DEBUG.ASSERT
385
- }), expected even number of arguments but got ${args.length} (${
386
- DEBUG.ASSERT
387
- } ${stringifyArgs(args)})`
388
- )
389
- for (let i = 0; i < args.length; i += 2) {
390
- const condition = evaluate(args[i], env)
391
- if (condition !== FALSE && condition !== TRUE)
392
- throw new TypeError(
393
- `Condition of (${
394
- DEBUG.ASSERT
395
- }) must be ${TRUE} or ${FALSE} but got (${
396
- DEBUG.ASSERT
397
- } ${stringifyArgs(args)})`
398
- )
399
- if (condition) {
400
- const error = args[i + 1]
401
- if (error[0][TYPE] === APPLY && error[0][VALUE] === KEYWORDS.ERROR)
402
- return evaluate(error, env)
403
- else
404
- throw new TypeError(
405
- `Concequence of (${DEBUG.ASSERT}) must be (${
406
- KEYWORDS.ERROR
407
- }) but got (${DEBUG.ASSERT} ${stringifyArgs(args)})`
408
- )
409
- }
410
- }
411
- return 0
412
- }
413
- }
414
- try {
415
- const evaluated = evaluate(ast, debugEnv)
416
- const block = ast[1][1]
417
- const temp = block.shift()
418
- block.unshift(temp, identity(DEBUG.LOG), identity(DEBUG.ASSERT))
419
- return {
420
- evaluated,
421
- compiled: onSuccess(ast),
422
- error: null
423
- }
424
- } catch (error) {
425
- const isMaxCallStack =
426
- error.message.includes('Maximum call stack size exceeded') ||
427
- error.message.includes('too much recursion')
428
- return {
429
- compiled: null,
430
- evaluated: null,
431
- error: {
432
- message: isMaxCallStack
433
- ? error.message
434
- : `${error.message}\n${debugEnv[DEBUG.CALLSTACK]
435
- .reverse()
436
- .map((x, i) => `${Array(i + 2).join(' ')}(${x} ...)`)
437
- .join('\n')}`,
438
- stack: debugEnv[DEBUG.CALLSTACK]
439
- }
440
- }
441
- }
442
- }