aboutsummaryrefslogtreecommitdiff
path: root/game1.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-08-18 17:08:15 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-08-18 17:08:15 -0700
commitb2d49dde55522429f805590c40f96a95bfc67106 (patch)
treede07c02be2f4ecb256f84c9bbfe7f4216cb404f7 /game1.moon
parent73051b34d9ea46710d1e36be4b867a50ccc01eac (diff)
Got rid of old versions.
Diffstat (limited to 'game1.moon')
-rwxr-xr-xgame1.moon416
1 files changed, 282 insertions, 134 deletions
diff --git a/game1.moon b/game1.moon
index 0f40053..11bc8ce 100755
--- a/game1.moon
+++ b/game1.moon
@@ -1,169 +1,317 @@
#!/usr/bin/env moon
Game = require 'nomic'
core_game = require 'core'
-game = Game(core_game)
-
------------------- BASIC TESTS ---------------------
-game\run [=[
-
-say "=========== INITIALIZING GAME ============"
-
-"fart" := {say "poot"}
-fart
-
-"fart twice" := {
- fart
- fart
-}
-
-fart twice
-
-["greet", "say hello"] := { say "Hello!" }
-
-greet
-
-say (return "returned value")
-say 6
-say -6
-say []
-say [1,2,3,4]
-say [[1,2],[3,[4,5]]]
-say (sum [1,2,3,4])
-help "fart"
-help "fart twice"
+game = Game(core_game)
-"fart thrice" := {
- fart
- fart
- fart
+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 #{Game.repr(action)} to #{Game.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 #{Game.repr(action)} to #{Game.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 #{Game.repr(action)} to use #{Game.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! >:("
+ }
}
-help "fart lol"
-help "yes"
-
-"five" := {return 5}
-say (6 times 6)
-"fitz" := {say (return 99)}
-fitz
-"bazwiz $x" := {say (sum $x)}
-bazwiz [10,20,30]
-"foobar $x" := {say (return $x)}
-foobar 55
-
-"$x squared" := {$x times $x}
-"$x plus one" := {$x + 1}
-"$x foo" := {($x * $x) + 1}
-say (5 foo)
-
-say (1 st in [1,2,3,4,5])
-say (2 nd in [1,2,3,4,5])
-say (3 rd in [1,2,3,4,5])
-say (4 th in [1,2,3,4,5])
-
-]=]
-
-game\def [[you]], (_)=> @you
-game\run [[you]]
-game\run [[say (you)]]
-
-game\run [[
- "five" := {return 5}
-]]
-game\run [[say (five)]]
-game\def [[$x squared]], (locals)=> locals.x^2
-game\run [[say ((five) squared)]]
+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)
+ }
-game\def [[remember that $key $relation $value]], (locals)=>
- with locals
- assert .relation, "no relation!!"
- if not @relations[.relation] then @relations[.relation] = {}
- @relations[.relation][.key] = .value
- return nil
+ "mark $who as approving $action" := {
+ # $who $action = "approved"
+ }
-game\run [[
- "remember that $key $relation" := {remember that $key $relation (true)}
-]]
+ "mark $who as rejecting $action" := {
+ # $who $action = "rejected"
+ }
-game\def [[forget about $key $relation]], (locals)=>
- with locals
- if not @relations[.relation] then @relations[.relation] = {}
- @relations[.relation][.key] = nil
- return nil
+ ["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
+ }
+ }
-game\def [[the value of $key $relation]], (locals)=>
- with locals
- return (@relations[.relation] or {})[.key]
+ ["reject", "vote no", "vote nay", "veto", "disapprove"] := {
+ let "pending" = ("pending proposal" "is" ?)
+ mark (you) as rejecting $pending
+ say "Voted no."
+ unpropose
+ }
-game\run [["it is true that $key $relation $value" := {(the value of $key $relation) == $value}]]
-game\run [["it is true that $key $relation" := {(the value of $key $relation) == (true)}]]
+ ["players", "everyone", "everybody", "all players"] := {
+ * "is a player" = (yes)
+ }
-game\run[[
- remember that "socrates" "is mortal"
- say (it is true that "socrates" "is mortal"))
-]]
+ "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
-game\run [[remember that "socrates" "is mortal"]]
-game\run [[say (the value of "socrates" "is mortal")]]
+propose {
+ say "fart"
+}
+approve
-game\def [[all keys where $relation is $value]], (locals)=>
- with locals
- result = {}
- for k,v in pairs(@relations[.relation] or {})
- if v == .value
- table.insert(result, k)
- return result
+"cheat" := {
+ say "CHEATER!!!"
+}
+sudo {
+ say "CHEATER!!!"
+}
-game\run [[if (1 == 1) then {say "Affirmative"} else {say "Negatory"}]]
-game\run [[if (1 == 2) then {say "Affirmative"} else {say "Negatory"}]]
-game\run [[if (1 == 2) then {say "Affirmative"}]]
+propose {
+ # "democracy" "is possible" = (yes)
+ if ("democracy" "is possible" ?) {
+ say "DEMOCRACY WORKS!!!"
+ }
+}
+approve
-game\run [[say (if (1 == 1) then {return "Ternary yes"} else {return "Ternary no"})]]
+propose {
+ "fart" := {
+ say "poot"
+ }
+}
+approve
+fart
-game\run [["$who is a member" := {return (it is true that $who "is a member")}]]
-game\run [["you are a member" := {return ((you) is a member)}]]
-game\run [[say (you are a member)]]
+propose {
+ "open election $candidates" := {
+ if ("candidates" "are" ?) {
+ say "An election is already in progress."
+ } else {
+ # "candidates" "are" = $candidates
+ }
+ }
-game\run [[
- if (you are a member) then {say "youre a member!"} else {say "youre not a member"}
-]]
+ "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"}
-[[
-def: $who is a member
- return it is true that $who "is a member"
+propose {
+ "take a shit" := {say "shit taken."}
+}
+approve
+as bill: {approve}
+as dave: {approve}
-def: you are a member
- return (you) is a member
-def: members
-alias: all members
- return all keys where "is a member" is "yes"
-def: let $someone join
- if (you are a member) then {
- remember that $someone "is a member"
+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
+ }
}
+}
-def: propose $def
- if (you are a member) then {
- remember that $def "is pending"
- vote yes on $def
+sudo {
+ "everyone approves" := {
+ say "Going into this code"
+ no
}
+}
+sudo {
+ say "BROKEN"
+}
-def: make $voter vote yes on $def
-alias: make $voter approve $def
- remember $def "is approved by" $voter
- if ((all members) == (all $def "is approved by" ?)) then {
- forget $def "is approved by" ?
- add def $def
+
+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
-def: vote yes on $def
- make (you) vote yes on $def
+arbitrarily define "ass" := {say "ASS"}
+ass
-]]
+]=]