1. Getting Started

1.1 Stand-alone Scripts

Sleep scripts run stand-alone from the command line using sleep.jar as the interpreter.

The traditional first script is usually the famous Hello World program. As I am a fan of tradition your first Sleep script will be a Hello World script as well.

To run the following script: type the source code into a text editor, save it as hello.sl, and then run it by typing: java -jar sleep.jar hello.sl

println("Hello World");

Hello World

Congratulations! You have written and ran your first Sleep script.

Command Line Arguments

Scripts run from the command line can receive arguments. Sleep stores arguments in the variable @ARGV. For example the following sleep script copies files. The source file is @ARGV[0] and the destination file is @ARGV[1].

# cp.sl [original file] [new file] $in = openf(@ARGV[0]); $data = readb($in, -1); $out = openf(">" . @ARGV[1]); writeb($out, $data); closef($in); closef($out);

$ java -jar sleep.jar cp.sl hello.sl hello.bak $ cat hello.bak println("Hello World");

The $__SCRIPT__ variable contains the name of the current script.

Command Line Options

Sleep accepts many command line arguments.

$ java -jar sleep.jar --help Sleep 2.1 (20080515) Usage: java [properties] -jar sleep.jar [options] [-|file|expression] properties: -Dsleep.assert=<true|false> -Dsleep.classpath=<path to locate 3rd party jars from> -Dsleep.debug=<debug level> -Dsleep.taint=<true|false> options: -a --ast display the abstract syntax tree of the specified script -c --check check the syntax of the specified file -e --eval evaluate a script as specified on command line -h --help display this help message -p --profile collect and display runtime profile statistics -t --time display total script runtime -v --version display version information -x --expr evaluate an expression as specified on the command line file: specify a '-' to read script from STDIN

Java passes the [properties] directly to Sleep. These properties are available from the &systemProperties function.

The sleep.assert property enables or disables assertions. This manual discusses assertions in 3.4 Assertions.

The sleep.classpath property specifies where Sleep should look for 3rd party jar files loaded with import [path] from: [jar file] and &use. Use a semicolon or colon between entries to specify more than one path. Chapter 7.1 Object Expressions: 3rd-party Jars discusses the import from ability in detail.

The sleep.debug property specifies the debug level to run the script with. This manual sprinkles script debugging topics throughout its pages.

The sleep.taint property enables or disables taint mode. When enabled, taint mode marks all data from external sources as tainted. Some Sleep functions do not accept tainted data. 6.1 Object Expressions: Taint Mode and Objects and 9.3 Sleep Integration: Taint Mode discuss this security feature in detail.

The Sleep console runs when there is no script file.

Can I start Sleep scripts with jrunscript?

Java 1.6 includes a new programming interface to allow interchangeable use of different script engines. Sleep 2.1 supports this interface and yes you can launch Sleep scripts with jrunscript. Make sure sleep.jar is in your classpath for the following to work:

$ jrunscript -l sleep -f hello.sl

@ARGV and $__SCRIPT__ are available to scripts run through jrunscript.

1.2 The Sleep Console

The Sleep console is an environment that runs Sleep code interactively. You will know you are in the Sleep console when you see the following:

$ java -jar sleep.jar >> Welcome to the Sleep scripting language >

Help within the Console

The help command is available for getting help. Simply type help command name to receive information about a command. The help command by itself simply lists the available commands.

Evaluate an Expression

To evaluate an expression using the Sleep console use the x command.

> x 3 + 4 7 > x split(" ", "Hello World") @('Hello', 'World') > x [Math PI] 3.141592653589793

To evaluate a predicate expression (an if condition) use the ? command.

> ? 3 eq "3" true > ? 3 eq 3.0 false > ? (3 == 3) && ($x is $null || 3 == 4) true

Interact Mode

You can type any amount of Sleep code with interactive mode. Launch interactive mode with the interact command. Place a period on a line by itself once the code is ready for evaluation.

> interact >> Welcome to interactive mode. Type your code and then '.' on a line by itself to execute the code. Type Ctrl+D or 'done' on a line by itself to leave interactive mode. println("Hello World"); . Hello World $x = 4 * atan2(1, 1); println($x); . 3.141592653589793 if ($x == [Math PI]) { println("We have an accurate PI value!!"); } . We have an accurate PI value!! done >

To leave interactive mode press Ctrl+D or type done on a line by itself.

Abstract Syntax Trees

The Sleep parser transforms code into an abstract syntax tree. Sleep uses this form to interpret your script. To view an abstract syntax tree for a script use the tree script name command. The tree command by itself shows the abstract syntax tree of the most recent script.

> tree [Decide]: [Condition]: [Predicate]: name->== negated->false [Setup]: [Get Item]: $x [Create Frame] [Object Access]: class java.lang.Math#PI [If true]: [Create Frame] [Parsed Literal] null [Element]: We have an accurate PI value!! [Function Call]: &println [If False]:

Debugging with the Console

Set the script debug level with the debug level command. Once set all script snippets run with the debug level. For example level 8 enables function call tracing.

> debug 8 Default debug level set > load hello.sl hello.sl loaded successfully. Hello World Trace: &println('Hello World') at line eval:1