shok / modes

A shok session is a voyage along two parallel modes: the command-line and its companion programming language.

Invoking code from the command-line

A { at the start of a command-line swaps you into a block of code.

shok:~ : {
shok:~ >     new x = "Cool!"
shok:~ >     print("This is a code block. " ~ x)
shok:~ > }
This is a code block. Cool!

That first { just flips you into code-mode. It does not create a new scope — variables declared here are global. Even after the closing }, you can get the variables again with another {.

Once you're in code, {}'s do create new scopes for temporary local variables.

shok:~ : {
shok:~ >     new x = "You found x!"
shok:~ >     {
shok:~ >>        new y = "a local var"
shok:~ >>    }
shok:~ >     # y is gone
shok:~ > }
shok:~ : cd /home/mike
shok:~ : {
shok:~ >     # x is still here
shok:~ >     print(x)
shok:~ > }
You found x!

A code block is a list of statements, and has no return value.

When used in the middle of a command-line, {}'s will evaluate a code expression, convert the result to a string, and plop it onto the command-line.

shok:~ : echo one {"two"} three
one two three
shok:~ : echo {4*3+7} is my lucky number
19 is my lucky number
shok:~ : 
shok:~ : {
shok:~ >     new x = "cho"
shok:~ > }
shok:~ : e{x} hello
shok:~ : {
shok:~ >     new y = "echo"
shok:~ > }
shok:~ : {y} hello
ERROR: Found an expression instead of a code-block at the start of a command-line.

Currently, an expression at the start of a command-line is not allowed. Whether this should be valid is debatable; it is a question of safety and ambiguity.

In shok, expressions are not statements, so there is no syntactic ambiguity between the two. Notably, this avoids the classic programming error of mixing = (assignment) vs. == (equality comparison); it is always a syntax error to have one instead of the other.

Invoking command-lines from within code

In code, use : to make a single command-line statement:

shok:~ : {
shok:~ >     : mkdir foo
shok:~ >     : echo "hello, world!"
shok:~ > }
hello, world!

The command-line lasts until either a newline, a semicolon, or the block is closed with a }.

For larger blocks of command-line statements, use {:: these funny braces ::} to make a command-line block:

shok:~ : {
shok:~ >     new pattern = "kitten"
shok:~ >     {::
shok:~ >>        cd ~/stories/
shok:~ >>        grep -n {pattern} cat_story.txt
shok:~ >>        echo "All done"
shok:~ >>    ::}
shok:~ > }
4:Mittens had four kittens.
17:The scared kitten ran away from the mouse!
All done

There is also a statement-keyword, shell, that will invoke an interactive scoped shell when the statement is run. This is useful for debugging.

Last updated: 2013-12-28