diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-24 14:57:24 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-24 14:57:35 -0700 |
| commit | 2e345e271f27147051b8ce1f2981ba728b14394a (patch) | |
| tree | 5f56bc1886ed887ad87ffb86b7591fcc5bf09368 /examples | |
| parent | ad94ed3653e2b7a9f68855670a32617aa80a637c (diff) | |
Misc changes, fixed up Object lib and tests.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/how_do_i.nom | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/examples/how_do_i.nom b/examples/how_do_i.nom index 3abaf46..13517fc 100644 --- a/examples/how_do_i.nom +++ b/examples/how_do_i.nom @@ -21,6 +21,8 @@ say "Hello world!" %str <- "Hello world" # Expressions that are more than just literal values require parentheses: %x <- (2 + 3) +%one-two <- 12 +say %one-two # How do I modify a variable? %x <- (%x + 1) @@ -28,50 +30,50 @@ say "Hello world!" %x +<- 1 # How do I define a mutli-line string? -%mutli_str <- ".." +%mutli-str <- ".." Start with "..", then put lines below it that are indented one level. The string will continue until the indentation ends. # How do I put values inside a string? -%format_str <- ".." +%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? These are some numbers: \[1+1,2+1,3+1] 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" +%format-str2 <- "Single-line strings can contain escape sequences like \", \\, \n, \065, and \x0A" # How do I define a list? -%my_list <- [1,2,"hello"] +%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 <- [..] +%my-really-long-list <- [..] 1,2,3,4 5,6 7 8,9,10 # How do I use a list? -%my_list <- ["first item", "second item", "third item"] +%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 +say %my-list.1 # List entries can be modified like this: -%my_list.1 <- "ONE!!!" -say (length of %my_list) +%my-list.1 <- "ONE!!!" +say (length of %my-list) # How do I define a dictionary/hash map? -%my_dict <- {x: 99, y: 101} -%my_dict <- {..} +%my-dict <- {x: 99, y: 101} +%my-dict <- {..} x: 101, y: 2 "99 bottles": 99 653: 292 # 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 -%my_dict.x <- 9999 +say %my-dict.x +%my-dict.x <- 9999 # How do I do conditional branching? if: 1 < 10 @@ -173,11 +175,11 @@ say (first fibonacci above 10) # Actions can have aliases, which may or may not have the arguments in different order 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 + 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!" + say "\(%better-things capitalized) rule and \%worse-things drool!" I like "dogs" more than "cats" I think "chihuahuas" are worse than "corgis" @@ -188,16 +190,16 @@ I think "chihuahuas" are worse than "corgis" say both "Hello" and also "again!" # Actions can even start with a parameter -action [%what_she_said is what she said] - say %what_she_said +action [%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 actions can have really funky names! -action [>> %foo_bar $$$^ --> % @& _~-^-~_~-^ %1 !] - say %foo_bar +action [>> %foo-bar $$$^ --> % @& _~-^-~_~-^ %1 !] + say %foo-bar say % say %1 @@ -283,12 +285,12 @@ if (1 > (TWENTY)) on opposite day # How do I use an action as a value? # Well... it's always *possible* to fall back to Lua behavior for something like this: -action [best of %items according to %key_fn] - <- {%best:nil, %best_key:nil} +action [best of %items according to %key-fn] + <- {%best:nil, %best-key:nil} for % in %items - %key <- (=lua "\%key_fn(\%)") - if: (%best is (nil)) or (%key > %best_key) - <- {%best:%, %best_key:%key} + %key <- (=lua "\%key-fn(\%)") + if: (%best is (nil)) or (%key > %best-key) + <- {%best:%, %best-key:%key} return %best immediately @@ -305,13 +307,13 @@ say: best of [2,-3,4,-8] according to (function %: % * %) could use a macro to generate a single block of code that inlines the expression you want to use: immediately - parse [best of %items according to %key_expr] as + parse [best of %items according to %key-expr] as result of - <- {%best:nil, %best_key:nil} + <- {%best:nil, %best-key:nil} for % in %items - %key <- %key_expr - if: (%best is (nil)) or (%key > %best_key) - <- {%best:%, %best_key:%key} + %key <- %key-expr + if: (%best is (nil)) or (%key > %best-key) + <- {%best:%, %best-key:%key} return %best # This results in generated code that is more efficient (no function calls in the inner loop) @@ -322,7 +324,7 @@ say: best of [2,-3,4,-8] according to (% * %) to get a new list with every entry multiplied by 2, but it's *much* more readable to do something like: %nums <- [1,2,3,4,5] -%double_nums <- ((2 * %num) for %num in %nums) +%double-nums <- ((2 * %num) for %num in %nums) # Nomsu comes with built-in list comprehensions, but the flexible macro system makes it incredibly easy to make similar constructs. @@ -335,5 +337,5 @@ immediately %result.(%N - %i + 1) <- %expr return %result -%double_nums <- ((2 * %num) for %num in %nums BACKWARDS!) -say %double_nums +%double-nums <- ((2 * %num) for %num in %nums BACKWARDS!) +say %double-nums |
