diff --git a/examples/scopes.ion b/examples/scopes.ion new file mode 100644 index 0000000000000000000000000000000000000000..dbf8a93da9f85179a57d577306db5116c9957426 --- /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 0000000000000000000000000000000000000000..9a7456b54df272ef6e98ab7ba0784c8970b04808 --- /dev/null +++ b/examples/scopes.out @@ -0,0 +1,2 @@ +2 + diff --git a/manual/src/SUMMARY.md b/manual/src/SUMMARY.md index be205fdc90cfaa534b869b642cd846b2437b98b8..abeb6c271c4e8c47ba20f95f5506a1ed5a0b4650 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 0000000000000000000000000000000000000000..ed4521956fdc3fbd525fca63b0d03f8e2a598cfb --- /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 +```