Basic Scripting Language
Although the Basic script syntax is very similar to VBScript, it is not identical and there are differences. See the Converting VBS to Basic section for details.
The Basic syntax supports:
•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 (x:=[ 1, 2, 3 ];)
•access to object properties and methods (ObjectName.SubObject.Property)
Identifier names in scripts (variable names, function and procedure names, etc.) follow the most common rules in Basic: they should begin with a character (a..z or A..Z), or '_', and can be followed by alphanumeric chars or '_' char. They cannot contain any other character or spaces.
•Valid: VarName, _Some, V1A2, _____Some____
•Invalid: 2Var, My Name, Some-more, This,is,not,valid
Assign statements (assign a value or expression result to a variable or object property) are built using "=".. Examples:
MyVar = 2
Button.Caption = "This " + "is ok."
The "new" statement is provided for the Basic syntax. Since you don't provide the method name in this statement, it looks for a method named "Create" in the specified class. If the method doesn't exist, the statement fails. For example:
MyLabel = new TLabel(Form1)
MyFont = new TFont
In the above examples, a method named "Create" for TLabel and TFont class will be called. The method must be registered. If the method receives parameters, you can pass the parameters in parenthesis, like the TLabel example above.
Strings (sequence of characters) are declared in Basic using the double quotes (") character. For example:
A = "This is a text"
Str = "Text "+"concat"
Comments can be inserted inside scripts. You can use ' chars or REM. A comment will finish at the end of the line. For example:
' This is a comment before ShowMessage
REM This is another comment
' And this is a comment
' with two lines
ShowMessage("End of okays")
To aid with VBScript compatibility, there is no need to declare variable types or even variables themselves. However, if you wish to declare variables you can using the DIM directive and the variable name. For example:
S = "Hello world!"
A = 0
A = A+1
You can also declare global variables as private or public using the following syntax :
B = 0
A = B + 1
Variables declared with the DIM statement are public by default. Private variables are not accessible from other scripts. Variables can be default initialized with the following syntax:
DIM A = "Hello world"
DIM B As Integer = 5
Strings, arrays and array properties can be indexed using "[" and "]" chars. For example, if Str is a string variable, the expression Str returns the third character in the string denoted by Str, while Str[I + 1] returns the character immediately after the one indexed by I. For example:
MyChar = MyStr
MyStr = "A"
MyArray[1,2] = 1530
Lines.Strings = "Some text"
Array constructors and variant arrays are supported. To construct an an array, use "[" and "]" chars. You can construct multi-index array nesting array constructors. You can then access arrays using indexes. If the array is multi-index, separate indexes using ",".
If a variable is a variant array, then indexing in that variable is supported. A variable is a variant array if it was assigned using an array constructor, if it is a direct reference to a Delphi variable which is a variant array or if it was created using the VarArrayCreate procedure.
Arrays are 0-based index. For example:
NewArray = [ 2,4,6,8 ]
Num = NewArray //Num receives "4"
MultiArray = [ ["green","red","blue"] , ["apple","orange","lemon"] ]
Str = MultiArray[0,2] //Str receives 'blue'
MultiArray[1,1] = "new orange"
There are two forms of if statement: if...then...end and the if...then...else...end if. Like normal Basic, if the if expression is true, the statement (or block) is executed. If there is an else part and the expression is false, then the statement (or block) after else is executed. For example:
FUNCTION Test(I, J)
IF J <> 0 THEN Result = I/J END IF
IF J = 0 THEN Exit Function ELSE Result = I/J END IF
IF J <> 0 THEN
Result = I/J
If the IF statement is in a single line, you don't need to finish it with END IF:
IF J <> 0 THEN Result = I/J
IF J = 0 THEN Exit ELSE Result = I/J
A while statement is used to repeat a statement or a block, while a control condition (expression) is evaluated as true. The control condition is evaluated before the statement. Hence, if the control condition is false at first iteration, the statement sequence is never executed. The while statement executes its constituent statement (or block) repeatedly, testing the expression before each iteration. As long as the expression returns True, execution continues. For example:
WHILE (Data[I] <> X) I = I + 1 END WHILE
WHILE (I > 0)
IF Odd(I) THEN Z = Z * X END IF
X = Sqr(X)
WHILE (not Eof(InputFile))
The possible syntax's are:
DO WHILE expr statements LOOP
DO UNTIL expr statements LOOP
DO statements LOOP WHILE expr
DO statement LOOP UNTIL expr
The statements will be executed WHILE expr is true, or UNTIL expr is true. If expr is before statements, then the control condition will be tested before iteration. Otherwise, control condition will be tested after iteration. For example:
K = I mod J
I = J
J = K
LOOP UNTIL J = 0
DO UNTIL I >= 0
Write("Enter a value (0..9): ")
K = I mod J
I = J
J = K
LOOP WHILE J <> 0
DO WHILE I < 0
Write("Enter a value (0..9): ")
For statements can have the following syntax:
FOR counter = initialValue TO finalValue STEP stepValue
The for statement sets counter to initialValue, repeats execution of statements until "next" and then increments value of counter by stepValue, until counter reaches finalValue. The step part is optional, and if omitted stepValue is considered to be 1.
FOR c = 1 TO 10 STEP 2
a = a + c
FOR I = a TO b
j = i ^ 2
sum = sum + j
select case statements
Case statements have the following syntax:
SELECT CASE selectorExpression
if selectorExpression matches the result of one of the caseexprn expressions, the respective statements will be executed. Otherwise, the elsestatement will be executed. The Else part of a case statement is optional. For example:
SELECT CASE uppercase(Fruit)
CASE "lime" ShowMessage("green")
CASE "apple" ShowMessage("red")
function and sub declarations
The declaration of functions and subs is similar to Basic. Functions return values, which are returned using the implicitly declared variable (with the same name as the function) or via the Return statement. Parameters by reference can also be used, by using the BYREF directive. For example:
TodayAsString = DateToStr(Date)
IF A>B THEN
MAX = A
MAX = B
SUB SwapValues(BYREF A, B)
TEMP = A
A = B
B = TEMP
You can use Return statement to exit subs and functions. For functions, you can also return a valid value:
'This line will be never reached
All Content: 2BrightSparks Pte Ltd © 2003-2017