back to index

Datatypes and variables

Basic datatypes

The following datatypes have designated code-paths in the scriptengine:

For reasons of simplicity, no further distinction between signed and unsigned integers is made (all integers are signed).

TKS requires at least a 32bit addressation scheme; the memory consumption of an integer hereby equals the one of the Pointer datatype (i.e. 4 bytes for 32bit architectures and 8 bytes for 64bit (**untested**)).

The script language automatically converts between the datatypes String, int and float.

Example:

int    i=3.14;     // => set i to 3
float  f="1.23";   // => set f to 1.23
String s=42;       // => set s to "42"

Dynamically typed variables

TkScript also supports dynamically typed variables ("variants"). The type of such a variable depends on the value assigned to it.

Please notice that (as of version 0.9.0.6) you cannot access members and methods of complex datatype variants; arrays and hashes will work, though.

// remember that dynamically typed values are usually slower than typed ones!
var i=42;
var f=1.23;
var s="hello, world.";

print "typename(i)="+typename(i); // => int
print "typename(f)="+typename(f); // => float
print "typename(s)="+typename(s); // => String

Builtin datatypes

Each of the following core datatypes is represented by a C++ class.

The special classes Integer and Float provide an object "hull" for the scalars int und float, which comes in handy when a reference to these basic datatypes has to be created.

The special classes Value and Variable provide an object "hull" for dynamically typed values.

Variable declarations

vardecl ::= <type> <identifierlist> ;
type ::= int | float | String | Pointer | <Class>
identifierlist ::= <identifier>[=<expression>] | 
<identifierlist>,<identifier>[=<expression>>]
identifier ::= <idchar1> | <idchar1>,<idcharseq>
identifierlist ::= <idchar> | <idcharseq><idchar>
idchar1 ::= [a-z,A-Z,_] 
idchar ::= [a-z,A-Z,_,0-9]
expression ::= ...arbitrary expression, e.g. "(1+2)*3"...

Variables may be declared wherever a statement is expected. Example:

int i=42;
float x,y=2.3,z; // declare multiple vars of the same type
String s,t="hello, world.",u;

Example:

for(int i=0; i<10; i++) // variable decls may appear whereever a statement is expected
   print i;

Example:

String s="world.";
function PrintHello() {
  String s="hello, ";  // variable shadows global variable "s"
  stdout s;            // not visible outside the function
  print Main.s;        // print shadowed global variable
}
PrintHello();

Example:

class MyClass {
  printHello();
}
MyClass::printHello {
  // ---- see above ----
  String s="hello, world.";
  print s;
}
MyClass mc; mc.printHello();

Scope

Once declared, a global variable is visible in the current module. Global variables in other modules are accessible by prepending the module's name (e.g. MMyModule.myvariable).

Variables declared in functions are only visible within the scope of the function in which they have been declared. Function variables are always static by default, i.e. their value is not reset when a function is entered. You may use the "local" attribute to declare a variable that is going to be placed in a (temporary) stackframe which is valid as long as the function is being executed.
Also see testlocal.tks, fibonacci.tks


back to index

TkScript and the TkScript documentation are (c) Copyright 2001-2005 by Bastian Spiegel.