about summary refs log tree commit diff stats
path: root/cpp/044space_surround.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-04-24 22:49:29 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-04-24 22:49:29 -0700
commit6d8ef6b12d37336a92c7a6b20b4b66f3ab424464 (patch)
tree49c4b2b75ebc85540861b2682e3d48fe958c5dc8 /cpp/044space_surround.cc
parent0b0cfb6f1f4579eef463ffcb41ba782ddbd56035 (diff)
downloadmu-6d8ef6b12d37336a92c7a6b20b4b66f3ab424464.tar.gz
1189 - add extensions to all layers
I'm sick of fighting vim's filetype detection. No modeline and files
highlight in random colors. I add a modeline and it stops highlighting
tangle comments. Even though it read my #$%# vimrc! Fuck this shite.
Diffstat (limited to 'cpp/044space_surround.cc')
-rw-r--r--cpp/044space_surround.cc50
1 files changed, 50 insertions, 0 deletions
diff --git a/cpp/044space_surround.cc b/cpp/044space_surround.cc
new file mode 100644
index 00000000..c5a03176
--- /dev/null
+++ b/cpp/044space_surround.cc
@@ -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:array:location <- copy 10:literal
+  0:address:array:location/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;
+}