@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,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-codes/browser-compilers",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
4
4
  "description": "Compilers that run in the browser, for use in livecodes.io",
5
5
  "author": "Hatem Hosny",
6
6
  "license": "MIT",
@@ -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
- compiler: new Promise((resolve) => {
4
- requirejs.config({
5
- baseUrl: 'https://cdn.jsdelivr.net/npm/turbopascal@1.0.3',
6
- paths: {
7
- underscore: 'https://cdn.jsdelivr.net/npm/underscore@1.13.2/underscore-umd.min',
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
- require([
12
- 'Stream',
13
- 'Lexer',
14
- 'CommentStripper',
15
- 'Parser',
16
- 'PascalError',
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
- if (!source) {
34
- return;
35
- }
36
-
37
- let stream = new Stream(source);
38
- let lexer = new CommentStripper(new Lexer(stream));
39
- let parser = new Parser(lexer);
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
- let result = { parsed: false, compiled: false };
35
+ let stream = new Stream(source);
36
+ let lexer = new CommentStripper(new Lexer(stream));
37
+ let parser = new Parser(lexer);
42
38
 
43
- try {
44
- // Create the symbol table of built-in constants, functions, and procedures.
45
- let builtinSymbolTable = SymbolTable.makeBuiltinSymbolTable();
39
+ try {
40
+ // Create the symbol table of built-in constants, functions, and procedures.
41
+ let builtinSymbolTable = SymbolTable.makeBuiltinSymbolTable();
46
42
 
47
- // Parse the program into a parse tree. Create the symbol table as we go.
48
- let root = parser.parse(builtinSymbolTable);
49
- result.tree = root.print('');
50
- result.parsed = true;
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
- // Compile to bytecode.
53
- let compiler = new Compiler();
54
- let bytecode = compiler.compile(root);
55
- result.compiled = true;
56
- result.bytecode = bytecode;
57
- result.pSrc = bytecode.print();
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
- if (run) {
60
- // Execute the bytecode.
61
- let machine = new Machine(bytecode, this.keyboard);
62
- if (DEBUG_TRACE) {
63
- machine.setDebugCallback(function (state) {
64
- $state.append(state + '\\n');
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
- machine.setFinishCallback(function (runningTime) {});
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
- machine.run();
82
- }
83
- } catch (e) {
84
- console.warn(e.message);
85
- // Print parsing errors.
86
- if (e instanceof PascalError) {
87
- console.warn(e.getMessage());
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
- console.warn(e.stack);
80
+ return result;
90
81
  }
91
- return result;
92
- }
93
- resolve(compile);
94
- });
95
- }),
82
+ resolve(compile);
83
+ });
84
+ }),
96
85
  };