-
Notifications
You must be signed in to change notification settings - Fork 12
/
shen-node.js
executable file
·174 lines (150 loc) · 4.29 KB
/
shen-node.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
#!/usr/bin/env node
module.exports = (function() {
var usage = ["Usage: shen-node.js [-noinit] [-c target_file] <files...>"],
shen = require("./shen"),
fs = require("fs"),
stream = require("stream"),
self = {},
home = process.env[(process.platform == "win32")
? "USERPROFILE" : "HOME"];
function main() {
var i, compile_dest, init = true;
for (i = 2; i < process.argv.length && process.argv[i].match(/^-/); ++i) {
switch (process.argv[i]) {
case "-noinit": init = false; break;
case "-c": case "-compile": compile_dest = process.argv[++i]; break;
default: usage_die();
}
}
if (init)
read_init_file(start);
else
start();
function start() {
if (i >= process.argv.length)
shen.start_repl();
else if (compile_dest)
compile(process.argv, i, compile_dest);
else
on_files(process.argv, i, load, function() {process.exit()});
}
}
function mk_read_byte(stream, vm) {
var chan = shen.Chan();
chan.end = -1;
stream.vm = vm;
stream.on("error", function(err) {
chan.write(new Error(err), stream.vm);
});
stream.on("data", function(data) {
var i, vm = stream.vm;
for (i = 0; i < data.length; ++i)
chan.write(data[i], vm);
});
stream.on("end", function() {
chan.close(stream.vm);
});
return function(vm) {
stream.vm = vm;
return chan.read(vm);
};
}
function mk_write_byte(stream) {
return function(byte, vm) {
var ret = stream.write(new Buffer([byte]), null, function(err) {
vm.resume(err ? err : byte);
});
vm.interrupt();
}
}
function mk_close(stream) {
return function() {
if (stream.end)
stream.end();
stream.removeAllListeners();
stream.on("error", function() {});
}
}
function wrap_stream(stream, dir, vm) {
switch (dir) {
case "r":
return shen.Stream(mk_read_byte(stream, vm), null, mk_close(stream));
case "w":
return shen.Stream(null, mk_write_byte(stream), mk_close(stream));
case "rw": case "wr": case "w+": case "r+":
return shen.Stream(mk_read_byte(stream, vm), mk_write_byte(stream),
mk_close(stream));
default: throw new Error("Unsupported stream type: " + dir);
}
}
function fix_path(path) {
return path.replace(/^~/, home);
}
function open(name, dir, vm) {
var path = fix_path(vm.glob["*home-directory*"] + name);
switch (dir.str) {
case "in":
wait_result(fs.createReadStream(path), "readable", function(err) {
vm.resume((err) ? err : wrap_stream(this, "r", vm));
});
break;
case "out":
wait_result(fs.createWriteStream(path), "drain", function(err) {
vm.resume((err) ? err : wrap_stream(this, "w", vm));
});
break;
default: return vm.error("Unsupported 'open' flags");
}
function wait_result(stream, ev, onready) {
stream.once("error", onready);
stream.once(ev, onready);
vm.interrupt();
}
}
function io(vm) {
var io = {};
io.open = open;
vm.glob["*stinput*"] = wrap_stream(process.stdin, "r", vm);
vm.glob["*stoutput*"] = wrap_stream(process.stdout, "w", vm);
return io;
}
function load(src, callback) {
shen.exec("load", [src], callback);
}
function on_files(files, i, fn, callback) {
if (i < files.length)
fn(files[i], function() {
on_files(files, i + 1, fn, callback);
});
else if (callback)
callback();
}
function compile(files, i, dest, callback) {
var list = shen.list(files.slice(i))
shen.exec("js.save-from-files", [list, dest], callback);
}
function read_init_file(callback) {
var path = home + "/.shen.shen";
fs.access(path, fs.R_OK, function(err) {
if (err)
callback();
else {
shen.glob["*hush*"] = true;
shen.exec("load", [path], function() {
shen.glob["*hush*"] = false;
callback();
});
}
});
}
function usage_die() {
process.stdout.write(usage.join(" ") + "\n");
process.exit(1);
}
self.wrap_stream = wrap_stream;
self.main = main;
shen.init({io: io, async: true});
return self;
})();
if (require.main === module)
module.exports.main();