RBScript

From Real Software Documentation

Jump to: navigation, search
Class (inherits from Object)

Used to execute Real Studio code within a running (compiled) application.


Events
CompilerError Print
Input RuntimeError


Properties
Context State
Source


Methods
Precompile Run
Reset


Class Constants

The following class constants can be used to specify the value of the State property.

Class Constant Description
kStateReady The script compiler is ready.
kStateRunning The script compiler is running.
kStateCompleted The script compiler has completed running the script.
kStateAborted The script compiler has aborted.


The following class constants can optionally be passed into Compile(). The default is kOptimizationLevelHigh. Run() does not take a level and defaults to kOptimizationLevelNone unless the script has already been compiled.

Class Constant Description
kOptimizationLevelNone No optimizations will be performed and the script will be compiled lazily (a JIT).
kOptimizationLevelLow The script will be compiled up front, but few optimizations will be applied.
kOptimizationLevelHigh The script will be compiled up front and all possible optimizations should be ran.

Notes

The RBScript language is an implementation of the Real Studio language that allows end users to write and execute Real Studio code within a compiled application. Scripts are compiled into machine language, rather then being interpreted.

Since RBScript is a class, you use it by creating an instance of this class either via code or by adding an RBScript control to a window. The easiest way to use RBScript is to assign the code to the Source property of the RBScript object and call the Run method.

To provide information to an RBScript while it's running, use the Input function. This function calls the Input event of the RBScript object where you can return the information you wish returned by the Input function in your RBScript code. In the following example, the results of the Input function are assigned to a variable:

Dim Years, Days as Integer
Years = Val(Input(""))
Days = Years * 365


The Input function takes a String that can be used to provide a prompt in case you are going to provide a dialog box in which the user enters the information. Since the Input function returns a String and we want to store the value as an integer, the Val function is used to convert the string to an integer. In this case, if the number of years is going to be entered into a TextField called TextField1, then the Input event of the RBScript object would look like this:

Function Input(prompt as String) as String
Return TextField1.text


When the Run method of the RBScript object is called, the code will be compiled and then executed. Since the Input function is called, the Input event of the RBScript object is executed and the contents of the Text property of the TextField is returned and assigned to the Years variable. Then the Days value is calculated.


Getting Information From RBScript

You obtain data from the RBScript using the Print method. This method takes a String and passes it to the Print event of the RBScript object. Here is the example modified to use the Print function:

Dim Years, Days as Integer
Years = Val(Input(""))
Days = Years * 365
Print Str(days)


You access the value passed to the Print method through the Print event. For example, if you want to assign the value to the Text property of a Label object, the code for the Print event of the RBScript object would look like this:


Sub Print(msg as String)
Label1.text = msg


Handling Errors in Your Code

If an error occurs while RBScript is compiling your code, the CompilerError event of the RBScript object will be called and will be passed appropriate error information so you can then decide how to handle the error. If the error occurs while the code is running, the RuntimeError event of the RBScript object is called and is passed appropriate error information. You can then decide how to respond to the error.


Variables and Constants

All numeric and alphanumeric operators are supported:


+, -, *, /, _, Mod, <, =, >, <=, >=, <>.

Logical operators: And, Not, Or.

All forms of comments are supported: ', //, and REM.

Data Types

RBScript supports the following data types:

Arrays can use any of these types.

Control Structures

All the control structures specified in this Language Reference are supported. This includes:


Classes

You can create a class using the Class…End Class structure. A new class can have properties, methods, and events.

Class NewClass
Dim i as Integer //Property definitions..
Dim c as Color
Sub myMethod (x as Integer, y as Integer)
//Method definition goes here
End Sub
//Class definition
End Class

A class can be subclassed from another class using the “Inherits” declaration.

The subscript operator can now be overloaded by classes. To use it, implement Operator_Subscript in your class. The ReDim statement can now be overloaded by a class. To use it, implement Operator_Redim in your class.

For example:

Class OneBasedArray
Sub Constructor(bounds as integer = 0)
redim mArray( bounds - 1 )
End Sub

Function Count() As integer
return mArray.ubound + 1
End Function

Sub Operator_Redim(newSize as integer)
redim mArray( newSize - 1 )
End Sub

Function Operator_Subscript(index as integer) As variant
return mArray( index - 1 )
End Function

Sub Operator_Subscript(index as integer, assigns v as variant)
mArray( index - 1 ) = v
End Sub

Private mArray() As Variant
End Class

Modules

You can create modules using the Module structure. For example:

Module Foo
Dim bar As Integer
Sub Baz ()
bar = 42
End Sub
End Module


Modules are similar to REAL Studio modules. The differences are that properties are always protected and constants are not allowed. You can get the effect of a public property or constant by simply putting the Dim or Const statement outside of the module.


TypeCasting

Typecasting is supported. Use the desired type's name as a function whose only argument is the object reference you want to cast. If it fails, it will trigger an IllegalCastException error.


"Super" keyword

The new "Super" keyword lets you call overridden methods without having to specify which class the method came from. This works for constructors as well. You can call overridden superclass methods using REAL Studio's classname.methodname() syntax.

Inheritance

Class inheritance is implemented with the "Inherits" declaration.

Class MyNewSubClass
Inherits NewClass
//continue coding here....
End Class


Events

Classes can declare new events using a New Event declaration.

Class myClass
New Event myEvent (myParameter as DataType) as DataType
End Class



Methods can handle such events using the Handles Event declaration. For example:

Class myClass
New Event myEvent (myParameter as DataType) as DataType
End Class
Class mySubClass
Inherits myClass
Function myEvent (myParameter as DataType) Handles Event
//continue with function here
End Function
End Class


Interfaces

RBScript supports interfaces, declared by the Interface...End Interface structure. Interface methods are declared with a Sub or Function line just as they would be declared inside a class, but they have no contents and need no End Sub or End Function line:

Interface MovableItem
Sub Move( x As Integer, y As Integer)
Function LocationX() As Integer
Function LocationY() As Integer
End Interface


Function declarations with no parameters must have empty parentheses, as shown above.

A class may declare that it implements an interface with the Implements keyword:

Class Box
Implements MovableItem
End Class


Classes can implement any number of interfaces, but they must provide all of the methods listed in the interface. Interfaces can be used in all the situations as in REAL Studio.

Input and Output

Function Comments
Input(Prompt as String) Retrieves input from the user. This function triggers the Input event.
Print(String) Sends output to the implied output device. This function triggers the Print event.

Functions

Standard library functions are supported as follows. These functions work as specified in this Language Reference unless comments indicate otherwise. String functions can be called using the syntax of methods, just as in REAL Studio.

Function Comment
Abs(Double) As Double
Acos(Double) As Double
Asc(String) As Integer
AscB(String) As Integer
Asin(Double) As Double
Atan(Double) As Double
Atan2(Double, Double) As Double
ByRef keyword
ByVal keyword
CDbl(String) As Double Identical to Val().
Ceil(Double) As Double
Chr(Double) As String
ChrB(Double) As String
CMY
Color
Const name = value
Cos(Double) As Double
CountFields(String, String) As Integer
CStr(Double) as String
Dim Allows you to declare multiple variables with different types rather than only multiple variables of the same type.

Empty arrays can be created by specifying -1 elements.

Do...Loop
Exit
Exp(Double) As Double
False
Floor(Double) As Double
For...Next Allows floating point loop counters and step values.
Format(Double, String) As String
Function Functions can be called before they are defined.
GOTO keyword
Hex(Integer) As String
HSV
If...Then...Else
InStr(Integer, String, String) As Integer
InStrB(Integer, String, String) As Integer
IsA operator Can be used with Object; it will return True for any non-null object.
Left(String, Integer) As String
LeftB(String, Integer) As String
Len(String) As Integer
LenB(String) As Integer
Log(Double) As Double
Lowercase(String) As String
LTrim(String) As String
Max(Double, Double) As Double
Me
Microseconds As Double
Mid(String, Integer, Integer) As String
MidB(String, Integer, Integer) As String
Min(Double, Double) As Double
Nil
NthField(String, String, Integer) As String
Oct(Integer) As String
Pow(Double, Double) As Double
Redim
Rem
Replace(String, String, String) As String
ReplaceB(String, String, String) As String
ReplaceAll(String, String, String) As String
ReplaceAllB(String, String, String) As String
RGB
Right(String, Integer) As String
RightB(String, Integer) As String
Rnd As Double
Round(Double) As Double
RTrim(String) As String
Select Case
Self
Sin(Double) As Double
Sqrt(Double) As Double
Str(Double) As String
StrComp(String, String, Integer) As Integer
Sub
Tan(Double) As Double
Ticks as Integer
Titlecase(String) As String
Trim(String) As String
True
Ubound(array) As Integer Supports second parameter for multi-dimensional arrays.
Uppercase(String) As String
Val(String) As Double Scientific format not recognized.
While...Wend


Error Codes

Compiler error numbers returned in errorNumber are shown below:

Error Number Description
1 Syntax does not make sense.
2 Type mismatch.
3 Select Case does not support that type of expression.
4 Obsolete. The compiler no longer generates this error.
5 The parser's internal stack has overflowed.
6 Too many parameters for this function.
7 Not enough parameters for this function call.
8 Wrong number of parameters for this function call.
9 Parameters are incompatible with this function.
10 Assignment of an incompatible data type.
11 Undefined identifier.
12 Undefined operator.
13 Logic operations require Boolean operands.
14 Array bounds must be integers.
15 Can't call a non-function.
16 Can't get an element from something that isn't an array.
17 Not enough subscripts for this array's dimensions.
18 Too many subscripts for this array's dimensions.
19 Can't assign an entire array.
20 Can't use an entire array in an expression.
21 Can't pass an expression as a ByRef parameter.
22 Duplicate identifier.
23 The backend code generator failed.
24 Ambiguous call to overloaded method.
25 Multiple inheritance is not allowed.
26 Cannot create an instance of an interface.
27 Cannot implement a class as though it were an interface.
28 Cannot inherit from something that is not a class.
29 This class does not fully implement the specified interface.
30 Event handlers cannot live outside of a class.
31 It is not legal to ignore the result of a function call.
32 Can't use "Self" keyword outside of a class.
33 Can't use "Me" keyword outside of a class.
34 Can't return a value from a Sub.
35 An exception object required here.
36 The function declares a different return type than the interface function it is implementing.
37 The function declares a different return type than the function in its superclass that it's overriding.
38 The method uses the "Handles" clause to specify that it is an event handler, but the method cannot handle events. For example, the method may not be in a class or may be a shared method.
39 The method uses the "Handles" clause but specifies an event that does not exist.
40 Destructors can't have parameters.
41 Can't use "Super" keyword outside of a class.
42 Can't use "Super" keyword in a class that has no parent.
43 This #else does not have a matching #if preceding it.
44 This #endif does not have a matching #if preceding it.
45 Only Boolean constants can be used with #if.
46 Only Boolean constants can be used with #if.
47 The variable specified after the Next statement does not match the loop's counter variable. For example, if you declare the For loop as using "i" as its counter, the loop must end in "Next" or "Next i".
48 The size of an array must be a constant or number.
49 You can't pass an array to an external function.
50 External functions cannot use objects as parameters.
51 External functions cannot use ordinary strings as parameters. Use CString, PString, WString, or CFStringRef instead.
52 This kind of array can not be sorted.
53 This property is protected. It can only be used from within its class.
54 This method is protected. It can only be called from within its class.
55 This local variable or constant has the same name as a Declare in this method. You must resolve this conflict.
56 This global variable has the same name as a global function. You must resolve this conflict.
57 This property has the same name as a method. You must resolve this conflict.
58 This property has the same name as an event. You must resolve this conflict.
59 This global variable has the same name as a class. You must resolve this conflict.
60 This global variable has the same name as a module. You must change one of them.
61 This local variable or parameter has the same name as a constant. You must resolve this conflict.
62 This identifier is reserved and can't be used as a variable or property name.
63 There is no class with this name.
64 The library name must be a string constant.
65 This Declare Function statement is missing its return type.
66 You can't use the New operator with this class.
67 This method doesn't return a value.
68 End quote missing.
69 A class cannot be its own superclass.
70 Cannot assign a value to this property.
71 Cannot get this property's value.
72 The If statement is missing its condition.
73 The current function must return a value, but this Return statement does not specify any value.
74 A method parameter has options that are mutually exclusive. For example, the "ByRef" and "ByVal" options cannot be used together.
75 This parameter option has already been specified. For example, this occurs if you declare a parameter as "ByRef ByRef i As Integer".
76 A parameter passed by reference cannot have a default value.
77 A ParamArray cannot have a default value.
78 An Assigns parameter cannot have a default value.
79 An Extends parameter cannot have a default value.
80 Only the first parameter may use the Extends option.
81 Only the last parameter may use the Assigns option.
82 An ordinary parameter cannot follow a ParamArray.
83 Only one parameter may use the Assigns option.
84 Only one parameter may use the ParamArray option.
85 A ParamArray cannot have more than one dimension.
86 The keyword "Then" is expected after this If statement's condition.
87 Obsolete. The compiler no longer generates this error.
88 Constants must be defined with constant values.
89 Illegal use of the Call keyword.
90 No cases may follow the Else block.
91 A computed property can only contain "Get" and "Set" blocks
92 A computed property getter or setter block ends with the wrong "end" line. For example, if you start the block with "Get", it must end with "End Get".
93 Duplicate method definition.
94 The library name for this declaration is blank.
95 This If statement is missing an End If statement.
96 This Select Case statement is missing an End Select statement.
97 This For loop is missing its Next statement.
98 This While loop is missing its Wend statement.
99 This Try statement is missing an End Try statement.
100 This Do loop is missing its Loop statement.
101 Too few parentheses.
102 Too many parentheses.
103 The Continue statement only works inside a loop.
104 There is no (%1) block to (%2) here.
105 Shared methods cannot access instance properties.
106 Shared methods cannot access instance methods.
107 Shared computed property accessors cannot access instance properties.
108 Shared computed property accessors cannot access instance methods.
109 The Constructor of this class is protected, and can only be called from within this class.
110 This String field needs to specify its length.
111 Structures cannot contain fields of this type.
112 Structures cannot contain multidimensional arrays.
113 Enumerated types can only contain integers.
114 An enumerations cannot be defined in terms of another enumeration.
115 This is a constant; its value can't be changed.
116 A String field must be at least 1 byte long.
117 The storage size specifier only applies to String fields.
118 A Structure cannot contain itself.
119 A structure is not a class, and cannot be instantiated with New.
120 An enumeration is not a class, and cannot be instantiated with New.
121 This type is private, and can only be used within its module.
122 Class members cannot be global.
123 Module members must be public or private; they cannot be protected.
124 Members of inner modules cannot be global.
125 A Dim statement creates only one new object at a time.
126 A constant was expected here, but this is some other kind of expression.
127 This module is private, and can only be used within its containing module.
128 Duplicate property definition.
129 This datatype cannot be used as an array element.
130 Delegate parameters cannot be optional.
131 Delegates cannot use Extends, Assigns, or ParamArray.
132 The Declare statement is illegal in RBScript.
133 It is not legal to dereference a Ptr value in an RBScript.
134 Delegate creation from Ptr values is not allowed in RBScript.
135 Delegate constant definition.
136 Ambiguous interface method implementation.
137 Illegal explicit interface method implementation. The class does not claim to implement this interface.
138 The interface does not declare this method.
139 This method contains a #If without a closing #endif statement.
140 This interface contains a cyclical interface aggregation.
141 The Extends modifier cannot be used on a class method.
142 You cannot assign a non-value type to a value.
143 Duplicate attribute name.
144 Delegates cannot return structures.
145 You cannot use AddressOf on this method. Possible causes include using AddressOf on event declarations or soft declares.
146 You cannot use an Operator_Convert method to perform a convert-to operation on an interface.
147 The ElseIf statement is missing its condition.
148 This type cannot be used as an explicit constant type.
149 Recursive constant declaration error.
150 Custom error created using "Error" pragma.
151 The second operand to the "Unused" pragma must be a local variable or parameter.
152 The maximum units in last position parameter must be a constant.
153 The maximum units in last position parameter is out of range.
154 The StructureAlignment attribute's value must be of the following: 1, 2, 4, 8, 16, 32, 64, or 128.
155 Pair creation via the ":" operator is not allowed in RBScript.
156 Introspection via the GetTypeInfo operator is not allowed in RBScript.
157 The For statement is missing its condition.
158 The While statement is missing its condition.
159 Unsigned integer used in negative step loops can cause an infinite loop.
160 Only objects can be used with the Is operator
161 Only objects can be used with AddHandler and RemoveHandler.
162 The object you are passing to AddHandler does not have the specified event
163 Converting Delegates to Ptrs is not allowed in RbScript.
164 WeakAddressOf can only be used on instance methods.
165 Declares directly into the runtime via Lib "" are no longer allowed.
166 Objective-C declares must have at least one parameter (the target of the message send).
167 The property being declared has a different type than the property it is shadowing.
Personal tools