/
lips.js
executable file
·201 lines (194 loc) · 5.99 KB
/
lips.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
const {
exec,
Formatter,
balanced_parenthesis,
tokenize,
env,
banner,
InputPort,
OutputPort } = require('../dist/lips');
const fs = require('fs');
const { format } = require('util');
const readline = require('readline');
// -----------------------------------------------------------------------------
// code taken from jQuery Terminal
function parse_options(arg, options) {
var settings = Object.assign({}, {
boolean: []
}, options);
var result = {
_: []
};
function token(value) {
this.value = value;
}
var rest = arg.reduce(function(acc, arg) {
if (typeof arg !== 'string') {
arg = String(arg);
}
if (arg.match(/^-/) && acc instanceof token) {
result[acc.value] = true;
}
if (arg.match(/^--/)) {
var name = arg.replace(/^--/, '');
if (settings.boolean.indexOf(name) === -1) {
return new token(name);
} else {
result[name] = true;
}
} else if (arg.match(/^-/)) {
var single = arg.replace(/^-/, '').split('');
if (settings.boolean.indexOf(single.slice(-1)[0]) === -1) {
var last = single.pop();
}
single.forEach(function(single) {
result[single] = true;
});
if (last) {
return new token(last);
}
} else if (acc instanceof token) {
result[acc.value] = arg;
} else if (arg) {
result._.push(arg);
}
return null;
}, null);
if (rest instanceof token) {
result[rest.value] = true;
}
return result;
}
// -----------------------------------------------------------------------------
function run(code, env) {
if (typeof code !== 'string') {
code = code.toString();
}
return exec(code, env).catch(function(e) {
console.error(e.message);
if (e.code) {
console.error(e.code.map((line, i) => `[${i + 1}]: ${line}`).join('\n'));
}
});
}
// -----------------------------------------------------------------------------
function print(result) {
if (result.length) {
var last = result.pop();
if (last !== undefined) {
console.log(env.get('repr')(last));
}
}
}
// -----------------------------------------------------------------------------
function boostrap() {
//, 'lib/R5RS.scm'
return Promise.all(['examples/helpers.lips'].map((name) => {
var path;
try {
path = require.resolve(`./${name}`);
console.log('A');
} catch (e) {
try {
path = require.resolve(`../${name}`);
console.log('B');
} catch (e) {
path = require.resolve(`@jcubic/lips/../${name}`);
console.log('C');
}
}
var data = fs.readFileSync(path);
return run(data, env);
}));
}
// -----------------------------------------------------------------------------
function indent(code, indent, offset) {
var formatter = new Formatter(code);
return formatter.indent({
indent,
offset
});
}
// -----------------------------------------------------------------------------
const options = parse_options(process.argv.slice(2));
if (options.c) {
boostrap().then(function() {
run(options.c, env).then(print);
});
} else if (options._.length === 1) {
fs.readFile(options._[0], function(err, data) {
if (err) {
console.error(err);
} else {
boostrap().then(function() {
return run(data.toString().replace(/^#!.*\n/, ''), env);
});
}
});
} else if (options.h) {
var name = process.argv[1];
console.log(format('%s\nusage:\n%s [-h]|[-c <code>]|<filename>\n\n\t-h this help message\n\t-c execute' +
' code\n\nif called without arguments it will run REPL and if called with one argument' +
'\nit will treat it as filename and execute it.', intro, name));
} else {
console.log(banner);
var prompt = 'lips> ';
var continuePrompt = '... ';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: prompt,
terminal: !!process.stdin.isTTY
});
if (process.stdin.isTTY) {
rl.prompt();
}
var code = '';
var multiline = false;
var resolve;
var e = env.inherit('name', {
stdin: InputPort(function() {
return new Promise(function(resolve) {
rl.question('', resolve);
});
}),
stdout: OutputPort(function(x) {
//rl.write(this.get('repr')(x));
console.log(this.get('repr')(x));
})
});
boostrap().then(function() {
rl.on('line', function(line) {
code += line + '\n';
if (balanced_parenthesis(code)) {
rl.pause();
run(code, e).then(function(result) {
if (process.stdin.isTTY) {
print(result);
if (multiline) {
rl.setPrompt(prompt);
}
code = '';
rl.prompt();
}
rl.resume();
}).catch(function() {
if (process.stdin.isTTY) {
if (multiline) {
rl.setPrompt(prompt);
}
rl.prompt();
}
code = '';
});
} else {
multiline = true;
var i = indent(code, 2, prompt.length - continuePrompt.length);
rl.setPrompt(continuePrompt);
rl.prompt();
rl.write(new Array(i + 1).join(' '));
}
});
});
}