Debian Codegraph @

blockly/demos/interpreter/index.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Blockly Demo: JS Interpreter</title>
6 <script src="acorn_interpreter.js"></script>
7 <script src="../../blockly_compressed.js"></script>
8 <script src="../../blocks_compressed.js"></script>
9 <script src="../../javascript_compressed.js"></script>
10 <script src="../../msg/js/en.js"></script>
11 <style>
12 body {
13 background-color: #fff;
14 font-family: sans-serif;
15 }
16 h1 {
17 font-weight: normal;
18 font-size: 140%;
19 }
20 </style>
21 </head>
22 <body>
23 <h1><a href="https://developers.google.com/blockly/">Blockly</a> &gt;
24 <a href="../index.html">Demos</a> &gt; JS Interpreter</h1>
25
26 <p>This is a simple demo of executing code with a sandboxed JavaScript interpreter.</p>
27
28 <p>&rarr; More info on <a href="https://developers.google.com/blockly/installation/js-interpreter">JS Interpreter</a>...</p>
29
30 <p>
31 <button onclick="parseCode()">Parse JavaScript</button>
32 <button onclick="stepCode()" id="stepButton" disabled="disabled">Step JavaScript</button>
33 </p>
34
35 <div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
36
37 <xml id="toolbox" style="display: none">
38 <category name="Logic">
39 <block type="controls_if"></block>
40 <block type="logic_compare"></block>
41 <block type="logic_operation"></block>
42 <block type="logic_negate"></block>
43 <block type="logic_boolean"></block>
44 </category>
45 <category name="Loops">
46 <block type="controls_repeat_ext">
47 <value name="TIMES">
48 <block type="math_number">
49 <field name="NUM">10</field>
50 </block>
51 </value>
52 </block>
53 <block type="controls_whileUntil"></block>
54 </category>
55 <category name="Math">
56 <block type="math_number"></block>
57 <block type="math_arithmetic"></block>
58 <block type="math_single"></block>
59 </category>
60 <category name="Text">
61 <block type="text"></block>
62 <block type="text_length"></block>
63 <block type="text_print"></block>
64 <block type="text_prompt_ext">
65 <value name="TEXT">
66 <block type="text"></block>
67 </value>
68 </block>
69 </category>
70 <category name="Variables" custom="VARIABLE"></category>
71 <category name="Functions" custom="PROCEDURE"></category>
72 </xml>
73
74 <xml id="startBlocks" style="display: none">
75 <block type="variables_set" inline="true" x="20" y="20">
76 <field name="VAR">n</field>
77 <value name="VALUE">
78 <block type="math_number">
79 <field name="NUM">1</field>
80 </block>
81 </value>
82 <next>
83 <block type="controls_repeat_ext" inline="true">
84 <value name="TIMES">
85 <block type="math_number">
86 <field name="NUM">4</field>
87 </block>
88 </value>
89 <statement name="DO">
90 <block type="variables_set" inline="true">
91 <field name="VAR">n</field>
92 <value name="VALUE">
93 <block type="math_arithmetic" inline="true">
94 <field name="OP">MULTIPLY</field>
95 <value name="A">
96 <block type="variables_get">
97 <field name="VAR">n</field>
98 </block>
99 </value>
100 <value name="B">
101 <block type="math_number">
102 <field name="NUM">2</field>
103 </block>
104 </value>
105 </block>
106 </value>
107 </block>
108 </statement>
109 <next>
110 <block type="text_print" inline="false">
111 <value name="TEXT">
112 <block type="variables_get">
113 <field name="VAR">n</field>
114 </block>
115 </value>
116 </block>
117 </next>
118 </block>
119 </next>
120 </block>
121 </xml>
122
123 <script>
124 var workspace = Blockly.inject('blocklyDiv',
125 {media: '../../media/',
126 toolbox: document.getElementById('toolbox')});
127 Blockly.Xml.domToWorkspace(workspace,
128 document.getElementById('startBlocks'));
129
130 var myInterpreter = null;
131
132 function initApi(interpreter, scope) {
133 // Add an API function for the alert() block.
134 var wrapper = function(text) {
135 text = text ? text.toString() : '';
136 return interpreter.createPrimitive(alert(text));
137 };
138 interpreter.setProperty(scope, 'alert',
139 interpreter.createNativeFunction(wrapper));
140
141 // Add an API function for the prompt() block.
142 var wrapper = function(text) {
143 text = text ? text.toString() : '';
144 return interpreter.createPrimitive(prompt(text));
145 };
146 interpreter.setProperty(scope, 'prompt',
147 interpreter.createNativeFunction(wrapper));
148
149 // Add an API function for highlighting blocks.
150 var wrapper = function(id) {
151 id = id ? id.toString() : '';
152 return interpreter.createPrimitive(highlightBlock(id));
153 };
154 interpreter.setProperty(scope, 'highlightBlock',
155 interpreter.createNativeFunction(wrapper));
156 }
157
158 var highlightPause = false;
159
160 function highlightBlock(id) {
161 workspace.highlightBlock(id);
162 highlightPause = true;
163 }
164
165 function parseCode() {
166 // Generate JavaScript code and parse it.
167 Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
168 Blockly.JavaScript.addReservedWords('highlightBlock');
169 var code = Blockly.JavaScript.workspaceToCode(workspace);
170 myInterpreter = new Interpreter(code, initApi);
171
172 alert('Ready to execute this code:\n\n' + code);
173 document.getElementById('stepButton').disabled = '';
174 highlightPause = false;
175 workspace.traceOn(true);
176 workspace.highlightBlock(null);
177 }
178
179 function stepCode() {
180 try {
181 var ok = myInterpreter.step();
182 } finally {
183 if (!ok) {
184 // Program complete, no more code to execute.
185 document.getElementById('stepButton').disabled = 'disabled';
186 return;
187 }
188 }
189 if (highlightPause) {
190 // A block has been highlighted. Pause execution here.
191 highlightPause = false;
192 } else {
193 // Keep executing until a highlight statement is reached.
194 stepCode();
195 }
196 }
197 </script>
198
199 </body>
200 </html>