fez-lisp 1.0.51 → 1.0.52

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/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { evaluate } from './src/interpreter.js'
1
+ import { evaluate } from './src/evaluator.js'
2
2
  import { LISP, AST } from './src/parser.js'
3
3
  import { fez, tree } from './src/utils.js'
4
4
  import std from './lib/baked/std.js'
5
- import { keywords } from './src/tokeniser.js'
6
- import { WORD, APPLY, ATOM, VALUE, TYPE } from './src/enums.js'
5
+ import { keywords } from './src/interpreter.js'
6
+ import { WORD, APPLY, ATOM, VALUE, TYPE } from './src/keywords.js'
7
7
  const types = { WORD, APPLY, ATOM, VALUE, TYPE }
8
8
  export { fez, keywords, evaluate, std, types, tree, LISP, AST }
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.52",
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 })