Scripter2 Help Page

So you're wanting to give Scripter2 a test run.. Well, you've came to the right place. The Scripter2 demo application is at http://scripter2.lastyearswishes.com

There are a few limitations to keep in mind for the demo application

  1. Scripts can be no longer than 1000 bytes long
  2. Scripts are sandboxed completely(so far). return is the only way to get data out of your scripts right now
  3. Scripts can take no longer than 5 seconds to execute
  4. Script results will be trimmed if they are larger than 2000 bytes
  5. Floating point operations are unimplemented so far
  6. && and || logical and/or is not supported yet (nor is truely logical !)

With that in mind, continue...

We'll start of course with a hello world application:

return "Hello World!";

Now I'll try to cover a few different topics:

Variable Declarations

Although variables can be created without prior declaration, it is not how I recommend doing things.

There are two different variable scopes available. Local and Global.

var l; //local variable
global var g; //global variables

Global variables can be accessed from any scope, including inside functions(without passing it in as an argument). Local variables are local to their scope. Scopes are "created" via {} or by function calls. For example:

var bar="baz";
{
  var foo="biz";
}
bar=foo; //bar is now `undefined` 
var f=function(){
  var fizz="123";
  return;
};
f();
bar=fizz; //bar is now `undefined`

Tables

Tables are the cure-all to all data structures you'll use in Scripter2. Tables can implement lists, hashes, dictionaries, arrays, classes, structures, and anything else you can think of.

var table=[]; //an empty table
table=[1,2,3] //a 0 indexed array containing 1, 2, 3
assert(table[0]==1);
assert(table[1]==2);
table=["foo" => "bar"];
assert(table["foo"]=="bar");
assert(table.foo=="bar");
//for string keys, short cutting similar to javascript is allowed

Tables can be added on to without any special operators as well.

var table=[];
table.foo="bar";
table[10]="foobar";

There are also several built in functions for transversing and getting information about tables.

var table=[1, 2, 3];
var keys=keylist(table); //A table is returned like [0 => 0, 1 => 1, 2 => 2]
var count=keycount(table); //returns 3

There is also a foreach operation as well:

var table=["foo" = > "bar", "biz" => "baz"];
foreach(value in table){
  //value will be "bar" and then "baz"
}
foreach(key => value in table){
  //key will be "foo" and then "biz"
  //value will be "bar" and then "baz"
}

Private Table Keys

Any key put in a table that is prefixed with _ will be not included in the keylist nor keycount, and therefore will not used in foreach either.

This is to allow for "private" variables. There are some more plans(such as operator overloading) up my sleeve, but so far these are not implemented.

Note private variables can still be modified from anywhere though:

var table=["_foo" => "bar"];
assert(keycount(table)==0);
assert(table._foo=="bar");
table._foo="biz";
assert(table._foo=="biz");

These private variables will be much more useful as the facilities for duck-typing are fleshed out further.

Built in keywords

We already discussed the foreach keyword. There is also if and while which behaves as you would expect:

var i=0;
while(i<10){
  i++;
}
if(i!=10){
  return "all is not well";
}else{
  return "all is well";
}

There are also some built in functions, such as keycount, keylist, and isdefined. There are also some built in value keywords. These are null, nil, true, false, and undefined

Is Defined

The isdefined builtin function will test if a variable is equal to undefined. Due to operator and casting issues, it's not possible to do this with an if statement.

isdefined can return some false positives, depending on how you look at it. For instance, if you have a variable like this: var v=undefined; then isdefined(v) though being "declared" will return true. I believe that this is proper behavior and I'm don't really see much of a better way to do it.

isdefined can also be used on tables. For instance,

var table=[];
assert(!isdefined(table.foo));

Type casting

Type casting is a bit odd right now(hopefully cleaned up as we get closer to 1.0), I'll just warn you.

There is implicit type casting done on nearly every operation. The type to the right of the operation is cast to the type left of the operation.

Some examples:

var s="0123";
var i=5;
assert(s+i=="01235");
assert(i+s==128);
s="000123";
i=123;
assert(i==s);
assert(s!=i);

There is also support for explicit type casting using the as operator.

Some examples:

var s="0123";
var i=5;
assert((i as string)+"123" == "5123");
assert((s as integer)+5 == 128);

You can also test for a type using the is operator.

assert([] is table);
assert 123 is integer);
assert("foo" is string);
assert(function(){} is function);

Functions

Function are treated basically the same as variables and are first class. Lambda support is not yet included however.

Declaring a function is quite easy and looks exactly like Javascript:

var f=function(x){
    return x+1;
}
assert(f(1)==2);

Functions can also be used in tables to produce things that look oddly similar to classes

var foo=[];
foo.func=function(){
  return "bar";
}

Functions, though they are first class and can be passed around with ease, have next to no operations that can be performed on them. They can not be cast to any other type and do not support any operators such as ==

Operator List

Here is a full list of operators:

x++; //postfix increment
x--; //postfix decrement
++x; //prefix increment
--x; //prefix decrement
x*y; //binary multiplication
x/y; //binary division
x%y; //binary modulo
x+y; //binary addition
x-y; //binary subtraction
-x; //unary minus
+x; //unary plus
!x; //logical not
~x; //bitwise not
x&y; //bitwise and
x|y; //bitwise or
x^y; //bitwise xor
x<<y; //shift left
x>>y; //shift right
x=y; //assignment
x==y; //equals
x!=y; //not equal
x<y;
x>y;
x<=y;
x>=y;
x as Y; //typecast
x is y; //type comparison
x[y]; //subscript
x.y; //subscript shortcut

Strings

There are two different quote characters allowed.

'foo';
"foo"; //both the same

There is nothing special between them and is just a matter of personal preference.

There are also some escape codes:

  1. \n newline
  2. \r linefeed
  3. \ \
  4. \' '
  5. \" "

Conclusion

Hopefully that explains enough for right now. If you find anything that causes, say a NullReferenceException or other error that isn't a Engine Error, I'd really appreciate a comment down below with the code that caused it.

Tags: scripter2
Posted: 6/29/2012 3:05:55 AM

Comments

Posting comments is currently disabled(probably due to spam)