fez-lisp 1.5.0 → 1.5.1
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/README.md +1 -14
- package/index.js +2 -2
- package/lib/baked/std.js +1 -1
- package/package.json +1 -1
- package/src/utils.js +0 -164
package/package.json
CHANGED
package/src/utils.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
import std from '../lib/baked/std.js'
|
2
|
-
import debugStd from '../lib/debug/std.js'
|
3
2
|
import { compile } from './compiler.js'
|
4
3
|
import {
|
5
4
|
APPLY,
|
@@ -277,166 +276,3 @@ export const parse = (source) =>
|
|
277
276
|
std
|
278
277
|
)
|
279
278
|
)
|
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
|
-
}
|