diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/sample_code.nom | 85 | ||||
| -rw-r--r-- | examples/sample_game.nom | 104 | ||||
| -rw-r--r-- | examples/tutorial.nom | 42 |
3 files changed, 114 insertions, 117 deletions
diff --git a/examples/sample_code.nom b/examples/sample_code.nom index 561a3c8..f8315a8 100644 --- a/examples/sample_code.nom +++ b/examples/sample_code.nom @@ -7,13 +7,11 @@ require "lib/core.nom" say "foo" -say (4) +say (-4 + (1.5 - .25)) -#.. "rule" is just a function that takes a function call spec and a block of code to run, +#.. "rule" is just a macro that takes a function call spec and a block of code to run, and stores the function definition -rule: fart ..=: say "poot" - -fart +rule [fart] =: say "poot" # multi-line strings: say ".." @@ -23,23 +21,28 @@ say ".." |long string | - | with + | with | rather - | silly + | silly # fake comments and + # Real comments look like this | indentation | and "quotes" |.." (even fakeouts like that) " |(done) + | + |^and trailing spaces | -rule: doublefart ..=: # this farts twice +say ".."|Longstrings have interpolation: \1 + 2\ <- like that + +rule [doublefart] =: # this farts twice say "poot" say "poot" doublefart -rule: subex work ..=: "subexpressions work" +rule [subex work] =: "subexpressions work" say (subex work) @@ -54,9 +57,9 @@ say [..] 1, 2 3 -rule: say both %one and %two ..=: - say %one - say %two +rule [say both %1 and %2] =: + say %1 + say %2 say both [..] 1,2 @@ -64,24 +67,21 @@ say both [..] 3,4 -say both.. - "hello" - and "world" - -rule: three ..=: 3 -say both .. - "a list:" - and [..] - 1,2,(three),(4) +rule [three] =: 3 +say both +.."a list:" +..and [..] + 1,2,three,4 if 1: yes ..else: no -if 1: yes ..else: no +if 1 (:yes) else (:no) say (do: return: 5) -rule: do %one also %two ..=: +# Some variables +rule [do %one also %two] =: do %one do %two @@ -92,9 +92,38 @@ say (do: return: "wow") say (1 + (-(2 * 3))) -say (2 + (..) - 3 * 4 -..) +say (..) + 2 + (..) + 3 * 4 + +when: + when %x == 1: + say "one" + when %x == 2: + say "two" + when %x: + say "nonzero" + else: + say "???" + +when: + * %x == 1: + say "one" + * %x == 2: + say "two" + * %x: + say "nonzero" + *: + say "???" + +when %x ==: + * 1: + say "one" + * 2: + say "two" + *: + say "???" + if %x: say "one" @@ -110,7 +139,7 @@ say ".." | with multiple lines | and an interpolated expression: \2 + 5\ -rule: %n bottles ..=: +rule [%n bottles] =: lua block ".." |do | print("running raw lua code...") @@ -122,7 +151,7 @@ rule: %n bottles ..=: nil 9 bottles -rule: dumsum %nums ..=: +rule [dumsum %nums] =: %sum =: 0 for %n in %nums: %sum +=: %n diff --git a/examples/sample_game.nom b/examples/sample_game.nom index e9ba18f..44b9d35 100644 --- a/examples/sample_game.nom +++ b/examples/sample_game.nom @@ -10,21 +10,18 @@ with secrets: | {by_name=function(self, key) return "User<"..key..">" end, | add=function(self, key) self[key] = self:by_name(key) end} |}) - rule: find user %name ..=: + rule [find user %name] =: lua expr "secrets.users:by_name(vars.name) or compiler:error('Failed to find user: '..tostring(vars.name))" - rule: add user %name ..=: + rule [add user %name] =: lua expr "secrets.users:add(vars.name)" - macro: @ %name_block ..=: + macro [@%name_block] =: %name_str =: lua expr "vars.name_block.value.value.src" ".." |compiler:call("find user %", \repr %name_str\) - 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: @@ -35,29 +32,29 @@ with secrets: | return t |end}) - rule: inventory ..=: + rule [inventory] =: dict (..) [%inv's "key", dict (%entry for %entry in (entries in (%inv's "value")))] - for %inv in (entries in (secret %inventory)) + ..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 ..=: + rule [%person's stock of %item] =: %item =: canonicalize %item ((%person's inventory)->%item) or 0 - rule: %person's stock of %item as str ..=: + 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 ..=: + rule [give %person %count %item] =: %item =: canonicalize %item (%person's inventory)-> %item =: (%person's stock of %item) + %count - rule: give %person %count %item from %donor ..=: + rule [give %person %count %item from %donor] =: %item =: canonicalize %item if ((%donor's stock of %item) < %count): say ".." @@ -66,26 +63,19 @@ with secrets: (%person's inventory)->%item =: (%person's stock of %item) + %count (%donor's inventory)->%item =: (%donor's stock of %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 "(compiler.you or 'Anonymous')" -rule: - make %person %action - make %person do %action -..=: +rule [make %person %action, make %person do %action] =: lua block ".." |do | local old_you = compiler.you @@ -100,10 +90,10 @@ 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 @@ -111,16 +101,18 @@ with secrets: say ".." |Proposal: |\%src\ + say "foo" + - macro block: propose %action ..=: + macro block [propose %action] =: %source =: source code from tree %action ".." |compiler:call("propose source %", \repr %source\) - 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 @@ -137,31 +129,21 @@ with secrets: secret %pending =: nil secret %approvals =: nil - rule: mark %who as rejecting ..=: + 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\! @@ -195,7 +177,7 @@ propose: approve propose: - rule: fart ..=: + rule [fart] =: say "poot" say "fart should have been defined" approve @@ -204,7 +186,7 @@ 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: @@ -213,25 +195,25 @@ propose: secret %action =: %action secret %winner =: nil - rule: close the election ..=: + rule [close the election] =: secret %candidates =: nil secret %votes =: nil secret %action =: nil secret %winner =: nil - rule: votes for %candidate ..=: + 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 @@ -248,16 +230,16 @@ propose: 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: @@ -268,7 +250,7 @@ approve as bill: join propose: - rule: as dave %action ..=: + rule [as dave %action] =: if ((you) == "Anonymous"): make "dave" do %action ..else: @@ -280,15 +262,15 @@ 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 ..=: + rule [elect president from %candidates] =: open election %candidates: say ".."|Hail to the chief: \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 @@ -303,7 +285,7 @@ as dave: declare war propose: - rule: take a shit ..=: say "shit taken." + rule [take a shit] =: say "shit taken." approve as bill: approve diff --git a/examples/tutorial.nom b/examples/tutorial.nom index 424148c..776f900 100644 --- a/examples/tutorial.nom +++ b/examples/tutorial.nom @@ -98,24 +98,18 @@ do: # Function definition: -rule: - say both %first and also %second -..=: +rule [say both %first and also %second] =: # Function arguments are accessed just like variables say %first say %second # The last line of a function is the return value -rule: - add %x and %y -..=: +rule [add %x and %y] =: %result =: %x + %y %result # Functions can use "return" to return a value early -rule: - first fibonacci above %n -..=: +rule [first fibonacci above %n] =: %f1 =: 0 %f2 =: 1 repeat: @@ -128,7 +122,7 @@ rule: say (first fibonacci above 10) # Functions can have aliases, which may or may not have the arguments in different order -rule: +rule [..] I hate %worse-things more than %better-things I think %worse-things are worse than %better-things I like %better-things more than %worse-things @@ -144,9 +138,7 @@ I think "chihuahuas" are worse than "corgis" say both "Hello" and also "again!" # Functions can even have their name at the end: -rule: - %what-she-said is what she said -..=: +rule [%what-she-said is what she said] =: say %what-she-said say "-- she said" @@ -154,9 +146,7 @@ rule: #.. The language only reserves []{}().,:;% as special characters, so functions and variables can have really funky names! -rule: - >> %foo-bar$$$^ --> %@@& _~-^-~_~-^ %1 ! -..=: +rule [>> %foo-bar$$$^ --> %@@& _~-^-~_~-^ %1 !] =: say %foo-bar$$$^ say %@@& say %1 @@ -166,9 +156,7 @@ rule: # Math and logic operations are just treated the same as function calls in the syntax say (2 + 3) # So it's easy to define your own operators -rule: - %a ++ %b -..=: +rule [%a ++ %b] =: 2 * (%a + %b) say (2 ++ 3) @@ -213,7 +201,7 @@ say both ".." ..and also.. "-- Abraham Lincoln" -rule: my favorite number ..=: 21 + 2 +rule [my favorite number] =: 21 + 2 # Subexpressions are wrapped in parentheses: say (my favorite number) @@ -231,9 +219,7 @@ say ".." number ..\, but this time it uses an indented subexpression! -rule: - sing %starting-bottles bottles of beer -..=: +rule [sing %starting-bottles bottles of beer] =: for %n in (%starting-bottles down through 0): say ".." |\%n if (%n > 0) else "No more"\ \"bottle" if (%n == 1) else "bottles"\ of beer on the wall. @@ -260,7 +246,7 @@ any of [0,0,0,0,1,0,0] # Macros: # The "lua block %" and "lua expr %" macros can be used to write raw lua code: -rule: say the time ..=: +rule [say the time] =: lua block ".." |io.write("The OS time is: ") |io.write(tostring(os.time()).."\\n") @@ -269,13 +255,13 @@ say ".."|Math expression result is: \lua expr "(1 + 2*3 + 3*4)^2"\ #.. In the lua environment, "vars" can be used to get local variables/function args, and "compiler" can be used to access the compiler, function defs, and other things -rule: square root of %n ..=: +rule [square root of %n] =: lua expr "math.sqrt(vars.n)" say ".."|The square root of 2 is \square root of 2\ # Macros can be defined as functions that take unprocessed syntax trees and return lua code # "macro block %" is for defining macros that produce blocks of code, not values -macro block: unless %condition %body ..=: ".." +macro block [unless %condition %body] =: ".." |if not (\%condition as lua expr\) then | \(lua expr "vars.body.value.value") as lua block\ |end @@ -285,7 +271,7 @@ unless (1 > 10): say "It looks like a keyword, but there's no magic here!" # and "macro %" is for defining macros that produce an expression -macro: %value as a boolean ..=: ".." +macro [%value as a boolean] =: ".." |(not not (\%value as lua expr\)) -macro: yep ..=: "true" +macro [yep] =: "true" |
