From 5855d8c836ee9623a8cbae741ce0ceffa14c8990 Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Mon, 11 Jun 2018 19:25:48 +0200
Subject: [PATCH] Document scopes & add test

---
 examples/scopes.ion          |  9 +++++++++
 examples/scopes.out          |  2 ++
 manual/src/SUMMARY.md        |  1 +
 manual/src/ch04-05-scopes.md | 28 ++++++++++++++++++++++++++++
 4 files changed, 40 insertions(+)
 create mode 100644 examples/scopes.ion
 create mode 100644 examples/scopes.out
 create mode 100644 manual/src/ch04-05-scopes.md

diff --git a/examples/scopes.ion b/examples/scopes.ion
new file mode 100644
index 00000000..dbf8a93d
--- /dev/null
+++ b/examples/scopes.ion
@@ -0,0 +1,9 @@
+let x = 5
+
+if test 1 == 1
+  let x = 2
+  let y = 3
+end
+
+echo "$x"
+echo "$y"
diff --git a/examples/scopes.out b/examples/scopes.out
new file mode 100644
index 00000000..9a7456b5
--- /dev/null
+++ b/examples/scopes.out
@@ -0,0 +1,2 @@
+2
+
diff --git a/manual/src/SUMMARY.md b/manual/src/SUMMARY.md
index be205fdc..abeb6c27 100644
--- a/manual/src/SUMMARY.md
+++ b/manual/src/SUMMARY.md
@@ -19,6 +19,7 @@
     - [Array Variables](ch04-02-arrays.md)
     - [Arithmetic Variables](ch04-03-arithmetic.md)
     - [Exporting Variables](ch04-04-exporting.md)
+    - [Scopes](ch04-05-scopes.md)
 
 - [Expansions](ch05-00-expansions.md)
 
diff --git a/manual/src/ch04-05-scopes.md b/manual/src/ch04-05-scopes.md
new file mode 100644
index 00000000..ed452195
--- /dev/null
+++ b/manual/src/ch04-05-scopes.md
@@ -0,0 +1,28 @@
+# Scopes
+
+A scope is a batch of commands, often ended by `end`.
+Things like `if`, `while`, etc all take a scope to execute.
+
+In ion, just like most other languages, all variables are destroyed once the scope they were defined in is gone.
+Similarly, variables from other scopes can still be overriden.
+However, ion has no dedicated keyword for updating an existing variable currently,
+so the first invokation of `let` gets to "own" the variable.
+
+*This is an early implementation and will be improved upon with time*
+
+```ion
+let x = 5 # defines x
+
+# This will always execute.
+# Only reason for this check is to show how
+# variables defined inside it are destroyed.
+if test 1 == 1
+  let x = 2 # updates existing x
+  let y = 3 # defines y
+
+  # end of scope, y is deleted since it's owned by it
+end
+
+echo "$x" # prints 2
+echo "$y" # prints nothing, y is deleted already
+```
-- 
GitLab