-
Notifications
You must be signed in to change notification settings - Fork 0
/
quine.js
154 lines (149 loc) · 5.04 KB
/
quine.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
const process = require('process');
const fs = require('fs');
const child_process = require('child_process');
function help() {
console.log(fs.readFileSync('help.txt', { encoding: 'utf-8' }));
}
function render(template, details) {
let result = '';
let i = 0;
while (i < template.length) {
let c = template[i];
if (c == '$') {
let j = 1;
while (template[i + j] != '$') {
j += 1;
}
let item = details.get(template.slice(i + 1, i + j));
result += '"' + item[1] + details.get(item[4])[3] + '"';
i += j;
} else {
result += c;
}
i += 1;
}
return result;
}
async function build() {
let filename = process.argv[3];
let langs = fs
.readFileSync(filename, { encoding: 'utf-8' })
.split('\n')
.filter(l => l != '')
.map(l => l.split('#'));
let details = await Promise.all(langs.map(async lang =>
[lang[0], [await new Promise((resolve,) => {
child_process.exec(lang[1] + ' template ' + filename, (err, stdout) => {
resolve(stdout.trim());
});
})]]
));
for (let i = 0; i < langs.length; i++) {
let exec_str = await new Promise((resolve,) => {
let proc = child_process.exec(langs[i][1] + ' exec', (err, stdout) => {
resolve(stdout);
});
proc.stdin.write(details[(i + 1) % langs.length][1][0] + '\n');
proc.stdin.end();
});
let exec_strs = exec_str.split('\n');
details[i][1].push(exec_strs[0]);
details[i][1].push(exec_strs.slice(1).join('\n').trim());
//
let antied = await new Promise((resolve,) => {
let proc = child_process.exec(langs[(i - 1 + langs.length) % langs.length][1] + ' anti-escape', (err, stdout) => {
resolve(stdout.trim());
});
proc.stdin.write(details[i][1][2] + '\n');
proc.stdin.end();
})
details[i][1].push(antied);
details[i][1].push(details[(i + 1) % langs.length][0]);
}
let detail_dict = new Map(details);
let code = render(details[0][1][0], detail_dict) + details[0][1][2];
let codefilename = process.argv[4];
fs.writeFileSync(codefilename, code + '\n');
let bashlist = ['cat ' + codefilename];
for (let lang of langs) {
bashlist.push(lang[2]);
}
bashlist.push(`diff ${codefilename} -`);
console.log('try run:', bashlist.join('|'));
}
function main() {
switch (process.argv[2]) {
case 'template':
let temp = fs
.readFileSync(process.argv[3], { encoding: 'utf-8' })
.split('\n')
.filter(l => l != '')
.map(l => l.split('#')[0])
.map(lang => `${lang}=$${lang}$;`).join('');
temp += '_=String.fromCharCode(34,10);__=JSON.stringify;';
console.log(temp);
break
case 'exec':
process.stdin.on('data', (data) => {
let temp = data.toString('utf-8').trim();
let exec_str = '';
let args = [];
let i = 0;
while (i < temp.length) {
let c = temp[i];
if (c == '"') {
exec_str += '%s';
args.push('_[0]');
} else if (c == '\n') {
exec_str += '%s';
args.push('_[1]');
} else if (c == '$') {
j = 1
while (temp[i + j] != '$') {
j += 1;
}
exec_str += '%s';
args.push('__(' + temp.slice(i + 1, i + j) + ')');
i += j;
} else {
exec_str += c;
}
i += 1;
}
let final_part = `console.log(javascript,${args.join(',')})`;
console.log(exec_str);
console.log(final_part);
});
process.stdin.read();
break
case 'anti-escape':
process.stdin.on('data', (data) => {
let temp = data.toString('utf-8').trim();
let exec_str = '';
let args = [];
let i = 0;
while (i < temp.length) {
let c = temp[i];
if (c == '%') {
exec_str += '%%';
} else {
exec_str += c;
}
i += 1;
}
console.log(exec_str);
});
process.stdin.read();
break
case 'build':
build()
break
case 'help':
help()
break
default:
console.log('unkown opntions, use "help" to get help.')
help()
}
}
main()