Wiki

Clone wiki

pool-spec / Statements

Statements

Overview

A statement does something. Statements are terminated by a semi-colon and and empty statement is valid. Here are some examples of statements.

	mutable i = 10;                 // This is a statement.
	i = 4;                          // This too.
	;                               // Also a statement.
	; i++; ; sout.writeln("Hello"); // Four statements.

	i =       // Part of a statement.
	    4 + 5 // More of it.
	   ;      // The end of it.

You should have noticed that lines have noting to do with statements.

Blocks.

As well as semi-colon terminated statements we also have compound statements called blocks. Blocks consist of any number of statements inside of braces.

	{ // This is one statement.
		mutable i;   // It does the statements inside of it.
		i = 5;
		i += 10; // And returns the value of the last statement inside of it.
	}

They are very useful for if statements.

	// mutable i:int

	mutable bt3;
	if ( i > 3 )
	{
		stdout.writeln("i is bigger than three!");
		bt3 = true;
	}
	else
	{
		stdout.writeln("i is smaller than three :(");
		bt3 = false;
	}

The value of a block is the value of the last statement executed. All exit paths from a block must provide a value that can be stored inside the location it is being sent to.

	mutable s = {
		mutable h = "1234567890"
		h = sha256(s);
		h = rot13(s);
	} // Now r is the rot13 of a hash.

	mutable i = {
		mutable g = RandomGenerator(time.unix());
		while ( g.next % 1_000_000 != 0 )
			;
		g.cur;
	} // r is now a number that is a multiple of a million.

	// value:int
	mutable i:real = {
		if ( value < 0 )
		{
			sout.writeln("Oops, less than zero"); // Error: returns OutStream
		}
		else if ( value < 100 ) 100 * value; // Returns int, can be cast to real.
		else                    100 / value; // Returns int, can be cast to real.
	}

	mutable v = {
		if    (interger) 5;
		else if (string) "five";
		else if  (float) 5.0;
		else             null;
	} // All will be cast to Object.  Possible compiler warning.

Updated