fez-lisp 1.2.60 → 1.2.62
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 +6 -6
- package/package.json +1 -1
- package/src/compiler.js +9 -18
- package/src/utils.js +2 -9
package/README.md
CHANGED
@@ -54,9 +54,9 @@ World
|
|
54
54
|
```
|
55
55
|
|
56
56
|
```lisp
|
57
|
-
(let Fizz (
|
58
|
-
(let Buzz (
|
59
|
-
(let FizzBuzz (
|
57
|
+
(let Fizz (array char:F char:i char:z char:z))
|
58
|
+
(let Buzz (array char:B char:u char:z char:z))
|
59
|
+
(let FizzBuzz (array Fizz Buzz))
|
60
60
|
|
61
61
|
(let fizz-buzz (lambda n
|
62
62
|
(cond
|
@@ -152,9 +152,9 @@ eval(
|
|
152
152
|
import { fez } from 'fez-lisp'
|
153
153
|
fez(
|
154
154
|
`
|
155
|
-
(let Fizz (
|
156
|
-
(let Buzz (
|
157
|
-
(let FizzBuzz (
|
155
|
+
(let Fizz (array char:F char:i char:z char:z))
|
156
|
+
(let Buzz (array char:B char:u char:z char:z))
|
157
|
+
(let FizzBuzz (array Fizz Buzz))
|
158
158
|
|
159
159
|
(let fizz-buzz (lambda n
|
160
160
|
(cond
|
package/package.json
CHANGED
package/src/compiler.js
CHANGED
@@ -8,23 +8,14 @@ import {
|
|
8
8
|
WORD,
|
9
9
|
SUGGAR
|
10
10
|
} from './keywords.js'
|
11
|
-
import { leaf, isLeaf } from './parser.js'
|
12
|
-
const
|
11
|
+
import { leaf, isLeaf, AST } from './parser.js'
|
12
|
+
const deepRename = (name, newName, tree) => {
|
13
13
|
if (!isLeaf(tree))
|
14
14
|
for (const leaf of tree) {
|
15
15
|
// Figure out a non mutable solution so
|
16
|
-
// I can get rid of deep
|
17
|
-
if (leaf[VALUE] === name) leaf[VALUE] = `()=>${newName}`
|
18
|
-
deepRenameTco(name, newName, leaf)
|
19
|
-
}
|
20
|
-
}
|
21
|
-
const deepRenameCache = (name, newName, tree) => {
|
22
|
-
if (!isLeaf(tree))
|
23
|
-
for (const leaf of tree) {
|
24
|
-
// Figure out a non mutable solution so
|
25
|
-
// I can get rid of deep copy
|
16
|
+
// I can get rid of deep clone AST.parse(AST.stringify(ast))
|
26
17
|
if (leaf[VALUE] === name) leaf[VALUE] = newName
|
27
|
-
|
18
|
+
deepRename(name, newName, leaf)
|
28
19
|
}
|
29
20
|
}
|
30
21
|
const earMuffsToLodashes = (name) => name.replace(new RegExp(/\*/g), '_')
|
@@ -93,7 +84,7 @@ const Helpers = {
|
|
93
84
|
__eq: '__eq=(a,b)=>+(a===b)',
|
94
85
|
__lteq: '__lteq=(a,b)=>+(a<=b)',
|
95
86
|
__lt: '__lt=(a,b)=>+(a<b)',
|
96
|
-
array: 'array=(
|
87
|
+
array: 'array=(...args)=>args',
|
97
88
|
not: 'not=(a)=>+!a',
|
98
89
|
and: `and=(...args)=>{
|
99
90
|
let circuit;
|
@@ -192,7 +183,7 @@ const compile = (tree, Drill) => {
|
|
192
183
|
const functionArgs = Arguments.at(-1).slice(1)
|
193
184
|
const body = functionArgs.pop()
|
194
185
|
const FunctionDrill = { Variables: new Set(), Helpers: Drill.Helpers }
|
195
|
-
|
186
|
+
deepRename(n, `()=>${newName}`, body)
|
196
187
|
const evaluatedBody = compile(body, FunctionDrill)
|
197
188
|
const vars = FunctionDrill.Variables.size
|
198
189
|
? `var ${[...FunctionDrill.Variables].join(',')};`
|
@@ -210,7 +201,7 @@ const compile = (tree, Drill) => {
|
|
210
201
|
Drill.Variables.add(name)
|
211
202
|
const functionArgs = Arguments.at(-1).slice(1)
|
212
203
|
const body = functionArgs.pop()
|
213
|
-
|
204
|
+
deepRename(n, newName, body)
|
214
205
|
const FunctionDrill = { Variables: new Set(), Helpers: Drill.Helpers }
|
215
206
|
const evaluatedBody = compile(body, FunctionDrill)
|
216
207
|
const vars = FunctionDrill.Variables.size
|
@@ -336,7 +327,7 @@ const compile = (tree, Drill) => {
|
|
336
327
|
const HelpersEntries = new Map(Object.entries(Helpers))
|
337
328
|
export const comp = (ast) => {
|
338
329
|
const Drill = { Variables: new Set(), Helpers: new Set() }
|
339
|
-
const raw = ast
|
330
|
+
const raw = AST.parse(AST.stringify(ast)) // cloning for fn renames mutations
|
340
331
|
.map((tree) => compile(tree, Drill))
|
341
332
|
.filter((x) => x !== undefined)
|
342
333
|
.join('\n')
|
@@ -355,5 +346,5 @@ export const comp = (ast) => {
|
|
355
346
|
? `var ${[...Drill.Variables].join(',')};`
|
356
347
|
: ''
|
357
348
|
const top = `${help}${vars}`
|
358
|
-
return {
|
349
|
+
return `${top}${program}`
|
359
350
|
}
|
package/src/utils.js
CHANGED
@@ -239,7 +239,6 @@ export const dfs = (tree, callback) => {
|
|
239
239
|
if (!isLeaf(tree)) for (const leaf of tree) dfs(leaf)
|
240
240
|
else callback(tree)
|
241
241
|
}
|
242
|
-
export const deepClone = (ast) => AST.parse(AST.stringify(ast))
|
243
242
|
export const interpret = (ast, keywords) =>
|
244
243
|
ast.reduce((_, x) => evaluate(x, keywords), 0)
|
245
244
|
export const fez = (source, options = {}) => {
|
@@ -255,19 +254,13 @@ export const fez = (source, options = {}) => {
|
|
255
254
|
const parsed = deSuggar(LISP.parse(code))
|
256
255
|
const ast = [...treeShake(parsed, std), ...parsed]
|
257
256
|
// if (options.check) typeCheck(ast)
|
258
|
-
if (options.compile)
|
259
|
-
const js = Object.values(comp(deepClone(ast))).join('')
|
260
|
-
return js
|
261
|
-
}
|
257
|
+
if (options.compile) return comp(ast)
|
262
258
|
return run(ast, env)
|
263
259
|
} else if (Array.isArray(source)) {
|
264
260
|
const ast = !options.mutation
|
265
261
|
? AST.parse(AST.stringify(source).replace(new RegExp(/!/g), 'ǃ'))
|
266
262
|
: source
|
267
|
-
if (options.compile)
|
268
|
-
const js = Object.values(comp(deepClone(ast))).join('')
|
269
|
-
return js
|
270
|
-
}
|
263
|
+
if (options.compile) return comp(ast)
|
271
264
|
return run(ast, env)
|
272
265
|
} else {
|
273
266
|
throw new Error('Source has to be either a lisp source code or an AST')
|