aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.nom
blob: 11596f1e93d22158bd902b88d109e3cc6145267a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require "lib/metaprogramming.nom"

# Error functions
rule (error!; panic!; fail!; abort!) =:
    nomsu "error" []
rule (error %msg) =:
    nomsu "error"[%msg]
parse (assert %condition) as: lua code ".."
    |if not (\(%condition)) then
    |    nomsu:error()
    |end
parse (assert %condition %msg) as: lua code ".."
    |if not (\(%condition)) then
    |    nomsu:error(\(%msg))
    |end

parse (show generated lua %block) as: lua code ".."
    |nomsu:writeln(\(repr (nomsu "tree_to_lua" [%block])))


# String functions
rule (join %strs) =:
    lua block ".."
        |local str_bits = {}
        |for i,bit in ipairs(vars.strs) do str_bits[i] = nomsu.utils.repr_if_not_string(bit) end
        |return table.concat(str_bits)

rule (join %strs with glue %glue) =:
    lua block ".."
        |local str_bits = {}
        |for i,bit in ipairs(vars.strs) do str_bits[i] = nomsu.utils.repr_if_not_string(bit) end
        |return table.concat(str_bits, vars.glue)

parse (capitalize %str; %str capitalized) as: lua expr "(\(%str)):gsub('%l', string.upper, 1)"

parse (say %str) as: lua block ".."
    |nomsu:writeln(nomsu.utils.repr_if_not_string(\(%str)))

# Number ranges
parse (%start to %stop) as: lua expr ".."
   |nomsu.utils.range(\(%start), \(%stop))
parse (%start to %stop by %step; %start to %stop via %step) as: lua expr ".."
   |nomsu.utils.range(\(%start), \(%stop), \(%step))

# Common utility functions
parse (random number; random; rand) as: lua expr "math.random()"
parse (random int %n; random integer %n; randint %n) as: lua expr "math.random(\(%n))"
parse (random from %low to %high; random number from %low to %high; rand %low %high) as:
    lua expr "math.random(\(%low), \(%high))"
rule (random choice from %elements; random choice %elements; random %elements) =:
    lua expr "vars.elements[math.random(#vars.elements)]"
parse (sum of %items; sum %items) as: lua expr "nomsu.utils.sum(\(%items))"
parse (product of %items; product %items) as: lua expr "nomsu.utils.product(\(%items))"
parse (all of %items) as: lua expr "nomsu.utils.all(\(%items))"
parse (any of %items) as: lua expr "nomsu.utils.any(\(%items))"
# This is a rule, not a macro so we can use vars.items twice without running it twice.
rule (avg of %items; average of %items) =:
    lua expr "(nomsu.utils.sum(vars.items)/#vars.items)"
parse (min of %items; smallest of %items; lowest of %items) as:
    lua expr "nomsu.utils.min(\(%items))"
parse (max of %items; biggest of %items; largest of %items; highest of %items) as:
    lua expr "nomsu.utils.max(\(%items))"
parse (min of %items with respect to %keys) as:
    lua expr "nomsu.utils.min(\(%items), \(%keys))"
parse (max of %items with respect to %keys) as:
    lua expr "nomsu.utils.max(\(%items), \(%keys))"