fez-lisp 1.3.1 → 1.3.3
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/lib/baked/macros.js +29 -4
- package/lib/baked/std.js +1 -1
- package/package.json +2 -3
- package/src/compiler.js +32 -10
- package/src/interpreter.js +314 -329
- package/src/keywords.js +0 -13
- package/src/macros.js +406 -0
- package/src/utils.js +15 -374
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.3.
|
5
|
+
"version": "1.3.3",
|
6
6
|
"type": "module",
|
7
7
|
"main": "index.js",
|
8
8
|
"keywords": [
|
@@ -29,6 +29,5 @@
|
|
29
29
|
"license": "MIT",
|
30
30
|
"devDependencies": {
|
31
31
|
"mocha": "^10.2.0"
|
32
|
-
}
|
33
|
-
"dependencies": {}
|
32
|
+
}
|
34
33
|
}
|
package/src/compiler.js
CHANGED
@@ -5,10 +5,13 @@ import {
|
|
5
5
|
KEYWORDS,
|
6
6
|
TYPE,
|
7
7
|
VALUE,
|
8
|
-
WORD
|
9
|
-
SUGGAR
|
8
|
+
WORD
|
10
9
|
} from './keywords.js'
|
11
10
|
import { leaf, isLeaf, AST } from './parser.js'
|
11
|
+
export const OPTIMIZATIONS = {
|
12
|
+
RECURSION: 'recursive',
|
13
|
+
CACHE: 'memoized'
|
14
|
+
}
|
12
15
|
const deepRename = (name, newName, tree) => {
|
13
16
|
if (!isLeaf(tree))
|
14
17
|
for (const leaf of tree) {
|
@@ -58,6 +61,20 @@ const keywordToHelper = (name) => {
|
|
58
61
|
return '__lt'
|
59
62
|
case KEYWORDS.LESS_THAN_OR_EQUAL:
|
60
63
|
return '__lteq'
|
64
|
+
case KEYWORDS.BITWISE_AND:
|
65
|
+
return '__bit_and'
|
66
|
+
case KEYWORDS.BITWISE_OR:
|
67
|
+
return '__bit_or'
|
68
|
+
case KEYWORDS.BITWISE_XOR:
|
69
|
+
return '__bit_xor'
|
70
|
+
case KEYWORDS.BITWISE_NOT:
|
71
|
+
return '__bit_not'
|
72
|
+
case KEYWORDS.BITWISE_LEFT_SHIFT:
|
73
|
+
return '__bit_lshift'
|
74
|
+
case KEYWORDS.BITWISE_RIGHT_SHIFT:
|
75
|
+
return '__bit_rshift'
|
76
|
+
case KEYWORDS.BITWISE_UNSIGNED_RIGHT_SHIFT:
|
77
|
+
return '__bit_urshift'
|
61
78
|
default:
|
62
79
|
return name
|
63
80
|
}
|
@@ -78,12 +95,19 @@ const Helpers = {
|
|
78
95
|
__add: `__add=(a,b)=>{return a+b}`,
|
79
96
|
__sub: `__sub=function(a,b){return arguments.length===1?-a:a-b}`,
|
80
97
|
__mult: `__mult=(a,b)=>{return a*b}`,
|
81
|
-
__div: `__div=
|
98
|
+
__div: `__div=(a,b)=>{return a/b}`,
|
82
99
|
__gteq: '__gteq=(a,b)=>+(a>=b)',
|
83
100
|
__gt: '__gt=(a,b)=>+(a>b)',
|
84
101
|
__eq: '__eq=(a,b)=>+(a===b)',
|
85
102
|
__lteq: '__lteq=(a,b)=>+(a<=b)',
|
86
103
|
__lt: '__lt=(a,b)=>+(a<b)',
|
104
|
+
__bit_and: '__bit_and=(a,b)=>a&b',
|
105
|
+
__bit_or: '__bit_or=(a,b)=>a|b',
|
106
|
+
__bit_xor: '__bit_xor=(a,b)=>a^b',
|
107
|
+
__bit_not: '__bit_not=(a)=>~a',
|
108
|
+
__bit_lshift: '__bit_lshift=(a,b)=>a<<b',
|
109
|
+
__bit_rshift: '__bit_rshift=(a,b)=>a>>b',
|
110
|
+
__bit_urshift: '__bit_urshift=(a,b)=>a>>>b',
|
87
111
|
array: 'array=(...args)=>args',
|
88
112
|
not: 'not=(a)=>+!a',
|
89
113
|
and: `and=(a, b)=>+(a&&b)`,
|
@@ -155,9 +179,9 @@ const compile = (tree, Drill) => {
|
|
155
179
|
case KEYWORDS.DEFINE_VARIABLE: {
|
156
180
|
const n = Arguments[0][VALUE]
|
157
181
|
const prefix = n.split(':')[0]
|
158
|
-
if (prefix ===
|
182
|
+
if (prefix === OPTIMIZATIONS.RECURSION) {
|
159
183
|
const name = lispToJavaScriptVariableName(n)
|
160
|
-
const newName = `${
|
184
|
+
const newName = `${OPTIMIZATIONS.RECURSION}_${performance
|
161
185
|
.now()
|
162
186
|
.toString()
|
163
187
|
.replace('.', 7)}`
|
@@ -178,10 +202,10 @@ const compile = (tree, Drill) => {
|
|
178
202
|
)})=>{${vars}return ${evaluatedBody
|
179
203
|
.toString()
|
180
204
|
.trim()}}, ${newName})));`
|
181
|
-
} else if (prefix ===
|
205
|
+
} else if (prefix === OPTIMIZATIONS.CACHE) {
|
182
206
|
// memoization here
|
183
207
|
const name = lispToJavaScriptVariableName(n)
|
184
|
-
const newName = name.substring(
|
208
|
+
const newName = name.substring(OPTIMIZATIONS.CACHE.length + 1)
|
185
209
|
Drill.Variables.add(name)
|
186
210
|
const functionArgs = Arguments.at(-1).slice(1)
|
187
211
|
const body = functionArgs.pop()
|
@@ -262,9 +286,7 @@ const compile = (tree, Drill) => {
|
|
262
286
|
case KEYWORDS.MULTIPLICATION:
|
263
287
|
return `(${parseArgs(Arguments, Drill, token)});`
|
264
288
|
case KEYWORDS.DIVISION:
|
265
|
-
return Arguments
|
266
|
-
? `(1/${compile(Arguments[0], Drill)});`
|
267
|
-
: `(${parseArgs(Arguments, Drill, token)});`
|
289
|
+
return `(${parseArgs(Arguments, Drill, token)});`
|
268
290
|
case KEYWORDS.ADDITION:
|
269
291
|
return `(${parseArgs(Arguments, Drill, token)});`
|
270
292
|
case KEYWORDS.BITWISE_AND:
|