-
Notifications
You must be signed in to change notification settings - Fork 2
/
repl.js
148 lines (126 loc) · 3.53 KB
/
repl.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
const readline = require('readline');
const process = require('process');
const path = require('path');
const fs = require('fs');
const zlib = require('zlib');
const ty = require('./elisp/types');
const parser = require('./elisp/parser');
const translate = require('./elisp/translate');
const elisp = require('./elisp/elisp');
var env = new elisp.Environment();
/* put special forms in the env */
translate.special_forms.forEach((form) => {
let dummy = ty.subr(form, [], function() {
throw new Error("Congratulations! You've just called a dummy, which should never happen");
});
env.fset(form, dummy);
});
env.fset('load', ty.subr('load', [], function(args) {
if (!ty.is_string(args[0]))
throw new ty.LispError('Wrong type argument: stringp, ' + args[0].to_string());
let tryFileName = (fname) => {
let tryfile = (f) => {
try { fs.accessSync(f, fs.constants.R_OK); return true; }
catch (e) { return false };
};
if (tryfile(fname)) return fname;
if (tryfile(fname + '.el')) return fname + '.el';
if (tryfile(fname + '.gz')) return fname + '.gz';
if (tryfile(fname + '.el.gz')) return fname + '.el.gz';
};
let filename = args[0].to_js();
let fullpath;
if (path.isAbsolute(filename)) {
fullpath = tryFileName(filename);
} else if (this.is_bound('load-path')) {
let loadpath = this.get('load-path');
for (let p = loadpath; !p.is_false; p = p.tl) {
let base = path.resolve(p.hd.is_false ? process.cwd() : p.hd.to_js());
fullpath = tryFileName(path.join(base, filename));
if (fullpath)
break;
}
} else
throw new ty.LispError('`load-path` is not set');
if (!fullpath)
throw new ty.LispError('Cannot open load file: ' + filename);
let file = fs.readFileSync(fullpath);
if (fullpath.endsWith('.gz'))
file = zlib.gunzipSync(file);
let text = file.toString('utf-8');
let forms = elisp.readtop(text, fullpath);
forms.forEach((form) => {
elisp.eval_lisp(form, this)
});
return ty.t;
}));
/*
* prelude
*/
let prelude = [
`(fset 'defmacro
'(macro lambda (name args body)
(list 'fset (list 'quote name) (list 'quote (list 'macro 'lambda args body)))))`,
`(fset 'defun
'(macro lambda (name args body)
(list 'fset (list 'quote name) (list 'lambda args body))))`,
`(fset 'defvar
'(macro lambda (name val)
(list 'setq name val)))`,
`(setq *jsdebug* nil)`,
];
prelude.forEach((stmt) => elisp.eval_text(stmt, env));
/*
* Arguments
*/
let argv = process.argv;
switch (argv[argv.length - 1]) {
case '--help':
console.error('Elisp to JS translator');
process.exit();
};
/*
* completer
*/
function completer(line) {
let par = line.lastIndexOf('(');
let gap = line.lastIndexOf(' ');
let start = ((par > gap) ? par : gap ) + 1;
let key = line.slice(start);
if (key.length == 0)
return [[], line];
let hits = [];
for (let name in ((par > gap ? env.fs : env.vs))) {
if (name.startsWith(key))
hits.push(name);
}
return [hits, key];
}
/*
* repl loop
*/
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'elisp> ',
completer: completer
});
rl.prompt();
rl.on('line', (line) => {
line = line.trim();
if (!line) return;
try {
let result = elisp.eval_text(line, env);
console.log(result);
} catch (e) {
let jsdebug = env.has_jsdebug();
if (e instanceof ty.LispError && !jsdebug)
console.error(e.name + ': ' + e.message);
else
console.error(e.stack);
};
rl.prompt();
});
rl.on('close', () => {
console.log('');
});