nomsu/core/math.nom

96 lines
4.7 KiB
Plaintext
Raw Normal View History

#..
This file defines some common math literals and functions
use "core/metaprogramming.nom"
use "core/text.nom"
use "core/operators.nom"
use "core/control_flow.nom"
# Literals:
2018-04-19 17:23:44 -07:00
compile [infinity, inf] to: Lua value "math.huge"
compile [not a number, NaN, nan] to: Lua value "(0/0)"
compile [pi, Pi, PI] to: Lua value "math.pi"
compile [tau, Tau, TAU] to: Lua value "(2*math.pi)"
compile [golden ratio] to: Lua value "((1+math.sqrt(5))/2)"
compile [e] to: Lua value "math.exp(1)"
# Functions:
2018-04-19 17:23:44 -07:00
compile [% as a number] to: Lua value "tonumber(\(% as lua expr))"
compile [absolute value %, | % |, abs %] to: Lua value "math.abs(\(% as lua expr))"
compile [square root %, square root of %, √%, sqrt %] to: Lua value "math.sqrt(\(% as lua expr))"
compile [sine %, sin %] to: Lua value "math.sin(\(% as lua expr))"
compile [cosine %, cos %] to: Lua value "math.cos(\(% as lua expr))"
compile [tangent %, tan %] to: Lua value "math.tan(\(% as lua expr))"
compile [arc sine %, asin %] to: Lua value "math.asin(\(% as lua expr))"
compile [arc cosine %, acos %] to: Lua value "math.acos(\(% as lua expr))"
compile [arc tangent %, atan %] to: Lua value "math.atan(\(% as lua expr))"
compile [arc tangent %y/%x, atan2 %y %x] to: Lua value "math.atan2(\(%y as lua expr), \(%x as lua expr))"
compile [hyperbolic sine %, sinh %] to: Lua value "math.sinh(\(% as lua expr))"
compile [hyperbolic cosine %, cosh %] to: Lua value "math.cosh(\(% as lua expr))"
compile [hyperbolic tangent %, tanh %] to: Lua value "math.tanh(\(% as lua expr))"
compile [e^%, exp %] to: Lua value "math.exp(\(% as lua expr))"
compile [natural log %, ln %, log %] to: Lua value "math.log(\(% as lua expr))"
compile [log % base %base, log_%base %, log base %base %] to: Lua value "math.log(\(% as lua expr), \(%base as lua expr))"
compile [floor %] to: Lua value "math.floor(\(% as lua expr))"
compile [ceiling %, ceil %] to: Lua value "math.ceil(\(% as lua expr))"
compile [round %, % rounded] to: Lua value "math.floor(\(% as lua expr) + .5)"
2018-01-31 15:31:06 -08:00
action [%n to the nearest %rounder]:
=lua "(\%rounder)*math.floor((\%n / \%rounder) + .5)"
# Any/all/none
2018-01-31 15:31:06 -08:00
compile [all of %items, all %items] to:
unless: (%items' "type") is "List"
2018-04-19 17:23:44 -07:00
return: Lua value "utils.all(\(%items as lua expr))"
%clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
2018-04-19 17:23:44 -07:00
return: Lua value "(\(%clauses joined with " and "))"
parse [not all of %items, not all %items] as: not (all of %items)
2018-01-31 15:31:06 -08:00
compile [any of %items, any %items] to:
unless: (%items' "type") is "List"
2018-04-19 17:23:44 -07:00
return: Lua value "utils.any(\(%items as lua expr))"
%clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
2018-04-19 17:23:44 -07:00
return: Lua value "(\(%clauses joined with " or "))"
parse [none of %items, none %items] as: not (any of %items)
2018-01-31 15:31:06 -08:00
compile [sum of %items, sum %items] to:
unless: (%items' "type") is "List"
2018-04-19 17:23:44 -07:00
return: Lua value "utils.sum(\(%items as lua expr))"
%clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
2018-04-19 17:23:44 -07:00
return: Lua value "(\(%clauses joined with " + "))"
2018-01-31 15:31:06 -08:00
compile [product of %items, product %items] to:
unless: (%items' "type") is "List"
2018-04-19 17:23:44 -07:00
return: Lua value "utils.product(\(%items as lua expr))"
%clauses <- []
for all (%items' "value"): lua> "table.insert(\%clauses, \(% as lua expr));"
2018-04-19 17:23:44 -07:00
return: Lua value "(\(%clauses joined with " * "))"
2018-01-31 15:31:06 -08:00
action [avg of %items, average of %items]:
=lua "(utils.sum(\%items)/#\%items)"
2018-04-19 17:23:44 -07:00
compile [min of %items, smallest of %items, lowest of %items] to
Lua value "utils.min(\(%items as lua expr))"
compile [max of %items, biggest of %items, largest of %items, highest of %items] to
Lua value "utils.max(\(%items as lua expr))"
compile [min of %items by %value_expr] to
Lua value ".."
utils.min(\(%items as lua expr), function(_)
return \(%value_expr as lua expr)
end)
2018-04-19 17:23:44 -07:00
compile [max of %items by %value_expr] to
Lua value ".."
utils.max(\(%items as lua expr), function(_)
return \(%value_expr as lua expr)
end)
# Random functions
2018-01-31 15:31:06 -08:00
action [seed random with %]:
lua> ".."
math.randomseed(\%);
for i=1,20 do math.random(); end
parse [seed random] as: seed random with (=lua "os.time()")
2018-04-19 17:23:44 -07:00
compile [random number, random, rand] to: Lua value "math.random()"
compile [random int %n, random integer %n, randint %n] to: Lua value "math.random(\(%n as lua expr))"
compile [random from %low to %high, random number from %low to %high, rand %low %high] to
"math.random(\(%low as lua expr), \(%high as lua expr))"
2018-01-31 15:31:06 -08:00
action [random choice from %elements, random choice %elements, random %elements]:
=lua "\%elements[math.random(#\%elements)]"