aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-03-13 20:55:24 -0700
committerBruce Hill <bruce@bruce-hill.com>2019-03-13 20:55:59 -0700
commit783eec9b4592ff3fa54ffa1a855dda2a71f2db64 (patch)
tree3c03408a582b26e6d7fa37f661b695bb6c39ece3 /lib
parent1e99bbbe0a12b85800d316fcccd993591dde2431 (diff)
Made iteration easier to work with by using .__inext and .__next for
custom iteration, and a custom ipairs() and pairs() to use that.
Diffstat (limited to 'lib')
-rw-r--r--lib/core/collections.nom4
-rw-r--r--lib/core/control_flow.nom15
2 files changed, 5 insertions, 14 deletions
diff --git a/lib/core/collections.nom b/lib/core/collections.nom
index 9741faa..60eee3c 100644
--- a/lib/core/collections.nom
+++ b/lib/core/collections.nom
@@ -180,7 +180,6 @@ test:
for $ in $r:
$visited, add $
assume ($visited == [1, 3, 5, 7, 9])
-$(inext) = (=lua "ipairs({})")
$range_mt = {
.__type = "a Range"
.__index =
@@ -212,7 +211,8 @@ $range_mt = {
($self.last == $other.last) and ($self.step == $other.step)
.backwards = (for $self ($self.last to $self.first by (- $self.step)))
- .__ipairs = (for $self: return $(inext) $self 0)
+ .__inext = $(inext)
+ .__next = $(inext)
.as_text =
for $self:
if ($self.step == 1):
diff --git a/lib/core/control_flow.nom b/lib/core/control_flow.nom
index a1fdeee..21e0618 100644
--- a/lib/core/control_flow.nom
+++ b/lib/core/control_flow.nom
@@ -167,13 +167,9 @@ test:
# This uses Lua's approach of only allowing loop-scoped variables in a loop
if (($var.type == "Action") and ($var.stub == "1 =")):
[$key, $value] = [$var.1, $var.3]
- go to (vars set)
- if (($var.type == "Action") and ($var.stub == "1 at")):
- [$key, $value] = [$var.3, $var.1]
..else:
[$key, $value] = [nil, $var]
- --- (vars set) ---
unless $value:
at (this tree) fail "No value here"
@@ -211,21 +207,16 @@ test:
if $key:
$loop =
Lua ("
- local _iterating = \($iterable as lua expr);
- local _next = getmetatable(_iterating).__next or next;
- for \($key as lua identifier),\($value as lua identifier) in _next,_iterating,nil do
- if \($value as lua identifier) == nil and _1_is_a_dead_coroutine(_iterating) then break end
+ for \($key as lua identifier),\($value as lua identifier) in pairs(\($iterable as lua expr)) do
")
..else:
$loop =
Lua ("
- local _iterating = _1_as_an_iterable(\($iterable as lua expr))
- for _i=1,#_iterating do
- local \($value as lua identifier) = _iterating[_i]
- if \($value as lua identifier) == nil and _1_is_a_dead_coroutine(_iterating) then break end
+ for _i,\($value as lua identifier) in _ipairs(\($iterable as lua expr)) do
")
--- (loop set) ---
+ # TODO: don't always wrap in block
$lua =
Lua ("
do -- for-loop