about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/.traces/surrounding_space29
-rw-r--r--cpp/027space6
-rw-r--r--cpp/028space_surround42
3 files changed, 76 insertions, 1 deletions
diff --git a/cpp/.traces/surrounding_space b/cpp/.traces/surrounding_space
new file mode 100644
index 00000000..5297428b
--- /dev/null
+++ b/cpp/.traces/surrounding_space
@@ -0,0 +1,29 @@
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "10", value: 0, type: 0, properties: [10: literal]}
+parse/0:   product: {name: "default-space", value: 0, type: 2-0, properties: [default-space: address:space]}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "20", value: 0, type: 0, properties: [20: literal]}
+parse/0:   product: {name: "0", value: 0, type: 2-0, properties: [0: address:space]}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "32", value: 0, type: 0, properties: [32: literal]}
+parse/0:   product: {name: "1", value: 0, type: 1, properties: [1: integer]}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "33", value: 0, type: 0, properties: [33: literal]}
+parse/0:   product: {name: "1", value: 0, type: 1, properties: [1: integer, space: 1]}
+name/0: assign default-space 1
+after-brace/0: recipe main
+after-brace/0: copy ...
+after-brace/0: copy ...
+after-brace/0: copy ...
+after-brace/0: copy ...
+run/0: instruction main/0
+run/0: ingredient 0 is 10
+run/0: instruction main/1
+run/0: ingredient 0 is 20
+mem/0: storing 20 in location 11
+run/0: instruction main/2
+run/0: ingredient 0 is 32
+mem/0: storing 32 in location 12
+run/0: instruction main/3
+run/0: ingredient 0 is 33
+mem/0: storing 33 in location 22
diff --git a/cpp/027space b/cpp/027space
index 2fa6f36d..0e1686f0 100644
--- a/cpp/027space
+++ b/cpp/027space
@@ -26,7 +26,7 @@ reagent absolutize(reagent x) {
 //?   cout << "not raw: " << x.to_string() << '\n'; //? 1
   assert(x.initialized);
   reagent r = x;
-  r.set_value(address(r.value, Current_routine.calls.top().default_space));
+  r.set_value(address(r.value, space(r)));
 //?   cout << "after absolutize: " << r.value << '\n'; //? 1
   if (r.properties.empty())
     r.properties.push_back(pair<string, vector<string> >("", vector<string>()));
@@ -35,6 +35,10 @@ reagent absolutize(reagent x) {
   return r;
 }
 
+int space(const reagent& x) {
+  return Current_routine.calls.top().default_space;
+}
+
 int address(int offset, int base) {
   if (base == 0) return offset;  // raw
   return base+offset+1;
diff --git a/cpp/028space_surround b/cpp/028space_surround
new file mode 100644
index 00000000..9899ed89
--- /dev/null
+++ b/cpp/028space_surround
@@ -0,0 +1,42 @@
+//: So far you can have global variables by not setting default-space, and
+//: local variables by setting default-space. You can isolate variables
+//: between those extremes by creating 'surrounding' spaces.
+//:
+//: (Surrounding spaces are like lexical scopes in other languages.)
+
+:(scenario "surrounding_space")
+# location 1 in space 1 refers to the space surrounding the default space, here 20.
+recipe main [
+  default-space:address:space <- copy 10:literal
+  0:address:space <- copy 20:literal
+  1:integer <- copy 32:literal
+  1:integer/space:1 <- copy 33:literal
+]
++run: instruction main/1
++mem: storing 20 in location 11
++run: instruction main/2
++mem: storing 32 in location 12
++run: instruction main/3
++mem: storing 33 in location 22
+
+//: If you think of a space as a collection of variables with a common
+//: lifetime, surrounding allows managing shorter lifetimes inside a longer
+//: one.
+
+:(replace{} "int space(const reagent& x)")
+int space(const reagent& x) {
+  return space(x, space_index(x), Current_routine.calls.top().default_space);
+}
+
+int space(const reagent& x, int space_index, int base) {
+  if (space_index == 0) return base;
+  return space(x, space_index-1, Memory[base+1]);
+}
+
+int space_index(const reagent& x) {
+  for (size_t i = 0; i < x.properties.size(); ++i) {
+    if (x.properties[i].first == "space")
+      return to_int(x.properties[i].second[0]);
+  }
+  return 0;
+}
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
vious revision' href='/akkartik/mu/blame/042name.cc?h=hlt&id=234ec878aa8c709dd61fb90f2556ff49da745ce0'>^
80b781cc ^
f385814f ^
1e38eee5 ^
f385814f ^

1e38eee5 ^

f385814f ^
1e38eee5 ^

80b781cc ^
6c96a437 ^
f385814f ^








80b781cc ^
f385814f ^
1e38eee5 ^
f385814f ^

1e38eee5 ^

f385814f ^
1e38eee5 ^

80b781cc ^

53188dd7 ^
1b76245c ^
ce2b0aec ^

e06dbec0 ^
c6034af3 ^
2b250717 ^
61880904 ^
235958e8 ^

ce2b0aec ^
ab6ed192 ^
1211a3ab ^
ce2b0aec ^

80b781cc ^
fc55fea0 ^
b24eb476 ^
f3760b0f ^
82ac0b7e ^

b24eb476 ^
82ac0b7e ^


d57bf669 ^
af023b32 ^


93d4cc93 ^
b55b15a9 ^

93d4cc93 ^



a3d9828c ^

aae198a9 ^






23d3a022 ^
795f5244 ^
6c96a437 ^
3adc9e08 ^
9dcbec39 ^
a3d9828c ^

23d3a022 ^







a3d9828c ^
ce2b0aec ^
ab6ed192 ^
ce2b0aec ^

0f125d5f ^
ce2b0aec ^


ab6ed192 ^
ce2b0aec ^

0f125d5f ^
ce2b0aec ^

ea5ff2d4 ^
ce2b0aec ^

ce2b0aec ^
1211a3ab ^
ce2b0aec ^


23d3a022 ^



b8348923 ^


192d59d3 ^
b8348923 ^
23d3a022 ^
b8348923 ^
23d3a022 ^
b8348923 ^


a0331a9b ^
192d59d3 ^
b8348923 ^
23d3a022 ^
b8348923 ^
23d3a022 ^
b8348923 ^
3b0aa1ac ^
fc55fea0 ^
1ead3562 ^
192d59d3 ^
fc55fea0 ^
23d3a022 ^

ad68bbce ^
198ad741 ^
18e626df ^
3b0aa1ac ^
5f98a10c ^
1ead3562 ^
192d59d3 ^
198ad741 ^
23d3a022 ^
192d59d3 ^
198ad741 ^
18e626df ^
5f98a10c ^

1ead3562 ^
192d59d3 ^
ce2b0aec ^
5f98a10c ^
ce2b0aec ^
5f98a10c ^

1ead3562 ^
192d59d3 ^

ce2b0aec ^
5f98a10c ^
ce2b0aec ^
5f98a10c ^
1ead3562 ^
192d59d3 ^
ce2b0aec ^
5f98a10c ^

ce2b0aec ^
5f98a10c ^
1ead3562 ^
192d59d3 ^
ce2b0aec ^
5f98a10c ^

ce2b0aec ^
53881567 ^

caec3c16 ^
c792fe8e ^


2b250717 ^
c792fe8e ^




3b0aa1ac ^
1ead3562 ^
01ce563d ^
760f683f ^

a3d9828c ^
db5c9550 ^

72cc3ae1 ^
dd2e01e4 ^
db5c9550 ^
53881567 ^
80411d01 ^




8b9e9fb1 ^
80411d01 ^
6179649e ^

d57bf669 ^
c6354606 ^
23d3a022 ^
c6354606 ^

6179649e ^
06b3eb96 ^

5bdc0334 ^






3b0aa1ac ^
1ead3562 ^
23d3a022 ^

72cc3ae1 ^
23d3a022 ^

caec3c16 ^
7284d503 ^
caec3c16 ^

3b0aa1ac ^
1ead3562 ^
192d59d3 ^


d52406cc ^
caec3c16 ^
5497090a ^
6e793202 ^
7bf9212f ^

caec3c16 ^
dd2e01e4 ^
caec3c16 ^
e6692482 ^
e4630643 ^
2b250717 ^
e4630643 ^

ab6ed192 ^
6179649e ^

d57bf669 ^
c6354606 ^
23d3a022 ^
c6354606 ^

6179649e ^
caec3c16 ^
5bdc0334 ^






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