aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-12-14 20:21:03 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-12-14 20:21:03 -0800
commit4fe63f253f58f87ab986fea38902d95f2a5ea338 (patch)
tree12094f1c69b8ab375eb17507c959c43f9295c6c2 /examples
parent6abec65843f0f37f7fc6032ac5db0fff3db71815 (diff)
Auto-updated to version 5
Diffstat (limited to 'examples')
-rw-r--r--examples/how_do_i.nom223
1 files changed, 111 insertions, 112 deletions
diff --git a/examples/how_do_i.nom b/examples/how_do_i.nom
index 6b83bfc..ef95f20 100644
--- a/examples/how_do_i.nom
+++ b/examples/how_do_i.nom
@@ -1,4 +1,4 @@
-#!/usr/bin/env nomsu -V4.12.12.8
+#!/usr/bin/env nomsu -V5.12.12.8
# How do I...
# Write a comment? Put a # and go till the end of the line
# How do I write a multi-line comment?
@@ -19,26 +19,26 @@ say "Hello world!"
# How do I set a variable?
# Variables have "%" prefix:
-%foobar = 1
-%text = "Hello world"
+$foobar = 1
+$text = "Hello world"
# Expressions that are more than just literal values require parentheses:
-%foobar = (2 + 3)
-%one_two = 12
-say %one_two
+$foobar = (2 + 3)
+$one_two = 12
+say $one_two
# How do I modify a variable?
-%foobar = (%foobar + 1)
+$foobar = ($foobar + 1)
# Or, as a shorthand, you can do this to increment a variable:
-%foobar += 1
+$foobar += 1
# How do I define a multi-line string?
# In Nomsu, the name "text" is used, rather than "string", and multi-line text looks like:
-%multi_text = "\
- ..Start with a quote mark and a backslash and an indented "..", then put indented
- lines below it. The indented lines will not include the indentation, except when
- the lines are indented more than 4 spaces relative to the original quote mark.
+$multi_text = "
+ Start with a quotation mark, then put indented lines below it. The indented
+ lines will not include the indentation, except when the lines are indented
+ more than 4 spaces relative to the original quote mark.
<- E.g. the 2 spaces here will be included as part of the text.
But this line will have no leading spaces.
@@ -46,10 +46,10 @@ say %one_two
indentation level."
# How do I put values inside text? (AKA string formatting, string interpolation)
-say "\
- ..Text can contain a backslash followed by a variable, list, dict, or parenthesized
+say "
+ Text can contain a backslash followed by a variable, list, dict, or parenthesized
expression. This escaped value will be converted to readable text, like so:
- The value of %foobar is \%foobar, isn't that nice?
+ The value of %foobar is \$foobar, isn't that nice?
These are some numbers: \[1, 2, 3]
The sum of 2 and 4 is \(2 + 4).
@@ -59,46 +59,45 @@ say "\
Or, two backlashes will be treated as a single backslash, no matter what follows,
like this: \\%foobar <- won't insert any values
- If you need to split a long line without inserting a newline, you can end a line with backslash and start the next line \
- ..with two periods, like that.
+ If you need to split a long line without inserting a newline, you can end a line with backslash and \
+ ..start the next line with two periods, like that.
Similarly, you can put a long interpolated indented value like: \(..)
- 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
- .. between a backslash and two periods."
-
+ 100 + 200 + 300 + 400 + 500 + 600 + 700 + 800 + 900
+ ..between a backslash and two periods."
say "Single-line text can contain escape sequences like \", \\, \000, and \n"
# How do I define a list?
-%my_list = ["first", "second", "third", 4]
+$my_list = ["first", "second", "third", 4]
# Really long lists can use [..] followed by a bunch of indented values delimited
by commas and/or newlines
-%my_really_long_list = [..]
+$my_really_long_list = [..]
10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 110000
120000, 130000, 140000, 150000, 160000, 170000
# How do I use a list?
# Lists are 1-indexed because they're implemented as Lua tables, so this prints "first"
-say %my_list.1
+say $my_list.1
# List entries can be modified like this:
-%my_list.1 = "ONE!!!"
+$my_list.1 = "ONE!!!"
# Or appended to/removed from:
-%my_list::add "extra item"
-%my_list::pop
+$my_list|add "extra item"
+$my_list|pop
# How do I define a dictionary/hash map?
One-word text keys don't need quotes, otherwise the key is an expression.
If the expression is more complex than a literal, it needs parentheses:
-%my_dict = {x: 101, y: 2, "how many bottles": 99, 653: 292, (5 + 6): 11}
+$my_dict = {x: 101, y: 2, "how many bottles": 99, 653: 292, (5 + 6): 11}
# How do I use a dict?
# Dicts are also implemented as Lua tables, so they're accessed and modified the same way as lists
-say %my_dict.x
-say %my_dict."how many bottles"
-say %my_dict.653
-%my_dict.x = 9999
+say $my_dict.x
+say $my_dict."how many bottles"
+say $my_dict.653
+$my_dict.x = 9999
# How do I do conditional branching?
if (1 < 10):
@@ -135,106 +134,106 @@ if (1 + 2) is:
say "this won't print"
# How do I loop over a list (a foreach loop)?
-%list = [1, 2, 3]
-for %x in %list:
- say "For %x loop #\%x"
+$list = [1, 2, 3]
+for $x in $list:
+ say "For %x loop #\$x"
# How do I loop over a number range?
# This is inclusive, so it will loop over 1,2, and 3
-for %i in 1 to 3:
- say "For %i in 1 to 3 loop #\%i"
+for $i in 1 to 3:
+ say "For %i in 1 to 3 loop #\$i"
# This will print 0,2, and 4
-for %even in 0 to 5 by 2:
- say "Even #\%even"
+for $even in 0 to 5 by 2:
+ say "Even #\$even"
-for %backwards in 3 to 1 by -1:
- say "Backwards #\%backwards"
+for $backwards in 3 to 1 by -1:
+ say "Backwards #\$backwards"
# How do I do a 'while' loop?
-%x = 1
-repeat while (%x <= 3):
- say "repeat while loop #\%x"
- %x += 1
-%x = 1
-repeat until (%x > 3):
- say "repeat until loop #\%x"
- %x += 1
+$x = 1
+repeat while ($x <= 3):
+ say "repeat while loop #\$x"
+ $x += 1
+$x = 1
+repeat until ($x > 3):
+ say "repeat until loop #\$x"
+ $x += 1
# How do I do an infinite loop?
-%x = 1
+$x = 1
repeat:
- say "repeat loop #\%x"
- %x += 1
- if (%x > 3): stop
+ say "repeat loop #\$x"
+ $x += 1
+ if ($x > 3): stop
# How do I do a 'goto'?
do:
- %x = 1
+ $x = 1
--- (my loop) ---
- say "GOTO loop #\%x"
- %x += 1
- if (%x <= 3):
+ say "GOTO loop #\$x"
+ $x += 1
+ if ($x <= 3):
go to (my loop)
say "finished going to"
# How do I define a function/method/procedure?
# In nomsu, they're called "action"s, and they can be declared like this:
-(say both %first and also %second) means:
- say %first
- say %second
+(say both $first and also $second) means:
+ say $first
+ say $second
# Actions can have parts of the action's name spread throughout.
Everything that's not a literal value is treated as part of the action's name
say both "Hello" and also "world!"
# Actions can use "return" to return a value early
-(first fibonacci above %n) means:
- %f1 = 0
- %f2 = 1
+(first fibonacci above $n) means:
+ $f1 = 0
+ $f2 = 1
repeat:
- %tmp = (%f1 + %f2)
- %f1 = %f2
- %f2 = %tmp
- if (%f2 > %n):
- return %f2
+ $tmp = ($f1 + $f2)
+ $f1 = $f2
+ $f2 = $tmp
+ if ($f2 > $n):
+ return $f2
say (first fibonacci above 10)
# Actions can have aliases, which may or may not have the arguments in different order
[..]
- 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
+ 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
..all mean:
- say "\(%better_things::capitalized) rule and \%worse_things drool!"
+ say "\($better_things|capitalized) rule and \$worse_things drool!"
I like "dogs" more than "cats"
I think "chihuahuas" are worse than "corgis"
# Actions can even start with a parameter
-(%what_she_said is what she said) means:
- say %what_she_said
+($what_she_said is what she said) means:
+ say $what_she_said
say "-- she said"
"Howdy pardner" is what she said
# The language only reserves []{}().,:;%#\ as special characters, so actions
can have really funky names!
-(>> %foo_bar $@&' -->< % @&_~-^-~_~-^ %1 !) means:
- say %foo_bar
- say %
- say %1
->> "wow" $@&' -->< "so flexible!" @&_~-^-~_~-^ "even numbers can be variables!" !
+(>> $foo_bar @&' -->< $ @&_~-^-~_~-^ $1 !) means:
+ say $foo_bar
+ say $
+ say $1
+>> "wow" @&' -->< "so flexible!" @&_~-^-~_~-^ "even numbers can be variables!" !
# There's also full unicode support
-%こんにちは = "こんにちは"
-(% と言う) means "\%世界"
-say (%こんにちは と言う)
+$こんにちは = "こんにちは"
+($ と言う) means "\($)世界"
+say ($こんにちは と言う)
# Math and logic operations are just treated the same as actions in the syntax
say (2 + 3)
# So you can define your own operators, although they will need to be parenthesized to
play nicely with other operators
-(%a ++ %b) means (2 * (%a + %b))
+($a ++ $b) means (2 * ($a + $b))
say (1 ++ (2 * 3))
# How do I do grouping?
@@ -245,8 +244,8 @@ say (2 + 3)
say (2 + 3)
# If you need to keep going after an indented region, you can start the next line with ".."
-say both "Very long first argument that needs its own line" and also "\
- ..short second arg"
+say both "Very long first argument that needs its own line" and also \
+.."short second arg"
(my favorite number) means (21 + 2)
# This can be nested:
@@ -260,19 +259,19 @@ say the time
say "Math expression result is: \(=lua "(1 + 2*3 + 3*4)^2 % 5")"
# Variables can be accessed via \%var
-(square root of %n) means (=lua "math.sqrt(\%n)")
+(square root of $n) means (=lua "math.sqrt(\$n)")
say "The square root of 2 is \(square root of 2)"
# Macros can be defined to transform one bit of nomsu code into another using "parse % as %":
-(if %condition is untrue %body) parses as (if (not %condition) %body)
+(if $condition is untrue $body) parses as (if (not $condition) $body)
# Or to transform nomsu code into custom lua code using "compile % to %"
-(debug only %body) compiles to:
- if %DEBUG_ENABLED:
- return (Lua "-- Debug code:\n\(%body as lua)")
+(debug only $body) compiles to:
+ if $DEBUG_ENABLED:
+ return (Lua "-- Debug code:\n\($body as lua)")
..else:
return (Lua "-- (debug code removed for production)")
-%DEBUG_ENABLED = (yes)
+$DEBUG_ENABLED = (yes)
# Constants can be defined as macros
(TWENTY) parses as 20
@@ -294,37 +293,37 @@ debug only:
# How do I use an action as a value?
# Well... it's always *possible* to fall back to Lua behavior for something like this:
-(best of %items according to %key_fn) means:
- %best = (nil)
- %best_key = (nil)
- for %item in %items:
- %key = (%key_fn %item)
- if ((%best is (nil)) or (%key > %best_key)):
- %best = %item
- %best_key = %key
- return %best
+(best of $items according to $key_fn) means:
+ $best = (nil)
+ $best_key = (nil)
+ for $item in $items:
+ $key = ($key_fn $item)
+ if (($best is (nil)) or ($key > $best_key)):
+ $best = $item
+ $best_key = $key
+ return $best
# Function literals look like: %x -> (%x * %x)
-say (best of [2, -3, 4, -8] according to (%x -> (%x * %x)))
+say (best of [2, -3, 4, -8] according to ($x -> ($x * $x)))
# Or, you can use ((foo %)'s meaning) to access the function that gets called by (foo %)
-(%x squared) means (%x * %x)
-say (best of [2, -3, 4, -8] according to ((% squared)'s meaning))
+($x squared) means ($x * $x)
+say (best of [2, -3, 4, -8] according to (($ squared)'s meaning))
# But nomsu was designed with flexible alternatives that are often better than passing functions.
For example, instead of calling a key function on every item, you could instead define a macro
that gives you a value based on an inlined expression:
-(best of %items where %item has score %key_expr) parses as (..)
+(best of $items where $item has score $key_expr) parses as (..)
result of:
- %best = (nil)
- %best_key = (nil)
- for %item in %items:
- %key = %key_expr
- if ((%best is (nil)) or (%key > %best_key)):
- %best = %item
- %best_key = %key
- return %best
+ $best = (nil)
+ $best_key = (nil)
+ for $item in $items:
+ $key = $key_expr
+ if (($best is (nil)) or ($key > $best_key)):
+ $best = $item
+ $best_key = $key
+ return $best
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-say (best of [2, -3, 4, -8] where %x has score (%x * %x))
+say (best of [2, -3, 4, -8] where $x has score ($x * $x))