fez-lisp 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/utils.js +1 -1
- package/cli/index.js +0 -162
package/package.json
CHANGED
package/src/utils.js
CHANGED
@@ -132,7 +132,7 @@ export const treeShake = (ast, libs) => {
|
|
132
132
|
const deps = libs.reduce((a, x) => a.add(x.at(1)[VALUE]), new Set())
|
133
133
|
const visited = new Set()
|
134
134
|
const dfs = (tree) => {
|
135
|
-
if (Array.isArray(tree)) tree.forEach(
|
135
|
+
if (Array.isArray(tree)) tree.forEach(dfs)
|
136
136
|
else if (
|
137
137
|
(tree[TYPE] === APPLY || tree[TYPE] === WORD) &&
|
138
138
|
deps.has(tree[VALUE]) &&
|
package/cli/index.js
DELETED
@@ -1,162 +0,0 @@
|
|
1
|
-
import { readFileSync, writeFileSync } from 'fs'
|
2
|
-
import { start } from 'repl'
|
3
|
-
import { run } from '../src/interpreter.js'
|
4
|
-
import { parse } from '../src/parser.js'
|
5
|
-
import STD from '../lib/baked/std.js'
|
6
|
-
|
7
|
-
// import wabt from 'wabt'
|
8
|
-
import {
|
9
|
-
handleUnbalancedParens,
|
10
|
-
handleUnbalancedQuotes,
|
11
|
-
logError,
|
12
|
-
removeNoCode,
|
13
|
-
treeShake,
|
14
|
-
} from '../src/utils.js'
|
15
|
-
import { comp } from '../src/compiler.js'
|
16
|
-
export default async () => {
|
17
|
-
const [, , ...argv] = process.argv
|
18
|
-
let file = '',
|
19
|
-
path = '',
|
20
|
-
env = {},
|
21
|
-
destination = undefined
|
22
|
-
while (argv.length) {
|
23
|
-
const flag = argv.shift()?.toLowerCase()
|
24
|
-
const value = argv.shift()
|
25
|
-
if (!flag) throw new Error('No flag provided')
|
26
|
-
switch (flag) {
|
27
|
-
case '-m':
|
28
|
-
writeFileSync(path, removeNoCode(file), 'utf-8')
|
29
|
-
break
|
30
|
-
case '-d':
|
31
|
-
destination = value
|
32
|
-
break
|
33
|
-
case '-s':
|
34
|
-
path = value
|
35
|
-
file = readFileSync(value, 'utf-8')
|
36
|
-
break
|
37
|
-
case '-r':
|
38
|
-
try {
|
39
|
-
run(
|
40
|
-
parse(
|
41
|
-
handleUnbalancedQuotes(handleUnbalancedParens(removeNoCode(file)))
|
42
|
-
),
|
43
|
-
env
|
44
|
-
)
|
45
|
-
} catch (err) {
|
46
|
-
logError('Error')
|
47
|
-
logError(err.message)
|
48
|
-
}
|
49
|
-
break
|
50
|
-
case '-c':
|
51
|
-
{
|
52
|
-
const parsed = parse(
|
53
|
-
handleUnbalancedQuotes(handleUnbalancedParens(removeNoCode(file)))
|
54
|
-
)
|
55
|
-
const tree = [...treeShake(parsed, STD), ...parsed]
|
56
|
-
if (Array.isArray(tree)) {
|
57
|
-
const { top, program } = comp(tree)
|
58
|
-
const JavaScript = `${top}${program}`
|
59
|
-
writeFileSync(
|
60
|
-
destination ?? './playground/dist/main.js',
|
61
|
-
JavaScript
|
62
|
-
)
|
63
|
-
}
|
64
|
-
}
|
65
|
-
break
|
66
|
-
case '-r':
|
67
|
-
try {
|
68
|
-
const parsed = parse(
|
69
|
-
handleUnbalancedQuotes(handleUnbalancedParens(removeNoCode(file)))
|
70
|
-
)
|
71
|
-
run([...treeShake(parsed, STD), ...parsed], env)
|
72
|
-
} catch (err) {
|
73
|
-
logError('Error')
|
74
|
-
logError(err.message)
|
75
|
-
}
|
76
|
-
break
|
77
|
-
case '-repl':
|
78
|
-
{
|
79
|
-
let source = ''
|
80
|
-
const inpColor = '\x1b[32m'
|
81
|
-
const outColor = '\x1b[33m'
|
82
|
-
const errColor = '\x1b[31m'
|
83
|
-
console.log(inpColor)
|
84
|
-
start({
|
85
|
-
prompt: '',
|
86
|
-
eval: (input) => {
|
87
|
-
input = input.trim()
|
88
|
-
if (!input || input[0] === ';') return
|
89
|
-
try {
|
90
|
-
let out = `${source}\n${file}\n(do ${input})`
|
91
|
-
const parsed = parse(
|
92
|
-
handleUnbalancedQuotes(
|
93
|
-
handleUnbalancedParens(removeNoCode(out))
|
94
|
-
)
|
95
|
-
)
|
96
|
-
const result = run([...treeShake(parsed, STD), ...parsed], env)
|
97
|
-
if (typeof result === 'function') {
|
98
|
-
console.log(inpColor, `λ`)
|
99
|
-
} else if (Array.isArray(result)) {
|
100
|
-
console.log(
|
101
|
-
outColor,
|
102
|
-
JSON.stringify(result, (_, value) => {
|
103
|
-
switch (typeof value) {
|
104
|
-
case 'bigint':
|
105
|
-
return Number(value)
|
106
|
-
case 'function':
|
107
|
-
return 'λ'
|
108
|
-
case 'undefined':
|
109
|
-
case 'symbol':
|
110
|
-
return 0
|
111
|
-
case 'boolean':
|
112
|
-
return +value
|
113
|
-
default:
|
114
|
-
return value
|
115
|
-
}
|
116
|
-
})
|
117
|
-
.replace(new RegExp(/\[/g), '(')
|
118
|
-
.replace(new RegExp(/\]/g), ')')
|
119
|
-
.replace(new RegExp(/\,/g), ' ')
|
120
|
-
.replace(new RegExp(/"λ"/g), 'λ'),
|
121
|
-
inpColor
|
122
|
-
)
|
123
|
-
} else if (typeof result === 'string') {
|
124
|
-
console.log(outColor, `"${result}"`, inpColor)
|
125
|
-
} else if (result == undefined) {
|
126
|
-
console.log(errColor, '(void)', inpColor)
|
127
|
-
} else {
|
128
|
-
console.log(outColor, result, inpColor)
|
129
|
-
}
|
130
|
-
source = out
|
131
|
-
} catch (err) {
|
132
|
-
console.log(errColor, err.message, inpColor)
|
133
|
-
}
|
134
|
-
},
|
135
|
-
})
|
136
|
-
}
|
137
|
-
break
|
138
|
-
case '-help':
|
139
|
-
case '-h':
|
140
|
-
default:
|
141
|
-
console.log(`
|
142
|
-
-------------------------------------
|
143
|
-
-help
|
144
|
-
-------------------------------------
|
145
|
-
-s prepare a file
|
146
|
-
-------------------------------------
|
147
|
-
-d file to compile js
|
148
|
-
-------------------------------------
|
149
|
-
-c compile to js
|
150
|
-
-------------------------------------
|
151
|
-
-r interpret & run
|
152
|
-
-------------------------------------
|
153
|
-
-p interpret & run with 0 deps
|
154
|
-
-------------------------------------
|
155
|
-
-m minify code
|
156
|
-
-------------------------------------
|
157
|
-
-repl start Read Eval Print Loop
|
158
|
-
-------------------------------------
|
159
|
-
`)
|
160
|
-
}
|
161
|
-
}
|
162
|
-
}
|