Skip to content

Commit 9ed4d92

Browse files
committed
wip: import reader
1 parent d5c6818 commit 9ed4d92

File tree

7 files changed

+86
-37
lines changed

7 files changed

+86
-37
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,8 @@
7171
"webpack": "^4.41.4",
7272
"webpack-cli": "^3.3.10",
7373
"webpack-shell-plugin": "^0.5.0"
74+
},
75+
"dependencies": {
76+
"node-fetch": "^2.6.0"
7477
}
7578
}

src/cli.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,21 @@ if (emptyCall || showHelp) {
6161

6262
program.parse(process.argv);
6363

64-
preprocess();
65-
66-
if (program.compile) {
67-
output(getCompiled());
68-
} else if (program.render) {
69-
doRender();
70-
} else if (program.interactive) {
71-
intreactive();
72-
} else {
73-
exec();
74-
}
64+
(async () => {
65+
preprocess();
66+
67+
if (program.compile) {
68+
output(await getCompiled());
69+
} else if (program.render) {
70+
doRender();
71+
} else if (program.interactive) {
72+
await intreactive();
73+
} else {
74+
await exec();
75+
}
76+
})().catch(e => {
77+
console.error(e);
78+
});
7579

7680
// ====== Utils ======
7781

@@ -103,16 +107,27 @@ function preprocess() {
103107

104108
function getCompiled() {
105109
const source = getSource();
110+
const importPaths = getImportPaths();
111+
// console.log(importPaths)
112+
106113
return compile(program.lang, source, {
107114
romanizeIdentifiers: program.roman,
108115
logCallback: logHandler(program.log, "a"),
109116
errorCallback: function(x) {
110117
console.error(x);
111118
process.exit();
112-
}
119+
},
120+
importPaths
113121
});
114122
}
115123

124+
function getImportPaths() {
125+
const dir = new Set(
126+
program.files.map(file => path.resolve(path.dirname(file)))
127+
);
128+
return [...dir, path.resolve(".")];
129+
}
130+
116131
function resolvePath(x) {
117132
return path.resolve(x);
118133
}
@@ -125,6 +140,7 @@ function getSource() {
125140
: fs.readFileSync(resolvePath(x)).toString()
126141
)
127142
.join("\n");
143+
128144
if (program.eval) scripts += `\n${program.eval}`;
129145

130146
return scripts;
@@ -176,25 +192,25 @@ function doRender() {
176192
}
177193
}
178194

179-
function intreactive() {
195+
async function intreactive() {
180196
if (program.lang !== "js") {
181197
console.error(
182198
`Target language "${program.lang}" is not supported for intreactive mode.`
183199
);
184200
process.exit(1);
185201
}
186202
replscope();
187-
repl(getCompiled());
203+
repl(await getCompiled());
188204
}
189-
function exec() {
205+
async function exec() {
190206
if (program.lang !== "js") {
191207
console.error(
192208
`Target language "${program.lang}" is not supported for direct executing. Please use --compile option instead.`
193209
);
194210
process.exit(1);
195211
}
196212

197-
evalCompiled(getCompiled(), {
213+
evalCompiled(await getCompiled(), {
198214
outputHanzi: program.outputHanzi,
199215
lang: program.lang
200216
});

src/macro.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ async function extractMacros(txt, { lib, reader, lang, importPaths }) {
108108
} else {
109109
isrc = await reader(imports[i], importPaths);
110110
}
111-
macros = macros.concat(await extractMacros(isrc, { lib, reader, lang }));
111+
macros = macros.concat(
112+
await extractMacros(isrc, { lib, reader, lang, importPaths })
113+
);
112114
}
113115
return macros;
114116
}

src/reader.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1+
function getFetch() {
2+
if (typeof window != "undefined" && window.fetch) return window.fetch;
3+
else return eval("require")("node-fetch");
4+
}
5+
16
async function defaultImportReader(moduleName, importPaths = []) {
2-
let fs, path;
3-
try {
4-
fs = eval("require")("fs");
5-
path = eval("require")("path");
6-
} catch (e) {
7-
throw new Error(
8-
`Cannot import ${moduleName}, please specify the "reader" option in compile. ${e}`
9-
);
10-
}
7+
const fetch = getFetch();
118

129
if (typeof importPaths === "string") importPaths = [importPaths];
1310

1411
for (dir of importPaths) {
15-
try {
16-
return fs.readFileSync(path.join(dir, moduleName + ".wy"), "utf-8");
17-
} catch (e) {}
12+
const filepath = dir + "/" + moduleName + ".wy";
13+
if (filepath.match(/^\w+\:\/\//)) {
14+
try {
15+
const data = await fetch(filepath);
16+
const text = await data.text();
17+
return text;
18+
} catch (e) {}
19+
} else {
20+
try {
21+
return eval("require")("fs").readFileSync(filepath, "utf-8");
22+
} catch (e) {}
23+
}
1824
}
1925

20-
try {
21-
return fs.readFileSync(moduleName + ".wy", "utf-8");
22-
} catch (e) {
23-
throw new Error(
24-
`Module "${moduleName}" is not found. Searched in ${importPaths}`
25-
);
26-
}
26+
throw new Error(
27+
`Module "${moduleName}" is not found. Searched in ${importPaths}`
28+
);
2729
}
2830

2931
try {
3032
module.exports = {
33+
normalizeImportPath,
3134
defaultImportReader
3235
};
3336
} catch (e) {}

test/__snapshots__/examples.test.js.mocha-snapshot

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,20 @@ exports["examples/romanizeIdentifiers/draw_heart(0)"] = "一一一一一一一
129129
exports["examples/romanizeIdentifiers/macro(0)"] = "問天地好在\n二。三。四。五。六。七\n二。三。四。五。六。七\n四。五。六\n四。五。六\n二十一\n二十一\n二十一\n";
130130

131131
exports["examples/romanizeIdentifiers/pi_liuhui(0)"] = "割圓 三千零七十二 觚。所得圓率者。\n三又一分四釐一毫五絲九忽二微一纖零五塵九埃九渺九漠 也。\n";
132+
133+
exports["examples/javascript/misc(1)"] = "問天地好在。\n甲之數 三十 也\n一\n二\n三\n四\n五\n六\n七\n八\n九\n一十\n一十一\n一十二\n一十三\n一十四\n一十五\n一十六\n一十七\n一十八\n一十九\n二十\n二十一\n二十二\n二十三\n二十四\n二十五\n二十六\n二十七\n二十八\n二十九\n三。五。八。二十。三十五。七百\n";
134+
135+
exports["examples/javascript/multiplication_table(1)"] = "一 一 得 一\n一 二 得 二\n二 二 得 四\n一 三 得 三\n二 三 得 六\n三 三 得 九\n一 四 得 四\n二 四 得 八\n三 四 一十二\n四 四 一十六\n一 五 得 五\n二 五 一十\n三 五 一十五\n四 五 二十\n五 五 二十五\n一 六 得 六\n二 六 一十二\n三 六 一十八\n四 六 二十四\n五 六 三十\n六 六 三十六\n一 七 得 七\n二 七 一十四\n三 七 二十一\n四 七 二十八\n五 七 三十五\n六 七 四十二\n七 七 四十九\n一 八 得 八\n二 八 一十六\n三 八 二十四\n四 八 三十二\n五 八 四十\n六 八 四十八\n七 八 五十六\n八 八 六十四\n一 九 得 九\n二 九 一十八\n三 九 二十七\n四 九 三十六\n五 九 四十五\n六 九 五十四\n七 九 六十三\n八 九 七十二\n九 九 八十一\n";
136+
137+
exports["examples/javascript/obj(1)"] = "{\"丙\":\"某甲\",\"丁\":5}\n{\"丙\":\"某甲\"}\n";
138+
139+
exports["examples/javascript/pi_liuhui(1)"] = "割圓 三千零七十二 觚。所得圓率者。\n三又一分四釐一毫五絲九忽二微一纖零五塵九埃九渺九漠 也。\n";
140+
141+
exports["examples/javascript/quicksort_inplace(1)"] = "陽 一。二。三。四。五\n";
142+
143+
exports["examples/javascript/quine2(1)"] = "吾有一術。名之曰星甲自己複製星乙。星丁欲行是術。是術曰。星丁星丁吾有一術。名之曰星甲置換星乙。星丁欲行是術。必先得三數。星丁曰星甲文字列星乙。曰星甲置換対象星乙。曰星甲置換先星乙是術曰。星丁星丙吾有一言。曰星甲星甲星乙星乙。名之曰星甲返品星乙。星丁星丙吾有一數。曰二。名之曰星甲未来星乙。星丁星丙吾有一數。曰陽。名之曰星甲実行条件星乙。星丁星丙凡星甲文字列星乙中之星甲文字星乙。星丁星丙星丙星丙若星甲実行条件星乙者。星丁星丙星丙星丙星丙若星甲文字星乙等於星甲星甲星星乙星乙星乙中無陰乎星甲文字列星乙之未来等於星甲置換対象星乙者。星丁星丙星丙星丙星丙星丙加星甲返品星乙以星甲置換先星乙。昔之星甲返品星乙者。今其是矣。星丁星丙星丙星丙星丙星丙昔之星甲実行条件星乙者。今陰是。星丁星丙星丙星丙星丙若非。星丁星丙星丙星丙星丙星丙加星甲返品星乙以星甲文字星乙。昔之星甲返品星乙者。今其是矣。星丁星丙星丙星丙星丙也。星丁星丙星丙星丙若非。星丁星丙星丙星丙星丙昔之星甲実行条件星乙者。今陽是。星丁星丙星丙星丙也。星丁星丙星丙加星甲未来星乙以一。昔之星甲未来星乙者。今其是矣。星丁星丙云云。星丁星丙乃得星甲返品星乙。星丁是謂星甲置換星乙之術也。星丁星丁吾有一言。曰String.fromCharCode名之曰星甲函数星乙。星丁施星甲函数星乙於一萬二千三百。昔之星甲始括弧星乙者。今其是矣。星丁施星甲函数星乙於一萬二千三百零一。昔之星甲終括弧星乙者。今其是矣。星丁施星甲函数星乙於三十二。昔之星甲空白星乙者。今其是矣。星丁加星甲空白星乙以星甲空白星乙。昔之星甲空白星乙者。今其是矣。星丁施星甲函数星乙於十。昔之星甲改行星乙者。今其是矣。星丁吾有一言。曰星甲星甲星戊星乙星乙。名之曰星甲原文星乙。星丁吾有一言。曰星甲星甲星乙星乙。名之曰星甲結果星乙。星丁施星甲置換星乙於星甲原文星乙。於星甲星甲甲星乙星乙。於星甲始括弧星乙。昔之星甲結果星乙者。今其是矣。星丁施星甲置換星乙於星甲結果星乙。於星甲星甲乙星乙星乙。於星甲終括弧星乙。昔之星甲結果星乙者。今其是矣。星丁施星甲置換星乙於星甲結果星乙。於星甲星甲丙星乙星乙。於星甲空白星乙。昔之星甲結果星乙者。今其是矣。星丁施星甲置換星乙於星甲結果星乙。於星甲星甲丁星乙星乙。於星甲改行星乙。昔之星甲結果星乙者。今其是矣。星丁施星甲置換星乙於星甲結果星乙。於星甲星甲戊星乙星乙。於星甲原文星乙。昔之星甲結果星乙者。今其是矣。星丁吾有一言。曰星甲結果星乙。書之。星丁星丁是謂星甲自己複製星乙之術也。星丁施星甲自己複製星乙。\n";
144+
145+
exports["examples/javascript/sieve(1)"] = "二。三。五。七。一十一。一十三。一十七。一十九。二十三。二十九。三十一。三十七。四十一。四十三。四十七。五十三。五十九。六十一。六十七。七十一。七十三。七十九。八十三。八十九。九十七\n";
146+
147+
exports["examples/javascript/try(1)"] = "嗚呼哀哉。伏维尚飨\n事不關心\n";
148+

test/examples.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ async function runExample(lang, name, options = {}) {
3636

3737
var compiled = await compile(lang, code, {
3838
logCallback: () => {},
39-
importPaths: [path.resolve(__dirname, "../examples/")],
39+
importPaths: [
40+
path.resolve(__dirname, "../lib/"),
41+
path.resolve(__dirname, "../examples/")
42+
],
4043
lib: lib,
4144
...options
4245
});

0 commit comments

Comments
 (0)