aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/parser_tests.nom183
-rw-r--r--examples/sample_code.nom30
-rw-r--r--examples/sample_game.nom185
3 files changed, 107 insertions, 291 deletions
diff --git a/examples/parser_tests.nom b/examples/parser_tests.nom
deleted file mode 100644
index 25d7e99..0000000
--- a/examples/parser_tests.nom
+++ /dev/null
@@ -1,183 +0,0 @@
-require "lib/core.nom"
-require "lib/testing.nom"
-
-test: say "foo"
-..yields ".."
- |FunctionCall:
- | Word:
- | "say"
- | String:
- | "foo"
-
-test: say (foo)
-..yields ".."
- |FunctionCall:
- | Word:
- | "say"
- | FunctionCall:
- | Word:
- | "foo"
-
-test:
- rule (fart) =: say "poot"
-..yields ".."
- |Call [rule % = %]:
- | List:
- | Call [fart]:
- | Block:
- | Call [say %]:
- | "poot"
-
-test: say (subexpressions work)
-..yields ".."
- |Call [say %]:
- | Call [subexpressions work]:
-
-test: say ["lists", "work"]
-..yields ".."
- |Call [say %]:
- | List:
- | "lists"
- | "work"
-
-test (say []) yields ".."
- |Call [say %]:
- | List:
-
-test:
- say [..]
- 1, 2
- 3
-..yields ".."
- |Call [say %]:
- | List:
- | 1
- | 2
- | 3
-
-test:
- say both [..]
- 1,2
- ..and [..]
- 3,4
-..yields ".."
- |Call [say both % and %]:
- | List:
- | 1
- | 2
- | List:
- | 3
- | 4
-
-
-test:
- if 1: yes
- ..else: no
-..yields ".."
- |Call [if % % else %]:
- | 1
- | Block:
- | Call [yes]:
- | Block:
- | Call [no]:
-
-test:
- if 1 (yes) else: no
-..yields ".."
- |Call [if % % else %]:
- | 1
- | Block:
- | Call [yes]:
- | Block:
- | Call [no]:
-
-test: say (do (return 5))
-..yields ".."
- |Call [say %]:
- | Call [do %]:
- | Block:
- | Call [return %]:
- | 5
-
-test:
- say:
- fn call
-..yields ".."
- |Call [say %]:
- | Call [fn call]:
-
-test:
- do: say "one liner"
- ..also: say "another one liner"
-..yields ".."
- |Call [do % also %]:
- | Block:
- | Call [say %]:
- | "one liner"
- | Block:
- | Call [say %]:
- | "another one liner"
-
-test:
- say:
- do:
- say "hi"
- return 5
- say "bye"
-..yields ".."
- |Call [say %]:
- | Call [do %]:
- | Block:
- | Call [say %]:
- | "hi"
- | Call [return %]:
- | 5
- | Call [say %]:
- | "bye"
-
-
-test: say (1 + (-(2 * 3)))
-..yields ".."
- |Call [say %]:
- | Call [% + %]:
- | 1
- | Call [- %]:
- | Call [% * %]:
- | 2
- | 3
-
-test:
- if %x:
- x
- ..else:
- if %y:
- y
- ..else:
- z
-..yields ".."
- |Call [if % % else %]:
- | Var["x"]
- | Block:
- | Call [x]:
- | Block:
- | Call [if % % else %]:
- | Var["y"]
- | Block:
- | Call [y]:
- | Block:
- | Call [z]:
-
-
-test:
- don't fuck this up
-..yields ".."
- |Call [don 't fuck this up]:
-
-test:
- %Brian's hat
-..yields ".."
- |Call [% 's hat]:
- | Var["Brian"]
-
-say "All tests passed!"
-
diff --git a/examples/sample_code.nom b/examples/sample_code.nom
index fe73ec7..1e1ccc6 100644
--- a/examples/sample_code.nom
+++ b/examples/sample_code.nom
@@ -34,7 +34,7 @@ say ".."
|^and trailing spaces
|
-say ".."|Longstrings have interpolation: \1 + 2\ <- like that
+say "Strings have interpolation: \(1 + 2) <- like that"
rule [doublefart] =: # this farts twice
say "poot"
@@ -76,9 +76,9 @@ say both
if 1: yes
..else: no
-if 1 (:yes) else (:no)
+if 1 {yes} else {no}
-say (do: return 5)
+say (do {return 5})
# Some variables
rule [do %one also %two] =:
@@ -88,7 +88,7 @@ rule [do %one also %two] =:
do: say "one liner"
..also: say "another one liner"
-say (do: return "wow")
+say (do {return "wow"})
say (1 + (-(2 * 3)))
@@ -97,21 +97,21 @@ say (..)
3 * 4
when:
- ? %x == 1:
+ * (%x == 1):
say "one"
- ? %x == 2:
+ * (%x == 2):
say "two"
- ? %x:
+ * %x:
say "nonzero"
- ?:
+ * else:
say "???"
-when %x:
- == 1:
+when %x == ?:
+ * 1:
say "one"
- == 2:
+ * 2:
say "two"
- ==:
+ * else:
say "???"
@@ -134,7 +134,7 @@ rule [%n bottles] =:
lua block ".."
|do
| print("running raw lua code...")
- | for i=\%n\,1,-1 do
+ | for i=\(%n),1,-1 do
| print(tostring(i).." bottles of beer on the wall. Take one down, pass it around,")
| end
| print("no more bottles of beer on the wall.")
@@ -143,9 +143,9 @@ rule [%n bottles] =:
9 bottles
rule [dumsum %nums] =:
- %sum =: 0
+ %sum = 0
for %n in %nums:
- %sum +=: %n
+ %sum += %n
return %sum
say (dumsum [1,2,3])
diff --git a/examples/sample_game.nom b/examples/sample_game.nom
index 3f9d6bc..95e5c26 100644
--- a/examples/sample_game.nom
+++ b/examples/sample_game.nom
@@ -5,72 +5,73 @@ require "lib/plurals.nom"
# Users:
with secrets:
- secret %users =: lua expr ".."
- |setmetatable({}, {__index=
- | {by_name=function(self, key) return "User<"..key..">" end,
- | add=function(self, key) self[key] = self:by_name(key) end}
- |})
- rule (find user %name; @%name) =:
+ secret %users = (..)
+ lua expr ".."
+ |setmetatable({}, {__index={
+ | by_name=function(self, key) return "User<"..key..">"; end,
+ | add=function(self, key) self[key] = self:by_name(key); end,
+ |}})
+ rule [find user %name, @%name] =:
lua expr "secrets.users:by_name(vars.name) or nomsu:error('Failed to find user: '..tostring(vars.name))"
- rule (add user %name) =:
+ rule [add user %name] =:
lua expr "secrets.users:add(vars.name)"
- rule (everybody; everyone) =:
+ rule [everybody, everyone] =:
(%entry's "key") for %entry in (entries in (secret %users))
- rule (rules that change users) =: ["add user %"]
+ rule [rules that change users] =: ["add user %"]
# Inventory:
with secrets:
- secret %inventory =: lua expr ".."
- |setmetatable({}, {__index=function(self,key)
- | local t = {}
- | self[key] = t
- | return t
- |end})
-
- rule (inventory) =: dict:
+ secret %inventory = (..)
+ lua expr ".."
+ |setmetatable({}, {__index=function(self,key)
+ | local t = setmetatable({}, {__index=function() return 0; end});
+ | self[key] = t;
+ | return t;
+ |end});
+
+ rule [inventory] =: dict:
[%inv's "key", dict (%entry for %entry in (entries in (%inv's "value")))]
..for %inv in (entries in (secret %inventory))
- rule (%person's inventory) =:
+ rule [%person's inventory] =:
dict (%entry for %entry in (entries in ((secret %inventory)->%person)))
- rule (%person's stock of %item) =:
- %item =: canonicalize %item
- ((%person's inventory)->%item) or 0
+ rule [%person's stock of %item] =:
+ %item = (canonicalize %item)
+ (((secret %inventory) -> %person) -> %item)
- rule (%person's stock of %item as str) =:
- %item =: canonicalize %item
- %count =: %person's stock of %item
- ".."
- |\(%count) \((singular %item) if (%count == 1) else (plural %item))
+ rule [%person's stock of %item as str] =:
+ %item = (canonicalize %item)
+ %count = (%person's stock of %item)
+ "\(%count) \((singular %item) if (%count == 1) else (plural %item))"
- rule (give %person %count %item) =:
- %item =: canonicalize %item
- (%person's inventory)-> %item =: (%person's stock of %item) + %count
+ rule [give %person %count %item] =:
+ %item = (canonicalize %item)
+ (((secret %inventory) -> %person) -> %item) += %count
- rule (give %person %count %item from %donor) =:
- %item =: canonicalize %item
+ rule [give %person %count %item from %donor] =:
+ %item = (canonicalize %item)
if ((%donor's stock of %item) < %count):
say ".."
|\(%donor) does not have enough \(%item) to transfer
..else:
- (%person's inventory)->%item =: (%person's stock of %item) + %count
- (%donor's inventory)->%item =: (%donor's stock of %item) - %count
+ (((secret %inventory) -> %person) -> %item) += %count
+ (((secret %inventory) -> %donor) -> %item) -= %count
- rule (%person has %item; %person has a %item; %person has an %item) =:
+ rule [%person has %item, %person has a %item, %person has an %item] =:
(%person's stock of %item) > 0
- rule (rules that change the inventory) =: [..]
+ rule [rules that change the inventory] =: [..]
"give % % %", "give % % % from %"
- rule (rules that view the inventory) =: [..]
+ rule [rules that view the inventory] =: [..]
"inventory", "%'s inventory", "%'s stock of %", "%'s stock of % as str"
-rule (you) =:
+rule [you] =:
lua expr "(nomsu.you or 'Anonymous')"
-rule (make %person %action; make %person do %action) =:
+rule [make %person %action, make %person do %action] =:
lua code ".."
|local old_you = nomsu.you
|nomsu.you = vars.person
@@ -84,31 +85,31 @@ say "===================================================="
# Unanimity for proposals
with secrets:
- rule (pending proposal) =:
+ rule [pending proposal] =:
secret %pending
- rule (propose source %src) =:
+ rule [propose source %src] =:
if (secret %pending):
error "A proposal is already pending."
- secret %pending =: %src
- secret %approvals =: []
+ secret %pending = %src
+ secret %approvals = []
say ".."
|Proposal:
|\(%src)
- parse (propose %action) as:
- propose source:
+ parse [propose %action] as:
+ propose source (..)
source code from tree \%action
- rule (with everyone's approval do %action) =:
+ rule [with everyone's approval do %action] =:
run %action
- rule (mark %who as approving) =:
+ rule [mark %who as approving] =:
if (not (pending proposal)):
say "No action pending"
return
- (secret %approvals)-> %who =: yes
+ (secret %approvals)-> %who = (yes)
# Check for uncounted votes:
for %user in (everybody):
@@ -117,34 +118,34 @@ with secrets:
# No one dissents
with everyone's approval do (pending proposal)
- secret %pending =: nil
- secret %approvals =: nil
+ secret %pending = (nil)
+ secret %approvals = (nil)
- rule (mark %who as rejecting) =:
- secret %pending =: nil
- secret %approvals =: nil
+ rule [mark %who as rejecting] =:
+ secret %pending = (nil)
+ secret %approvals = (nil)
- rule (approve; vote yes; vote yea) =:
+ rule [approve, vote yes, vote yea] =:
mark (you) as approving
- rule (reject; vote no; vote nay; veto; disapprove) =:
+ rule [reject, vote no, vote nay, veto, disapprove] =:
mark (you) as rejecting
restrict "with everyone's approval do %" to within "mark % as approving"
restrict "mark % as approving" to within "approve"
restrict "mark % as rejecting" to within "reject"
-rule (join) =:
+rule [join] =:
add user (you)
say ".."
|Welcome to the game, \(you)!
restrict (rules that change users) to within "join"
-
-restrict: flatten [..]
- ["rule % = %", "make % %", "restrict % to within %"]
- (rules that change the inventory)
+restrict (..)
+ flatten [..]
+ ["rule % = %", "make % %", "restrict % to within %"]
+ (rules that change the inventory)
..to within "with everyone's approval do %"
say "Rule making is now restricted"
@@ -167,7 +168,7 @@ propose:
approve
propose:
- rule (fart) =:
+ rule [fart] =:
say "poot"
say "fart should have been defined"
approve
@@ -176,34 +177,34 @@ fart
propose:
with secrets:
- rule (open election %candidates %action) =:
+ rule [open election %candidates %action] =:
if (secret %candidates):
error "An election is already in progress."
..else:
- secret %candidates =: %candidates
- secret %votes =: []
- secret %action =: %action
- secret %winner =: nil
-
- rule (close the election) =:
- secret %candidates =: nil
- secret %votes =: nil
- secret %action =: nil
- secret %winner =: nil
-
- rule (votes for %candidate) =:
- %votes =: []
+ secret %candidates = %candidates
+ secret %votes = []
+ secret %action = %action
+ secret %winner = (nil)
+
+ rule [close the election] =:
+ secret %candidates = (nil)
+ secret %votes = (nil)
+ secret %action = (nil)
+ secret %winner = (nil)
+
+ rule [votes for %candidate] =:
+ %votes = []
for %entry in (entries in (secret %votes)):
if ((%entry's "value") == %candidate):
add (%entry's "key") to %votes
%votes
- rule (after winning a fair election do %action) =:
+ rule [after winning a fair election do %action] =:
do %action
- rule (the winner) =: secret %winner
+ rule [the winner] =: secret %winner
- rule (vote for %candidate) =:
+ rule [vote for %candidate] =:
for %c in (secret %candidates):
if (%c == %candidate):
go to %candidate-is-legit
@@ -212,58 +213,56 @@ propose:
-> %candidate-is-legit
(secret %votes)->(you) =: %candidate
- %vote-percent =: (number of (votes for %candidate)) / (number of (everyone))
+ %vote-percent = ((number of (votes for %candidate)) / (number of (everyone)))
say ".."
|Vote cast. \(%candidate) now has \(100 * %vote-percent)% of the votes.
if (%vote-percent > 0.5):
- secret %winner =: %candidate
+ secret %winner = %candidate
after winning a fair election do (secret %action)
close the election
- rule (rules that change the election) =: [..]
+ rule [rules that change the election] =: [..]
"open election %", "close the election", "vote for %"
- rule (rules that view the election) =: [..]
+ rule [rules that view the election] =: [..]
"votes for %"
approve
propose:
- rule (_as bill %action) =:
+ rule [as bill %action] =:
if ((you) == "Anonymous"):
make "bill" do %action
..else:
say ".."
|Who do you think you are, \(you)?
- parse (as bill %) as (_as bill \%)
- allow ["_as bill %"] to use ["make % %"]
+ allow ["as bill %"] to use ["make % %"]
approve
as bill: join
propose:
- rule (_as dave %action) =:
+ rule [as dave %action] =:
if ((you) == "Anonymous"):
make "dave" do %action
..else:
say ".."
|Who do you think you are, \(you)?
- parse (as dave %) as (_as dave \%)
- allow ["_as dave %"] to use ["make % %"]
+ allow ["as dave %"] to use ["make % %"]
approve
as bill: approve
as dave: join
propose:
- rule (declare war) =: say "WAR!!!"
+ rule [declare war] =: say "WAR!!!"
with secrets:
- secret %president =: nil
- rule (elect president from %candidates) =:
+ secret %president = (nil)
+ rule [elect president from %candidates] =:
open election %candidates:
say ".."
|Hail to the chief: \(the winner)
- secret %president =: the winner
+ secret %president = (the winner)
- rule (as the president do %action) =: do %action
+ rule [as the president do %action] =: do %action
restrict "declare war" to within "as the president do %"
approve
@@ -278,7 +277,7 @@ as dave:
declare war
propose:
- rule (take a shit) =: say "shit taken."
+ rule [take a shit] =: say "shit taken."
approve
as bill: approve