123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 | #!/usr/bin/env nomsu -V8.0.0
###
This file defines some common math literals and functions
use "core/metaprogramming"
use "core/text"
use "core/operators"
use "core/control_flow"
use "core/collections"
at compile time:
### The values in a table or block that's written out in the code, or nothing if
they're only known when the code runs
(clauses in $items) means:
unless (($items.type == "Table") or ($items.type == "Block")): return (nil)
return ($ for $ in $items if ($.type != "Comment"))
### (chain of {a, b, c} joined by "and" from $source) is the code (a and b and c)
(chain of $clauses joined by $operator from $source) means:
if (#$clauses == 1): return $clauses.1
$chain = ("Action" tree from $source with $clauses.1)
for $i in (2 to #$clauses):
$chain, add $operator
$chain, add $clauses.$i
return $chain
### Lua 5.3+ only has math.atan2 in compatibility builds, but math.atan(y, x) works
$math_atan2 = ($math.atan2 or $math.atan)
### Lua 5.3+ only has math.sinh, math.cosh, and math.tanh in compatibility builds
$math_sinh = ($math.sinh or ($x -> (((e^ $x) - (e^ (- $x))) / 2)))
$math_cosh = ($math.cosh or ($x -> (((e^ $x) + (e^ (- $x))) / 2)))
$math_tanh = ($math.tanh or ($x -> ((sinh $x) / (cosh $x))))
public:
### Literals:
test:
unless (all of {infinity, NaN, pi, e}):
fail "math constants failed"
$nan = (NaN)
unless ($nan != $nan):
fail "NaN failed"
(infinity) means $math.huge
{(not a number), (NaN)} all parse as (0 / 0)
(pi) means $math.pi
(e) means ($math.exp 1)
### Functions:
test:
assume (("5" as a number) == 5)
$($ as a number) = $(tonumber $)
test:
unless
all of {
abs 5, | 5 |, sqrt 5, √5, sine 5, cosine 5, tangent 5, arc sine 5, arc cosine 5
arc tangent 5, arc tangent 5 / 10, hyperbolic sine 5, hyperbolic cosine 5
hyperbolic tangent 5, e^ 5, ln 5, log 5 base 2, floor 5, ceiling 5, 5 rounded
}
..:
fail "math functions failed"
{$(absolute value of $), $(| $ |), $(abs $)} = {$math.abs, $math.abs, $math.abs}
{$(square root of $), $(√$), $(sqrt $)} = {$math.sqrt, $math.sqrt, $math.sqrt}
{$(sine $), $(sin $)} = {$math.sin, $math.sin}
{$(cosine $), $(cos $)} = {$math.cos, $math.cos}
{$(tangent $), $(tan $)} = {$math.tan, $math.tan}
{$(arc sine $), $(asin $)} = {$math.asin, $math.asin}
{$(arc cosine $), $(acos $)} = {$math.acos, $math.acos}
{$(arc tangent $), $(atan $)} = {$math.atan, $math.atan}
{$(arc tangent $y / $x), $(atan2 $y $x)} = {$math_atan2, $math_atan2}
{$(hyperbolic sine $), $(sinh $)} = {$math_sinh, $math_sinh}
{$(hyperbolic cosine $), $(cosh $)} = {$math_cosh, $math_cosh}
{$(hyperbolic tangent $), $(tanh $)} = {$math_tanh, $math_tanh}
{$(e^ $), $(exp $)} = {$math.exp, $math.exp}
{$(natural log $), $(ln $), $(log $), $(log $ base $)} =
{$math.log, $math.log, $math.log, $math.log}
$(floor $) = $math.floor
{$(ceiling $), $(ceil $)} = {$math.ceil, $math.ceil}
($ rounded) means (floor ($ + 0.5))
($ rounded down) means (floor $)
($ rounded up) means (ceiling $)
test:
unless ((463 to the nearest 100) == 500): fail "rounding failed"
unless ((2.6 to the nearest 0.25) == 2.5): fail "rounding failed"
($n to the nearest $rounder) means ($rounder * (floor ($n / $rounder + 0.5)))
### Any/all
test:
assume (all of {yes, 1, "x"})
assume (not (all of {yes, no, yes}))
assume (all of {})
$list = {yes, yes}
assume (all of $list)
add (no) to $list
assume (not (all of $list))
assume
all of:
1 < 2
2 < 3
assume (not (all of {no, (fail "all of didn't short circuit")}))
assume
not
all of:
1 > 2
fail "all of didn't short circuit"
### For tables and blocks that are written out in the code, (all of) and (any of) stop
at the first value that decides the answer, like (and) and (or) do. Otherwise,
they check every value in the list.
(all of $items) compiles to:
$clauses = (clauses in $items)
unless $clauses: return \(not (\$items has (no)))
if (#$clauses == 0): return \(yes)
$chain = (chain of $clauses joined by "and" from $items.source)
return \(not (not \$chain))
(not all of $items) parses as (not (all of $items))
test:
assume (any of {no, 1, no})
assume (not (any of {no, no}))
assume (not (any of {}))
$list = {no, no}
assume (not (any of $list))
add (yes) to $list
assume (any of $list)
assume
any of:
1 > 2
2 < 3
assume (any of {yes, (fail "any of didn't short circuit")})
assume
any of:
1 < 2
fail "any of didn't short circuit"
(any of $items) compiles to:
$clauses = (clauses in $items)
unless $clauses: return \(((not $) for $ in \$items) has (no))
if (#$clauses == 0): return \(no)
$chain = (chain of $clauses joined by "or" from $items.source)
return \(not (not \$chain))
(none of $items) parses as (not (any of $items))
### Sum/product
(sum of $items) means:
$total = 0
for $ in $items:
$total += $
return $total
(product of $items) means:
$prod = 1
for $ in $items:
$prod *= $
return $prod
(average of $items) means ((sum of $items) / #$items)
### Min/max
{min of $items, smallest of $items} all mean:
$best = (nil)
for $ in $items:
if (($best == (nil)) or ($ < $best)): $best = $
return $best
{max of $items, largest of $items} all mean:
$best = (nil)
for $ in $items:
if (($best == (nil)) or ($ > $best)): $best = $
return $best
test:
assume ((min of {3, -4, 1, 2} by $ = ($ * $)) == 1)
assume ((max of {3, -4, 1, 2} by $ = ($ * $)) == -4)
(min of $items by $item = $value_expr) parses as
result of:
$best = (nil)
$best_key = (nil)
for $item in $items:
$key = $value_expr
if (($best == (nil)) or ($key < $best_key)):
$best = $item
$best_key = $key
return $best
(max of $items by $item = $value_expr) parses as
result of:
$best = (nil)
$best_key = (nil)
for $item in $items:
$key = $value_expr
if (($best == (nil)) or ($key > $best_key)):
$best = $item
$best_key = $key
return $best
test:
assume ((100 clamped between 0 and 10) == 10)
($ clamped between $min and $max) means:
when:
($ < $min):
return $min
($ > $max):
return $max
else:
return $
### Random functions
(seed random with $) means:
($math.randomseed $)
for $i in (1 to 20): random number
### With no argument, reseed from the generator itself (a sandbox has no clock)
(seed random) means (seed random with (random integer 1073741824))
$(random number) = $math.random
$(random integer $n) = $math.random
$(random number from $low to $high) = $math.random
(random choice from $elements) means $elements.(random integer #$elements)
|