1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 085scenario_console.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.muScenario { color: #00af00; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #ecf32c; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Clean syntax to manipulate and check the console in scenarios.</span>
<span class="Comment">//: Instruction 'assume-console' implicitly creates a variable called</span>
<span class="Comment">//: 'console' that is accessible inside other 'run' instructions in the</span>
<span class="Comment">//: scenario. Like with the fake screen, 'assume-console' transparently</span>
<span class="Comment">//: supports unicode.</span>
<span class="Comment">//: first make sure we don't mangle these functions in other transforms</span>
<span class="Delimiter">:(before "End initialize_transform_rewrite_literal_string_to_text()")</span>
recipes_taking_literal_strings<span class="Delimiter">.</span>insert<span class="Delimiter">(</span><span class="Constant">"assume-console"</span><span class="Delimiter">);</span>
<span class="Delimiter">:(scenarios run_mu_scenario)</span>
<span class="Delimiter">:(scenario keyboard_in_scenario)</span>
<span class="muScenario">scenario</span> keyboard-in-scenario [
assume-console [
type [abc]
]
run [
<span class="Constant">1</span>:<span class="Normal">char</span><span class="Delimiter">,</span> console<span class="Delimiter">,</span> <span class="Constant">2</span>:<span class="Normal">bool</span><span class="Special"> <- </span>read-key console
<span class="Constant">3</span>:<span class="Normal">char</span><span class="Delimiter">,</span> console<span class="Delimiter">,</span> <span class="Constant">4</span>:<span class="Normal">bool</span><span class="Special"> <- </span>read-key console
<span class="Constant">5</span>:<span class="Normal">char</span><span class="Delimiter">,</span> console<span class="Delimiter">,</span> <span class="Constant">6</span>:<span class="Normal">bool</span><span class="Special"> <- </span>read-key console
<span class="Constant">7</span>:<span class="Normal">char</span><span class="Delimiter">,</span> console<span class="Delimiter">,</span> <span class="Constant">8</span>:<span class="Normal">bool</span><span class="Delimiter">,</span> <span class="Constant">9</span>:<span class="Normal">bool</span><span class="Special"> <- </span>read-key console
]
memory-should-contain [
<span class="Constant">1</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">1</span>
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">98</span> <span class="Comment"># 'b'</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">1</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">99</span> <span class="Comment"># 'c'</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">1</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unset</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">1</span>
<span class="Constant">9</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># end of test events</span>
]
]
<span class="Delimiter">:(before "End Scenario Globals")</span>
<span class="Normal">extern</span> <span class="Normal">const</span> <span class="Normal">int</span> CONSOLE = Next_predefined_global_for_scenarios++<span class="Delimiter">;</span>
<span class="Comment">//: give 'console' a fixed location in scenarios</span>
<span class="Delimiter">:(before "End Special Scenario Variable Names(r)")</span>
Name[r][<span class="Constant">"console"</span>] = CONSOLE<span class="Delimiter">;</span>
<span class="Comment">//: make 'console' always a raw location in scenarios</span>
<span class="Delimiter">:(before "End is_special_name Cases")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">"console"</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
ASSUME_CONSOLE<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"assume-console"</span><span class="Delimiter">,</span> ASSUME_CONSOLE<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> ASSUME_CONSOLE: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> ASSUME_CONSOLE: <span class="Delimiter">{</span>
<span class="Comment">// create a temporary recipe just for parsing; it won't contain valid instructions</span>
istringstream in<span class="Delimiter">(</span><span class="Constant">"["</span> + current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name + <span class="Constant">"]"</span><span class="Delimiter">);</span>
recipe r<span class="Delimiter">;</span>
slurp_body<span class="Delimiter">(</span>in<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Normal">int</span> num_events = count_events<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
<span class="Comment">// initialize the events like in new-fake-console</span>
<span class="Normal">int</span> size = <span class="Comment">/*</span><span class="Comment">space for refcount and length</span><span class="Comment">*/</span><span class="Constant">2</span> + num_events*size_of_event<span class="Delimiter">();</span>
<span class="Normal">int</span> event_data_address = allocate<span class="Delimiter">(</span>size<span class="Delimiter">);</span>
<span class="Comment">// store length</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> event_data_address+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> num_events<span class="Delimiter">);</span>
<span class="Normal">int</span> curr_address = event_data_address + <span class="Comment">/*</span><span class="Comment">skip refcount and length</span><span class="Comment">*/</span><span class="Constant">2</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>r<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = r<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"left-click"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing 'left-click' event starting at "</span> << Current_routine<span class="Delimiter">-></span>alloc << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'touch-event' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">2</span><span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Comment">/*</span><span class="Comment">skip tag</span><span class="Comment">*/</span><span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'type' in 'mouse-event'</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">,</span> TB_KEY_MOUSE_LEFT<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Comment">/*</span><span class="Comment">skip tag</span><span class="Comment">*/</span><span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'row' in 'mouse-event'</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> to_integer<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">));</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Comment">/*</span><span class="Comment">skip tag</span><span class="Comment">*/</span><span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'column' in 'mouse-event'</span><span class="Comment">*/</span><span class="Constant">2</span><span class="Delimiter">,</span> to_integer<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>name<span class="Delimiter">));</span>
curr_address += size_of_event<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"press"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing 'press' event starting at "</span> << curr_address << end<span class="Delimiter">();</span>
string key = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_integer<span class="Delimiter">(</span>key<span class="Delimiter">))</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Constant">1</span><span class="Delimiter">,</span> to_integer<span class="Delimiter">(</span>key<span class="Delimiter">));</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Key<span class="Delimiter">,</span> key<span class="Delimiter">))</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Constant">1</span><span class="Delimiter">,</span> Key[key]<span class="Delimiter">);</span>
<span class="Normal">else</span>
raise << <span class="Constant">"assume-console: can't press '"</span> << key << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Constant">1</span><span class="Delimiter">)</span> < <span class="Constant">256</span><span class="Delimiter">)</span>
<span class="Comment">// these keys are in ascii</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'text' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// distinguish from unicode</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'keycode' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
curr_address += size_of_event<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">// End Event Handlers</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// keyboard input</span>
assert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"type"</span><span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing 'type' event starting at "</span> << curr_address << end<span class="Delimiter">();</span>
<span class="Normal">const</span> string& contents = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">;</span>
<span class="Normal">const</span> <span class="Normal">char</span>* raw_contents = contents<span class="Delimiter">.</span>c_str<span class="Delimiter">();</span>
<span class="Normal">int</span> num_keyboard_events = unicode_length<span class="Delimiter">(</span>contents<span class="Delimiter">);</span>
<span class="Normal">int</span> curr = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < num_keyboard_events<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing 'text' tag at "</span> << curr_address << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'text' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Normal">uint32_t</span> curr_character<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>curr < SIZE<span class="Delimiter">(</span>contents<span class="Delimiter">));</span>
tb_utf8_char_to_unicode<span class="Delimiter">(</span>&curr_character<span class="Delimiter">,</span> &raw_contents[curr]<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing character "</span> << curr_character << <span class="Constant">" at "</span> << curr_address+<span class="Comment">/*</span><span class="Comment">skip exclusive container tag</span><span class="Comment">*/</span><span class="Constant">1</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr_address+<span class="Comment">/*</span><span class="Comment">skip exclusive container tag</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> curr_character<span class="Delimiter">);</span>
curr += tb_utf8_char_length<span class="Delimiter">(</span>raw_contents[curr]<span class="Delimiter">);</span>
curr_address += size_of_event<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
assert<span class="Delimiter">(</span>curr_address == event_data_address+size<span class="Delimiter">);</span>
<span class="Comment">// wrap the array of events in a console object</span>
<span class="Normal">int</span> console_address = allocate<span class="Delimiter">(</span>size_of_console<span class="Delimiter">());</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing console in "</span> << console_address << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">,</span> console_address<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing console data in "</span> << console_address+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'data' in container 'events'</span><span class="Comment">*/</span><span class="Constant">1</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_address+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'data' in container 'events'</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> event_data_address<span class="Delimiter">);</span>
<span class="Comment">// increment refcount for event data</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> event_data_address<span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Comment">// increment refcount for console</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_address<span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Globals")</span>
map<string<span class="Delimiter">,</span> <span class="Normal">int</span>> Key<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End One-time Setup")</span>
initialize_key_names<span class="Delimiter">();</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> initialize_key_names<span class="Delimiter">()</span> <span class="Delimiter">{</span>
Key[<span class="Constant">"F1"</span>] = TB_KEY_F1<span class="Delimiter">;</span>
Key[<span class="Constant">"F2"</span>] = TB_KEY_F2<span class="Delimiter">;</span>
Key[<span class="Constant">"F3"</span>] = TB_KEY_F3<span class="Delimiter">;</span>
Key[<span class="Constant">"F4"</span>] = TB_KEY_F4<span class="Delimiter">;</span>
Key[<span class="Constant">"F5"</span>] = TB_KEY_F5<span class="Delimiter">;</span>
Key[<span class="Constant">"F6"</span>] = TB_KEY_F6<span class="Delimiter">;</span>
Key[<span class="Constant">"F7"</span>] = TB_KEY_F7<span class="Delimiter">;</span>
Key[<span class="Constant">"F8"</span>] = TB_KEY_F8<span class="Delimiter">;</span>
Key[<span class="Constant">"F9"</span>] = TB_KEY_F9<span class="Delimiter">;</span>
Key[<span class="Constant">"F10"</span>] = TB_KEY_F10<span class="Delimiter">;</span>
Key[<span class="Constant">"F11"</span>] = TB_KEY_F11<span class="Delimiter">;</span>
Key[<span class="Constant">"F12"</span>] = TB_KEY_F12<span class="Delimiter">;</span>
Key[<span class="Constant">"insert"</span>] = TB_KEY_INSERT<span class="Delimiter">;</span>
Key[<span class="Constant">"delete"</span>] = TB_KEY_DELETE<span class="Delimiter">;</span>
Key[<span class="Constant">"home"</span>] = TB_KEY_HOME<span class="Delimiter">;</span>
Key[<span class="Constant">"end"</span>] = TB_KEY_END<span class="Delimiter">;</span>
Key[<span class="Constant">"page-up"</span>] = TB_KEY_PGUP<span class="Delimiter">;</span>
Key[<span class="Constant">"page-down"</span>] = TB_KEY_PGDN<span class="Delimiter">;</span>
Key[<span class="Constant">"up-arrow"</span>] = TB_KEY_ARROW_UP<span class="Delimiter">;</span>
Key[<span class="Constant">"down-arrow"</span>] = TB_KEY_ARROW_DOWN<span class="Delimiter">;</span>
Key[<span class="Constant">"left-arrow"</span>] = TB_KEY_ARROW_LEFT<span class="Delimiter">;</span>
Key[<span class="Constant">"right-arrow"</span>] = TB_KEY_ARROW_RIGHT<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-a"</span>] = TB_KEY_CTRL_A<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-b"</span>] = TB_KEY_CTRL_B<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-c"</span>] = TB_KEY_CTRL_C<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-d"</span>] = TB_KEY_CTRL_D<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-e"</span>] = TB_KEY_CTRL_E<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-f"</span>] = TB_KEY_CTRL_F<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-g"</span>] = TB_KEY_CTRL_G<span class="Delimiter">;</span>
Key[<span class="Constant">"backspace"</span>] = TB_KEY_BACKSPACE<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-h"</span>] = TB_KEY_CTRL_H<span class="Delimiter">;</span>
Key[<span class="Constant">"tab"</span>] = TB_KEY_TAB<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-i"</span>] = TB_KEY_CTRL_I<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-j"</span>] = TB_KEY_CTRL_J<span class="Delimiter">;</span>
Key[<span class="Constant">"enter"</span>] = TB_KEY_NEWLINE<span class="Delimiter">;</span> <span class="Comment">// ignore CR/LF distinction; there is only 'enter'</span>
Key[<span class="Constant">"ctrl-k"</span>] = TB_KEY_CTRL_K<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-l"</span>] = TB_KEY_CTRL_L<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-m"</span>] = TB_KEY_CTRL_M<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-n"</span>] = TB_KEY_CTRL_N<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-o"</span>] = TB_KEY_CTRL_O<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-p"</span>] = TB_KEY_CTRL_P<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-q"</span>] = TB_KEY_CTRL_Q<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-r"</span>] = TB_KEY_CTRL_R<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-s"</span>] = TB_KEY_CTRL_S<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-t"</span>] = TB_KEY_CTRL_T<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-u"</span>] = TB_KEY_CTRL_U<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-v"</span>] = TB_KEY_CTRL_V<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-w"</span>] = TB_KEY_CTRL_W<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-x"</span>] = TB_KEY_CTRL_X<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-y"</span>] = TB_KEY_CTRL_Y<span class="Delimiter">;</span>
Key[<span class="Constant">"ctrl-z"</span>] = TB_KEY_CTRL_Z<span class="Delimiter">;</span>
Key[<span class="Constant">"escape"</span>] = TB_KEY_ESC<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after "Begin check_or_set_invalid_types(r)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_scenario<span class="Delimiter">(</span>caller<span class="Delimiter">))</span>
initialize_special_name<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_scenario<span class="Delimiter">(</span><span class="Normal">const</span> recipe& caller<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> starts_with<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">,</span> <span class="Constant">"scenario_"</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> initialize_special_name<span class="Delimiter">(</span>reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Comment">// no need for screen</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>r<span class="Delimiter">.</span>name == <span class="Constant">"console"</span><span class="Delimiter">)</span> r<span class="Delimiter">.</span>type = new_type_tree<span class="Delimiter">(</span><span class="Constant">"address:console"</span><span class="Delimiter">);</span>
<span class="Comment">// End Initialize Type Of Special Name In Scenario(r)</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario events_in_scenario)</span>
<span class="muScenario">scenario</span> events-in-scenario [
assume-console [
type [abc]
left-click <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">1</span>
press up-arrow
type [d]
]
run [
<span class="Comment"># 3 keyboard events; each event occupies 4 locations</span>
<span class="Constant">1</span>:event<span class="Special"> <- </span>read-event console
<span class="Constant">5</span>:event<span class="Special"> <- </span>read-event console
<span class="Constant">9</span>:event<span class="Special"> <- </span>read-event console
<span class="Comment"># mouse click</span>
<span class="Constant">13</span>:event<span class="Special"> <- </span>read-event console
<span class="Comment"># non-character keycode</span>
<span class="Constant">17</span>:event<span class="Special"> <- </span>read-event console
<span class="Comment"># final keyboard event</span>
<span class="Constant">21</span>:event<span class="Special"> <- </span>read-event console
]
memory-should-contain [
<span class="Constant">1</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># 'text'</span>
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># 'text'</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">98</span> <span class="Comment"># 'b'</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">9</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># 'text'</span>
<span class="Constant">10</span><span class="Special"> <- </span><span class="Constant">99</span> <span class="Comment"># 'c'</span>
<span class="Constant">11</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">12</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">13</span><span class="Special"> <- </span><span class="Constant">2</span> <span class="Comment"># 'mouse'</span>
<span class="Constant">14</span><span class="Special"> <- </span><span class="Constant">65513</span> <span class="Comment"># mouse click</span>
<span class="Constant">15</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># row</span>
<span class="Constant">16</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># column</span>
<span class="Constant">17</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># 'keycode'</span>
<span class="Constant">18</span><span class="Special"> <- </span><span class="Constant">65517</span> <span class="Comment"># up arrow</span>
<span class="Constant">19</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">20</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">21</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># 'text'</span>
<span class="Constant">22</span><span class="Special"> <- </span><span class="Constant">100</span> <span class="Comment"># 'd'</span>
<span class="Constant">23</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">24</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">25</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="Comment">//: Deal with special keys and unmatched brackets by allowing each test to</span>
<span class="Comment">//: independently choose the unicode symbol to denote them.</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
REPLACE_IN_CONSOLE<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"replace-in-console"</span><span class="Delimiter">,</span> REPLACE_IN_CONSOLE<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> REPLACE_IN_CONSOLE: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> REPLACE_IN_CONSOLE: <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"console not initialized</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> console_address = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">);</span>
<span class="Normal">int</span> console_data = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_address+<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Normal">int</span> length = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_data<span class="Delimiter">);</span> <span class="Comment">// array length</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">,</span> curr = console_data+<span class="Constant">1</span><span class="Delimiter">;</span> i < length<span class="Delimiter">;</span> ++i<span class="Delimiter">,</span> curr+=size_of_event<span class="Delimiter">())</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">)</span> != <span class="Comment">/*</span><span class="Comment">text</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr+<span class="Constant">1</span><span class="Delimiter">)</span> != ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> n = <span class="Constant">0</span><span class="Delimiter">;</span> n < size_of_event<span class="Delimiter">();</span> ++n<span class="Delimiter">)</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr+n<span class="Delimiter">,</span> ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span>n<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">int</span> count_events<span class="Delimiter">(</span><span class="Normal">const</span> recipe& r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>r<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& curr = r<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">"type"</span><span class="Delimiter">)</span>
result += unicode_length<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">);</span>
<span class="Normal">else</span>
++result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> size_of_event<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Comment">// memoize result if already computed</span>
<span class="Normal">static</span> <span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">)</span> <span class="Identifier">return</span> result<span class="Delimiter">;</span>
type_tree* type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">"event"</span><span class="Delimiter">);</span>
result = size_of<span class="Delimiter">(</span>type<span class="Delimiter">);</span>
<span class="Normal">delete</span> type<span class="Delimiter">;</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> size_of_console<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Comment">// memoize result if already computed</span>
<span class="Normal">static</span> <span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">)</span> <span class="Identifier">return</span> result<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"console"</span><span class="Delimiter">));</span>
type_tree* type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">"console"</span><span class="Delimiter">);</span>
result = size_of<span class="Delimiter">(</span>type<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Normal">delete</span> type<span class="Delimiter">;</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|