123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 | #!/usr/bin/env nomsu -V8.0.0
### How do I...
### Write a comment? Put ### 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?
use "consolecolor"
use "things"
### How do I print stuff?
say "Hello world!"
### How do I set a variable?
### Variables have "$" prefix:
$foobar = 1
$text = "Hello world"
### 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?
$foobar = ($foobar + 1)
### 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:
$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.
The text will continue until a closing quotation at the end of the text's
indentation level.
")
### How do I put values inside text? (AKA string formatting, string interpolation)
say ("
Text can contain a backslash followed by a variable, table, or parenthesized
expression. This escaped value will be converted to readable text, like so:
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).
Multi-line text has no other escape sequences, so a backslash that isn't followed
by a value, like this: \ or this: \n, is just a backslash. Two backslashes are one
backslash, so \\$foobar won't insert any values.
If you need to split a long line without inserting a newline, start the next line
.... with four periods in place of its indentation, like this. The space after the
.... periods is part of the text, so these lines are joined with single spaces.
You can put a long interpolated value on its own indented lines: \(
1000 + 2000 + 3000 + 4000 + 5000 + 6000 + 7000 + 8000 + 9000
) and the text continues after the closing parenthesis.
")
say "Single-line text can contain escape sequences like \", \\, \000, and \n"
### How do I define a list?
### Lists and dictionaries are both Lua tables, written with curly braces. Values
without a key get numbered automatically, starting at 1:
$my_list = {"first", "second", "third", 4}
### Really long tables can use {..} with 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 plain Lua tables, so they're 1-indexed and this prints "first"
say $my_list.1
### List entries can be modified like this:
$my_list.1 = "ONE!!!"
### Or appended to/removed from:
add "extra item" to $my_list
pop from $my_list
### How do I make a list from another list?
$doubled = (($x * 2) for $x in {1, 2, 3})
$evens = ($x for $x in (1 to 10) if (($x mod 2) == 0))
### 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 the same Lua tables as lists, so they're accessed and modified the same way
say $my_dict.x
say $my_dict."how many bottles"
say $my_dict.653
$my_dict.x = 9999
### Tables can also be built with comprehensions:
$squares = ($x = ($x * $x) for $x in {1, 2, 3})
### Since dicts and lists are plain Lua tables, (==) checks whether two values are the
same table, and (deeply equals) compares their contents:
assume ({1, 2} != {1, 2})
assume ({1, 2} deeply equals {1, 2})
### How do I do conditional branching?
if (1 < 10):
say "1 is indeed < 10"
if (1 > 10):
say "this won't print"
..else:
say "this will print"
### There's no "elseif", so for longer conditionals, a "when" branch is the best option
when:
(3 > 3):
say "this won't print"
(3 > 6) (3 > 5) (3 > 4):
say "this won't print because none of the conditions on the line above are true"
(3 > 2):
say "this will print"
else:
say "this is the default case"
### How do I do a switch statement?
when (1 + 2) is:
0 1:
say "this won't print"
2 3:
say "this will print"
else:
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"
### 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):
say "Even #\$even"
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
### 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'?
do:
$x = 1
--- (my loop) ---
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
### 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
repeat:
$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
} all mean:
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
say "-- she said"
"Howdy pardner" is what she said
### The language only uses {}().,:$\ as special characters (and reserves [, ], and ;
for the future), 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:
$こんにちは = "こんにちは"
($ と言う) 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))
say (1 ++ (2 * 3))
### How do I do grouping?
### Expressions can be grouped by enclosing parentheses:
say (2 + 3)
### 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:
say both (my favorite number) and also "foo"
### 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 (($x == $other.x) and ($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})
$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:
(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
(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)
### Or to transform nomsu code into custom lua code using "$ compiles to $"
(debug only $body) compiles to:
if $DEBUG_ENABLED:
return
Lua ("
-- Debug code:
\($body as lua)
")
..else:
return (Lua "-- (debug code removed for production)")
### Compile rules run while the file is being compiled, before the file itself runs. So
if a compile rule needs a value, set it in an (at compile time:) block:
at compile time: $DEBUG_ENABLED = (yes)
### Constants can be defined as macros
### When they're invoked, they'll need parentheses just like a function call
(TWENTY) parses as 20
### Compile-time actions can be used anywhere after they're defined, including later in
the same file.
if (1 > (TWENTY)) is untrue:
say "Nomsu parsing macros work!"
say "It looks like a keyword, but there's no magic here!"
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:
(best of $items according to $key_fn) means:
{$best, $best_key} = {nil, nil}
for $item in $items:
$key = ($key_fn $item)
if (($best == (nil)) or ($key > $best_key)):
{$best, $best_key} = {$item, $key}
return $best
### Function literals look like: $x -> ($x * $x)
say (best of {2, -3, 4, -8} according to ($x -> ($x * $x)))
### Functions with more than one argument take a list of them, and "->:" takes a block:
$distance =
{$x, $y} ->:
$squared = ($x * $x + $y * $y)
return (sqrt $squared)
say ($distance 3 4)
### (call) does the same thing, and also works for functions that take no arguments:
say (call $distance 3 4)
$roll = (-> (random integer 6))
say (call $roll)
### 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
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:
(best of $items where $item has score $key_expr) parses as
result of:
{$best, $best_key} = {nil, nil}
for $item in $items:
$key = $key_expr
if (($best == (nil)) or ($key > $best_key)):
{$best, $best_key} = {$item, $key}
return $best
say (best of {2, -3, 4, -8} where $x has score ($x * $x))
### The line above expands to:
say
result of:
{$best, $best_key} = {nil, nil}
for $x in {2, -3, 4, -8}:
$key = ($x * $x)
if (($best == (nil)) or ($key > $best_key)):
{$best, $best_key} = {$x, $key}
return $best
|