@live-codes/browser-compilers 0.4.8 → 0.4.9

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.
@@ -0,0 +1,96 @@
1
+ // based on https://github.com/MikeRalphson/turbopascal/blob/master/tpc.js
2
+ 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
+ });
10
+
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;
32
+
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);
40
+
41
+ let result = { parsed: false, compiled: false };
42
+
43
+ try {
44
+ // Create the symbol table of built-in constants, functions, and procedures.
45
+ let builtinSymbolTable = SymbolTable.makeBuiltinSymbolTable();
46
+
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;
51
+
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();
58
+
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');
65
+ });
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);
79
+ });
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());
88
+ }
89
+ console.warn(e.stack);
90
+ }
91
+ return result;
92
+ }
93
+ resolve(compile);
94
+ });
95
+ }),
96
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-codes/browser-compilers",
3
- "version": "0.4.8",
3
+ "version": "0.4.9",
4
4
  "description": "Compilers that run in the browser, for use in livecodes.io",
5
5
  "author": "Hatem Hosny",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@ function mkdirp(dir) {
21
21
  }
22
22
  }
23
23
 
24
- var vendor_modules = path.resolve(__dirname + '/../vendor_modules/src');
24
+ var vendor_modules_src = path.resolve(__dirname + '/../vendor_modules/src');
25
25
  var targetDir = path.resolve(__dirname + '/../dist');
26
26
 
27
27
  /** @type {Partial<esbuild.BuildOptions>} */
@@ -75,21 +75,21 @@ esbuild.buildSync({
75
75
  // asciidoctor.css
76
76
  mkdirp(targetDir + '/asciidoctor.css');
77
77
  fs.copyFileSync(
78
- path.resolve(vendor_modules + '/asciidoctor.css/asciidoctor.css'),
78
+ path.resolve(vendor_modules_src + '/asciidoctor.css/asciidoctor.css'),
79
79
  path.resolve(targetDir + '/asciidoctor.css/asciidoctor.css'),
80
80
  );
81
81
 
82
82
  // stylus
83
83
  mkdirp(targetDir + '/stylus');
84
84
  fs.copyFileSync(
85
- path.resolve(vendor_modules + '/stylus/stylus.min.js'),
85
+ path.resolve(vendor_modules_src + '/stylus/stylus.min.js'),
86
86
  path.resolve(targetDir + '/stylus/stylus.min.js'),
87
87
  );
88
88
 
89
89
  // pug
90
90
  mkdirp(targetDir + '/pug');
91
91
  fs.copyFileSync(
92
- path.resolve(vendor_modules + '/pug/pug.min.js'),
92
+ path.resolve(vendor_modules_src + '/pug/pug.min.js'),
93
93
  path.resolve(targetDir + '/pug/pug.min.js'),
94
94
  );
95
95
 
@@ -147,7 +147,7 @@ esbuild.buildSync({
147
147
  // clientside-haml-js
148
148
  mkdirp(targetDir + '/clientside-haml-js');
149
149
  fs.copyFileSync(
150
- path.resolve(vendor_modules + '/clientside-haml-js/haml.js'),
150
+ path.resolve(vendor_modules_src + '/clientside-haml-js/haml.js'),
151
151
  path.resolve(targetDir + '/clientside-haml-js/haml.js'),
152
152
  );
153
153
 
@@ -163,12 +163,12 @@ esbuild.build({
163
163
  // livescript
164
164
  mkdirp(targetDir + '/livescript');
165
165
  fs.copyFileSync(
166
- path.resolve(vendor_modules + '/livescript/livescript-min.js'),
166
+ path.resolve(vendor_modules_src + '/livescript/livescript-min.js'),
167
167
  path.resolve(targetDir + '/livescript/livescript-min.js'),
168
168
  );
169
169
  // prelude.ls (livescript base library)
170
170
  fs.copyFileSync(
171
- path.resolve(vendor_modules + '/livescript/prelude-browser-min.js'),
171
+ path.resolve(vendor_modules_src + '/livescript/prelude-browser-min.js'),
172
172
  path.resolve(targetDir + '/livescript/prelude-browser-min.js'),
173
173
  );
174
174
 
@@ -208,7 +208,7 @@ esbuild.build({
208
208
  // JSCPP
209
209
  mkdirp(targetDir + '/jscpp');
210
210
  fs.copyFileSync(
211
- path.resolve(vendor_modules + '/jscpp/JSCPP.es5.min.js'),
211
+ path.resolve(vendor_modules_src + '/jscpp/JSCPP.es5.min.js'),
212
212
  path.resolve(targetDir + '/jscpp/JSCPP.es5.min.js'),
213
213
  );
214
214
 
@@ -216,18 +216,25 @@ fs.copyFileSync(
216
216
  mkdirp(targetDir + '/wacl');
217
217
  mkdirp(targetDir + '/wacl/tcl');
218
218
  fs.copyFileSync(
219
- path.resolve(vendor_modules + '/wacl/tcl/wacl.js'),
219
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl.js'),
220
220
  path.resolve(targetDir + '/wacl/tcl/wacl.js'),
221
221
  );
222
222
  fs.copyFileSync(
223
- path.resolve(vendor_modules + '/wacl/tcl/wacl.wasm'),
223
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl.wasm'),
224
224
  path.resolve(targetDir + '/wacl/tcl/wacl.wasm'),
225
225
  );
226
226
  fs.copyFileSync(
227
- path.resolve(vendor_modules + '/wacl/tcl/wacl-custom.data'),
227
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl-custom.data'),
228
228
  path.resolve(targetDir + '/wacl/tcl/wacl-custom.data'),
229
229
  );
230
230
  fs.copyFileSync(
231
- path.resolve(vendor_modules + '/wacl/tcl/wacl-library.data'),
231
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl-library.data'),
232
232
  path.resolve(targetDir + '/wacl/tcl/wacl-library.data'),
233
233
  );
234
+
235
+ // turbopascal
236
+ mkdirp(targetDir + '/turbopascal');
237
+ fs.copyFileSync(
238
+ path.resolve(vendor_modules_src + '/turbopascal/turbopascal.js'),
239
+ path.resolve(targetDir + '/turbopascal/turbopascal.js'),
240
+ );
@@ -46,6 +46,8 @@ Stylus: [MIT License](https://github.com/stylus/stylus/blob/59bc665db295981d4e3f
46
46
 
47
47
  Svelte: [MIT License](https://github.com/sveltejs/svelte/blob/dafbdc286eef3de2243088a9a826e6899e20465c/LICENSE)
48
48
 
49
+ turbopascal: [BSD 2-Clause License](https://github.com/MikeRalphson/turbopascal/blob/cfcc5ee6d93f32664bb6127aa751c68841ca5c1d/LICENSE)
50
+
49
51
  wacl: [BSD 3-Clause License](https://github.com/ecky-l/wacl/blob/9daacabb0102a9986f33263261350edfeebdd83b/LICENSE)
50
52
 
51
53
  wasm-refmt: [MIT License](https://github.com/xtuc/webassemblyjs/blob/45f733aa96476d74c8ac57598e13406a48a6fdc8/LICENSE)
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013, Lawrence Kesteloot
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ - Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ - Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
17
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,96 @@
1
+ // based on https://github.com/MikeRalphson/turbopascal/blob/master/tpc.js
2
+ 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
+ });
10
+
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;
32
+
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);
40
+
41
+ let result = { parsed: false, compiled: false };
42
+
43
+ try {
44
+ // Create the symbol table of built-in constants, functions, and procedures.
45
+ let builtinSymbolTable = SymbolTable.makeBuiltinSymbolTable();
46
+
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;
51
+
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();
58
+
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');
65
+ });
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);
79
+ });
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());
88
+ }
89
+ console.warn(e.stack);
90
+ }
91
+ return result;
92
+ }
93
+ resolve(compile);
94
+ });
95
+ }),
96
+ };