aboutsummaryrefslogtreecommitdiff
path: root/lib/core
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/collections.nom13
-rw-r--r--lib/core/control_flow.nom65
-rw-r--r--lib/core/math.nom4
-rw-r--r--lib/core/operators.nom3
4 files changed, 46 insertions, 39 deletions
diff --git a/lib/core/collections.nom b/lib/core/collections.nom
index 60eee3c..02f9302 100644
--- a/lib/core/collections.nom
+++ b/lib/core/collections.nom
@@ -24,7 +24,7 @@ test:
assume (($list, first) == 1)
assume ($list, has 3)
assume (($list, index of 3) == 3)
- assume ((size of $list) == 5)
+ assume ((#$list) == 5)
$list, add 6
assume (($list, last) == 6)
$list, pop
@@ -36,7 +36,7 @@ test:
# Dict functionality
test:
$dict = {.x = 1, .y = 2, .z = 3}
- assume (size of $dict) == 3
+ assume (#$dict) == 3
assume [: for $k = $v in {.x = 1}: add {.key = $k, .value = $v}] ==
[{.key = "x", .value = 1}]
assume ({.x = 1, .y = 1} + {.y = 10, .z = 10}) == {.x = 1, .y = 11, .z = 10}
@@ -44,13 +44,13 @@ test:
assume ({.x = 1, .y = 1} | {.y = 10, .z = 10}) == {.x = 1, .y = 1, .z = 10}
assume ({.x = 1, .y = 1} & {.y = 10, .z = 10}) == {.y = 1}
assume ({.x = 1, .y = 1} ~ {.y = 10, .z = 10}) == {.x = 1, .z = 10}
-
+
# Set compliments:
assume (~{.x}).y
assume ((~{.x}).x == (nil))
$sc = (~{.x, .y})
$sc.y = 99
-
+
# For now, whether $sc.y == 99 or $sc.y == (yes) is unspecified,
(but the actual behavior is (yes))
assume ($sc.y and (not $sc.x))
@@ -180,6 +180,7 @@ test:
for $ in $r:
$visited, add $
assume ($visited == [1, 3, 5, 7, 9])
+
$range_mt = {
.__type = "a Range"
.__index =
@@ -211,8 +212,7 @@ $range_mt = {
($self.last == $other.last) and ($self.step == $other.step)
.backwards = (for $self ($self.last to $self.first by (- $self.step)))
- .__inext = $(inext)
- .__next = $(inext)
+ .__inext = $(inext), .__next = $(inext)
.as_text =
for $self:
if ($self.step == 1):
@@ -220,6 +220,7 @@ $range_mt = {
..else:
return "(\($self.first) to \($self.last) by \($self.step))"
}
+
$range_mt.reversed = $range_mt.backwards
$range_mt.__unm = $range_mt.backwards
$range_mt.__tostring = $range_mt.as_text
diff --git a/lib/core/control_flow.nom b/lib/core/control_flow.nom
index 21e0618..fafc45d 100644
--- a/lib/core/control_flow.nom
+++ b/lib/core/control_flow.nom
@@ -42,8 +42,8 @@ test:
(else $) compiles to:
at (this tree) fail ("
Compile error: This 'else' is not connected to any 'if' or 'unless' condition.
- Hint: You should probably have a ".." in front of the "else", to indicate \
- ..that it's attached to the previous condition.
+ Hint: You should probably have a ".." in front of the "else", to indicate that it's attached \
+ ..to the previous condition.
")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -164,15 +164,16 @@ test:
(for $var in $iterable $body) compiles to:
unless $var:
at (this tree) fail "No var here"
+
# 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]
..else:
[$key, $value] = [nil, $var]
-
+
unless $value:
at (this tree) fail "No value here"
-
+
# Numeric loop:
if (($iterable.type == "Action") and (($iterable, get stub) == "1 to")):
[$start, $stop] = [$iterable.1, $iterable.3]
@@ -181,13 +182,15 @@ test:
local _start = \($start as lua expr)
for \($value as lua identifier)=_start,\($stop as lua expr) do
")
+
if $key:
$loop, add ("
-
+
local \($key as lua identifier) = \($value as lua identifier) - _start + 1;
")
+
go to (loop set)
-
+
# Numeric loop with step:
if (($iterable.type == "Action") and (($iterable, get stub) == "1 to 2 by")):
[$start, $stop, $step] = [$iterable.1, $iterable.3, $iterable.5]
@@ -196,13 +199,15 @@ test:
local _start, _step = \($start as lua expr), \($step as lua expr);
for \($value as lua identifier)=_start,\($stop as lua expr),_step do
")
+
if $key:
$loop, add ("
-
+
local \($key as lua identifier) = (\($value as lua identifier) - _start)/_step + 1
")
+
go to (loop set)
-
+
# for $ in (...):
if $key:
$loop =
@@ -216,6 +221,7 @@ test:
")
--- (loop set) ---
+
# TODO: don't always wrap in block
$lua =
Lua ("
@@ -236,8 +242,10 @@ test:
$lua, add "\n end"
if ($key and ($body has subtree \(stop $key))):
$lua, add "\n " (\(---stop $key ---) as lua)
+
if ($body has subtree \(stop $value)):
$lua, add "\n " (\(---stop $value ---) as lua)
+
$lua, add "\nend -- for-loop"
$lua, remove free vars [($value as lua identifier, text), $key and ($key as lua identifier, text)]
return $lua
@@ -293,7 +301,6 @@ test:
repeat 5 times:
$x += 1
assume $x == 5
-
(repeat $n times $body) parses as (for (=lua "_i") in (1 to $n by 1) $body)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -325,41 +332,41 @@ test:
for $line in $body:
unless
- (($line.type == "Action") and ((size of $line) >= 2)) and
- $line.(size of $line) is "Block" syntax tree
+ (($line.type == "Action") and ((#$line) >= 2)) and
+ $line.(#$line) is "Block" syntax tree
..:
at $line fail ("
Compile error: Invalid line for the body of an 'if' block.
- Hint: Each line should contain one or more conditional expressions followed \
- ..by a block, or "else" followed by a block.
+ Hint: Each line should contain one or more conditional expressions followed by a block, \
+ ..or "else" followed by a block.
")
- $action = $line.(size of $line)
- if (($line.1 == "else") and ((size of $line) == 2)):
+ $action = $line.(#$line)
+ if (($line.1 == "else") and ((#$line) == 2)):
unless $else_allowed:
at $line fail ("
Compile error: You can't have two 'else' blocks.
Hint: Merge all of the 'else' blocks together.
")
- unless ((size of "\$code") > 0):
+ unless ((#"\$code") > 0):
at $line fail ("
Compile error: You can't have an 'else' block without a preceding condition.
- Hint: If you want the code in this block to always execute, you don't need a \
- ..conditional block around it. Otherwise, make sure the 'else' block comes last.
+ Hint: If you want the code in this block to always execute, you don't need a conditional \
+ ..block around it. Otherwise, make sure the 'else' block comes last.
")
$code, add "\nelse\n " ($action as lua)
$else_allowed = (no)
..else:
$code, add $clause " "
- for $i in 1 to ((size of $line) - 1):
+ for $i in 1 to ((#$line) - 1):
if ($i > 1):
$code, add " or "
$code, add ($line.$i as lua expr)
$code, add " then\n " ($action as lua)
$clause = "\nelseif"
- if ((size of "\$code") == 0):
+ if ((#"\$code") == 0):
at $body fail ("
Compile error: 'if' block has an empty body.
Hint: This means nothing would happen, so the 'if' block should be deleted.
@@ -396,40 +403,40 @@ test:
for $line in $body:
unless
- (($line.type == "Action") and ((size of $line) >= 2)) and
- $line.(size of $line) is "Block" syntax tree
+ (($line.type == "Action") and ((#$line) >= 2)) and
+ $line.(#$line) is "Block" syntax tree
..:
at $line fail ("
Compile error: Invalid line for 'if' block.
Hint: Each line should contain expressions followed by a block, or "else" followed by a block.
")
- $action = $line.(size of $line)
- if (($line.1 == "else") and ((size of $line) == 2)):
+ $action = $line.(#$line)
+ if (($line.1 == "else") and ((#$line) == 2)):
unless $else_allowed:
at $line fail ("
Compile error: You can't have two 'else' blocks.
Hint: Merge all of the 'else' blocks together.
")
- unless ((size of "\$code") > 0):
+ unless ((#"\$code") > 0):
at $line fail ("
Compile error: You can't have an 'else' block without a preceding condition.
- Hint: If you want the code in this block to always execute, you don't need \
- ..a conditional block around it. Otherwise, make sure the 'else' block comes last.
+ Hint: If you want the code in this block to always execute, you don't need a conditional \
+ ..block around it. Otherwise, make sure the 'else' block comes last.
")
$code, add "\nelse\n " ($action as lua)
$else_allowed = (no)
..else:
$code, add $clause " "
- for $i in 1 to ((size of $line) - 1):
+ for $i in 1 to ((#$line) - 1):
if ($i > 1):
$code, add " or "
$code, add "\(mangle "branch value") == " ($line.$i as lua expr)
$code, add " then\n " ($action as lua)
$clause = "\nelseif"
- if ((size of "\$code") == 0):
+ if ((#"\$code") == 0):
at $body fail ("
Compile error: 'if' block has an empty body.
Hint: This means nothing would happen, so the 'if' block should be deleted.
diff --git a/lib/core/math.nom b/lib/core/math.nom
index e2c759a..3aab2b5 100644
--- a/lib/core/math.nom
+++ b/lib/core/math.nom
@@ -94,7 +94,7 @@ external:
$prod *= $
return $prod
- [avg of $items, average of $items] all mean ((sum of $items) / (size of $items))
+ [avg of $items, average of $items] all mean ((sum of $items) / (#$items))
# Min/max
[min of $items, smallest of $items, lowest of $items] all mean:
@@ -207,4 +207,4 @@ external:
..all compile to "math.random(\($low as lua expr), \($high as lua expr))"
[random choice from $elements, random choice $elements, random $elements]
- ..all mean (=lua "\$elements[math.random(#\$elements)]")
+ ..all mean (=lua "\$elements[math.random(#\$elements)]") \ No newline at end of file
diff --git a/lib/core/operators.nom b/lib/core/operators.nom
index d868174..6f1fd78 100644
--- a/lib/core/operators.nom
+++ b/lib/core/operators.nom
@@ -15,8 +15,7 @@ test:
($x <= $y) compiles to "(\($x as lua expr) <= \($y as lua expr))"
($x >= $y) compiles to "(\($x as lua expr) >= \($y as lua expr))"
($a == $b) compiles to "(\($a as lua expr) == \($b as lua expr))"
-[$a not= $b, $a != $b] all compile to
- "(\($a as lua expr) ~= \($b as lua expr))"
+[$a not= $b, $a != $b] all compile to "(\($a as lua expr) ~= \($b as lua expr))"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~