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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
#!/usr/bin/env moon
utils = require 'utils'
Game = require 'nomic'
core_game = require 'core'
game = Game(core_game)
game\def "# $key $relation = $value", (args)=>
--print "Setting #{args.key} #{args.relation} = #{args.value}"
@relations[args.relation][args.key] = args.value
return args.value
game\def "set all * $relation = $value", (args)=>
rels = @relations[args.relation]
for k,_ in pairs rels
rels[k] = args.value
if next(rels) == nil
@relations[args.relation] = nil
return args.value
game\def "$key $relation ?", (args)=>
return @relations[args.relation][args.key]
deep_pairs = (t)->
coroutine.wrap ->
for k,v in pairs(t)
coroutine.yield k, v
mt = getmetatable(t)
return unless mt and mt.__index and type(mt.__index) == 'table' and mt.__index != t
for k,v in deep_pairs mt.__index
if t[k] == nil
coroutine.yield k,v
game\def "* $relation = $value", (args)=>
--print "Finding all * #{args.relation} = #{args.value}"
[key for key, value in deep_pairs(@relations[args.relation]) when value == args.value]
game\def {"restrict $actions to $whitelist"}, (args)=>
with args
actions = @canonicalize(if type(.actions) == 'table' then .actions else {.actions})
whitelist = @all_aliases(if type(.whitelist) == 'table' then .whitelist else {.whitelist})
@\set_whitelist actions, whitelist
for action in *actions
print("Restricting #{utils.repr(action)} to #{utils.repr(whitelist)}")
game\def {"permit $whitelist to $actions"}, (args)=>
with args
actions = @canonicalize(if type(.actions) == 'table' then .actions else {.actions})
whitelist = @all_aliases(if type(.whitelist) == 'table' then .whitelist else {.whitelist})
for action in *actions
if not @authorized[action]
print "#{action} is already available to everyone."
continue
print("Permitting #{utils.repr(action)} to #{utils.repr(whitelist)}")
for w in *whitelist
@authorized[action][w] = true
game\def {"revoke $actions rights from $whitelist"}, (args)=>
with args
actions = @canonicalize(if type(.actions) == 'table' then .actions else {.actions})
whitelist = @all_aliases(if type(.whitelist) == 'table' then .whitelist else {.whitelist})
for action in *actions
if not @authorized[action]
print "#{action} is available to everyone, it can't be restricted."
continue
print("Revoking the right of #{utils.repr(action)} to use #{utils.repr(whitelist)}")
for w in *whitelist
@authorized[action][w] = nil
game\def "print callstack", (args)=>
print("Callstack:")
for fn in *@callstack
print fn
game\def {"do $action"}, (args)=>
(args.action)(@, {})
game\def {"make $who $action"}, (args)=>
with args
old_you = @you
print("Setting you=#{.who}")
rawset(@, "you", .who)
(.action)(@, {})
rawset(@, "you", old_you)
game\def "you", (args)=> @you
game\run[=[
say "===================================================="
say " NEW GAME"
say "===================================================="
"everyone approves $action" := {yes}
"sudo $action" := {
if (everyone approves $action) {
do $action
} else {
say "You do not have the will of the people! >:("
}
}
restrict "$ := $" to "sudo $"
restrict "make $ $" to "sudo $"
restrict "set all * $ = $" to "sudo $"
restrict "# $ $ = $" to "sudo $"
restrict "restrict $ to $" to "sudo $"
sudo {
"propose $action" := {
if ("pending proposal" "is" ?) {
say "Sorry, an action is already pending."
} else {
say "Proposing..."
# "pending proposal" "is" = $action
}
}
"unpropose" := {
let "pending" = ("pending proposal" "is" ?)
set all * $pending = (nil)
# "pending proposal" "is" = (nil)
}
"mark $who as approving $action" := {
# $who $action = "approved"
}
"mark $who as rejecting $action" := {
# $who $action = "rejected"
}
["approve", "vote yes", "vote yea"] := {
let "pending" = ("pending proposal" "is" ?)
mark (you) as approving $pending
say "Voted yes."
if (everyone approves $pending) {
sudo $pending
unpropose
} else {
let "approvers" = (# (* $pending = "approved"))
let "num-players" = (# (players))
printf [$approvers, "/", $num-players, " players have approved"]
}
}
["reject", "vote no", "vote nay", "veto", "disapprove"] := {
let "pending" = ("pending proposal" "is" ?)
mark (you) as rejecting $pending
say "Voted no."
unpropose
}
["players", "everyone", "everybody", "all players"] := {
* "is a player" = (yes)
}
"join" := {
# (you) "is a player" = (yes)
printf ["Welcome to the game, ",(you),"!"]
}
permit "unpropose" to "set all * $ = $"
permit ["join", "mark $ as approving $", "mark $ as rejecting $", "propose $", "unpropose"] to "# $ $ = $"
restrict "unpropose" to ["approve", "reject"]
restrict "mark $ as approving $" to ["propose $", "approve"]
restrict "mark $ as rejecting $" to ["propose $", "reject"]
"everyone approves $action" := {
(# (players)) == (# (* $action = "approved"))
}
}
join
propose {
say "fart"
}
approve
"cheat" := {
say "CHEATER!!!"
}
sudo {
say "CHEATER!!!"
}
propose {
# "democracy" "is possible" = (yes)
if ("democracy" "is possible" ?) {
say "DEMOCRACY WORKS!!!"
}
}
approve
propose {
"fart" := {
say "poot"
}
say "fart should have been defined"
}
approve
say "doop"
fart
propose {
"open election $candidates" := {
if ("candidates" "are" ?) {
say "An election is already in progress."
} else {
# "candidates" "are" = $candidates
}
}
"close election" := {
let "pending" = ("pending proposal" "is" ?)
set all * "votes for" = (nil)
# "candidates" "are" = (nil)
}
"vote for $candidate" := {
# (you) "votes for" = $candidate
let "vote-percent" = ((# (* "votes for" = $candidate)) / (# (players)))
printf ["Vote cast. ",$candidate," now has ",(100 * $vote-percent),"% of the votes."]
if ($vote-percent > 0.5) {
printf ["The winner of the election is:", $candidate]
close election
}
}
permit ["open election $", "close election", "vote for $"] to ["# $ $ = $"]
permit ["close election"] to ["set all * $ = $"]
}
approve
propose {
"as bill: $action" := {
if ((you) == "Anonymous") {
make "bill" $action
} else {
printf ["Who do you think you are?", (you)]
}
}
permit ["as bill: $"] to ["make $ $"]
}
approve
as bill: {join}
propose {
"as dave: $action" := {
if ((you) == "Anonymous") {
make "dave" $action
} else {
printf ["Who do you think you are?", (you)]
}
}
permit ["as dave: $"] to ["make $ $"]
}
approve
as bill: {approve}
as dave: {join}
open election ["tom", "dick", "harry"]
vote for "dick"
as bill: {vote for "dick"}
propose {
"take a shit" := {say "shit taken."}
}
approve
as bill: {approve}
as dave: {approve}
sudo {
"everyone approves" := {
(# (players)) == (# (* "votes" (yes)))
}
["approve", "vote yes", "vote yea"] := {
# (you) "votes" = (yes)
if (everyone approves) {
do pending action
}
}
["disapprove", "vote no", "vote nay", "veto"] := {
say "The proposal has failed."
# (you) "approves" = (yes)
if (everyone approves) {
do pending action
}
}
}
sudo {
"everyone approves" := {
say "Going into this code"
no
}
}
sudo {
say "BROKEN"
}
propose {
"arbitrarily define $signature := $body" := {
if ($signature == "butts") {
$signature := $body
} else {
say "Not my style."
}
}
permit "arbitrarily define $ := $" to "$ := $"
say "Arbitrary is a go."
}
as bill: {approve}
as dave: {approve}
approve
arbitrarily define "butts" := {say "BUTTS"}
butts
]=]
|