about summary refs log tree commit diff stats
path: root/counters.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-01-02 18:13:04 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-01-02 18:20:18 -0800
commitd1c12218229989dc9a6e15b0190ae0ca05ecb20f (patch)
tree639797bac7cd33b315d599edb696309483ee25e0 /counters.mu
parentd99607231dd767df599478bb0bbc68d0a3483d1a (diff)
downloadmu-d1c12218229989dc9a6e15b0190ae0ca05ecb20f.tar.gz
497 - strengthen the concept of 'space'
'default-scope' is now 'default-space'
'closure-generator' is now 'next-space-generator'
The connection to high-level syntax for closures is now tenuous, so
we'll call the 'outer scope' the 'next space'.

So, let's try to create a few sentences with all these related ideas:

  Names map to addresses offset from a default-space when it's provided.

  Spaces can be strung together. The zeroth variable points to the next
  space, the one that is accessed when a variable has /space:1.

  To map a name to an address in the next space, you need to know what
  function generated that space. A corollary is that the space passed in
  to a function should always be generated by a single function.

Spaces can be used to construct lexical scopes and objects.
Diffstat (limited to 'counters.mu')
-rw-r--r--counters.mu33
1 files changed, 33 insertions, 0 deletions
diff --git a/counters.mu b/counters.mu
new file mode 100644
index 00000000..cd8825ce
--- /dev/null
+++ b/counters.mu
@@ -0,0 +1,33 @@
+(function init-counter [
+  (default-space:space-address <- new space:literal 30:literal)
+  (n:integer <- next-input)
+  (reply default-space:space-address)
+ ])
+
+(function increment-counter [
+  (default-space:space-address <- new space:literal 30:literal)
+  (0:space-address/names:init-counter <- next-input)  ; setup outer space
+  (x:integer <- next-input)
+  (n:integer/space:1 <- add n:integer/space:1 x:integer)
+  (reply n:integer/space:1)
+ ])
+
+(function main [
+  (default-space:space-address <- new space:literal 30:literal)
+  ; counter A
+  (a:space-address <- init-counter 34:literal)
+  ; counter B
+  (b:space-address <- init-counter 23:literal)
+  ; increment both by 2 but in different ways
+  (increment-counter a:space-address 1:literal)
+  (bres:integer <- increment-counter b:space-address 2:literal)
+  (ares:integer <- increment-counter a:space-address 1:literal)
+  ; check results
+  (print-primitive (("Contents of counters a: " literal)))
+  (print-primitive ares:integer)
+  (print-primitive ((" b: " literal)))
+  (print-primitive bres:integer)
+  (print-primitive (("\n" literal)))
+ ])
+
+; compare http://www.paulgraham.com/accgen.html