aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/tutorial.nom27
-rw-r--r--utils.lua24
2 files changed, 45 insertions, 6 deletions
diff --git a/examples/tutorial.nom b/examples/tutorial.nom
index 776f900..55e558b 100644
--- a/examples/tutorial.nom
+++ b/examples/tutorial.nom
@@ -52,12 +52,27 @@ if (1 > 10):
..else:
say "this will print"
-if (1 > 3):
- say "this won't print"
-..else: if (1 < 2):
- say "this won't print"
-..else:
- say "this will print"
+# For longer conditionals, a "when" branch is the best option
+when:
+ ? 1 > 4:
+ say "this won't print"
+ ? 1 > 3:
+ say "this won't print"
+ ? 1 > 2:
+ say "this won't print"
+ ?:
+ say "this will print"
+
+# When "when" is given an argument, it works like a switch statement
+when 3:
+ == 1:
+ say "this won't print"
+ == 2:
+ say "this won't print"
+ == 3:
+ say "this will print"
+ ==:
+ say "this won't print"
# Loops look like this:
for %x in [1,2,3]:
diff --git a/utils.lua b/utils.lua
index dde056c..b50a88f 100644
--- a/utils.lua
+++ b/utils.lua
@@ -91,6 +91,8 @@ utils = {
start, stop, step = 1, start, 1
elseif step == nil then
step = 1
+ elseif step == 0 then
+ error("Range step cannot be zero.")
end
return setmetatable({
start = start,
@@ -105,9 +107,31 @@ utils = {
end
end
return iter, self, 0
+ end,
+ __index = function(self, i)
+ if type(i) ~= "Number" then
+ return nil
+ end
+ if i % 1 ~= 0 then
+ return nil
+ end
+ if i <= 0 or i - 1 > (self.stop - self.start) / self.step then
+ return nil
+ end
+ return self.start + (i - 1) * self.step
+ end,
+ __len = function(self)
+ local len = (self.stop - self.start) / self.step
+ if len < 0 then
+ len = 0
+ end
+ return len
end
})
end,
+ nth_to_last = function(list, n)
+ return list[#list - n + 1]
+ end,
keys = function(t)
local _accum_0 = { }
local _len_0 = 1