diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-09-14 02:41:10 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-09-14 02:41:10 -0700 |
| commit | 83463f11c530cb8be311cd418ef7b7f842b21890 (patch) | |
| tree | 6464093bae5d85f0e572268ea913205bb00c19a0 /examples | |
| parent | 0f228d2d25f292b7d841c84cfa8b8f02229c993a (diff) | |
Working string interpolation!
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/sample_code.nom | 11 | ||||
| -rw-r--r-- | examples/sample_game.nom | 18 | ||||
| -rw-r--r-- | examples/tutorial.nom | 47 |
3 files changed, 42 insertions, 34 deletions
diff --git a/examples/sample_code.nom b/examples/sample_code.nom index 63078d7..2917be1 100644 --- a/examples/sample_code.nom +++ b/examples/sample_code.nom @@ -104,12 +104,11 @@ if %x: say "three" -printf [..] - ".." - |this is a longstring - | - .., "with", ".." - | multiple lines +say ".." + |this is a longstring + | + | with multiple lines + | and an interpolated expression: \2 + 5\ rule "%n bottles": lua block [..] diff --git a/examples/sample_game.nom b/examples/sample_game.nom index 8ac7e7a..52137d2 100644 --- a/examples/sample_game.nom +++ b/examples/sample_game.nom @@ -89,7 +89,8 @@ with everyone's approval: ..else: let "approvers" = (number of (* %pending = "approved")) let "num-players" = (number of (players)) - printf [%approvers, "/", %num-players, " players have approved"] + say ".." + |\number of (* %pending = "approved")\/\number of (players)\ players have approved. rule ["reject", "vote no", "vote nay", "veto", "disapprove"]: let "pending" = ("pending proposal" "is" ?) @@ -104,7 +105,8 @@ with everyone's approval: rule "join": (you) "is a player" = (yes) - printf ["Welcome to the game, ", you,"!"] + say ".." + |Welcome to the game, \you\! allow "unpropose" to use "set all * % = %" allow ["join", "mark % as approving %", "mark % as rejecting %", "propose %", "unpropose"] to use "% % = %" @@ -149,9 +151,11 @@ propose: 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."] + say ".." + |Vote cast. \%candidate\ now has \100 * %vote-percent\% of the votes. if (%vote-percent > 0.5): - printf ["The winner of the election is:", %candidate] + say ".." + |The winner of the election is: \%candidate\ close the election allow ["open election %", "close the election", "vote for %"] to use ["% % = %"] @@ -163,7 +167,8 @@ propose: if ((you) == "Anonymous"): make "bill" do %action ..else: - printf ["Who do you think you are, ", you,"?"] + say ".." + |Who do you think you are, \you\? allow ["as bill %"] to use ["make % %"] approve as bill: join @@ -173,7 +178,8 @@ propose: if ((you) == "Anonymous"): make "dave" do %action ..else: - printf ["Who do you think you are, ", you,"?"] + say ".." + |Who do you think you are, \you\? allow ["as dave %"] to use ["make % %"] approve as bill: approve diff --git a/examples/tutorial.nom b/examples/tutorial.nom index 4a9c213..14a487f 100644 --- a/examples/tutorial.nom +++ b/examples/tutorial.nom @@ -13,8 +13,7 @@ run file "core.nom" # Strings: "asdf" -".." - |This is a multi-line string with a #.. fake comment +".."|This is a multi-line string with a #.. fake comment |that starts with ".." and includes each indented line that starts with a "|" |until the indentation ends @@ -101,14 +100,22 @@ say both ".." "-- Abraham Lincoln" rule "my favorite number": return 23 + # Subexpressions are wrapped in parentheses: -# printf takes a list of bits that are converted to strings and concatenated together, and printed -printf ["My favorite number is ", my favorite number] +say (my favorite number) # There's a multi-line indented block form for subexpressions too: -printf [..] - "My favorite number is still ", (..) +say (..) + my favorite + number + +# Block strings can interpolate values by enclosing an expression in a pair of \s +say ".."|My favorite number is \my favorite number\! + +say ".." + |My favorite number is still \(..) my favorite number + ..\, but this time it uses an indented subexpression! #.. There's a few macros in the language for things like conditional branches and logic/math operations, but they can be thought of as basically the same as functions. @@ -146,17 +153,15 @@ if (1 > 10): let "numbers" = [5,6,7] # Looping: -printf ["Looping over: ",%numbers,"!"] +say ".."|Looping over \%numbers\: for "number" in %numbers: say (%number + 100) rule "sing %starting-bottles bottles of beer": for "n" in (%starting-bottles down through 0): - printf [..] - (%n if (%n > 0) else "No more") - (" bottle" if (%n == 1) else " bottles") - " of beer on the wall." - ("" if (%n == 0) else " Take one down, pass it around...") + say ".." + |\%n if (%n > 0) else "No more"\ \"bottle" if (%n == 1) else "bottles"\ of beer on the wall. + |\"" if (%n == 0) else " Take one down, pass it around..."\ sing 9 bottles of beer @@ -182,25 +187,23 @@ any of [0,0,0,0,1,0,0] rule "say the time": lua block ".." |io.write("The OS time is: ") - |io.write(tostring(os.time()).."\n") + |io.write(tostring(os.time()).."\\n") say the time -printf ["Math expression result is: ", lua expr "(1 + 2*3 + 3*4)^2"] +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": return (lua expr "math.sqrt(vars.n)") -printf ["the square root of 2 is ", square root of 2] +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": - concat [..] - # "% as lua expr" and "% as lua block" are two useful helper functions here. - "if not (", %condition as lua expr, ") then" - # Extract the inner part of the code block's body and insert it: - "\n ", (lua expr "vars.body.value.value") as lua block - "\nend" + ".." + |if not (\%condition as lua expr\) then + | \(lua expr "vars.body.value.value") as lua block\ + |end unless (1 > 10): say "Macros work!" @@ -208,6 +211,6 @@ unless (1 > 10): # and "macro %" is for defining macros that produce an expression macro "%value as a boolean": - concat ["(not not (", %value as lua expr, "))"] + ".."|(not not (\%value as lua expr\)) macro "yep": "true" |
