Player APIs (BrightScript & Ja...
...
BrightScript
Language Reference

Program Statements

29min
brightscript supports the following statement types (note that brightscript is not case sensitive) the syntax of each statement is documented in more detail later in this chapter library dim = (assignment) end stop goto rem \<or> ' print for / to / end for / step / exit for \<or> next for each / in / end for / exit for \<or> next while / end while / exit while function / end function / as / return example function main() as void dim cavemen\[10] cavemen push("fred") cavemen push("barney") cavemen push("wilma") cavemen push("betty") for each caveman in cavemen print caveman end for end function statement syntax each line may contain a single statement however, a colon ( ) may be used to separate multiple statements on a single line example myname = "fred" if myname="fred" then yourname = "barney"\ print yourname library library filename brs the library statement allows you to include your own brightscript libraries ( brs files), which can then be utilized by your script the library statement(s) must occur at the beginning of a script, before any other statements, functions, operators, etc the system locates a library by searching the directory containing the current script, as well as the sys /script lib/ directory note that the run() function does not currently change the path of a library statement to that of the called script (i e the system will continue searching the directory of the caller script) on the other hand, running a script directly from the brightsign shell does modify the library search path to that of the called script the first statement will include a library in the same folder as the script, while the second will include a library in a sub folder library "mybsl1 brs" library "new lib/mybsl2 brs" the following statement will include the docid\ leoo6do arr2igzxcripi library, which has some useful brightscript features, from the sys /script lib/ directory library "v30/bslcore brs" dim dim name (dim1, dim2, …, dimk) the dim (“dimension”) statement provides a shortcut for creating roarray objects it sets the variable name to type “roarray” it can create arrays of arrays as needed for multi dimensionality the dimension passed to dim is the index of the maximum entry to be allocated (i e the array initial size = dimension+1), though the array will be resized larger automatically if needed the following two lines create identical arrays dim array\[5] array = createobject("roarray", 6, true) the expression x\[a,b] is equivalent to x\[a]\[b] the following script demonstrates useful operations on a dim array dim c\[5, 4, 6] for x = 1 to 5 for y = 1 to 4 for z = 1 to 6 c\[x, y, z] = k k = k + 1 end for end for end for k=0 for x = 1 to 5 for y = 1 to 4 for z = 1 to 6 if c\[x, y, z] <> k then print"error" stop k = k + 1 end for end for end for assignment ("=") variable = expression the assignment statement (“=”) assigns a variable to a new value in each of the following lines, the variable on the left side of the equals operator is assigned the value of the constant or expression on the right side of the equals operator a$="a rose is a rose" b1=1 23 x=2 23 x=x b1 end the end statement terminates script execution normally stop the stop statement interrupts script execution, returns a “stop” error, and invokes the debugger use the cont command at the debugger prompt to continue execution of the script or the step command to execute a single step in the script goto goto label the goto statement transfers program control to the line number specified by label the goto label statement results in a branching operation a label is an identifier terminated with a colon on a line that contains no other statements or expressions example mylabel print "hello world" goto mylabel return return expression the return statement returns from a function back to its caller if the function is not type void, return can also return a value to the caller print print \[#output object], \[@location], item list the print statement prints an item or list of items in the console the item(s) may be strings, integers, floats, variables, or expressions an object with an ifint , iffloat , or ifstring interface may also be printed if the output object is specified, this statement will print to an object with an ifstreamsend interface if the statement is printing a list of items, the items must be separated with semicolons or commas if semicolons are used, spaces are not inserted between printed items; if commas are used, the cursor will automatically advance to the next print zone before printing the next item positive numbers and zero are printed with a leading space (without a plus sign) spaces are not inserted before or after strings example x = 5 print 25; " is equal to"; x ^2 ' prints "25 is equal to 25" example a$ = "string" print a$;a$,a$;" ";a$ 'prints "stringstring string string" each print zone in the following example is 16 characters wide the cursor moves to the next print zone each time a comma is encountered \> print "zone 1","zone 2","zone 3","zone 4" 'prints "zone 1 zone 2 zone 3 zone 4" example print "print statement #1 "; print "print statement #2" 'prints "print statement #1 print statement #2" in some cases, semicolons can be dropped for example, the following statement is legal print "this is a five "5"!!" a trailing semicolon overrides the cursor return so that the next print statement begins where the last left off if no trailing punctuation is used with a print statement, the cursor drops to the beginning of the next line \[@location] if the console you are printing to has the iftextfield interface, you can use the @ character to specify where printing will begin example print #m text field,@width (height/2 1)+(width len(msg$))/2,msg$; whenever you use print @ on the bottom line of the display, an automatic line feed causes all displayed lines to move up one line to prevent this from happening, use a trailing semicolon at the end of the statement tab (expression) this statement moves the cursor to the specified position on the current line (modulo the width of the console if the tab position is greater than the console width) example print tab(5)"tabbed 5";tab(25)"tabbed 25" note the following about the tab statement the tab statement may be used several times in a print list no punctuation is required after a tab statement numerical expressions may be used to specify a tab position the tab statement cannot be used to move the cursor to the left if the cursor is beyond the specified position, the tab statement is ignored pos(x) this statement returns an integer that indicates the current cursor position from 0 to the maximum width of the window this statement requires a dummy argument in the form of any numeric expression print tab(40) pos(0) 'prints 40 at position 40 print "these" tab(pos(0)+5)"words" tab(pos(0)+5)"are"; print tab(pos(0)+5)"evenly" tab(pos(0)+5)"spaced" for / end for for counter variable = initial value to final value step increment / end for \<or> next the for statement creates an iterative loop that allows a sequence of program statements to be executed a specified number of times the initial value , final value , and increment can be any expression the first time the for statement is executed, these three variables are evaluated and their values are saved; changing the variables during the loop will have no affect on the operation of the loop however, the counter variable must not be changed, or the loop will not operate normally the first time the for statement is executed, the counter is set to both the value and type of the initial value at the beginning of each loop, the value of the counter variable is compared with the final value if the value of the counter variable is greater than the final value , the loop will complete and execution will continue with the statement following the end for (or next ) statement if, on the other hand, the counter has not yet exceeded the final value , control passes to the first statement after the for statement if increment is a negative number, the loop will complete when the value of the counter variable is less than the final value when program flow reaches the end for (or next ) statement, the counter is incremented by the specified increment amount (or decremented if increment is a negative value) if the step \[increment] language is not included in the for statement, the increment defaults to 1 use exit for to exit a for block prematurely the following script decrements i at the beginning of each loop until it is less than 1 for i=10 to 1 step 1 print i end for for each in / end for for each item in object / end for \<or> next the for each statement can iterate through a set of items in any object that has an ifenum interface (i e an enumerator) the for block is terminated with the end for (or next ) statement objects that are ordered intrinsically (such as rolist ) are enumerated in order, while objects that have no intrinsic order (such as roassociativearray ) are enumerated in apparent random order it is possible to delete entries as they are enumerated use exit for to exit a for block prematurely the following objects can be enumerated rolist , roarray , roassociativearray , romessageport the following script iterates over an associative array in random order, prints each key/value pair, then deletes it aa={joe 10, fred 11, sue 9} for each n in aa print n;aa\[n] aa delete\[n] end for nesting for each statements the for each statement can only track one pointer per object instance, so nesting for each loops that iterate on the same object may cause the series to terminate prematurely values = {a 1, b 2, c 3, d 4, e 5, f 6} for each key outer in values &#x9;print "outer ";key outer;" ";values\[key outer] &#x9;for each key inner in values 'this will cause problematic behavior &#x9; print "inner ";key inner;" ";values\[key inner] &#x9;end for end for to work around this limitation with associative arrays, copy the keys to an array and iterate on the array in the nested for each loop values = {a 1, b 2, c 3, d 4, e 5, f 6} valuesarray = \[] for each key in values 'copy the keys to an array &#x9;valuesarray push(key) end for for each key outer in values &#x9;print "outer ";key outer;" ";values\[key outer] &#x9;for each key inner in valuesarray 'iterate over the array of keys &#x9; print "inner ";key inner;" ";values\[key inner] &#x9;end for end for while / exit while while expression / exit while a while loop executes until the specified expression is false use the exit while statement to exit a while block prematurely k=0 while k<>0 k=1 print "loop once" end while while true print "loop once" exit while end while if / then / else if expression then statements \[else statements] this is the single line form of the if then else statement; see the next section for more details about the block form of the if then else statement the if statement instructs the interpreter to test the following expression if the expression is true, control will proceed to the statements immediately following the expression if the expression is false, control will jump to either the matching else statement (if there is one) or to the next program line after the block example if x>127 then print "out of range" end then is optional in the above and similar statements however, then is sometimes required to eliminate ambiguity, as in the following example if y=m then m=o 'won't work without then block if / elseif / then / endif the block (i e multi line) form of if / then / else has the following syntax if booleanexpression \[ then ] \[ block ] \[ elseifstatement+ ] \[ elsestatement ] end if elseifstatement = elseif booleanexpression \[ then ] \[ block ] elsestatement = else \[ block ] example vp msg loop msg=wait(tiut, p) if type(msg)="rovideoevent" then if debug then print "video event";msg getint() if lm=0 and msg getint() = meden then if debug then print "videofinished" retcode=5 return endif else if type(msg)="rogpiobutton" then if debug then print "button press";msg if esc0 and msg=b0 then retcode=1\ return if esc1 and msg=b1 then retcode=2\ return if esc2 and msg=b2 then retcode=3\ return if esc3 and msg=b3 then retcode=4\ return else if type(msg)=" invalid" then if debug then print "timeout" retcode=6 return endif goto vp msg loop function() as type / end function function name(parameter as type, …) as type each function has its own scope a function is declared using the function() statement the parentheses may contain one or more optional parameters; parameters can also have default values and expressions the type of each parameter may be declared the return type of the function may also be declared if a parameter type or return type is not declared, it is dynamic by default intrinsic types are passed by value (and a copy is made), while objects are passed by reference the sub statement can be used instead of function as a shortcut for creating a function with return type void a parameter can be one of the following types integer float double string object dynamic the function return can be one of the following types void integer float double string object dynamic "m" identifier if a function is called from an associative array, then the local variable m is set to the associative array in which the function is stored if the function is not called from an associative array, then its m variable is set to an associative array that is global to the module and persists across calls the m identifier should only be used for the purpose stated above we do not recommend using m as a general purpose identifier example sub main() obj={ add add a 5 b 10 } obj add() print obj result end sub function add() as void m result=m a+m b end function anonymous functions a function without a name declaration is considered anonymous the following is a simple anonymous function declaration myfunc=function (a, b) return a+b end function print myfunc(1,2) anonymous functions can also be used with associative array literals q = { starring function(o, e) str = e getbody() print "starring " + str toks = box(str) tokenize(",") for each act in toks actx = box(act) trim() if actx <> "" then print "actor \[" + actx + "]" o actors push(actx) endif end for return 0 end function } q starring(myobj, myxml)