aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/how_do_i.nom143
1 files changed, 73 insertions, 70 deletions
diff --git a/examples/how_do_i.nom b/examples/how_do_i.nom
index 5802b19..cdd8a07 100644
--- a/examples/how_do_i.nom
+++ b/examples/how_do_i.nom
@@ -1,38 +1,39 @@
-#!/usr/bin/env nomsu -V6.15.13.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?
+#!/usr/bin/env nomsu -V7.0.0
+
+### How do I...
+### Write a comment? Put a # and go till the end of the line
+### How do I write a multi-line comment?
After a comment line, any indented text
is considered part of the comment
(including any deeper-level indented text)
The comment ends when the indentation ends
-# How do I import a libarary?
+### How do I import a libarary?
use "consolecolor"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-# How do I print stuff?
+### How do I print stuff?
say "Hello world!"
-# How do I set a variable?
-# Variables have "$" prefix:
+### How do I set a variable?
+### Variables have "$" prefix:
$foobar = 1
$text = "Hello world"
-# Expressions that are more than just literal values require parentheses:
+### Expressions that are more than just literal values require parentheses:
$foobar = (2 + 3)
$one_two = 12
say $one_two
-# How do I modify a variable?
+### How do I modify a variable?
$foobar = ($foobar + 1)
-# Or, as a shorthand, you can do this to increment a variable:
+### Or, as a shorthand, you can do this to increment a variable:
$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:
+### 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 quotation mark, then put indented lines below it. The indented
lines will not include the indentation, except when the lines are indented
@@ -44,7 +45,7 @@ $multi_text = ("
indentation level.
")
-# How do I put values inside text? (AKA string formatting, string interpolation)
+### 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
expression. This escaped value will be converted to readable text, like so:
@@ -67,40 +68,40 @@ say ("
")
say "Single-line text can contain escape sequences like \", \\, \000, and \n"
-# How do I define a list?
+### How do I define a list?
$my_list = ["first", "second", "third", 4]
-# Really long lists can use [..] followed by a bunch of indented values delimited
+### Really long lists can use [..] followed by a bunch of indented values delimited
by commas and/or newlines
$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"
+### 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
-# List entries can be modified like this:
+### List entries can be modified like this:
$my_list.1 = "ONE!!!"
-# Or appended to/removed from:
+### Or appended to/removed from:
$my_list, add "extra item"
$my_list, pop
-# How do I define a dictionary/hash map?
+### 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, ."my value" = 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
+### 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
-# How do I do conditional branching?
+### How do I do conditional branching?
if (1 < 10):
say "1 is indeed < 10"
@@ -109,7 +110,7 @@ if (1 > 10):
..else:
say "this will print"
-# There's no "elseif", so for longer conditionals, a "when" branch is the best option
+### There's no "elseif", so for longer conditionals, a "when" branch is the best option
when:
(3 > 3):
say "this won't print"
@@ -123,7 +124,7 @@ when:
else:
say "this is the default case"
-# How do I do a switch statement?
+### How do I do a switch statement?
if (1 + 2) is:
0 1:
say "this won't print"
@@ -134,24 +135,24 @@ if (1 + 2) is:
else:
say "this won't print"
-# How do I loop over a list (a foreach loop)?
+### How do I loop over a list (a foreach loop)?
$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:
+### 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"
-# This will print 0,2, and 4
-for $even in 0 to 5 by 2:
+### This will print 0,2, and 4
+for $even in (0 to 5 by 2):
say "Even #\$even"
-for $backwards in 3 to 1 by -1:
+for $backwards in (3 to 1 by -1):
say "Backwards #\$backwards"
-# How do I do a 'while' loop?
+### How do I do a 'while' loop?
$x = 1
repeat while ($x <= 3):
say "repeat while loop #\$x"
@@ -161,14 +162,14 @@ repeat until ($x > 3):
say "repeat until loop #\$x"
$x += 1
-# How do I do an infinite loop?
+### How do I do an infinite loop?
$x = 1
repeat:
say "repeat loop #\$x"
$x += 1
if ($x > 3): stop
-# How do I do a 'goto'?
+### How do I do a 'goto'?
do:
$x = 1
--- (my loop) ---
@@ -178,17 +179,17 @@ do:
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:
+### 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
-# Actions can have parts of the action's name spread throughout.
+### 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
+### Actions can use "return" to return a value early
(first fibonacci above $n) means:
$f1 = 0
$f2 = 1
@@ -200,7 +201,7 @@ say both "Hello" and also "world!"
return $f2
say (first fibonacci above 10)
-# Actions can have aliases, which may or may not have the arguments in different order
+### 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
@@ -210,52 +211,54 @@ say (first fibonacci above 10)
I like "dogs" more than "cats"
I think "chihuahuas" are worse than "corgis"
-# Actions can even start with a parameter
+### Actions can even start with a parameter
($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
+### The language only reserves []{}().,:;$\ as special characters, so actions
and variables can use symbols freely:
(>> $(∐) @&' -->< $ @&_~-^ ⊗⊞√ $1 !) means:
say $(∐)
say $
say $1
->> "wow" @&' -->< "so flexible!" @&_~-^ ⊗⊞√ "even numbers can be variables!" !
-# You can also use unicode in variable and action names:
+>> "wow" @&' -->< "so flexible!" @&_~-^ ⊗⊞√
+.."even numbers can be variables!" !
+
+### You can also use unicode in variable and action names:
$こんにちは = "こんにちは"
($ と言う) means "\($)世界"
say ($こんにちは と言う)
-# Math and logic operations are just treated the same as actions in the syntax
+### 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
+### 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))
say (1 ++ (2 * 3))
-# How do I do grouping?
-# Expressions can be grouped by enclosing parentheses:
+### How do I do grouping?
+### Expressions can be grouped by enclosing parentheses:
say (2 + 3)
-# Or by (..) followed by an indented region
+### Or by (..) followed by an indented region
say (2 + 3)
-# If you need to keep going after an indented region, you can start the next line with ".."
+### If you need to keep going after an indented region, you can start the next line with ".."
say both "Very very very very long first argument that needs its own line"
..and also "short second arg"
(my favorite number) means (21 + 2)
-# This can be nested:
+### This can be nested:
say both (my favorite number) and also "foo"
-# Object-oriented programming:
-# How do I define a class?
+### Object-oriented programming:
+### How do I define a class?
(a Vec) is (a thing) with [$x, $y]:
- ($self, + $other) means (Vec ($x + $other.x) ($y + $other.y))
+ ($self, +$other) means (Vec ($x + $other.x) ($y + $other.y))
($self, length) means (sqrt ($x * $x + $y * $y))
$(a Vec).is_a_vec = (yes)
(Vec $x $y) means (a Vec {.x = $x, .y = $y})
@@ -263,21 +266,21 @@ $v1 = (Vec 1 2)
assume ($v1 + $v1) == (Vec 2 4)
say $v1
-# Macros:
-# The "lua>" and "=lua" macros can be used to write raw lua code:
+### Macros:
+### The "lua>" and "=lua" macros can be used to write raw lua code:
(say the time) means:
lua> "io.write(\"The OS time is: \", os.time(), \"\\n\");"
say the time
say "Math expression result is: \(=lua "(1 + 2*3 + 3*4)^2 % 5")"
-# Variables can be accessed via \$var
+### Variables can be accessed via \$var
(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 $":
+### 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)
-# Or to transform nomsu code into custom lua code using "compile $ to $"
+### Or to transform nomsu code into custom lua code using "compile $ to $"
(debug only $body) compiles to:
if $DEBUG_ENABLED:
return
@@ -289,15 +292,15 @@ say "The square root of 2 is \(square root of 2)"
return (Lua "-- (debug code removed for production)")
$DEBUG_ENABLED = (yes)
-# Constants can be defined as macros
+### Constants can be defined as macros
(TWENTY) parses as 20
-# When they're invoked, they'll need parentheses just like a function call
+### When they're invoked, they'll need parentheses just like a function call
(TWENTY ONE) parses as 21
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-# If you need to use compile-time actions in the same file that they're defined in, you
+### If you need to use compile-time actions in the same file that they're defined in, you
can add a line of tildes (3 or more), and the file will be split into chunks, and each
chunk will run before the next one compiles. (note that each chunk has its own scope)
if (1 > (TWENTY)) is untrue:
@@ -307,8 +310,8 @@ if (1 > (TWENTY)) is untrue:
debug only:
say "Lua compiling macros work!"
-# How do I use an action as a value?
-# Well... it's always *possible* to fall back to Lua behavior for something like this:
+### 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, $best_key] = [nil, nil]
for $item in $items:
@@ -317,14 +320,14 @@ debug only:
[$best, $best_key] = [$item, $key]
return $best
-# Function literals look like: $x -> ($x * $x)
+### Function literals look like: $x -> ($x * $x)
say (best of [2, -3, 4, -8] according to ($x -> ($x * $x)))
-# Or, you can surround an action with $(...) to refer to it:
+### Or, you can surround an action with $(...) to refer to it:
($x squared) means ($x * $x)
say (best of [2, -3, 4, -8] according to $($ squared))
-# However, nomsu was designed with flexible alternatives that are often better
+### However, 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 will inline an expression
to produce faster code:
@@ -341,7 +344,7 @@ say (best of [2, -3, 4, -8] according to $($ squared))
say (best of [2, -3, 4, -8] where $x has score ($x * $x))
-# The line above expands to:
+### The line above expands to:
say
result of:
[$best, $best_key] = [nil, nil]
@@ -349,4 +352,4 @@ say
$key = ($x * $x)
if (($best == (nil)) or ($key > $best_key)):
[$best, $best_key] = [$x, $key]
- return $best
+ return $best \ No newline at end of file