Skip to content

Commit 4ce2e3d

Browse files
committed
fix: 🐛 fix compiling, now stdlib sources placed in compiled file
1 parent 34d984a commit 4ce2e3d

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

src/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import UglifyJS from 'uglify-js';
2+
13
import Parser from './parser/Parser.js';
24
import InputStream from './tokenizer/InputStream.js';
35
import TokenStream from './tokenizer/TokenStream.js';
46

57
import evaluate from './runtime/Evaluator.js';
68
import Executor from './runtime/Executor.js';
79

8-
import makeEnv from './stdlib/StdEnvJS.js';
9-
import stdEnv from './stdlib/StdEnvYAS.js';
10-
import makeCompilerEnv from './stdlib/StdCompilerEnv.js';
10+
import makeEnv from './stdlib/interpreter/StdEnvJS.js';
11+
import stdEnv from './stdlib/interpreter/StdEnvYAS.js';
12+
13+
import makeCompilerEnv from './stdlib/compiler/StdCompilerEnv.js';
1114

1215
import CompilerJS from './codegen/CompilerJS.js';
1316

@@ -27,9 +30,9 @@ export function compile(code) {
2730
const tokenStream = new TokenStream(inputStream);
2831
const parser = new Parser(tokenStream);
2932
const ast = parser.parse();
30-
makeCompilerEnv();
3133
const compiler = new CompilerJS(stdEnv(ast));
32-
return compiler.compile();
34+
const js = makeCompilerEnv() + compiler.compile();
35+
return UglifyJS.minify(js).code;
3336
}
3437

3538
export function runJS(jsCode) {

src/stdlib/StdCompilerEnv.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export default function makeCompilerEnv() {
2+
return `
3+
global.print = function(...txt) {
4+
txt.forEach(i => process.stdout.write(i.toString()));
5+
};
6+
7+
global.println = function(...txt) {
8+
txt.forEach(i => console.log(i));
9+
};
10+
11+
global.clear = console.clear;
12+
13+
global.time = (callback, func) => {
14+
console.log(callback, func);
15+
console.time('time');
16+
func((ret) => {
17+
console.timeEnd('time');
18+
callback(ret);
19+
});
20+
};
21+
22+
global.sleep = (callback, delay) => {
23+
setTimeout(() => {
24+
callback();
25+
}, delay);
26+
};
27+
28+
global.callcc = (callback, fn, ...args) => {
29+
fn(callback, (discard, ret) => {
30+
callback(ret);
31+
}, ...args);
32+
};
33+
34+
global.halt = (k) => {};
35+
36+
global.pstack = [];
37+
38+
global._goto = function(f) {
39+
f((r) => {
40+
const h = pstack.pop();
41+
h(r);
42+
});
43+
};
44+
45+
global.reset = function(KRESET, th) {
46+
pstack.push(KRESET);
47+
_goto(th);
48+
};
49+
50+
global.shift = function(KSHIFT, f) {
51+
_goto((KGOTO) => {
52+
f(KGOTO, (k1, v) => {
53+
pstack.push(k1);
54+
KSHIFT(v);
55+
});
56+
});
57+
};`;
58+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Environment from '../runtime/Environment.js';
2-
import Executor from '../runtime/Executor.js';
1+
import Environment from '../../runtime/Environment.js';
2+
import Executor from '../../runtime/Executor.js';
33

44
export default function makeEnv() {
55
const globalEnv = new Environment();
@@ -49,6 +49,7 @@ export default function makeEnv() {
4949
h(r);
5050
});
5151
}
52+
globalEnv.def('_goto', _goto);
5253

5354
function reset(KRESET, th) {
5455
pstack.push(KRESET);
@@ -64,6 +65,7 @@ export default function makeEnv() {
6465
});
6566
});
6667
}
68+
globalEnv.def('shift', shift);
6769

6870
return globalEnv;
6971
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Parser from '../parser/Parser.js';
2-
import InputStream from '../tokenizer/InputStream.js';
3-
import TokenStream from '../tokenizer/TokenStream.js';
1+
import Parser from '../../parser/Parser.js';
2+
import InputStream from '../../tokenizer/InputStream.js';
3+
import TokenStream from '../../tokenizer/TokenStream.js';
44

55
export default function stdEnv(ast) {
66
const std = `def cons = fn(a, b) -> fn(f) -> f(a, b);

0 commit comments

Comments
 (0)