aboutsummaryrefslogtreecommitdiff
path: root/examples/tutorial.nom
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tutorial.nom')
-rw-r--r--examples/tutorial.nom191
1 files changed, 122 insertions, 69 deletions
diff --git a/examples/tutorial.nom b/examples/tutorial.nom
index 7106f94..5f0acb3 100644
--- a/examples/tutorial.nom
+++ b/examples/tutorial.nom
@@ -25,7 +25,7 @@ run file "core.nom"
5
6,7,8
-# Dicts:
+# Dictionaries (AKA hash maps):
dict [["x", 99], ["y", 101]]
dict [..]
["z", 222]
@@ -34,22 +34,105 @@ dict [..]
# Function calls:
say "Hello world!"
+# Variables use the % sign:
+%str =: "Hello again"
+say %str
+
+# There are +=:, -=:, *=:, /=:, ^=:, and=:, or=:, and mod=: operators for updating variables
+%x =: 1
+%x +=: 100
+say %x
+
+# Conditionals look like this:
+if (1 < 10):
+ say "1 is indeed < 10"
+
+if (1 > 10):
+ say "this won't print"
+..else:
+ say "this will print"
+
+if (1 > 3):
+ say "this won't print"
+..else: if (1 < 2):
+ say "this won't print"
+..else:
+ say "this will print"
+
+# Loops look like this:
+for %x in [1,2,3]:
+ say ".."|for loop over list #\%x\
+
+# If you want to iterate over a numeric range, you can use some built in functions like this:
+for %x in (1 through 3):
+ say ".."|for loop over number range #\%x\
+
+# While loops:
+%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
+
+# Infinite loop:
+%x =: 1
+repeat:
+ say ".."|repeat loop #\%x\
+ %x +=: 1
+ if (%x > 3):
+ break
+
+# GOTOs:
+do:
+ %x =: 1
+ -> %again
+ say ".."|go to loop #\%x\
+ %x +=: 1
+ if (%x <= 3):
+ go to %again
+ say "finished going to"
+
+
# Function definition:
-rule "say both %first and also %second":
- # Variables use the "%" sign:
+rule:
+ say both %first and also %second
+..=:
+ # Function arguments are accessed just like variables
say %first
say %second
-rule "get x from %dict":
- #.. Functions can return values explicitly, but if not, the last line in the function
- is the return value.
- return (%dict's "x")
+# The last line of a function is the return value
+rule:
+ add %x and %y
+..=:
+ %result =: %x + %y
+ %result
+
+# Functions can use "return" to return a value early
+rule:
+ first fibonacci above %n
+..=:
+ %f1 =: 0
+ %f2 =: 1
+ repeat:
+ %tmp =: %f1 + %f2
+ %f1 =: %f2
+ %f2 =: %tmp
+ if (%f2 > %n):
+ return: %f2
+
+say (first fibonacci above 10)
# Functions can have aliases, which may or may not have the arguments in different order
-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"
-..:
+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
+..=:
say ".."|\%better-things capitalized\ rule and \%worse-things\ drool!
I like "dogs" more than "cats"
@@ -61,24 +144,33 @@ 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"
+
"Howdy pardner" is what she said
#.. 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
+
>> "wow" --> "so flexible!" _~-^-~_~-^ "even numbers can be variables!" !
-# Though literals can't be used in function names
# 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": 2 * (%a + %b)
+rule:
+ %a ++ %b
+..=:
+ 2 * (%a + %b)
+
say (2 ++ 3)
@@ -121,7 +213,7 @@ say both ".."
..and also..
"-- Abraham Lincoln"
-rule "my favorite number": return 23
+rule: my favorite number ..=: 21 + 2
# Subexpressions are wrapped in parentheses:
say (my favorite number)
@@ -139,48 +231,10 @@ say ".."
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.
- There are no keywords in the language!
-if (1 < 10):
- say "One is less than ten"
-..else:
- say "One is not less than ten"
-
-#.. Breakdown of the above:
- Function call (actually a macro) to "if % % else %"
- First argument is a subexpression that is a function call (also a macro) to "% < %"
- that performs a comparison on its arguments, 1 and 10
- Second argument is a block of code that includes a function call to "say %", the "if" body
- Third argument is a block of code that includes a different function call to "say %", the "else" body
-
-# Line continuations can be used for "elseif"
-if (1 > 10):
- say "First condition"
-..else: if (1 > 5):
- say "Second condition"
-..else:
- say "Last condition"
-
-# ^that's the same as:
-if (1 > 10):
- say "First condition"
-..else:
- if (1 > 5):
- say "Second condition"
- ..else:
- say "Last condition"
-
-# Variables are modified with a macro, "let % = %"
-let "numbers" = [5,6,7]
-
-# Looping:
-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):
+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.
|\"" if (%n == 0) else " Take one down, pass it around..."\
@@ -206,7 +260,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")
@@ -215,24 +269,23 @@ 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)")
+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":
- ".."
- |if not (\%condition as lua expr\) then
- | \(lua expr "vars.body.value.value") as lua block\
- |end
+macro block: unless %condition %body ..=: ".."
+ |if not (\%condition as lua expr\) then
+ | \(lua expr "vars.body.value.value") as lua block\
+ |end
unless (1 > 10):
say "Macros work!"
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":
- ".."|(not not (\%value as lua expr\))
-macro "yep": "true"
+macro: %value as a boolean ..=: ".."
+ |(not not (\%value as lua expr\))
+macro: yep ..=: "true"