fix other mandelbrot variants
[mu.git] / archive / 1.vm / 044space_surround.cc
blob5a4afb5e7ae553bacca7a086ccdcf498cf3d39a5
1 //: So far you can have global variables by not setting default-space, and
2 //: local variables by setting default-space. You can isolate variables
3 //: between those extremes by creating 'surrounding' spaces.
4 //:
5 //: (Surrounding spaces are like lexical scopes in other languages.)
7 void test_surrounding_space() {
8 run(
9 // location 2 in space 1 (remember that locations 0 and 1 are reserved in all
10 // spaces) refers to the space surrounding the default space, here 20.
11 "def main [\n"
12 // prepare default-space address
13 " 10:num/alloc-id, 11:num <- copy 0, 1000\n"
14 // prepare default-space payload
15 " 1000:num <- copy 0\n" // alloc id of payload
16 " 1001:num <- copy 5\n" // length
17 // prepare address of chained space
18 " 20:num/alloc-id, 21:num <- copy 0, 2000\n"
19 // prepare payload of chained space
20 " 2000:num <- copy 0\n" // alloc id of payload
21 " 2001:num <- copy 5\n" // length
22 // actual start of this recipe
23 " default-space:space <- copy 10:space\n"
24 // later layers will explain the /names: property
25 " 0:space/names:dummy <- copy 20:space/raw\n"
26 " 2:num <- copy 94\n"
27 " 2:num/space:1 <- copy 95\n"
28 "]\n"
29 "def dummy [\n" // just for the /names: property above
30 "]\n"
32 CHECK_TRACE_CONTENTS(
33 // chain space: 1000 + (alloc id) 1 + (length) 1
34 "mem: storing 0 in location 1002\n"
35 "mem: storing 2000 in location 1003\n"
36 // store to default space: 1000 + (alloc id) 1 + (length) 1 + (index) 2
37 "mem: storing 94 in location 1004\n"
38 // store to chained space: (contents of location 1003) 2000 + (alloc id) 1 + (length) 1 + (index) 2
39 "mem: storing 95 in location 2004\n"
43 //: If you think of a space as a collection of variables with a common
44 //: lifetime, surrounding allows managing shorter lifetimes inside a longer
45 //: one.
47 :(replace{} "int space_base(const reagent& x)")
48 int space_base(const reagent& x) {
49 int base = current_call().default_space ? (current_call().default_space+/*skip alloc id*/1) : 0;
50 return space_base(x, space_index(x), base);
53 int space_base(const reagent& x, int space_index, int base) {
54 if (space_index == 0)
55 return base;
56 double chained_space_address = base+/*skip length*/1+/*skip alloc id of chaining slot*/1;
57 double chained_space_base = get_or_insert(Memory, chained_space_address) + /*skip alloc id of chained space*/1;
58 return space_base(x, space_index-1, chained_space_base);
61 int space_index(const reagent& x) {
62 for (int i = 0; i < SIZE(x.properties); ++i) {
63 if (x.properties.at(i).first == "space") {
64 if (!x.properties.at(i).second || x.properties.at(i).second->right)
65 raise << maybe(current_recipe_name()) << "/space metadata should take exactly one value in '" << x.original_string << "'\n" << end();
66 return to_integer(x.properties.at(i).second->value);
69 return 0;
72 :(code)
73 void test_permit_space_as_variable_name() {
74 run(
75 "def main [\n"
76 " space:num <- copy 0\n"
77 "]\n"