Language Reference

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Operator .=)
(Operator ..=)
Line 1,085: Line 1,085:
  
 
Concatenates/joins two strings (Adds text to beginning of variable) assignment. e.g.
 
Concatenates/joins two strings (Adds text to beginning of variable) assignment. e.g.
(Can be used on arrays)
 
  
 
<pre>
 
<pre>
 
$var ..= "Hello";
 
$var ..= "Hello";
 
</pre>
 
</pre>
 +
 +
(Can be used on arrays)
  
 
==== Operator + ====
 
==== Operator + ====

Revision as of 15:43, 13 August 2013

Contents

Language Reference

Datatypes

Brief

In Sputnik there is only one datatype called an SV. A SV can numeric or string data (And more advanced arrays/hashes) and decides how to use the data depending on the situation it is being used in. For example, if you try and multiply two SV variables they will be treated as numbers, if you try and concatenate (join) two variants they will be treated as strings.

Some examples:

$var = 100; // equals the number 100 
 
$var = 100 * 20; // equals the number 2000 
 
$var = 100 + (100 / 2); // equals the number 150
 
$var = 10 . 20; // equals the string "1020" (. is used to join strings) 
 
$var = 10 * "omg"; // equals 0 since if a string is used as a number, it will be converted to a number; If it doesn't contain a valid number, it will be assumed to equal 0.
 
// Example of variable conversions.....
10 / 20 // This action will two Int64 and they will remain Int64s
10 / 20.0 // We have defined one of them as a double so it will convert them both to a double when it does the conversion
// What this means is if you want to make sure your math is indeed using doubles add the . to it such 20.0
// Why? Consider this "$y = 1 % 3/4;" it wil return NaN ie fail... But if we do "$y = 1 % 3.0/4.0;" it will work as expected.

Numbers

Numbers can be standard decimal numbers like 2, 4.566, and -7.

Scientific notation is also supported; therefore, you could write 1.5e3 instead of 1500.

Integers (whole numbers) can also be represented in hexadecimal notation by preceding the integer with 0x as in 0x409 or 0x4fff (when using hex notation only 32-bit/64-bit integers numbers are valid not floats/doubles).

Strings

Sputniks strings are ALL UNICODE you can only print UNICODE specific symbols to the console if you change the front to Consolas or something.

Strings are enclosed in double-quotes like "this". If you want a string to actually contain a double-quote use it twice like:

// "This type of string is parsed for escapes"
// 'This type of string is static and is never parsed'
// qq(This type of string is parsed for escapes)
// qq/This type of string is parsed for escapes/
// qq{This type of string is parsed for escapes}
// q(This type of string is static and is never parsed)
// q/This type of string is static and is never parsed/
// q{This type of string is static and is never parsed}
// @"This type of string is also never parsed"

$var = "here is a \"double-quote\" - ok?"; // the \n is an escape charactor to cause a special symbol to be placed inside a string in this case it will place a double quote " inside a string.

You can mix quote types to make for easier working and to avoid having to double-up your quotes to get what you want. For example if you want to use a lot of double-quotes in your strings then you should use single-quotes for declaring them:

'This "sentence" contains "lots" of "double-quotes" does it not?'

However if you choose to use to define your strings and then you want to place a ' inside the string it must be done like this:

'Hello this is my string'

In this case is used to place a ' inside a string made of chars.

The "" strings can place variables inside them for example:

"Hello the variable is $val ok"

And Arrays:

"Hello the variable is $val[77] ok"

And Hashes:

"Hello the variable is $val[test] ok"

Warning - Strings made using 'string' are STATIC this means you cannot place variables inside the string like you can with a regular "string"

Note - "" string allows escapes such as \n to form newline however strings do not resolve escapes.

/*
Escape Sequence		Represents
\$			Dollar sign
\@			At Sign
\a			Bell (alert)
\b			Backspace
\f			Formfeed
\n			New line
\r			Carriage return
\t			Horizontal tab
\v			Vertical tab
\'			Single quotation mark
\"			Double quotation mark
\\			Backslash
\?			Literal question mark
\xhh			ASCII character in hexadecimal notation (Expects 2 digits)
\xhhhh			UNICODE character in hexadecimal notation (Expects 4 digits)
\C			The following characters become Sputnik code
\c			Ends \C and executes the code found between the \C and \c
\L			Transform all following letters to lowercase
\l			Transform the next letter to lowercase
\I			Transform all following letters to uppercase
\i			Transform the next letter to uppercase
\K			Transform all following letters to the opposite case
\Q			Do not match the following patterns (such as \n \r etc)
\E			Ends \I, \L,, \K or \Q functions
\|			Nothing - This sequence outputs nothing
\{			Outputs {
{			The following characters become Sputnik code until } is reached
 
Example of using the \| escape:
$a = "cat";
println("$a\|Dog"); // Note this allows you to place the $a followed by Dog directly
println("$aDog"); // Where as this would fail
 
Example of using the \C escape:
my $scalar = 6;
say "Code: \C$scalar * 2\c"; # 'Code: 12'
 
Example of using the { escape:
my $scalar = 6;
say "Code: { $scalar * 2 }"; # 'Code: 12'
 
More complex example of using the { escape:
my $Str = "\x48\x65\x6c\x6c\x6f World!";
foreach($Str as $c)
{
	println("Char '$c' | Hex: '0x{Hex(Asc('$c'),2)}' | Dec '{Asc('$c')}'");
}
 
If you don't want to print the return of a \C or { you need to add the ! flag example
my $i = 0;
println("Value $i {\$i++!}") until($i == 10);
// The ! flag coming before the } example !} will cause
// the result value of the operation to not be appended
// to the final string
 
To ignore an escape just place a \ next to it for example:
\\f
*/

Multiline Strings

All string can use multiple lines for example:

println(@"

This is a multiline string\n

");

This type of string does not resolve escapes such as \n so the \n in this string will really be seen as \n literally.

If you wish to place a " inside the @"" string you must place 2 of them like this

println(@"

This is a multiline string and im ""quoted"" ok

");

It also does not need to be on multiple lines example

println(@"Hello world!");

Booleans

Booleans are logical values. Only two Boolean values exist: true and false.

They can be used in variable assignments, together with the Boolean operators and, or and not.

Examples:

$Boolean1 = true;
$Boolean2 = false;

If you use arithmetics together with Boolean values (which is not advisable!), the following rules apply:

A Boolean true will be converted into the numeric value 1

A Boolean false will be converted into the numeric value 0

Example:

$Boolean1 = true;
$Number1 = 100;
$Number2 = $Boolean1 + $Number1;
println( $Number2 ); // This will result in $Number2 to be the numeric value 101
 
$Boolean1 = true;
$String1 = "Test is: ";
$String2 = $String1 . $Boolean1;
println( $String2 ) // This will result in $String2 being the string value "Test is: True"

Arrays

Variables can also be arrays storing multiple variables in a single variable accessible by its [] index.

(Its worth noting a variable can contain both an array and a hash at the same time)

// WARNING - A variable will only act like an array
// if you are using a NUMERIC value in the [] bracket for example
$lol[10] // this is an array....
$lol["10"] // this is an array...
$lol["moo"] // this is not an array now...
 
// If your $variable is NOT numeric it will act like a "" dictionary
// Note - This is only true if teh $variable cannot be converted to
// a numeric.
 
// You can check if it is numeric like so:
if( IsNumeric($value) )
// This will tell you if the variable will be used as an array index
// or a dictionary key

Heres a simple example:

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
 
// Print the details of the array
println("lol :" . $lol);
println("lol[0] :" . $lol[0]);
println("lol[1] :" . $lol[1]);
println("lol[2] :" . $lol[2]);
println("lol[3] :" . $lol[3]);

An example of adding stuff to end and beginning of an array:

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
$lol .= array(5, 6); // Add 2 Elements to end of the array
$lol ..= array(0); // Add 1 Element to start of the array
 
// Print the details of the array
println("lol :" . $lol);
println("lol[0] :" . $lol[0]);
println("lol[1] :" . $lol[1]);
println("lol[2] :" . $lol[2]);
println("lol[3] :" . $lol[3]);
println("lol[4] :" . $lol[4]);
println("lol[5] :" . $lol[5]);
println("lol[6] :" . $lol[6]);

To delete an array you can simply define it as anything including strings but the best way is:

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
$lol = array(); // Delete the array
 
// Print the details of the array
println("lol :" . $lol);
println("lol[0] :" . $lol[0]);
println("lol[1] :" . $lol[1]);

Copying an array is simple too example:

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
 
$test = $lol;
 
// Print the details of the array
println("lol :" . $lol);
println("lol[0] :" . $lol[0]);
println("lol[1] :" . $lol[1]);
 
println("test :" . $test);
println("test[0] :" . $test[0]);
println("test[1] :" . $test[1]);

That was a full COPY of the array changing elements in one array will not effect the other unless your first array contained references but we will get into that later.

Arrays can also be modified directly example:

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
 
$lol[2] = "Hello";
$lol[1] = "hmmm";
$lol[99] = "hi there!";
 
// Print the details of the array
println("lol :" . $lol);
println("lol[0] :" . $lol[0]);
println("lol[1] :" . $lol[1]);
println("lol[2] :" . $lol[2]);
println("lol[98] :" . $lol[98]);
println("lol[99] :" . $lol[99]);

Add items to end of an array

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
 
$lol[] = 5;
$lol[] = 6;
$lol[] = 7;
 
// Print the details of the array
printr($lol);

Add items to beginning of an array

$lol = array(1, 2, 3, 4); // Create a simple array with 4 elements
 
$lol[] = 5;
$lol[] = 6;
$lol[] = 7;
 
$lol[]! = 0;
 
// Print the details of the array
printr($lol);

In the above example you can see simply typing $var[index] = will cause it to create that index (part of the array) and fill it with what you type, The array will be automatically expanded to the size you request all the elements in the gap that it fills in will be empty strings ready for use (As seen in index 98).

Example of Multi-dimensional array

$lol = array(0 => array(3, 4), 1 => array(3 =>array(8, 9))); // Create a simple array with 4 elements
 
$lol[0][0] = "Hello";
 
// Print the details of the array
println( "lol :" . $lol );
println( "lol[0] :" . $lol[0] );
println( "lol[0][0] :" . $lol[0][0] );
println( "lol[0][1] :" . $lol[0][1] );
println( "lol[1][3] :" . $lol[1][3] );
println( "lol[1][3][0] :" . $lol[1][3][0] );
println( "lol[1][3][1] :" . $lol[1][3][1] );

You can also use the qw// to produce simple arrays example

// the qw// accepts only alphanumeric letters separated by spaces
$arr = qw/test omg lol/;
foreach($arr as $lol)
{
	println("First test :" . $lol);
}
 
// Is equal to
$arr = array("test", "omg", "lol");
foreach($arr as $lol)
{
	println("Second test :" . $lol);
}

Comparing arrays

// Compare the full array and all elements requiring a perfect match
if($firstarray == $secondarray)
// Reverse of above
if($firstarray != $secondarray)
// Compare the full array and all elements requiring a even more perfect match
if($firstarray === $secondarray)
// Reverse of above
if($firstarray !== $secondarray)
// Compare the full array and all elements requiring a perfect match
if($firstarray eq $secondarray)
// Reverse of above
if($firstarray neq $secondarray)
// Compare the full array and all elements requiring a perfect match (case insensitive)
if($firstarray eqi $secondarray)
// Reverse of above
if($firstarray neqi $secondarray)
 
// Note if you use < > <= >= <> etc
// the arrays will be converted into their index size
// so a 10 element array becomes the number 10 in such an IF
// only the == etc can be used to compare the whole array

// Print all elements in the array (Similar to Join() function)

my $array = array(1, 2, 3);
println("An $array[<>]");
// Or
println($array[<>]);
// Or
println $array[<>];

Theres a lot more arrays can be used for and theres a lot of functions to use with them but that will be shown in another part of this wiki.

Hashes (Dictionary)

A Hash (Dictionary) is used to store variables under a key name this is useful for creating a kind of key/value system and is more useful then arrays in situations where you need this.

(Its worth noting a variable can contain both an array and a hash at the same time. This is because an array and an hash share the same data structure the arrays indexes are simply just keys in the hash. Sputnik will keep the hash organized and sorted numerically then alphabetically for use in loops and whatever else.)

The key in a hash is case insensitive.

Heres a brief example:

$cc = array("One" => "cat", "Two" => "dog", "Three" => "mouse");
 
println("cc :" . $cc);
println("cc[one] :" . $cc["One"]);
println("cc[two] :" . $cc["Two"]);
println("cc[three] :" . $cc["Three"]);

You can also add more elements to the Hash using the ..= example:

$cc = array("One" => "cat", "Two" => "dog", "Three" => "mouse");
$cc .= array("Four" => "woman", "Five" => "man");
 
println("cc :" . $cc);
println("cc[one] :" . $cc["One"]);
println("cc[two] :" . $cc["Two"]);
println("cc[three] :" . $cc["Three"]);
println("cc[four] :" . $cc["Four"]);
println("cc[five] :" . $cc["Five"]);

A more simple approach to adding new values is to simply modify the value directly example:

$cc = array("One" => "cat", "Two" => "dog", "Three" => "mouse");
$cc .= array("Four" => "woman", "Five" => "man");
 
$cc["Six"] = "Sheep";
$cc["One"] = "Not a cat!";
$cc["Seven"] = "Donkey";
 
println("cc :" . $cc);
println("cc[one] :" . $cc["One"]);
println("cc[two] :" . $cc["Two"]);
println("cc[three] :" . $cc["Three"]);
println("cc[four] :" . $cc["Four"]);
println("cc[five] :" . $cc["Five"]);
println("cc[six] :" . $cc["Six"]);
println("cc[seven] :" . $cc["Seven"]);

Example of Multi-dimensional hash

$lol = array("One" => array(3, 4), "Two" => array(3 =>array(8, 9))); // Create a simple array with 4 elements
 
$lol['One'][0] = "Hello";
 
// Print the details of the array
println( "lol :" . $lol );
println( "lol['One'] :" . $lol['One'] );
println( "lol['One'][0] :" . $lol['One'][0] );
println( "lol['One'][1] :" . $lol['One'][1] );
println( "lol['Two'][3] :" . $lol['Two'][3] );
println( "lol['Two'][3][0] :" . $lol['Two'][3][0] );
println( "lol['Two'][3][1] :" . $lol['Two'][3][1] );
 
// Note you can mix array index and hash strings to create all kinds of MD arrays

There is a lot more you can do with hashes including functions to make using them easier but that will be shown in another part of the wiki.

Enum

Enums are a variable that lets you get a number from it and each one is unique.

A local scope MY enum

my enum
{
	$Cat,
	$Dog,
	$Fox,
	$Cow,
	$Pig
};
println("Cat: " . $Cat); // Prints 0
println("Dog: " . $Dog); // Prints 1
println("Fox: " . $Fox); // Prints 2
println("Cow: " . $Cow); // Prints 3
println("Pig: " . $Pig); // Prints 4

Another local scope but this time with a few base variables

my enum
{
	$Cat,
	$Dog = 20,
	$Fox,
	$Cow = 55,
	$Pig
};
 
println("Cat: " . $Cat); // Prints 0
println("Dog: " . $Dog); // Prints 20
println("Fox: " . $Fox); // Prints 21
println("Cow: " . $Cow); // Prints 55
println("Pig: " . $Pig); // Prints 56

Global scope enum accessible by all

enum Countries
{
	$England,
	$France,
	$Germany,
	$Netherlands,
	$Portugal = 50,
	$Italy,
	$Russia
};
 
println("England: " . Countries->$England); // Prints 0
println("France: " . Countries->$France); // Prints 1
println("Germany: " . Countries->$Germany); // Prints 2
println("Netherlands: " . Countries->$Netherlands); // Prints 3
println("Portugal: " . Countries->$Portugal); // Prints 50
println("Italy: " . Countries->$Italy); // Prints 51
println("Russia: " . Countries->$Russia); // Prints 52

Variables

A variable is a place to store information in a way that is easy to get and change.

Declaring Variables

There are 2 ways to create variables either scoped or unscoped example:

$var = 100;

That will either modify an existing variable called $var and change its value to 100 or it will create the variable on the stack in this case it will create a GLOBAL scope variable.

The second way to create variables is to define its scope example:

my $var = 100;

That will either modify an existing LOCAL variable called $var and change its value to 100 or it will create the variable on the stack in this case it will create a LOCAL scope variable.


Global $var = 100

That will either modify an existing GLOBAL variable called $var and change its value to 100 or it will create the variable on the stack in this case it will create a GLOBAL scope variable.

Scope

A variable's scope is controlled by when and how you declare the variable. In most cases your variables will be Global unless you specifically set them to Local. Global scope and can be read or changed from anywhere in the script.

If you declare a variable inside a function and you made it Local scope it can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.

The same is true for classes.

So inside a function you only have the function/class(and class function) itself as a LOCAL scope to use and outside a function your local scope is the actual script itself.

However if you "really" need a new local scope any any point you can use the {...} statement example:

my $lol = 100;
{
	my $lol = 88;
	println("lol is: " . $lol);
}
println("lol is: " . $lol);

Every local variable defined inside the block exists only in the block.

You can also stack blocks inside each other.

Casting

There are 2 ways to cast a value as something else the first:

println( "The cast is : " . Int(777.42) )

This will cause everything inside the Int() to be converted to an int this uses the function Int32( $variable ) (Alias: Int( $variable )).

The second way is similar to C and works like this:

println( "The cast is : " . (int)777.42 )

The "(int)value" type will only convert what it *touches* so if you want to convert a large expression you must cover with () example:

$a = 11.6
println( "The cast is : " . (int)(777.42 + $a) )

Casting is very useful since there will be times when you really need to be using only INTs or FLOATs and by casting you will make sure that you do.

Its worth noting the actual "(type)value" cast supports casting overloading on Classes where as the function "type(value)" does not.

Bit Flags

Bit flags , or bit fields are a great way of storing several boolean values in a single byte (or set of bytes), and are internally represented as binary. In this tutorial we will work with bitwise operators, so if you need to brush up this is what we're using; the following key binary operators.

These examples should be self-explanatory.

enum Options {
  $Flag1 = 0x01,
  $Flag2 = 0x02,
  $Flag3 = 0x04,
  $Flag4 = 0x08,
  $Flag5 = 0x10,
  $Flag6 = 0x20
};
 
// Make a variable to store the flags
Global $Opt = 0; // No flags are set
$Opt |= Options->$Flag2; // Set Flag2 to TRUE
$Opt |= Options->$Flag3; // Set Flag3 to TRUE
$Opt |= Options->$Flag5; // Set Flag5 to TRUE
TestFlags("Test1");
 
$Opt &= ~Options->$Flag5; // Set Flag5 to FALSE
TestFlags("Test2");
 
$Opt &= ~Options->$Flag3; // Set Flag3 to FALSE
TestFlags("Test3");
 
$Opt |= Options->$Flag5; // Set Flag5 to TRUE
$Opt |= Options->$Flag3; // Set Flag3 to TRUE
TestFlags("Test4");
 
$Opt &= ~(Options->$Flag3 | Options->$Flag5); // Set Flag3 and Flag5 to FALSE
TestFlags("Test4");
 
$Opt |= (Options->$Flag1 | Options->$Flag6); // Set Flag1 and Flag6 to TRUE
TestFlags("Test5");
 
Function TestFlags($str)
{
	println("### $str BELOW");
	println("\$Opt = {Hex('$Opt', 2)}");
	foreach( Enumerate("Options") as $Flag => $ID )
	{
		if($Opt & $ID)
			println("$Flag is TRUE");
		else
			println("$Flag is FALSE");
	}
	println("### $str ABOVE\n");
}

Another example

enum {
    $OPT_A = 0x01,
    $OPT_B = 0x02,
    $OPT_C = 0x04,
    $OPT_H = 0x08
};
 
 
//to store the option flags
my $opt = 0x0;
 
//argument array.
my $argv = qw(a b);
foreach($argv as $c)
{
	switch($c)
	{ 
		case 'a':
		//assign option bits to "opt" bit array
			$opt |= $OPT_A;  break;
		case 'b':
			$opt |= $OPT_B;  break;
		case 'c':
			$opt |= $OPT_C;  break;
		case 'h':
			$opt |= $OPT_H;  break;
		//this will happen if they enter an invalid option:
		default: 
			print("Unknown option $c");
			return 1; //break out of application
	}
}
 
//apply bitwise AND to check for assignedness a few times
if($opt & $OPT_A)
	print("Hello World!\n");
 
if($opt & $OPT_B) {
	my $foo = 2000;
	print("Foo has been initialized.\n");
}
 
//compare if two flags were specifically set
if (($opt & ($OPT_B | $OPT_C)) == ($OPT_B | $OPT_C)) 
	print("Flags B and C were set.\n");
 
if($opt & $OPT_H) {
	//print help, may wish to create exit point to stop program from executing
	print("\tHelp is not implemented yet\n\tAllowable options: [abch]\n");
	return 0;
}
 
//----------------- Some fun extras: ---------------------//
 
//Reset bitflag completely
$opt = 0;
 
//Apply bitwise OR to append multiple flags
$opt = ($OPT_A | $OPT_B | $OPT_C);
 
//Apply bitwise AND+EQUALS to add or remove flags to existing option field
//Then we apply bitwise NOT (a complement) to remove both flags
$opt &= ~($OPT_A | $OPT_B);
 
//Options A and B are now removed
 
//Check if BOTH flags are not set
if (($opt & ($OPT_A | $OPT_B)) == 0)
	print("Flags A and B are not set\n");
 
//check if only one is not set
if (($opt & $OPT_A) == 0)
	printf("Flag A is not set\n");
 
 
//end program
return 0;

Macros

Sputnik has an number of Macros that are special read-only variables. Macros start with the @ character instead of the usual $ so are easy to tell apart. As with normal variables you can use macros in expressions but you cannot assign a value to them.

The pre-defined macros are generally used to provide easy access to information and constants such as @PI etc.

Go here for a complete list.

Classes

Sputnik does support some pbject-oriented programming however its too big for this page alone so its best to go see the classes page

Classes

Operators

Operator x

Repetition. e.g.

$value = "Test" x 10;
printr $value;
 
$value = array("Cat", "Dog") x 10;
printr $value;

Operator xx

Repetition as array. e.g.

$value = "Test" xx 10;
printr $value;
 
$value = array("Cat", "Dog") xx 10;
printr $value;

Operator []

Append to end of array. e.g.

my $myArray = array("One", "Two", "Three");
 
$myArray[] = "Four";
$myArray[] = "Five";
 
printr($myArray);

Operator []!

Append to beginning of array. e.g.

my $myArray = array("One", "Two", "Three");
 
$myArray[]! = "Zero";
$myArray[] = "Four";
 
printr($myArray);

Operator [<>]

Print array array. e.g.

my $array = array(1, 2, 3);
println("An $array[<>]");
// Or
println($array[<>]);
// Or
println $array[<>];

Operator is

Type checking. e.g.

// Used to check if a variable is a certain type
$a = (int32)1;
 
if($a is Int32)
{
	println(@"$a is an Int32");
}
else
{
	println(@"$a is NOT an Int32");
}
 
// Can be used for classes and other objects too
Class Testy
{
};
 
$b = new Testy();
 
if($b is Testy)
{
	println(@"$a is an instance of class Testy");
}
else
{
	println(@"$a is NOT an instance of class Testy");
}

Operator ~~

Type checking. e.g.

// Used to check if a variable is a certain type
$a = (int32)1;
 
if($a ~~ Int32)
{
	println(@"$a is an Int32");
}
else
{
	println(@"$a is NOT an Int32");
}
 
// Can be used for classes and other objects too
Class Testy
{
};
 
$b = new Testy();
 
if($b ~~ Testy)
{
	println(@"$a is an instance of class Testy");
}
else
{
	println(@"$a is NOT an instance of class Testy");
}

Operator isnot

Type checking. e.g.

// Used to check if a variable is not a certain type
$a = (int32)1;
 
if($a isnot Int32)
{
	println(@"$a is NOT an Int32");
}
else
{
	println(@"$a is an Int32");
}

Operator !~

Type checking. e.g.

// Used to check if a variable is not a certain type
$a = (int32)1;
if($a !~ Int32)
{
	println(@"$a is NOT an Int32");
}
else
{
	println(@"$a is an Int32");
}

Operator =

Assignment. e.g.

$var = 5;     (assigns the number 5 to $var)

Operator ++

Increase assignment. e.g.

$var++;     (adds 1 to $var) // Returns x, then increments x by one
++$var;     (adds 1 to $var) // Increments x by one, then returns x

Operator --

Decrease assignment. e.g.

$var--;     (subs 1 from $var) // Returns x, then decrements x by one
--$var;     (subs 1 from $var) // Decrements x by one, then returns x

Operator +=

Addition assignment. e.g.

$var += 1;     (adds 1 to $var)

Operator -=

Subtraction assignment.

$var -= 1;     (subs 1 to $var)

Operator /=

Division assignment. e.g.

$var /= 2;     (divive $var by 2)

Operator *=

Multiplication assignment. e.g.

$var /= 2;     (multiply $var by 2)

Operator **=

Raises a number to the power assignment. e.g.

$var **= 2;     (raise $var by 2)

Operator |=

BitwiseOR assignment. e.g.

$var |= 2;

Operator ^=

BitwiseExclusiveOR assignment. e.g.

$var |= 2;

Operator %=

BitwiseAND assignment. e.g.

$var |= 2;

Operator >>=

BitwiseSHIFT RIGHT assignment. e.g.

$var >>= 2;

Operator <<=

BitwiseSHIFT LEFT assignment. e.g.

$var <<= 2;


Operator <<<=

BitwiseUnsignedSHIFT LEFT assignment. e.g.

$var <<= 2;

Operator >>>=

BitwiseUnsignedSHIFT RIGHT assignment. e.g.

$var >>= 2;

Operator .=

Concatenates/joins two strings (Adds text to end of variable) assignment. e.g.

$var .= "Hello";

(Can be used on arrays)

Operator ..=

Concatenates/joins two strings (Adds text to beginning of variable) assignment. e.g.

$var ..= "Hello";

(Can be used on arrays)

Operator +

Adds two numbers. e.g.

10 + 20;    (equals 30)

Operator -

Subtracts two numbers. e.g.

20 - 10;    (equals 10)

Operator *

Multiplies two numbers. e.g.

20 * 10;    (equals 200)

Operator /

Divides two numbers. e.g.

20 / 10;    (equals 2)

Operator **

Raises a number to the power. e.g.

2 ** 4;    (equals 16) 

Operator <<

BitwiseSHIFT Left. e.g.

14 << 2;    (equals 56 because 1110b left-shifted twice is 111000b == 56) 

Operator >>

BitwiseSHIFT Right. e.g.

14 >> 2;    (equals 3 because 1110b right-shifted twice is 11b == 3) 

Operator >>>

BitwiseUnsignedSHIFT Right. e.g.

-1 >>> 2;


Operator <<<

BitwiseUnsignedSHIFT Left. e.g.

14 <<< 2;

Operator ~

BitwiseNOT. e.g.

~ 5;
    ; Result is -6 because for 32-bit numbers
    ; 5 == 00000000000000000000000000000101 binary
    ; -6 == 11111111111111111111111111111010 binary
    ; and the first bit is signed

Operator .

Concatenates/joins two strings. e.g.

"one" . 10;    (equals "one10")

Operator ==

Tests if two values are equal (case sensitive if used with strings)

Operator ===

Tests if two values are equal and the same type (case sensitive if used with strings)

//define variables..  
$str = '9';  
$int = 9;  
 
//Returns true since both variable contains the same value..  
$res = ($str==$int); 
println("Str '9' == Int 9; " . ($res ? "True" : "False"));
 
//Returns false since the two variables are not of the same type..  
$res = ($str===$int);
println("Str '9' === Int 9; " . ($res ? "True" : "False"));

Operator !=

Tests if two values are not equal (case sensitive if used with strings)

Operator !==

Tests if two values are not equal and not the same type (case sensitive if used with strings)

Operator <

Tests if the first value is less than the second.

Operator <=

Tests if the first value is less than or equal to the second.

Operator >

Tests if the first value is greater than the second.

Operator >=

Tests if the first value is greater than or equal to the second.

Operator <>

Test if first value is lower or higher than the second.

Operator <=>

Test if second value value is lower , equal or higher respectively than the first value returning as either: -1 0 1

Operator eq

Tests if two objects (treated as strings) are equal (case sensitive)

Operator eqi

Tests if two objects (treated as strings) are equal (case insensitive)

Operator neq

Tests if two objects (treated as strings) are not equal (case sensitive)

Operator neqi

Tests if two objects (treated as strings) are not equal (case insensitive)

Operator cmp

Tests if two objects (treated as strings) are lower(-1), equal (0) or higher (1) (case sensitive)

Operator cmpi

Tests if two objects (treated as strings) are lower(-1), equal (0) or higher (1) (case insensitive)

Operator lt

Tests if the first value is less than the second (both treated as strings).

Operator le

Tests if the first value is less than or equal to the second (both treated as strings).

Operator gt

Tests if the first value is greater than the second (both treated as strings).

Operator ge

Tests if the first value is greater than or equal to the second (both treated as strings).

Operator lti

Tests if the first value is less than the second (both treated as strings, case insensitive).

Operator lei

Tests if the first value is less than or equal to the second (both treated as strings, case insensitive).

Operator gti

Tests if the first value is greater than the second (both treated as strings, case insensitive).

Operator gei

Tests if the first value is greater than or equal to the second (both treated as strings, case insensitive).

Operator lg

Test if first value is lower or higher than the second (both treated as strings).

Operator lgi

Test if first value is lower or higher than the second (both treated as strings, case insensitive).

Operator &&

Logical AND operation. e.g.

If($var == 5 && $var2 > 6)    (True if $var equals 5 and $var2 is greater than 6)

Operator ||

Logical OR operation. e.g.

If($var == 5 || $var2 > 6)    (True if $var equals 5 or $var2 is greater than 6)

Operator !

Logical NOT operation. e.g.

If(!$var == 5)

Operator ??

Boolean expression TRUE or switch. eg.

$a = false;
println( $a ?? "The variable is not true" );

Operator !!

Boolean expression FALSE or switch. eg.

$a = true;
println( $a !! "The variable is true" );

Operator ? :

Boolean expression TRUE or FALSE switch. eg.

Println ( 1 == 2 ? "True" : "False" );


// Heres an example of why it looks better
if( $a == 100 )
{
	println ( "True" );
}
else
{
	println ( "False" );
}

// Is best written like so
println ( $a == 100 ? "True" : "False" );

Operator !? :

Booleon expression TRUE or FALSE switch. eg.

Println ( 1 == 2 !? "False" : "True" );


// Heres an example of why it looks better
unless( $a == 100 )
{
	println ( "False" );
}
else
{
	println ( "True" );
}

// Is best written like so
println ( $a == 100 !? "False" : "True" );


Operator <stm> if( <expression> )

Execute code if expression TRUE. eg.

$test = 200;
println( "Hello World" ) if( $test == 100 );
println( "Goodbye World" ) if( $test == 200 );

Operator <stm> if( <expression> ) else <stm>

Execute code if expression TRUE else execute other code. eg.

$test = 200;
println( "Hello World" ) if( $test == 100 ) else println( "Goodbye World" );

Conditional Statements

Loop Statements

Exception Handling

User Defined & Core Language Functions

There is many functions built into language for easy use for a list go here

There are also functions created using Sputnik that you can include in your projects and use.

To create your own funtions see the Function page.

Character Sets

There are many macros that contain character sets go here for a complete list.

This takes numeric values same as AscW() and returns a string or array

$a = c65..c70;
print($a); // Prints ABCDEF
 
$b = array(c65..c70);
foreach($b as $c)
{
	print("$c,"); // Prints A,B,C,D,E,F,
}


This takes 2 chars returns a string or array

$a = 'A'..'F';
print($a); // Prints ABCDEF
 
$a = 'A'..'F' . 'a'..'f' . '0'..'9';
print($a); // Prints ABCDEFabcdef0123456789
 
$a = '0'..'9';
print($a); // Prints 0123456789
 
$a = 'z'..'a';
print($a); // Prints zyxwvutsrqponmlkjihgfedcba
 
$b = array('A'..'F');
foreach($b as $c)
{
	print("$c,"); // Prints A,B,C,D,E,F,
}
 
$b = array('A'..'F','a'..'f','0'..'9');
foreach($b as $c)
{
	print("$c,"); // Prints A,B,C,D,E,F,a,b,c,d,e,f,0,1,2,3,4,5,6,7,8,9,
}

Takes decimals and returns string

$a = 0..20;
print($a); // Prints 01234567891011121314151617181920
 
$b = array(0..20);
foreach($b as $c)
{
	print("$c,"); // Prints 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
}

Comments

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox