aboutsummaryrefslogtreecommitdiff
path: root/core.moon
blob: 4c84f66f53cc7d0d8e93b7052f6aadbf232fab01 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env moon
Nomic = require 'nomic'
utils = require 'utils'


class PermissionNomic extends Nomic
    new: (...)=>
        super(...)
        @callstack = {}

    call: (fn_name,...)=>
        fn_info = @defs[fn_name]
        if fn_info == nil
            error "Attempt to call undefined function: #{fn_name}"
        unless self\check_permission(fn_name)
            error "You do not have the authority to call: #{fn_name}"
        table.insert @callstack, fn_name
        {:fn, :arg_names} = fn_info
        args = {name, select(i,...) for i,name in ipairs(arg_names)}
        if @debug
            print "Calling #{fn_name} with args: #{utils.repr(args)}"
        ret = fn(self, args)
        table.remove @callstack
        return ret

    check_permission: (fn_name)=>
        fn_info = @defs[fn_name]
        if fn_info == nil
            error "Undefined function: #{fn_name}"
        if fn_info.whitelist == nil then return true
        for caller in *@callstack
            if fn_info.whitelist[caller]
                return true
        return false


g = PermissionNomic()

g\defmacro [[lua %lua_code]], (vars, kind)=>
    lua_code = vars.lua_code.value
    as_lua_code = (str)->
        switch str.type
            when "String"
                escapes = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r"
                unescaped = str.value\gsub("\\(.)", ((c)-> escapes[c] or c))
                return unescaped

            when "Longstring"
                -- TODO: handle comments?
                result = [line for line in str.value\gmatch("[ \t]*|([^\n]*)")]
                return table.concat(result, "\n")
            else
                return @tree_to_lua(str)

    switch lua_code.type
        when "List"
            -- TODO: handle subexpressions
            return table.concat([as_lua_code(i.value) for i in *lua_code.value]), true
        else
            return as_lua_code(lua_code), true

g\def {"restrict %fn to %whitelist"}, (vars)=>
    fns = if type(vars.fn) == 'string' then {vars.fn} else vars.fn
    whitelist = if type(vars.whitelist) == 'string' then {vars.whitelist} else vars.whitelist
    whitelist = {w,true for w in *whitelist}
    for fn in *fns
        fn_info = @defs[fn]
        if fn_info == nil
            print "Undefined function: #{fn}"
            continue
        unless self\check_permission(fn)
            print "You do not have permission to restrict function: #{fn}"
            continue
        @defs[fn] = whitelist

g\def {"allow %whitelist to %fn"}, (vars)=>
    fns = if type(vars.fn) == 'string' then {vars.fn} else vars.fn
    whitelist = if type(vars.whitelist) == 'string' then {vars.whitelist} else vars.whitelist
    for fn in *fns
        fn_info = @defs[fn]
        if fn_info == nil
            print "Undefined function: #{fn}"
            continue
        if fn_info.whitelist == nil
            print "Function is already allowed by everyone: #{fn}"
            continue
        unless self\check_permission(fn)
            print "You do not have permission to grant permissions for function: #{fn}"
            continue
        for w in *whitelist do fn_info.whitelist[w] = true

g\def {"forbid %blacklist to %fn"}, (vars)=>
    fns = if type(vars.fn) == 'string' then {vars.fn} else vars.fn
    blacklist = if type(vars.blacklist) == 'string' then {vars.blacklist} else vars.blacklist
    for fn in *fns
        fn_info = @defs[fn]
        if fn_info == nil
            print "Undefined function: #{fn}"
            continue
        if fn_info.whitelist == nil
            print "Cannot remove items from a whitelist when there is no whitelist on function: #{fn}"
            continue
        unless self\check_permission(fn)
            print "You do not have permission to restrict function: #{fn}"
            continue
        for b in *blacklist do fn_info.whitelist[b] = nil


g\def {"say %x", "print %x"}, (vars)=>
    print(utils.repr(vars.x))

g\def [[printf %str]], (args)=>
    for s in *args.str do io.write(utils.repr(s))
    io.write("\n")

g\def [[concat %strs]], (vars)=>
    return table.concat([utils.repr(s) for s in *vars.strs], "")

g\def [[quote %str]], (vars)=>
    return utils.repr(vars.str, true)

g\defmacro "return %retval", (vars, kind)=>
    if kind == "Expression"
        error("Cannot use a return statement as an expression")
    return "do return "..((@tree_to_lua(vars.retval))\match("%s*(.*)")).." end", true

g\defmacro "let %varname = %value", (vars, kind)=>
    if kind == "Expression"
        error("Cannot set a variable in an expression.")
    return "vars[#{@tree_to_lua(vars.varname)}] = #{@tree_to_lua(vars.value)}", true

singleton = (aliases, value)->
    g\defmacro aliases, ((vars)=> value)

infix = (ops)->
    for op in *ops
        alias = op
        if type(op) == 'table'
            {alias,op} = op
        g\defmacro "%x #{alias} %y", (vars)=>
            return "(#{@tree_to_lua(vars.x)} #{op} #{@tree_to_lua(vars.y)})"

unary = (ops)->
    for op in *ops
        g\defmacro "#{op} %x", (vars)=>
            return "#{op}(#{@tree_to_lua(vars.x)})"

singleton {"true","yes"}, "true"
singleton {"false","no"}, "false"
singleton {"nil","null","nop","pass"}, "nil"
infix{"+","-","*","/","==",{"!=","~="},"<","<=",">",">=","^","and","or",{"mod","%"}}
unary{"-","#","not"}
g\def [[%x == %y]], (args)=> utils.equivalent(args.x, args.y)

g\def "rule %spec %body", (vars)=>
    self\def vars.spec, vars.body

-- TODO: write help


g\def [[random]], -> math.random()

g\def [[sum %items]], (args)=> utils.sum(args.items)
g\def [[all %items]], (args)=> utils.all(args.items)
g\def [[any %items]], (args)=> utils.any(args.items)
g\def {[[average %items]], [[avg %items]]}, (args)=> utils.sum(items)/#items
g\def {[[min %items]], [[smallest %items]], [[lowest %items]], [[fewest %items]]}, (args)=>
    utils.min(args.items)

g\def {[[max %items]], [[largest %items]], [[highest %items]], [[most %items]]}, (args)=>
    utils.max(args.items)

g\def {[[argmin %items]]}, (args)=>
    utils.min(args.items, ((i)->i[2]))
g\def {[[argmax %items]]}, (args)=>
    utils.max(args.items, ((i)->i[2]))

g\def {[[min %items with respect to %keys]]}, (args)=>
    utils.min(args.items, args.keys)
g\def {[[max %items with respect to %keys]]}, (args)=>
    utils.max(args.items, args.keys)

g\def {[[%index st in %list]], [[%index nd in %list]], [[%index rd in %list]], [[%index th in %list]]}, (args)=>
    with args
        if type(.list) != 'table'
            print "Not a list: #{.list}"
            return
        .list[.index]

g\def {[[index of %item in %list]]}, (args)=>
    with args
        if type(.list) != 'table'
            print "Not a list: #{.list}"
            return
        utils.key_for(args.list, args.item)

g\run [=[
rule ["%item is in %list", "%list contains %item"]: (index of %item in %list) != (nil)
]=]

g\def {[[# %list]], [[length of %list]], [[size of %list]]}, (args)=>
    with args
        if type(.list) != 'table'
            print "Not a list: #{.list}"
            return
        return #(.list)

g\defmacro "if %condition %if_body else %else_body", (vars, kind)=>
    if kind == "Expression"
        return ([[(function(game, vars)
            if (%s) then
                %s
            else
                %s
            end
        end)(game, vars)]])\format(@tree_to_lua(vars.condition),
            @tree_to_lua(vars.if_body.value.value),
            @tree_to_lua(vars.else_body.value.value))
    else
        return ([[
            if (%s) then
                %s
            else
                %s
            end
        ]])\format(@tree_to_lua(vars.condition),
            @tree_to_lua(vars.if_body.value.value),
            @tree_to_lua(vars.else_body.value.value)), true

g\defmacro "for %varname in %iterable %body", (vars, kind)=>
    if kind == "Expression"
        return "
(function(game, vars)
    local comprehension, old_loopval = {}, vars[#{@tree_to_lua(vars.varname)}]
    for i, value in ipairs(#{@tree_to_lua(vars.iterable)}) do
        local ret
        vars[#{@tree_to_lua(vars.varname)}] = value
        #{@tree_to_lua(vars.body.value.value)}
        table.insert(comprehension, ret)
    end
    vars[#{@tree_to_lua(vars.varname)}] = old_loopval
    return comprehension
end)(game, vars)"
    else
        return "
do
    local comprehension, old_loopval = {}, vars[#{@tree_to_lua(vars.varname)}]
    for i, value in ipairs(#{@tree_to_lua(vars.iterable)}) do
        vars[#{@tree_to_lua(vars.varname)}] = value
        #{@tree_to_lua(vars.body.value.value)}
    end
    vars[#{@tree_to_lua(vars.varname)}] = old_loopval
end", true

g\simplemacro "for %varname = %start to %stop %body", [[for %varname in (lua ["utils.range(",%start,",",%stop,")"]) %body]]

g\simplemacro "if %condition %body", [[
if %condition %body
..else: nil
]]

g\simplemacro "unless %condition %body", [[
if (not %condition) %body
..else: nil
]]

g\def [[do %action]], (vars)=> return vars.action(self,vars)


g\defmacro [[macro %spec %body]], (vars, kind)=>
    if kind == "Expression" then error("Cannot use a macro definition in an expression.")
    self\simplemacro vars.spec.value.value, vars.body.src
    return "", true

g\defmacro [[test %code yields %tree]], (vars, kind)=>
    if kind == "Expression" then error("Tests must be statements.")
    got = self\stringify_tree(vars.code.value)
    got = got\match("Thunk:\n    (.*)")\gsub("\n    ","\n")
    got = utils.repr(got,true)
    expected = @tree_to_lua(vars.tree)
    return "
do
    local got = #{got}
    local expected = #{expected}
    if got ~= expected then
        error('Test failed. Expected:\\n'..expected..'\\n\\nBut got:\\n'..got)
    end
end", true

return g