Language Reference

From Sputnik Wiki
Revision as of 09:01, 7 November 2011 by UberFoX (Talk | contribs)
Jump to: navigation, search

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.

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

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

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

Macros

Operators

Conditional Statements

Loop Statements

User Defined Functions

Core Language Functions

Comments

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox