https://github.com/akkartik/mu/blob/master/044space_surround.cc
 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.)
 6 
 7 :(scenario surrounding_space)
 8 # location 2 in space 1 (remember that locations 0 and 1 are reserved in all
 9 # spaces) refers to the space surrounding the default space, here 20.
10 def main [
11   # prepare default-space address
12   10:num/alloc-id, 11:num <- copy 0, 1000
13   # prepare default-space payload
14   1000:num <- copy 0  # alloc id of payload
15   1001:num <- copy 5  # length
16   # prepare address of chained space
17   20:num/alloc-id, 21:num <- copy 0, 2000
18   # prepare payload of chained space
19   2000:num <- copy 0  # alloc id of payload
20   2001:num <- copy 5  # length
21   # actual start of this recipe
22   default-space:space <- copy 10:space
23   #: later layers will explain the /names: property
24   0:space/names:dummy <- copy 20:space/raw
25   2:num <- copy 94
26   2:num/space:1 <- copy 95
27 ]
28 def dummy [  # just for the /names: property above
29 ]
30 # chain space: 1000 + (alloc id) 1 + (length) 1
31 +mem: storing 0 in location 1002
32 +mem: storing 2000 in location 1003
33 # store to default space: 1000 + (alloc id) 1 + (length) 1 + (index) 2
34 +mem: storing 94 in location 1004
35 # store to chained space: (contents of location 1003) 2000 + (alloc id) 1 + (length) 1 + (index) 2
36 +mem: storing 95 in location 2004
37 
38 //: If you think of a space as a collection of variables with a common
39 //: lifetime, surrounding allows managing shorter lifetimes inside a longer
40 //: one.
41 
42 :(replace{} "int space_base(const reagent& x)")
43 int space_base(const reagent& x) {
44   int base = current_call().default_space ? (current_call().default_space+/*skip alloc id*/1) : 0;
45   return space_base(x, space_index(x), base);
46 }
47 
48 int space_base(const reagent& x, int space_index, int base) {
49   if (space_index == 0)
50     return base;
51   double chained_space_address = base+/*skip length*/1+/*skip alloc id of chaining slot*/1;
52   double chained_space_base = get_or_insert(Memory, chained_space_address) + /*skip alloc id of chained space*/1;
53   return space_base(x, space_index-1, chained_space_base);
54 }
55 
56 int space_index(const reagent& x) {
57   for (int i = 0;  i < SIZE(x.properties);  ++i) {
58     if (x.properties.at(i).first == "space") {
59       if (!x.properties.at(i).second || x.properties.at(i).second->right)
60         raise << maybe(current_recipe_name()) << "/space metadata should take exactly one value in '" << x.original_string << "'\n" << end();
61       return to_integer(x.properties.at(i).second->value);
62     }
63   }
64   return 0;
65 }
66 
67 :(scenario permit_space_as_variable_name)
68 def main [
69   space:num <- copy 0
70 ]