aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.nom
blob: 1afc33ade9e8e55f2586f3bd0e9fee304a70415a (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
67
68
69
70
71
72
73
require "lib/metaprogramming.nom"

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

macro statement [show generated lua %block] =: ".."
    |nomsu:writeln(\lua expr "nomsu:repr(nomsu:tree_to_lua(vars.block.value))"\)


# 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)

macro [capitalize %str, %str capitalized] =: ".."
    |(\%str as lua\):gsub("%l", string.upper, 1)

macro [repr %obj] =:
    ".."|nomsu:repr(\%obj as lua\)

macro [%obj as string] =:
    ".."|nomsu.utils.repr_if_not_string(\%obj as lua\)

macro [say %str] =:
    ".."|nomsu:writeln(nomsu.utils.repr_if_not_string(\%str as lua\))

# Number ranges
macro [%start to %stop] =: ".."
   |nomsu.utils.range(\%start as lua\, \%stop as lua\)
macro [%start to %stop by %step, %start to %stop via %step] =: ".."
   |nomsu.utils.range(\%start as lua\, \%stop as lua\, \%step as lua\)

# Common utility functions
macro [random number, random, rand] =: "math.random()"
macro [random int %n, random integer %n, randint %n] =: ".."|math.random(\%n as lua\)
macro [random from %low to %high, random number from %low to %high, rand %low %high] =: ".."
    |math.random(\%low as lua\, \%high as lua\)
rule [random choice from %elements, random choice %elements, random %elements] =:
    lua expr ".."|vars.elements[math.random(#vars.elements)]
macro [sum of %items, sum %items] =: ".."|nomsu.utils.sum(\%items as lua\)
macro [product of %items, product %items] =: ".."|nomsu.utils.product(\%items as lua\)
macro [all of %items] =: ".."|nomsu.utils.all(\%items as lua\)
macro [any of %items] =: ".."|nomsu.utils.any(\%items as lua\)
# 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)
macro [min of %items, smallest of %items, lowest of %items] =:
    ".."|nomsu.utils.min(\%items as lua\)
macro [max of %items, biggest of %items, largest of %items, highest of %items] =:
    ".."|nomsu.utils.min(\%items as lua\)
macro [min of %items with respect to %keys] =:
    ".."|nomsu.utils.min(\%items as lua\, \%keys as lua\)
macro [max of %items with respect to %keys] =:
    ".."|nomsu.utils.max(\%items as lua\, \%keys as lua\)