Language Reference

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Structs)
(Variables)
Line 232: Line 232:
  
 
=== Variables ===
 
=== Variables ===
 +
 +
==== Casting ====
 +
 +
There are 2 ways to cast a value as something else the first:
 +
 +
<syntaxhighlight lang="sputnik">
 +
println( "The cast is : " . Int(777.42) )
 +
</syntaxhighlight>
 +
 +
This will cause everything inside the Int() to be converted to an int this uses the function [[Core Function Int32|Int32( $variable )]] (Alias: Int( $variable )).
 +
 +
The second way is similar to C and works like this:
 +
 +
<syntaxhighlight lang="sputnik">
 +
println( "The cast is : " . (int)777.42 )
 +
</syntaxhighlight>
 +
 +
The "(int)value" type will only convert what it *touches* so if you want to convert a large expression you must cover with () example:
 +
 +
<syntaxhighlight lang="sputnik">
 +
$a = 11.6
 +
println( "The cast is : " . (int)(777.42 + $a) )
 +
</syntaxhighlight>
 +
 +
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.
 +
 +
The 2 alternative methods to casting provided purely for choice allowing you to decide which looks best a function() or a c style cast?.
 +
 
=== Classes ===
 
=== Classes ===
  

Revision as of 03:20, 9 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

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.

The 2 alternative methods to casting provided purely for choice allowing you to decide which looks best a function() or a c style cast?.

Classes

A Class is an advanced object you can create to do tasks it is good because you can create reuseable code much easier and allow your program to be more modularized.

A Class in Sputnik isnt meant to be advanced as its C++ counterpart however it meant to be enough to provide whats needed for scripting.

A Class can do the following :

Heres a brief example of a class that demonstrates a few important parts and should get your started

; Define Vec3 Class
Class vec3
	; Define our local variables and their starting values
	local $x = 0
	local $y = 0
	local $z = 0
 
	; Our constructor will take 0 or 3 params and default will be 0
	; Anything you want to do when a vec3 is created goes here this
	; usually is for setting up initial variables
	local Func vec3($x1 = 0, $y1 = 0, $z1 = 0)
		$this->$x = $x1
		$this->$y = $y1
		$this->$z = $z1
	EndFunc
 
	; Our deconstructor anything you want to do when a vec3 is destroyed goes here
	local Func _vec3()
		println("destroying..." )
	EndFunc
 
	; A function that will print the vec3 contents to the console when we need to
	global Func print()
		println("VECTOR3 = (X: $x, Y: $y Z: $z)" )
	EndFunc
 
	; Overload the += operator so it comes to our class
	Operator += ( $vec )
		Switch
			Case isNumber($vec)
				$this->$x += $vec
				$this->$y += $vec
				$this->$z += $vec
				break
			Case isClass($vec, "vec3")
				$this->$x += $vec->$x
				$this->$y += $vec->$y
				$this->$z += $vec->$z
				break
		EndSwitch
	EndOperator
 
	; Overload the -= operator so it comes to our class
	Operator -= ( $vec )
		Switch
			Case isNumber($vec)
				$this->$x -= $vec
				$this->$y -= $vec
				$this->$z -= $vec
				break
			Case isClass($vec, "vec3")
				$this->$x -= $vec->$x
				$this->$y -= $vec->$y
				$this->$z -= $vec->$z
				break
		EndSwitch
	EndOperator
 
	; Overload the *= operator so it comes to our class
	Operator *= ( $vec )
		Switch
			Case isNumber($vec)
				$this->$x *= $vec
				$this->$y *= $vec
				$this->$z *= $vec
				break
			Case isClass($vec, "vec3")
				$this->$x *= $vec->$x
				$this->$y *= $vec->$y
				$this->$z *= $vec->$z
				break
		EndSwitch
	EndOperator
 
	; Overload the /= operator so it comes to our class
	Operator /= ( $vec )
		Switch
			Case isNumber($vec)
				$this->$x /= $vec
				$this->$y /= $vec
				$this->$z /= $vec
				break
			Case isClass($vec, "vec3")
				$this->$x /= $vec->$x
				$this->$y /= $vec->$y
				$this->$z /= $vec->$z
				break
		EndSwitch
	EndOperator
 
	; Overload the == operator
	Operator == ( $vec )
		If isClass($vec, "vec3") Then
			If $this->$x == $vec->$x && $this->$y == $vec->$y && $this->$z == $vec->$z Then
				Return(1)
			EndIf
		EndIf
		Return(0)
	EndOperator
 
	; Fortunately we dont need to make a large function like the == operator instead we will simply call it and get its results
	Operator != ( $vec )
		If $this == $vec Then
			return(0)
		Else
			return(1)
		EndIf
	EndOperator
EndClass
 
; Create the vectors
$v1 = new vec3(0, 0, 0)
$v2 = new vec3(10, 20, 30)
 
$anumber = 100 ; Create a number for testing
 
$v1 += $anumber ; Add the number to the $v1 vec3 using its += operator defined in the class
$v1 += $v2 ; Add $v2 vec's values to $v1 vec's values using the += operator defined in the class
 
; print the conents of $v1 vec3
$v1 => print()
 
; Now lets directly edit the values
$v1->$x = 22
$v1->$y = 33
$v1->$z = 44
 
; Print the values using regular prints
println("Values are: " )
println("X: " . $v1->$x )
println("Y: " . $v1->$y )
println("Z: " . $v1->$z )
 
; Begin a test of the == and != overloaded operators
println("\nEQUAL Test now....\n")
 
; Set the vectors to equal values
$v1->$x = 10
$v1->$y = 10
$v1->$z = 10
 
$v2->$x = 10
$v2->$y = 10
$v2->$z = 10
 
$v1=>print()
$v2=>print()
If $v1 == $v2 Then ; Set if the vectors have the same X Y Z variables using our overloaded == operator
	println("The vectors are EQUAL")
Else
	println("The vectors are NOT EQUAL")
EndIf
 
println("\nLets change a value and try again\n")
 
$v1->$x = 99 ; Change something
 
$v1=>print()
$v2=>print()
If $v1 != $v2 Then ; Set if the vectors do not have X Y Z variables using our overloaded != operator (which uses our ==)
	println("The vectors are NOT EQUAL")
Else
	println("The vectors are EQUAL")
EndIf

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 )

Heres an alternative way to create Structs that is a little easier example:

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(10, 20, 30)
 
; Create another instance of the struct Vector3
$var2 = new Vector3(64, 32, 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 )

Its also worth noting a Struct is a very simple object and lacks functions/operator overloading etc but you can of course still use structs in custom functions just not define them in the struct like you can with a class.

Structs were well ARE very popular with the language C.

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.

Operators

=

Assignment. e.g.

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

++

Increase assignment. e.g.

$var++     (adds 1 to $var)

++

Decrease assignment. e.g.

$var--     (subs 1 from $var)

+=

Addition assignment. e.g.

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

-=

Subtraction assignment.

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

/=

Division assignment. e.g.

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

*=

Multiplication assignment. e.g.

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

**=

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

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

|=

BitwiseOR assignment. e.g.

$var |= 2

^=

BitwiseExclusiveOR assignment. e.g.

$var |= 2

 %=

BitwiseAND assignment. e.g.

$var |= 2

.=

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

$var .= "Hello"

..=

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

$var ..= "Hello"

+

Adds two numbers. e.g.

10 + 20    (equals 30)

-

Subtracts two numbers. e.g.

20 - 10    (equals 10)

*

Multiplies two numbers. e.g.

20 * 10    (equals 200)

/

Divides two numbers. e.g.

20 / 10    (equals 2)

**

Raises a number to the power. e.g.

2 ** 4    (equals 16) 

.

Concatenates/joins two strings. e.g.

"one" . 10    (equals "one10")

==

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

 !=

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

<

Tests if the first value is less than the second.

<=

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

>

Tests if the first value is greater than the second.

>=

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

<>

Test if two values are not equal.

eq

Tests if two strings are equal (case sensitive)

eqi

Tests if two strings are equal (case insensitive)

neq

Tests if two strings are not equal (case sensitive)

neqi

Tests if two strings are not equal (case insensitive)

and

Logical AND operation. e.g.

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

&&

Logical AND operation. e.g.

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

or

Logical OR operation. e.g.

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

||

Logical OR operation. e.g.

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

not

Logical NOT operation. e.g.

If NOT $var == 5

 !

Logical NOT operation. e.g.

If ! $var == 5

Conditional Statements

Loop Statements

User Defined Functions

Core Language Functions

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

Comments

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox