@live-codes/browser-compilers 0.4.9 → 0.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,96 +1,85 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
1
2
|
// based on https://github.com/MikeRalphson/turbopascal/blob/master/tpc.js
|
|
2
3
|
const turbopascal = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
createCompiler: (options = {}) =>
|
|
5
|
+
new Promise((resolve) => {
|
|
6
|
+
const inputCallback =
|
|
7
|
+
options.inputCallback ||
|
|
8
|
+
((callback) => {
|
|
9
|
+
const input = window.prompt('please supply the input');
|
|
10
|
+
callback(input);
|
|
11
|
+
});
|
|
12
|
+
const outputCallback = options.outputCallback || console.log;
|
|
13
|
+
const errorCallback = options.errorCallback || console.warn;
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
'Compiler',
|
|
18
|
-
'Machine',
|
|
19
|
-
'SymbolTable',
|
|
20
|
-
], function (
|
|
21
|
-
Stream,
|
|
22
|
-
Lexer,
|
|
23
|
-
CommentStripper,
|
|
24
|
-
Parser,
|
|
25
|
-
PascalError,
|
|
26
|
-
Compiler,
|
|
27
|
-
Machine,
|
|
28
|
-
SymbolTable,
|
|
29
|
-
) {
|
|
30
|
-
function compile(source, run, DEBUG_TRACE) {
|
|
31
|
-
let self = this;
|
|
15
|
+
requirejs.config({
|
|
16
|
+
baseUrl: 'https://cdn.jsdelivr.net/npm/turbopascal@1.0.3',
|
|
17
|
+
paths: {
|
|
18
|
+
underscore: 'https://cdn.jsdelivr.net/npm/underscore@1.13.2/underscore-umd.min',
|
|
19
|
+
},
|
|
20
|
+
});
|
|
32
21
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
22
|
+
require([
|
|
23
|
+
'Stream',
|
|
24
|
+
'Lexer',
|
|
25
|
+
'CommentStripper',
|
|
26
|
+
'Parser',
|
|
27
|
+
'Compiler',
|
|
28
|
+
'Machine',
|
|
29
|
+
'SymbolTable',
|
|
30
|
+
], function (Stream, Lexer, CommentStripper, Parser, Compiler, Machine, SymbolTable) {
|
|
31
|
+
function compile(source, run, DEBUG_TRACE) {
|
|
32
|
+
let result = { parsed: false, compiled: false };
|
|
33
|
+
if (!source) return result;
|
|
40
34
|
|
|
41
|
-
|
|
35
|
+
let stream = new Stream(source);
|
|
36
|
+
let lexer = new CommentStripper(new Lexer(stream));
|
|
37
|
+
let parser = new Parser(lexer);
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
try {
|
|
40
|
+
// Create the symbol table of built-in constants, functions, and procedures.
|
|
41
|
+
let builtinSymbolTable = SymbolTable.makeBuiltinSymbolTable();
|
|
46
42
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
// Parse the program into a parse tree. Create the symbol table as we go.
|
|
44
|
+
let root = parser.parse(builtinSymbolTable);
|
|
45
|
+
result.tree = root.print('');
|
|
46
|
+
result.parsed = true;
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
48
|
+
// Compile to bytecode.
|
|
49
|
+
let compiler = new Compiler();
|
|
50
|
+
let bytecode = compiler.compile(root);
|
|
51
|
+
result.compiled = true;
|
|
52
|
+
result.bytecode = bytecode;
|
|
53
|
+
result.pSrc = bytecode.print();
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
55
|
+
if (run) {
|
|
56
|
+
// Execute the bytecode.
|
|
57
|
+
let machine = new Machine(bytecode, this.keyboard);
|
|
58
|
+
if (DEBUG_TRACE) {
|
|
59
|
+
machine.setDebugCallback(function (state) {
|
|
60
|
+
$state.append(state + '\\n');
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
machine.setFinishCallback(function (runningTime) {});
|
|
64
|
+
machine.setOutputCallback(function (line) {
|
|
65
|
+
outputCallback(line);
|
|
65
66
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
machine.setOutputCallback(function (line) {
|
|
69
|
-
console.log(line);
|
|
70
|
-
});
|
|
71
|
-
machine.setOutChCallback(function (line) {
|
|
72
|
-
console.log(line);
|
|
73
|
-
});
|
|
74
|
-
machine.setInputCallback(function (callback) {
|
|
75
|
-
self.screen.addCursor();
|
|
76
|
-
self._setInputMode(INPUT_STRING, function (line) {
|
|
77
|
-
self._setInputMode(INPUT_RUNNING);
|
|
78
|
-
callback(line);
|
|
67
|
+
machine.setOutChCallback(function (line) {
|
|
68
|
+
outputCallback(line);
|
|
79
69
|
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
70
|
+
machine.setInputCallback(function (callback) {
|
|
71
|
+
inputCallback(callback);
|
|
72
|
+
});
|
|
73
|
+
machine.run();
|
|
74
|
+
}
|
|
75
|
+
} catch (e) {
|
|
76
|
+
// Print errors.
|
|
77
|
+
const msg = e.getMessage() || e.message || e;
|
|
78
|
+
errorCallback(msg);
|
|
88
79
|
}
|
|
89
|
-
|
|
80
|
+
return result;
|
|
90
81
|
}
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
});
|
|
95
|
-
}),
|
|
82
|
+
resolve(compile);
|
|
83
|
+
});
|
|
84
|
+
}),
|
|
96
85
|
};
|