October 8, 2008
New DekiScript Syntax for 8.08.1
Arne Claassen @ 1:12 pm
For the upcoming 8.08.1 release we’ve added a bit of new DekiScript syntax to make code easier to write and allow more control over the flow.
Multiple variable declarations and declarations without a value
The old syntax for var required just one variable name and an initial value, like var x = 123. With the new syntax, multiple variables can be declared at once and variables can be declared without a value. This means that all of these are now valid:
var x; var x,y; var x = 1, y =2, z=3; var x = 1, y, z;
Single statement blocks do not need curly braces
Both if/else and foreach required their statements to be enclosed in { ... }, even if there was only a single statement. A by product of this was a complicated and hard to read syntax for if/else-if/else blocks, since the second if/else effectively was the statement block of the first else, resulting in statements like this:
if( x == 1 ) { "1"; } else { if(x == 2) { "2"; } else { "3"; } }
With the new syntax, curly braces can be omitted if there is only a single statement in the block.
if( x == 1 ) "1"; else if(x == 2) "2"; else "3";
The same simplification works also applies to foreach.
Foreach flow control with break and continue
Another addition for foreach is the introduction of break and continue control flow statements. The break statement will immediately terminate the current foreach loop block and continue executing the code that follows the foreach block. The continue statement on the other hand will terminate the loop block and restart it with the next iteration value.
foreach (var x in [1,2,3,4])
{
x;
if( x == 2 ) continue;
else if(x==3) break;
".";
}
"x";
This code with produce the output “1.23x“.
Introducing the switch statement
The final addition to DekiScript for this release is the switch/case statement. So, while if/else-if/else is more concise now, for flow control relying on picking among many paths based on an expression, switch/case offers a much better syntax. The easiest way to illustrate switch/case is with an example:
var x = 2;
switch(x)
{
case 1:
"one";
case 2:
"two";
case 3:
case 4:
"bigger than 2";
default:
"bigger than 4";
}
Here, we take a number and try to emit a string appropriate to the number. The output for this particular bit of code would simply be “two“. The case for 3 illustrates fallthrough, i.e. in our switch implementation there exists implicit fallthrough, but only if the case does not have a block of its own. Therefore the cases for 2 and 3 return the same value. Finally we have the special case of default, which will trigger if no other case matched. default is not required.
We hope these additions make it even easier to create custom dynamic content with dekiscript, so check them out once 8.08.1 is released and let us know how you like them.
categories: Deki





No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URL
Leave a comment