Skip to content

Commit 26d41cd

Browse files
committed
feat: compile api
1 parent 4870b17 commit 26d41cd

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

src/macro.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function extractMacros(lang, txt, { lib, reader }) {
1+
function extractMacros(txt, { lib, reader, lang }) {
22
function getImports() {
33
var imps = [];
44
for (var i = 0; i < txt.length; i++) {
@@ -108,7 +108,7 @@ function extractMacros(lang, txt, { lib, reader }) {
108108
} else {
109109
isrc = reader(imports[i]);
110110
}
111-
macros = macros.concat(extractMacros(lang, isrc, { lib, reader }));
111+
macros = macros.concat(extractMacros(isrc, { lib, reader, lang }));
112112
}
113113
return macros;
114114
}

src/parser.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,21 @@ function defaultReader(x) {
653653
}
654654
}
655655

656-
function compile(lang, txt, options = {}) {
656+
function compile(arg1, arg2, arg3) {
657+
let options = {};
658+
let txt = "";
659+
660+
if (typeof arg2 === "string") {
661+
// backward compatible
662+
txt = arg2;
663+
options = { ...arg3, lang: arg1 };
664+
} else {
665+
txt = arg1;
666+
options = arg2;
667+
}
668+
657669
const {
670+
lang = "js",
658671
romanizeIdentifiers = "none",
659672
resetVarCnt,
660673
logCallback = x =>
@@ -695,7 +708,7 @@ function compile(lang, txt, options = {}) {
695708
return 0;
696709
}
697710

698-
var macros = extractMacros(lang, txt, { lib, reader });
711+
var macros = extractMacros(txt, { lib, reader, lang });
699712
txt = expandMacros(txt, macros);
700713

701714
logCallback("\n\n=== [PASS 0] EXPAND-MACROS ===");
@@ -743,7 +756,8 @@ function compile(lang, txt, options = {}) {
743756
targ =
744757
mwrapper(
745758
imports[i],
746-
compile(lang, isrc, {
759+
compile(isrc, {
760+
lang,
747761
romanizeIdentifiers,
748762
resetVarCnt: false,
749763
logCallback,
@@ -816,7 +830,7 @@ function evalCompiled(compiledCode, options = {}) {
816830
function execute(source, options = {}) {
817831
const { lang = "js" } = options;
818832
isLangSupportedForEval(lang);
819-
const compiled = compile(lang, source, options);
833+
const compiled = compile(source, options);
820834
evalCompiled(compiled, options);
821835
}
822836

tools/make_ide.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ function main() {
143143
function run() {
144144
highlightCode();
145145
document.getElementById("out").innerText = "";
146-
var code = compile("js", ed.innerText, {
146+
var code = compile(ed.innerText, {
147+
lang: "js",
147148
romanizeIdentifiers: selr.value,
148149
resetVarCnt: true,
149-
errorCallback: log2div,
150+
errorCallback: (...args) => (outdiv.innerText += args.join(" ") + "\n"),
150151
lib: STDLIB,
151152
reader: x => prgms[x]
152153
});

tools/make_site.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function main() {
4040
highlight([ed]);
4141
out.innerText = "";
4242
var hasError = false;
43-
var code = compile("js", ed.innerText, {
43+
var code = compile(ed.innerText, {
44+
lang: "js",
4445
romanizeIdentifiers: "none",
4546
errorCallback: function(x) {
4647
hasError = true;
@@ -50,13 +51,15 @@ function main() {
5051
if (i == 0) {
5152
document.getElementById("js").innerText =
5253
"// JavaScript\n" + js_beautify(code);
53-
var py = compile("py", ed.innerText, {
54+
var py = compile(ed.innerText, {
55+
lang: "py",
5456
romanizeIdentifiers: "none",
5557
errorCallback: () => 0
5658
});
5759
document.getElementById("py").innerText =
5860
"# Python\n" + py.split("#####\n")[1];
59-
var rb = compile("rb", ed.innerText, {
61+
var rb = compile(ed.innerText, {
62+
lang: "rb",
6063
romanizeIdentifiers: "none",
6164
errorCallback: () => 0
6265
});

0 commit comments

Comments
 (0)