aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-01-19 18:13:02 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-01-19 18:13:13 -0800
commit376dd652786b39c8b599cef4789ae8763ed92ae5 (patch)
treea6d41c033578344b56f5ddf2adfcb109be153eb7
parent4764842fe66c3ece4f4a96ba72b0b36afae7590c (diff)
Updated and fixed up how_do_i
-rw-r--r--examples/how_do_i.nom174
-rw-r--r--lib/control_flow.nom2
-rw-r--r--lib/metaprogramming.nom2
3 files changed, 88 insertions, 90 deletions
diff --git a/examples/how_do_i.nom b/examples/how_do_i.nom
index 4e1a18d..28b70f9 100644
--- a/examples/how_do_i.nom
+++ b/examples/how_do_i.nom
@@ -8,182 +8,181 @@
indented area.
# How do I import files?
-require "lib/core.nom"
+use "lib/core.nom"
# Declare a variable?
-%x = 1
-%str = "Hello world"
+local %x
+# Otherwise, variables are implicitly global (hah, sorry, I know that sucks)
+
+# Set a variable?
+set %x = 1
+set %str = "Hello world"
# Expressions that are more than just literal values require parentheses:
-%x = (2 + 3)
+set %x = (2 + 3)
# Modify a variable?
-%x = 100
+set %x = 100
%x += 1
# Print stuff?
say "Hello world!"
# Define a mutli-line string?
-%mutli_str = ".."
+set %mutli_str = ".."
Start with "..", then put lines below it
that are indented one level.
The string will continue until the indentation ends.
# Format a string?
-%format_str = ".."
+set %format_str = ".."
Strings can contain a backslash followed by a variable, list, dict, or parenthesized
expression. This escaped value will be converted to a readable string, like so:
The value of %x is \%x, isn't that nice?
The sum of 2 and 4 is \(2 + 4).
If you need to use a plain ol' backslash, you can do \\ <-- that
-%format_str2 = "Single-line strings can contain escape sequences like \", \\, \n, \065, and \x0A"
+set %format_str2 = "Single-line strings can contain escape sequences like \", \\, \n, \065, and \x0A"
# Define a list?
-%my_list = [1,2,"hello"]
+set %my_list = [1,2,"hello"]
#.. Really long lists can use [..] followed by a bunch of indented values delimited
by commas and/or newlines
-%my_really_long_list = [..]
+set %my_really_long_list = [..]
1,2,3,4
5,6
7
8,9,10
# Use a list?
-%my_list = ["first item", "second item", "third item"]
+set %my_list = ["first item", "second item", "third item"]
# Lists are 1-indexed because they're implemented as Lua tables, so this prints "first item"
say (%my_list->1)
# These do the same thing:
say (%my_list's 1)
say (1 in %my_list)
# List entries can be modified like this:
-%my_list -> 1 = "ONE!!!"
+set (%my_list -> 1) = "ONE!!!"
say (size of %my_list)
# Define a dictionary/hash map?
-%my_dict = {x = 99, y = 101}
-%my_dict = {..}
- x = 101, y = 2
- "99 bottles" = 99
- 653 = 292
+set %my_dict = {x: 99, y: 101}
+set %my_dict = {..}
+ x: 101, y: 2
+ "99 bottles": 99
+ 653: 292
# 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")
-%my_dict->"x" = 9999
+set (%my_dict->"x") = 9999
# Do conditional branching?
-if (1 < 10):
+if: 1 < 10
say "1 is indeed < 10"
-if (1 > 10):
+if: 1 > 10
say "this won't print"
-..else:
+..else
say "this will print"
# There's no "elseif", so for longer conditionals, a "when" branch is the best option
-when:
+when
* (3 > 6)
* (3 > 5)
- * (3 > 4):
+ * (3 > 4)
say "this won't print"
- * (3 > 3):
+ * (3 > 3)
say "this won't print"
- * (3 > 2):
+ * (3 > 2)
say "this will print"
- * else:
+ * else
say "this is the default case"
# Do a switch statement?
-when 3 == ?:
+when 3 = ?
* 0
* 1
- * 2:
+ * 2
say "this won't print"
- * 3:
+ * 3
say "this will print"
- * else:
+ * else
say "this won't print"
# Loop over a list (a foreach loop)?
-%list = [1,2,3]
-for %x in %list:
+set %list = [1,2,3]
+for %x in %list
say "For %x loop #\%x"
# There's also a slightly more concise version that automatically populates a loop variable "%"
-for all %list:
+for all %list
say "For all loop #\%"
# Loop over a number range?
# This is inclusive, so it will loop over 1,2, and 3
-for %i from 1 to 3:
+for %i from 1 to 3
say "For %i from 1 to 3 loop #\%i"
-for all 1 to 3:
+for all 1 to 3
say "For all 1 to 3 loop #\%"
# This will print 0,2, and 4
-for %even from 0 to 5 by 2:
+for %even from 0 to 5 by 2
say "Even #\%even"
-for %backwards from 3 to 1 by -1:
+for %backwards from 3 to 1 by -1
say "Backwards #\%backwards"
# While loops:
-%x = 1
-repeat while (%x <= 3):
+set %x = 1
+repeat while: %x <= 3
say "repeat while loop #\%x"
%x += 1
-%x = 1
-repeat until (%x > 3):
+set %x = 1
+repeat until: %x > 3
say "repeat until loop #\%x"
%x += 1
# Infinite loop:
-%x = 1
-repeat:
+set %x = 1
+repeat
say "repeat loop #\%x"
%x += 1
- if (%x > 3):
+ if: %x > 3
stop repeat-loop
# GOTOs:
-do:
- %x = 1
+do
+ set %x = 1
-> %again
say "GOTO loop #\%x"
%x += 1
- if (%x <= 3):
+ if: %x <= 3
go to %again
say "finished going to"
# Function definition:
-rule [say both %first and also %second] =:
+action [say both %first and also %second]
say %first
# Function arguments are accessed just like variables
say %second
-# 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):
+action [first fibonacci above %n]
+ set %f1 = 0
+ set %f2 = 1
+ repeat
+ set %tmp = (%f1 + %f2)
+ set %f1 = %f2
+ set %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 [..]
+action [..]
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"
@@ -195,7 +194,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] =:
+action [%what_she_said is what she said]
say %what_she_said
say "-- she said"
@@ -203,7 +202,7 @@ rule [%what_she_said is what she said] =:
#.. The language only reserves []{}().,:;% as special characters, so functions
can have really funky names!
-rule [>> %foo_bar $$$^ --> % @& _~-^-~_~-^ %1 !] =:
+action [>> %foo_bar $$$^ --> % @& _~-^-~_~-^ %1 !]
say %foo_bar
say %
say %1
@@ -213,31 +212,30 @@ rule [>> %foo_bar $$$^ --> % @& _~-^-~_~-^ %1 !] =:
# 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] =:
+action [%a ++ %b]
2 * (%a + %b)
#.. The following are characters won't "stick" to their neighbors, so the
compiler treats them as solitary single-character tokens: '~`!@$^&*-+=|<>?/
which means you can jam things together:
-(5++2) == ( 5 ++ 2 )
-(5 ++ 2) == (5 + + 2)
+assume: (5++2) is ( 5 ++ 2 )
# Do grouping?
# Expressions can be grouped by enclosing parentheses:
say (2 + 3)
-# Or by a (..) followed by an indented region
-say (..)
+# Or by an indented region
+say
2 + 3
# If you need to keep going after an indented region, you can start the next line with ".."
-say both (..)
+say both
"Very long first argument that needs its own line"
..and also "short second arg"
-rule [my favorite number] =: 21 + 2
+action [my favorite number]: 21 + 2
# This can be nested:
-say both (..)
+say both
my favorite
..number
..and also "foo"
@@ -245,33 +243,33 @@ say both (..)
# Macros:
# The "lua> %" and "=lua %" macros can be used to write raw lua code:
-rule [say the time] =:
+action [say the time]
lua> ".."
nomsu:writeln("The OS time is: "..os.time());
say the time
say "Math expression result is: \(=lua "(1 + 2*3 + 3*4)^2")"
-#.. In the lua environment, "vars" can be used to get local variables/function args, and
- "nomsu" can be used to access the compiler, function defs, and other things
-rule [square root of %n] =:
- =lua "math.sqrt(vars.n)"
+# Variables can be accessed via \%varname
+action [square root of %n]
+ =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 %":
-parse [if %condition is untrue %body] as:
- if (not %condition): %body
+immediately
+ # Macros can be defined to transform one bit of nomsu code into another using "parse % as %":
+ parse [if %condition is untrue %body] as
+ if (not %condition) %body
-# Or to transform nomsu code into custom lua code using "compile % to code %"
-compile [if %condition on opposite day %body] to code: ".."
- if not (\(%condition as lua)) then
- \(%body as lua statements)
- end
+ # Or to transform nomsu code into custom lua code using "compile % to code %"
+ compile [if %condition on opposite day %body] to code ".."
+ if not (\(%condition as lua)) then
+ \(%body as lua statements)
+ end
-if (1 > 10) is untrue:
+if (1 > 10) is untrue
say "Nomsu parsing macros work!"
say "It looks like a keyword, but there's no magic here!"
-if (1 > 10) on opposite day:
+if (1 > 10) on opposite day
say "Lua compiling macros work!"
say "It looks like a keyword, but there's no magic here!"
diff --git a/lib/control_flow.nom b/lib/control_flow.nom
index 269a2a5..b05862c 100644
--- a/lib/control_flow.nom
+++ b/lib/control_flow.nom
@@ -103,7 +103,7 @@ immediately
::stop_repeat::;
end --while-loop label scope
return %code
- parse [repeat %body] as: repeat while (true) %body
+ parse [repeat %body] as: repeat while (yes) %body
parse [repeat until %condition %body] as: repeat while (not %condition) %body
# For loop control flow:
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom
index f07e2d1..5ff1ab3 100644
--- a/lib/metaprogramming.nom
+++ b/lib/metaprogramming.nom
@@ -97,7 +97,7 @@ immediately
end
template = repr(table.concat(template, "\\n"));
else
- template = repr(\%longhand.src);
+ template = repr(nomsu:dedent(\%longhand.src));
end
local junk, arg_names, junk = nomsu:get_stub(\%shorthand.value[1]);
local replacements = {};