fez-lisp 1.0.51 → 1.0.53

Sign up to get free protection for your applications and to get access to all the features.
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.0.51",
5
+ "version": "1.0.53",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "keywords": [
package/src/compiler.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  TYPE,
7
7
  VALUE,
8
8
  WORD
9
- } from './enums.js'
9
+ } from './keywords.js'
10
10
  import { leaf, isLeaf } from './parser.js'
11
11
  import { deepRename, lispToJavaScriptVariableName } from './utils.js'
12
12
  const Helpers = {
@@ -0,0 +1,43 @@
1
+ import { APPLY, ATOM, KEYWORDS, TYPE, VALUE, WORD } from './keywords.js'
2
+ import { isLeaf } from './parser.js'
3
+ import { keywords } from './interpreter.js'
4
+ import { stringifyArgs } from './utils.js'
5
+
6
+ export const evaluate = (exp, env) => {
7
+ const [first, ...rest] = isLeaf(exp) ? [exp] : exp
8
+ if (first == undefined) return []
9
+ switch (first[TYPE]) {
10
+ case WORD: {
11
+ const word = env[first[VALUE]]
12
+ if (word == undefined)
13
+ throw new ReferenceError(`Undefined variable ${first[VALUE]}`)
14
+ return word
15
+ }
16
+ case APPLY:
17
+ const apply = env[first[VALUE]]
18
+ if (apply == undefined)
19
+ throw new ReferenceError(
20
+ `Undefined (${KEYWORDS.ANONYMOUS_FUNCTION}) ${first[VALUE]}`
21
+ )
22
+ if (typeof apply !== 'function')
23
+ throw new TypeError(
24
+ `${first[VALUE]} is not a (${KEYWORDS.ANONYMOUS_FUNCTION})`
25
+ )
26
+ return apply(rest, env)
27
+ case ATOM:
28
+ return first[VALUE]
29
+ default:
30
+ throw new ReferenceError(
31
+ `Attempting to acces Undefined near ${stringifyArgs(exp)}`
32
+ )
33
+ }
34
+ }
35
+ export const isAtom = (arg, env) => {
36
+ if (arg[TYPE] === ATOM) return 1
37
+ else {
38
+ const atom = evaluate(arg, env)
39
+ return +(typeof atom === 'number' || typeof atom === 'string')
40
+ }
41
+ }
42
+ export const run = (tree, env = {}) =>
43
+ keywords[KEYWORDS.BLOCK](tree, { ...keywords, ...env })