diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-04-17 11:00:56 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-04-17 11:00:56 -0700 |
commit | c495d2ac7ca0d7f1c1a9bbb7d1ad5072a7eface5 (patch) | |
tree | 95db0f8d742a87eba6c15833d0ac9b982e98174d /cpp/044space_surround | |
parent | 353a452e691a8b55e28d7b3b1d09cf294265eba0 (diff) | |
download | mu-c495d2ac7ca0d7f1c1a9bbb7d1ad5072a7eface5.tar.gz |
1075
Diffstat (limited to 'cpp/044space_surround')
-rw-r--r-- | cpp/044space_surround | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/cpp/044space_surround b/cpp/044space_surround new file mode 100644 index 00000000..76cd5807 --- /dev/null +++ b/cpp/044space_surround @@ -0,0 +1,50 @@ +//: 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 [ + 10:integer <- copy 5:literal # pretend array + 20:integer <- copy 5:literal # pretend array + default-space:address:space <- copy 10:literal + 0:address:space/names:dummy <- copy 20:literal # later layers will explain the /names: property + 1:integer <- copy 32:literal + 1:integer/space:1 <- copy 33:literal +] ++run: instruction main/3 ++mem: storing 20 in location 11 ++run: instruction main/4 ++mem: storing 32 in location 12 ++run: instruction main/5 ++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_base(const reagent& x)") +int space_base(const reagent& x) { + return space_base(x, space_index(x), Current_routine->calls.top().default_space); +} + +int space_base(const reagent& x, int space_index, int base) { +//? trace("foo") << "base of space " << space_index << '\n'; //? 1 + if (space_index == 0) { +//? trace("foo") << "base of space " << space_index << " is " << base << '\n'; //? 1 + return base; + } +//? trace("foo") << "base of space " << space_index << " is " << Memory[base+1] << '\n'; //? 1 + int result = space_base(x, space_index-1, Memory[base+1]); + return result; +} + +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; +} |