Trimming some dead code.
This commit is contained in:
parent
e4ca1cace7
commit
052504a86b
326
game1.moon
326
game1.moon
@ -1,326 +0,0 @@
|
||||
#!/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
|
||||
|
||||
]=]
|
||||
|
||||
|
||||
|
||||
|
||||
|
125
game2.moon
125
game2.moon
@ -1,125 +0,0 @@
|
||||
#!/usr/bin/env moon
|
||||
utils = require 'utils'
|
||||
Game = require 'nomic'
|
||||
g = Game(require'core')
|
||||
|
||||
print("===========================================================================================")
|
||||
|
||||
|
||||
g\run[[
|
||||
|
||||
say [..]
|
||||
"this is a stupidly long list", "the items go way past the 80 character", "limit that older consoles"
|
||||
"had.", "It just keeps going and going"
|
||||
|
||||
rule "dumbfunc %a %b %c %d %e":
|
||||
say "doop"
|
||||
|
||||
dumbfunc..
|
||||
"this is a stupidly long set of arguments" "the items go way past the 80 character" "limit that older consoles"
|
||||
"had." "It just keeps going and going"
|
||||
|
||||
]]
|
||||
g\run[[
|
||||
rule "four": return 4
|
||||
rule "say both %one and %two":
|
||||
say %one
|
||||
say %two
|
||||
|
||||
say both ..
|
||||
"a list:"
|
||||
and [..]
|
||||
1,2,3,(four),(5)
|
||||
|
||||
say "done"
|
||||
]]
|
||||
|
||||
g\run[[
|
||||
rule "do %thing also %also-thing":
|
||||
do %thing
|
||||
do %also-thing
|
||||
return 99
|
||||
]]
|
||||
g\run[[
|
||||
do: say "one liner"
|
||||
..also: say "another one liner"
|
||||
]]
|
||||
g\run[[
|
||||
|
||||
say (..)
|
||||
do:
|
||||
say "hi"
|
||||
return 5
|
||||
say "bye"
|
||||
|
||||
]]
|
||||
g\run[[
|
||||
say (do: return "wow")
|
||||
if 1: say "hi1" ..else: say "bye1"
|
||||
|
||||
if 1: say "hi2"
|
||||
..else: say "bye2"
|
||||
|
||||
]]
|
||||
g\run[[
|
||||
rule "foo %x":
|
||||
if %x:
|
||||
say "YES"
|
||||
55
|
||||
..else:
|
||||
say "NO"
|
||||
-99
|
||||
|
||||
say (foo 1)
|
||||
say (foo (false))
|
||||
|
||||
]]
|
||||
|
||||
g\run[[
|
||||
say (1 + (-(2 * 3)))
|
||||
]]
|
||||
|
||||
g\run[[
|
||||
for "x" in ["A","B","C"]:
|
||||
say %x
|
||||
]]
|
||||
g\run[[
|
||||
say (for "x" in [1,2,3]:%x + 100)
|
||||
say (..)
|
||||
for "x" in [1,2,3]:
|
||||
%x + 200
|
||||
]]
|
||||
|
||||
g\run[[
|
||||
if (1 == 1):
|
||||
say "Simple macros work!"
|
||||
unless (1 > 2):
|
||||
say "This one too!"
|
||||
]]
|
||||
|
||||
g\simplemacro [[smet %varname = %value]],[[
|
||||
lua ["vars[\"", %varname, "\"] = ", %value]
|
||||
lua ["vars[\"", %varname, "\"] = 2*vars[\"", %varname, "\"]"]
|
||||
]]
|
||||
|
||||
g\run[[
|
||||
smet "fnord" = 23
|
||||
say %fnord
|
||||
]]
|
||||
|
||||
g\run[[
|
||||
|
||||
rule "fizz buzz":
|
||||
for "i" = 1 to 100:
|
||||
if ((%i mod 15) == 0):
|
||||
say "fizzbuzz"
|
||||
..else: if ((%i mod 3) == 0):
|
||||
say "fizz"
|
||||
..else: if ((%i mod 5) == 0):
|
||||
say "buzz"
|
||||
..else:
|
||||
say %i
|
||||
|
||||
fizz buzz
|
||||
|
||||
]]
|
188
sample_game.nom
Normal file
188
sample_game.nom
Normal file
@ -0,0 +1,188 @@
|
||||
run file "core.nom"
|
||||
lua block ".."
|
||||
|do -- Use a closure to hide this behind the accessor rules
|
||||
| local relations = setmetatable({}, {__index=function(self,key)
|
||||
| local t = {}
|
||||
| self[key] = t
|
||||
| return t
|
||||
| end})
|
||||
| compiler:def("%key %relation ?", function(_,vars)
|
||||
| return relations[vars.relation][vars.key]
|
||||
| end)
|
||||
| compiler:def("%key %relation = %value", function(_,vars)
|
||||
| relations[vars.relation][vars.key] = vars.value
|
||||
| end)
|
||||
| compiler:def("* %relation = %value", function(_,vars)
|
||||
| local result = {}
|
||||
| for k,v in pairs(relations) do
|
||||
| if utils.equivalent(v, vars.value) then
|
||||
| table.insert(result, k)
|
||||
| end
|
||||
| end
|
||||
| return result
|
||||
| end)
|
||||
|end
|
||||
|
||||
rule "set all * %relation = %value":
|
||||
for "key" in (* %relation = %value):
|
||||
%key %relation = %value
|
||||
|
||||
rule "you":
|
||||
lua expr "(you or 'Anonymous')"
|
||||
|
||||
rule ["make %person %action", "make %person do %action"]:
|
||||
lua block [..]
|
||||
"do"
|
||||
"\n local old_you = you"
|
||||
"\n you = vars.person"
|
||||
"\n ret = compiler:call('do %', vars.action)"
|
||||
"\n you = old_you"
|
||||
"\nend"
|
||||
|
||||
say "===================================================="
|
||||
say " NEW GAME"
|
||||
say "===================================================="
|
||||
rule "everyone approves %action": yes
|
||||
|
||||
rule "with everyone's approval %action":
|
||||
if (everyone approves %action):
|
||||
do %action
|
||||
..else:
|
||||
say "You do not have the will of the people! >:("
|
||||
|
||||
restrict "rule % %" to within "with everyone's approval %"
|
||||
restrict "make % %" to within "with everyone's approval %"
|
||||
restrict "set all * % = %" to within "with everyone's approval %"
|
||||
restrict "% % = %" to within "with everyone's approval %"
|
||||
restrict "restrict % to within %" to within "with everyone's approval %"
|
||||
|
||||
with everyone's approval:
|
||||
rule "propose %action":
|
||||
if ("pending proposal" "is" ?):
|
||||
say "Sorry, an action is already pending."
|
||||
..else:
|
||||
say "Proposing..."
|
||||
"pending proposal" "is" = %action
|
||||
|
||||
rule "unpropose":
|
||||
let "pending" = ("pending proposal" "is" ?)
|
||||
set all * %pending = (nil)
|
||||
"pending proposal" "is" = (nil)
|
||||
|
||||
|
||||
rule "mark %who as approving %action":
|
||||
%who %action = "approved"
|
||||
|
||||
rule "mark %who as rejecting %action":
|
||||
%who %action = "rejected"
|
||||
|
||||
rule ["approve", "vote yes", "vote yea"]:
|
||||
let "pending" = ("pending proposal" "is" ?)
|
||||
mark (you) as approving %pending
|
||||
say "Voted yes."
|
||||
if (everyone approves %pending):
|
||||
with everyone's approval %pending
|
||||
unpropose
|
||||
..else:
|
||||
let "approvers" = (number of (* %pending = "approved"))
|
||||
let "num-players" = (number of (players))
|
||||
printf [%approvers, "/", %num-players, " players have approved"]
|
||||
|
||||
rule ["reject", "vote no", "vote nay", "veto", "disapprove"]:
|
||||
let "pending" = ("pending proposal" "is" ?)
|
||||
mark (you) as rejecting %pending
|
||||
say "Voted no."
|
||||
unpropose
|
||||
|
||||
rule ["players", "everyone", "everybody", "all players"]:
|
||||
* "is a player" = (yes)
|
||||
|
||||
rule "join":
|
||||
(you) "is a player" = (yes)
|
||||
printf ["Welcome to the game, ", you,"!"]
|
||||
|
||||
allow "unpropose" to use "set all * % = %"
|
||||
allow ["join", "mark % as approving %", "mark % as rejecting %", "propose %", "unpropose"] to use "% % = %"
|
||||
restrict "unpropose" to within ["approve", "reject"]
|
||||
restrict "mark % as approving %" to within ["propose %", "approve"]
|
||||
restrict "mark % as rejecting %" to within ["propose %", "reject"]
|
||||
|
||||
rule "everyone approves %action":
|
||||
(number of (players)) == (number of (* %action = "approved"))
|
||||
|
||||
join
|
||||
|
||||
propose:
|
||||
say "fart"
|
||||
approve
|
||||
|
||||
propose:
|
||||
"democracy" "is possible" = (yes)
|
||||
if ("democracy" "is possible" ?):
|
||||
say "DEMOCRACY WORKS!!!"
|
||||
approve
|
||||
|
||||
propose:
|
||||
rule "fart":
|
||||
say "poot"
|
||||
say "fart should have been defined"
|
||||
approve
|
||||
say "doop"
|
||||
fart
|
||||
|
||||
propose:
|
||||
rule "open election %candidates":
|
||||
if ("candidates" "are" ?):
|
||||
say "An election is already in progress."
|
||||
..else:
|
||||
"candidates" "are" = %candidates
|
||||
|
||||
rule "close the election":
|
||||
set all * "votes for" = (nil)
|
||||
"candidates" "are" = (nil)
|
||||
|
||||
rule "vote for %candidate":
|
||||
(you) "votes for" = %candidate
|
||||
let "vote-percent" = ((number of (* "votes for" = %candidate)) / (number of (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 the election
|
||||
|
||||
allow ["open election %", "close the election", "vote for %"] to use ["% % = %"]
|
||||
allow ["close the election"] to use ["set all * % = %"]
|
||||
approve
|
||||
|
||||
propose:
|
||||
rule "as bill %action":
|
||||
if ((you) == "Anonymous"):
|
||||
make "bill" do %action
|
||||
..else:
|
||||
printf ["Who do you think you are, ", you,"?"]
|
||||
allow ["as bill %"] to use ["make % %"]
|
||||
approve
|
||||
as bill: join
|
||||
|
||||
propose:
|
||||
rule "as dave %action":
|
||||
if ((you) == "Anonymous"):
|
||||
make "dave" do %action
|
||||
..else:
|
||||
printf ["Who do you think you are, ", you,"?"]
|
||||
allow ["as dave %"] to use ["make % %"]
|
||||
approve
|
||||
as bill: approve
|
||||
as dave: join
|
||||
|
||||
open election ["tom", "dick", "harry"]
|
||||
vote for "dick"
|
||||
as bill: vote for "dick"
|
||||
|
||||
propose:
|
||||
rule "take a shit": say "shit taken."
|
||||
|
||||
approve
|
||||
as bill: approve
|
||||
as dave: approve
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user