# Language Overview (Basic) - Scripting

In addition to Pascal, the Scripting Module can also execute scripts written in the BASIC language. It currently supports the following BASIC syntax: ▪sub .. end and function .. end declarations ▪byref and dim directives ▪if .. then .. else .. end constructor ▪for .. to .. step .. next constructor ▪do .. while .. loop and do .. loop .. while constructors ▪do .. until .. loop and do .. loop .. until constructors ▪^ , * , / , and , + , - , or , <> , >=, <= , = , > , < , div , mod , xor , shl , shr operators ▪try .. except and try .. finally blocks ▪try .. catch .. end try and try .. finally .. end try blocks ▪select case .. end select constructor ▪array constructors ▪exit statement ▪access to object properties and methods (ObjectName.SubObject.Property) The following pages describe: ▪Script Structure ▪Identifiers ▪Assign Statements ▪Character Strings ▪Comments ▪Variables ▪Indexes ▪Arrays ▪if Statements ▪while Statements ▪loop Statements ▪for Statements ▪select case Statements ▪FUNCTION and SUB Declarations ▪DLL Functions Script Structure Script structure is made of two major blocks: a) function and sub declarations and b) main block. Both are optional, but at least one should be present in script. Examples: ' SCRIPT 1: SUB DoSomething CallSomething END SUB ' SCRIPT 2: FUNCTION MyFunction MyFunction = "Ok!" END FUNCTION Like in normal BASIC, statements in a single line can be separated by ":" characters. Identifiers Identifier names in script (variable names, function and procedure names, etc.) follow the most common rules in BASIC: ▪ They must begin with a character (a..z or A..Z), or the '_' symbol. ▪ They can be followed by alphanumeric characters or the '_' symbol. ▪ They cannot contain any other character or spaces. Valid identifiers: VarName _Some V1A2 _____Some____ Invalid identifiers: 2Var My Name Some-more This,is,not,valid Assign Statements To assign a value or expression result to a variable or object property, use '='. Examples: MyVar = 2 Button.Caption = "This " + "is ok." Character Strings Strings (sequence of characters) are declared in BASIC by using the double quote (") character. Examples: A = "This is a text" Str = "Text " + "concat" Comments Comments can be inserted inside scripts using the ' (single quote) character or REM. Examples: ' This is a comment before ShowMessage ShowMessage("Ok") REM This is another comment ShowMessage("More ok!") ' And this is a comment ' with two lines ShowMessage("End of okays") Variables You can declare variables using the DIM directive and its name. It is optional as to whether you include the variable's type in its declaration. Examples: ' SCRIPT 1: SUB Msg DIM S S = "Hello world!" ShowMessage(S) END SUB ' SCRIPT 2: DIM A A = 0 A = A + 1 ShowMessage(A) ' SCRIPT 3: DIM A AS Integer = 5 ShowMessage(A) Note you do not need to actually use the DIM directive, as variables are implicitly declared. The directive is provided to allow clarity in the script. Indexes Strings, arrays, and array properties can be indexed using the "[" and "]" characters. For example, if Str is a string variable, the expression Str[3] returns the third character in the string, while Str[I + 1] returns the character immediately after the one indexed by I. Examples: MyChar = MyStr[2] MyStr[1] = "A" MyArray[1,2] = 1530 Lines.Strings[2] = "Some text" Arrays Scripting supports array constructors and variant arrays. To construct an array, use the "[" and "]" characters. You can construct a multi-dimensional (multi-indexed) array nesting array constructors. You can then access arrays using indexes. If an array is multi-indexed, separate the indexes using ",". If a variable is a variant array, the Scripting Module automatically supports indexing in that variable. A variable is a variant array if it was assigned using an array constructor or if it was created using the VarArrayCreate procedure. Note: Arrays are zero-based, i.e., they are indexed from 0. Examples: NewArray = [2, 4, 6, 8] Num = NewArray[1] ' Num receives "4" ' Create a 2-dimensional array MultiArray = [["green","red","blue"], ["apple","orange","lemon"]] Str = MultiArray[0,2] ' Str receives "blue" Str = MultiArray[1,0] ' Str receives "apple" MultiArray[1,1] = "pineapple" ' Replace "orange" with "pineapple" if Statements There are two forms of the if statement: IF...THEN…END IF and IF...THEN...ELSE...END IF. In the case that the IF expression is true, the statement (or block) after it is executed. If there is an ELSE part and the expression is false, the statement (or block) after the ELSE is executed. (Note, if the IF statement is in a single line, you don't need to finish it with END IF) Examples: IF J <> 0 THEN Result = I/J IF J = 0 THEN Exit ELSE Result = I/J IF J <> 0 THEN Result = I/J Count = Count + 1 ELSE Done = True END IF while Statements A while statement is used to repeat a statement or a block, while a control condition is evaluated as true. The control condition is evaluated before the statement. Hence, if the condition is false at the first iteration, the statement sequence is never executed. The while statement executes its statement (or block) repeatedly, testing the control condition before each iteration. As long as the condition returns true, execution of the statement (or block) continues. Examples: WHILE (Data[I] <> X) I = I + 1 END WHILE WHILE (I > 0) IF Odd(I) THEN Z = Z * X END IF X = Sqr(X) END WHILE WHILE (not Eof(InputFile)) Readln(InputFile, Line) Process(Line) END WHILE loop Statements The Scripting Module supports several loop statements: DO WHILE expr statements LOOP DO UNTIL expr statements LOOP DO statements LOOP WHILE expr DO statement LOOP UNTIL expr These statements will be executed WHILE expr is true, or UNTIL expr is true. If expr is before the statements, then the control condition will be tested before each iteration. Otherwise, the control condition will be tested after each iteration. Examples: DO K = I mod J I = J J = K LOOP UNTIL J = 0 DO UNTIL I >= 0 Write("Enter a value (0..9): ") Readln(I) LOOP DO K = I mod J I = J J = K LOOP WHILE J <> 0 DO WHILE I < 0 Write("Enter a value (0..9): ") Readln(I) LOOP for Statements The Scripting Module supports FOR statements with the following syntax: FOR counter = initialValue TO finalValue STEP stepValue statements NEXT The FOR statement sets counter to initialValue, repeats execution of its statement(s) until NEXT and increments the value of counter by stepValue until counter reaches finalValue. (Note, the STEP part is optional, and if omitted stepValue is considered 1.) Examples: ' SCRIPT 1: FOR c = 1 TO 10 STEP 2 a = a + c NEXT ' SCRIPT 2: FOR I = a TO b j = I ^ 2 sum = sum + j NEXT select case Statements The Scripting Module supports SELECT CASE statements with the following syntax: SELECT CASE selectorExpression CASE caseexpr1 statement1 ... CASE caseexprn statementn CASE ELSE elsestatement END SELECT If the selectorExpression matches the result of one of the caseexprn expressions, the respective statement(s) will be executed. Otherwise, the elsestatement will be executed. The else part of the case statement is optional. Example: SELECT CASE uppercase(Fruit) CASE "lime" ShowMessage("green") CASE "orange" ShowMessage("orange") CASE "apple" ShowMessage("red") CASE ELSE ShowMessage("black") END SELECT FUNCTION and SUB Declarations Declaration of functions and subs are similar to BASIC. In functions to return function values, use the implicitly declared variable which has the same name of the function, or use the Return statement. Parameters by reference can also be used, using BYREF directive. Examples: SUB HelloWord ShowMessage("Hello world!") END SUB SUB UpcaseMessage(Msg) ShowMessage(Uppercase(Msg)) END SUB FUNCTION TodayAsString TodayAsString = DateToStr(Date) END FUNCTION FUNCTION TodayAsString2 Return DateToStr(Date) END FUNCTION FUNCTION Max(A,B) IF A>B THEN MAX = A ELSE MAX = B END IF END FUNCTION SUB SwapValues(BYREF A, B) DIM TEMP TEMP = A A = B B = TEMP END SUB DLL Functions The Scripting Module allows the importing and calling of external DLL functions, using the following syntax: function lib 'libName.dll' [alias ExternalFunctionName] [callingConvention] functionName(arguments) as resultType; For example, the following declaration: function lib "CustomLib.dll" MyFunction(arg as integer) as integer imports a function called MyFunction from CustomLib.dll. The Scripting Module also allows you to use a different name for the DLL function, like the following declaration: function MessageBox lib "User32.dll" alias "MessageBoxA" stdcall (hwnd as pointer, text as string, caption as string, msgtype as integer) as integer which imports the 'MessageBox' function from User32.dll (a Windows API library), renaming it to 'MessageBoxA' to be used in script. Example: ' Declaration of external function function MessageBox lib "User32.dll" alias "MessageBoxA" stdcall (hwnd as pointer, text as string, caption as string, msgtype as integer) as integer ' Windows constants CONST MB_ICONQUESTION = $20 CONST MB_YESNO = $4 CONST IDYES = 6 IF MessageBox(nil, "Are you sure? ", "Confirmation", MB_ICONQUESTION + MB_YESNO) = IDYES THEN ShowMessage("User answered YES") ELSE ShowMessage("User answered NO") END IF (After Save To File) Scripting | Menus | File | Save to Binary Library Save the contents of the Scripting Code window to an external file. (After View) Scripting | Menus | Script | Pascal Scripting | Menus | Script | Basic