aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.nom
blob: 13c7d49f18a3737edc3aea36cd6c88d6d202629a (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require "lib/metaprogramming.nom"

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

# String functions
rule [join %strs with glue %glue] =:
    lua do> ".."
        |local str_bits = {}
        |for i,bit in ipairs(vars.strs) do str_bits[i] = nomsu:stringify(bit) end
        |return table.concat(str_bits, vars.glue)
parse [join %strs] as: join %strs with glue ""

compile [capitalize %str, %str capitalized] to:
    "(\(%str as lua)):gsub('%l', string.upper, 1)"

compile [say %str] to: ".."
    |nomsu:writeln(nomsu:stringify(\(%str as lua)))

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

# Random functions
lua> ".." # Seed
    |math.randomseed(os.time());
    |for i=1,20 do; math.random(); end;
compile [random number, random, rand] to: "math.random()"
compile [random int %n, random integer %n, randint %n] to: "math.random(\(%n as lua))"
compile [random from %low to %high, random number from %low to %high, rand %low %high] to:
    "math.random(\(%low as lua), \(%high as lua))"
rule [random choice from %elements, random choice %elements, random %elements] =:
    =lua "\(%elements)[math.random(#\(%elements))]"

# Math functions
compile [abs %, absolute value of %, | % |] to: "math.abs(\(% as lua))"
compile [sqrt %, square root of %] to: "math.sqrt(\(% as lua))"
compile [sin %, sine %] to: "math.sin(\(% as lua))"
compile [cos %, cosine %] to: "math.cos(\(% as lua))"
compile [tan %, tangent %] to: "math.tan(\(% as lua))"
compile [asin %, arc sine %] to: "math.asin(\(% as lua))"
compile [acos %, arc cosine %] to: "math.acos(\(% as lua))"
compile [atan %, arc tangent %] to: "math.atan(\(% as lua))"
compile [atan2 %y %x] to: "math.atan2(\(%y as lua), \(%x as lua))"
compile [sinh %, hyperbolic sine %] to: "math.sinh(\(% as lua))"
compile [cosh %, hyperbolic cosine %] to: "math.cosh(\(% as lua))"
compile [tanh %, hyperbolic tangent %] to: "math.tanh(\(% as lua))"
compile [ceil %, ceiling %] to: "math.ceil(\(% as lua))"
compile [exp %, e^ %] to: "math.exp(\(% as lua))"
compile [log %, ln %, natural log %] to: "math.log(\(% as lua))"
compile [log % base %base] to: "math.log(\(% as lua), \(%base as lua))"
compile [floor %] to: "math.floor(\(% as lua))"
compile [round %, % rounded] to: "math.floor(\(% as lua) + .5)"
rule [%n rounded to the nearest %rounder] =:
    =lua "(\(%rounder))*math.floor(\(%n)/\(%rounder) + .5)"
compile [tau] to: "(2*math.pi)"
compile [pi] to: "math.pi"
compile [e] to: "math.e"
compile [golden ratio, phi] to: "((1 + math.sqrt(5)) / 2)"

# Common utility functions
compile [sum of %items, sum %items] to: "nomsu.utils.sum(\(%items as lua))"
compile [product of %items, product %items] to: "nomsu.utils.product(\(%items as lua))"
compile [all of %items] to: "nomsu.utils.all(\(%items as lua))"
compile [any of %items] to: "nomsu.utils.any(\(%items as lua))"
rule [avg of %items, average of %items] =:
    =lua "(nomsu.utils.sum(\(%items))/#\(%items))"
compile [min of %items, smallest of %items, lowest of %items] to:
    "nomsu.utils.min(\(%items as lua))"
compile [max of %items, biggest of %items, largest of %items, highest of %items] to:
    "nomsu.utils.max(\(%items as lua))"
compile [min of %items by %value_expr] to:
    ".."
        |nomsu.utils.min(\(%items as lua), function(item)
        |    local vars = setmetatable({['']=item}, {__index=vars})
        |    return \(%value_expr as lua)
        |end)
compile [max of %items by %value_expr] to:
    ".."
        |nomsu.utils.max(\(%items as lua), function(item)
        |    local vars = setmetatable({['']=item}, {__index=vars})
        |    return \(%value_expr as lua)
        |end)
compile [sort %items] to: "table.sort(\(%items as lua))"
rule [sort %items by %key] =: =lua ".."
    |nomsu.utils.sort(\(%items), function(x)
    |    return (\(%key))(nomsu, {['']=x});
    |end)

# String utilities
compile [nl, newline, line feed, linefeed, lf] to: ".."
    |"\n"
compile [tab] to: ".."
    |"\t"
compile [bell] to: ".."
    |"\a"
compile [cr, carriage return] to: ".."
    |"\r"
compile [backspace] to: ".."
    |"\b"
compile [form feed, formfeed] to: ".."
    |"\f"
compile [vertical tab] to: ".."
    |"\v"

lua> ".."
    |do;
    |    local colors = {
    |        ["reset color"] = 0, bright = 1, dim = 2, underscore = 4, blink = 5,
    |        inverse = 7, hidden = 8, black = 30, red = 31, green = 32, yellow = 33,
    |        blue = 34, magenta = 35, cyan = 36, white = 37, ["on black"] = 40,
    |        ["on red"] = 41, ["on green"] = 42, ["on yellow"] = 43, ["on blue"] = 44,
    |        ["on magenta"] = 45, ["on cyan"] = 46, ["on white"] = 47,
    |    };
    |    for name,code in pairs(colors) do;
    |        local escape = "\\"\\\\27["..tostring(code).."m\\""
    |        nomsu:defmacro(name, function() return escape end, "");
    |    end;
    |end;