git @ Cat's Eye Technologies ZOWIE / rel_1_1_2019_0122
Merge pull request #1 from catseye/develop-2019-1 Develop 2019-1 Chris Pressey authored 2 years ago GitHub committed 2 years ago
5 changed file(s) with 157 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
+0
-4
.hgignore less more
0 syntax: glob
1
2 *.pyc
3 zowie-c
+0
-3
.hgtags less more
0 0bca35687a3430b8b3909c109c7b9f40d91c6e49 rel_1_0_2012_0325
1 10f2fc44756b1d4e02ea6653e7484a82e7348a93 rel_1_0_2014_0819
2 1bc4cc116b0abedabdb82f51da9b4d4a249f1db9 rel_1_1
3535 jaw-dropping way, the minor version number was bumped.
3636 * Uses of regular expressions and old-style classes `zowie.py` were dropped.
3737 * Added ability to compile `zowie.py` with RPython from PyPy version 2.3.1.
38
39 Version 1.1 revision 2019.0122
40 ------------------------------
41
42 * Added example Javascript files demonstrating how zowie.py can be run
43 under Skulpt in a web browser.
0 examplePrograms = [
1 [
2 "chars.zow",
3 "; Display the Roman alphabet in reverse, in ZOWIE\n; This example source is in the public domain.\n\nMOV R10, 90 ; initially it's \"Z\"\nMOV R1, R1 ; BEGIN TRANSACTION for \"REPEAT\"\nMOV R0, R10 ; output character\nMOV R8, R10 ; decrement character\nMOV R5, 1\nMOV R10, R8\nMOV R8, R10 ; test if character is above \"@\"\nMOV R5, 64\nMOV R3, R8 ; COMMIT AND REPEAT if non-zero\n"
4 ],
5 [
6 "fact.zow",
7 "; Compute a factorial, in ZOWIE\n; This example source is in the public domain.\n\n; Expected output is MODIFIER LETTER TRIANGULAR COLON (Unicode character 720).\n; NOTE: this code is also able to properly compute 0! = 1.\n\nMOV R11, 6 ; let's find 6!\nMOV R10, 1 ; accumulator\n\nMOV R8, R11 ; increase multiplicand\nMOV R4, 1\nMOV R11, R8\n\nMOV R1, R1 ; BEGIN TRANSACTION for \"REPEAT\"\n\nMOV R8, R11 ; decrease multiplicand\nMOV R5, 1\nMOV R11, R8\n\nMOV R1, R1 ; BEGIN TRANSACTION for \"IF\"\n\nMOV R8, R10 ; accumulator\nMOV R6, R11 ; multiplied\nMOV R10, R8\n\nMOV R2, R11 ; COMMIT if multiplicand above zero, or ROLLBACK otherwise\nMOV R3, R11 ; COMMIT AND REPEAT if multiplicand above zero\n\nMOV R0, R10 ; output accumulator (as single Unicode character)\n"
8 ],
9 [
10 "hello.zow",
11 "; Hello, world! in ZOWIE\n; This example source is in the public domain.\n\nMOV R0, 72\nMOV R0, 101\nMOV R0, 108\nMOV R0, 108\nMOV R0, 111\nMOV R0, 44\nMOV R0, 32\nMOV R0, 119\nMOV R0, 111\nMOV R0, 114\nMOV R0, 108\nMOV R0, 100\nMOV R0, 33\nMOV R0, 10\n"
12 ]
13 ];
0 // Example of running the ZOWIE reference interpreter, zowie.py,
1 // under Skulpt, in a web browser. As of this writing, this
2 // can be seen in action here: http://catseye.tc/installation/ZOWIE
3
4 function load_file(url, callback)
5 {
6 var xhr = new XMLHttpRequest();
7 xhr.onload = function(e) {
8 if (xhr.readyState === 4 && xhr.responseText) {
9 if (xhr.status === 200) {
10 callback(xhr.responseText);
11 } else {
12 alert("Error: could not load " + url + ": " + xhr.statusText);
13 }
14 }
15 };
16 xhr.open("get", url, true);
17 xhr.send();
18 }
19
20 PresetManager = function() {
21 this.init = function(cfg) {
22 this.selectElem = cfg.selectElem;
23 this.clear();
24 var $this = this;
25 this.selectElem.onchange = function() {
26 $this._select(this.options[this.selectedIndex].value);
27 };
28 return this;
29 };
30
31 this.clear = function() {
32 this.reactTo = {};
33 while (this.selectElem.firstChild) {
34 this.selectElem.removeChild(this.selectElem.firstChild);
35 }
36 this.add('(select one...)', function() {});
37 return this;
38 };
39
40 this.add = function(id, callback) {
41 var opt = document.createElement("option");
42 opt.text = id;
43 opt.value = id;
44 this.selectElem.options.add(opt);
45 var $this = this;
46 this.reactTo[id] = callback;
47 return this;
48 };
49
50 this._select = function(id) {
51 this.reactTo[id](id);
52 if (this.onselect) {
53 this.onselect(id);
54 }
55 };
56
57 this.select = function(id) {
58 var i = 0;
59 var opt = this.selectElem.options[i];
60 while (opt) {
61 if (opt.value === id) {
62 this.selectElem.selectedIndex = i;
63 this._select(id);
64 return this;
65 }
66 i++;
67 opt = this.selectElem.options[i];
68 }
69 // if not found, select the "(select one...)" option
70 this.selectElem.selectedIndex = 0;
71 return this;
72 };
73 };
74
75 function launch(prefix, container, config) {
76 if (typeof container === 'string') {
77 container = document.getElementById(container);
78 }
79 config = config || {};
80
81 document.getElementById('installation').innerHTML =
82 '<div id="control_panel"></div>' +
83 '<button type="button" id="run">Run</button>' +
84 '<span class="control-container">example program: <select id="select_source"></select></span>' +
85 '<pre id="output"></pre>' +
86 '<textarea id="editor" rows="14" cols="60" wrap="off"></textarea>';
87
88 var presetManager = (new PresetManager()).init({
89 selectElem: document.getElementById('select_source')
90 });
91 function makeCallback(sourceText) {
92 return function(id) {
93 document.getElementById('editor').value = sourceText;
94 }
95 }
96 for (var i = 0; i < examplePrograms.length; i++) {
97 presetManager.add(examplePrograms[i][0], makeCallback(examplePrograms[i][1]));
98 }
99 presetManager.select(examplePrograms[0][0]);
100
101 document.getElementById("run").style.enabled = false;
102 var zowie_interpreter;
103
104 load_file("../modules/zowie/src/zowie.py", function(text) {
105 zowie_interpreter = text;
106 document.getElementById("run").style.enabled = true;
107 });
108
109 document.getElementById("run").onclick = function() {
110 var esoprog = document.getElementById("editor").value;
111 var mypre = document.getElementById("output");
112 mypre.innerHTML = '';
113 // Sk is the Skulpt object, which we assume has been loaded first
114 Sk.canvas = "mycanvas";
115 Sk.pre = "output";
116 Sk.configure({
117 output: function(text) {
118 var mypre = document.getElementById("output");
119 mypre.innerHTML = mypre.innerHTML + text.replace(/\n$/, "");
120 },
121 read: function(x) {
122 if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined) {
123 throw "File not found: '" + x + "'";
124 }
125 return Sk.builtinFiles["files"][x];
126 }
127 });
128 var prog = zowie_interpreter + "\n";
129 prog += 'p = Processor()\n';
130 prog += 'p.load_string("""\n';
131 prog += esoprog;
132 prog += '""")\n';
133 prog += 'p.run()\n';
134 eval(Sk.importMainWithBody("<stdin>", false, prog));
135 };
136 }