fez-lisp 1.0.50 → 1.0.52
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +3 -3
- package/lib/baked/std.js +1 -1
- package/package.json +1 -1
- package/src/compiler.js +1 -1
- package/src/evaluator.js +43 -0
- package/src/interpreter.js +1129 -34
- package/src/parser.js +1 -1
- package/src/utils.js +2 -2
- package/src/tokeniser.js +0 -1145
- /package/src/{enums.js → keywords.js} +0 -0
package/package.json
CHANGED
package/src/compiler.js
CHANGED
package/src/evaluator.js
ADDED
@@ -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 })
|