Language Reference

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Structs)
(Structs)
Line 273: Line 273:
  
 
You can however place anything in the Structs variables (Even other structs) as if it was any other variable.
 
You can however place anything in the Structs variables (Even other structs) as if it was any other variable.
 +
 +
Do to the nature of Structs both "isClass()" and "isStruct()" will register TRUE if used on a Struct variable (This is because a struct is actually a wrapper for a class) however "isStruct()" will register false if used on a class variable since this function.
  
 
=== Macros ===
 
=== Macros ===

Revision as of 14:51, 7 November 2011

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.

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

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

$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"

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.

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])

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).

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.

Heres a brief example:

$cc = hash(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 = hash(One = "cat", Two = "dog", Three = "mouse")
$cc .= hash(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 = hash(One = "cat", Two = "dog", Three = "mouse")
$cc .= hash(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"])

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.

Variables

Classes

Structs

The struct is bascially a simplified wrapper for the Class therefor it is actually a class and will act like one however it is easier to setup and use.

Heres an example struct :

Struct Vector3 ; Define the struct
	; These variables are equal to:    local $x = ""    in a Class
	; Note - Since a struct is a highly simplified Class you must have one $var per line and you cannot put a = etc
	$x
	$y
	$z
EndStruct
 
; Create one instance of the struct Vector3
$var1 = new Vector3()
$var1->$x = 100
$var1->$y = 200
$var1->$z = 300
 
; Create another instance of the struct Vector3
$var2 = new Vector3()
$var2->$x = 64
$var2->$y = 32
$var2->$z = 96
 
; Print the first one
println( "First Vec X: " . $var1->$x )
println( "First Vec Y: " . $var1->$y )
println( "First Vec Z: " . $var1->$z )
 
; Print the second one
println( "Second Vec X: " . $var2->$x )
println( "Second Vec Y: " . $var2->$y )
println( "Second Vec Z: " . $var2->$z )

Note - Since struct is a simplified class you cannot simply type "$var1 = new Vector3(10, 20, 30)" since that will not work if you wish to do something like that consider using a Class instead since the Structs are designed to be minimalistic.

You can however place anything in the Structs variables (Even other structs) as if it was any other variable.

Do to the nature of Structs both "isClass()" and "isStruct()" will register TRUE if used on a Struct variable (This is because a struct is actually a wrapper for a class) however "isStruct()" will register false if used on a class variable since this function.

Macros

Operators

Conditional Statements

Loop Statements

User Defined Functions

Core Language Functions

Comments

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox