The text of a manual · part 5 of 18
Qlarity-Based Terminal Programmer's Reference Manual: Chapter 2 Qlarity Language Syntax, pages 19 to 25
Pages 19 to 25 of 134, from Qlarity-Based Terminal Programmer's Reference Manual
The original document is the PDF (1.5 MB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
Filed in the catalog under the QSI Corp QTERM-G70
Page 19
CHAPTER 2 QLARITY LANGUAGE SYNTAX
The syntax of the Qlarity language is based on the BASIC programming language, with extensions to handle objects, allow structured programming, and facilitate the development of user interface applications. When referring to language syntax, the following notations are used: Notation
Description font
Language examples are shown in monospaced font.
[ ]
Any part of a statement in brackets ([ ]) is optional.
<>
A word that appears in angle brackets (< >) must be replaced by an appropriate name, keyword, or value.
'this is another comment Description: Comments help to explain and increase the readability of the code. Comments can be placed on the same line as a statement or they can be placed on their own line. Everything between the “rem” keyword or apostrophe and the end of the line is considered part of the comment and ignored. NOTE: The apostrophe character is also used to denote character literals. For example: 'X '. For this reason, you cannot specify an apostrophe as the second character of a comment. Example: 'I'll fix this tomorrow
Would be an error. This could be alternately written:
2.1 Qlarity Statements A statement is defined as a single line of code. In order for the compiler to distinguish between statements, each statement must be separated by a “newline” character. If a statement must be split into multiple lines, type -> at the end of the line to tell the compiler to look for the rest of the statement on the next line.
REM
I'll fix this tomorrow or ' I'll fix this tomorrow (space after the first apostrophe)
While spaces and tabs are ignored by the compiler, judicious use of white space is encouraged to make programs more readable.
There is another class of comments that may be used to define documentation meta data for an object, property or method. These documentation meta data comments, referred to as AutoDoc, are used to document Qlarity software elements and are described in the Qlarity Foundry User's Manual. Regardless of which type of comment you use, it is ignored by the compiler.
2.3 Comments
2.4 Naming of Identifiers
Syntax:
Reserved keywords may not be used as object, variable, or constant names in Qlarity. Identifier names must begin with any letter A through Z or the underscore character (_). After the initial character, any alphanumeric character is legal, and names may optionally end with a #, $, or % character. Identifier names and keywords in Qlarity are not case sensitive.
2.2 White Space rem <comment> '<comment>
(where'is an apostrophe)
Example: rem this is a comment
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 20
8
QSI Qlarity-based Terminal Programmer’s Reference
2.5 Built-In Data Types
Data Type
Data types are used to tell the compiler how a specific variable or property will be used. A variable must be given a type when it is declared. The following built-in types are supported: Data Type
Description
-2,147,483,648 to 2,147,483,647
A 32-bit floating point number
-3.402823e+38 to 3.402823e+38, smallest positive float is 1.175494351e-38 boolean
True/false
TRUE, FALSE byte
8-bit unsigned value
0 to 255 unibyte
16-bit unsigned value
0 to 65535 string
Array of bytes unistring
Array of unibytes color
Reference to a color integer float
A raster font resource ttfont
A TrueType font resource bitmap
A bitmap image resource objref
Reference to an object anytype
Aggregate%
Array of Bytes
Colormap%
8-bit unsigned value
0 to 255
Stylemap%
32-bit signed whole number
-2,147,483,648 to 2,147,483,647
2.6 User-Defined Data Types In addition to the built-in data types, there are four kinds of user-defined types. Objects are the major forms of userdefined types and are described elsewhere in this manual. Constants, enumerations, and starttypes are other userdefined types. NOTE: User-defined data types must be declared in the global space; they cannot be declared inside of object templates or instances.
COL_0 to COL_255
User defined Items have integer enumerations types (integer valvalues ues) bdffont
Range
Any type is allowed (only allowed in API functions)
Range
A 32-bit signed whole number
Description
2.6.1
Constants
Syntax: constant <name> := <value> [ as <data_type> ] or const <name> := <value> [ as <data_type> ]
Example: const myConstant := 100
Description: With the “constant” keyword, a value of any type can be assigned to a name for use throughout the application. That is, the value and the name are synonymous to the compiler. If desired, the constant can be given a data type. Because it is a constant, its value cannot be changed at runtime. 2.6.2
Enumerations
Syntax: enumerate <enumeration_name> as <name_1> -> [,<name_n>] or
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 21
QSI Qlarity-based Terminal Programmer’s Reference enumerate <enumeration_name> as <name_1> -> := <value_1> [,<name_2> := <value_2>]
NOTE: The keyword “enum” can be substituted for “enumerate.”
2.7 Variables 2.7.1
Declaration
Syntax: dimension <name> as <data_type>
Example: enumerate mysize as small := 1, medium -> := 2, large := 3 enum stooge as larry,curly,moe,shemp dim favorite as stooge or dim <name> as <data_type>
Example: dim myVar as integer dimension myVar as integer
Description: An enumeration allows you to define a data type that has a restricted set of values. Any variable declared with an enumeration type can only hold the values defined in the enumeration. The default value of an enumeration is the first value that it defines. Numeric integer values can also be given to each enumeration item. Comparisons such as “greater than” or “less than” can be used between variables of the enumeration type. In the example above, the variable “favorite” can only be assigned the values “larry,” “curly,” “moe,” and “shemp.” 2.6.3
9
Start Type
Description: Variables that are used must be declared somewhere in the application. A variable must be assigned a type when it is declared. The “dim” keyword is used to declare a variable, as shown above. When a variable is declared, it is automatically initialized to a default value. The “init” keyword is used to initialize a variable to a specific value (see section 2.7.2). Variables declared outside of object definitions and functions are considered global. These variables are accessible anywhere in the application.
Syntax: starttype <name> as <data_type> [<name1> := <value1>] [<name2> := <value2>] [etc.] endtype
Example: starttype control_char as byte STX := 0x02 ETX := 0x03 ACK := 0x06 NAK := 0x15 endtype
Description: “Starttype” is similar to an enumeration. The difference is that the items in a “starttype” must be initialized to a value and can be used in any mathematical operations available to the base type. “Starttypes” can only be defined for booleans, unibytes, bytes, integers, and floats. There is no limit to the number of values that can be placed in a “starttype.” The first value listed is the default.
Variables declared inside an object definition (but outside the object methods) are considered to be properties of the object. Each instance of an object maintains its own object properties. Inside the object definition or instance, a variable is referenced with its name only. Outside the instance or object definition, the property of a given object instance can be referenced using the instance name and the . (dot) operator (as described in section 2.10.4). Variables declared inside a function (either a global function or object methods) are local to that function and are not accessible outside the function. The declaration of each variable need not be stated before it is initialized or used on a global level or in an object definition. This is not true in a function; function variable declarations must appear before any other code in the function. It is possible to declare multiple variables of the same type on one line, as follows:
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 22
10
QSI Qlarity-based Terminal Programmer’s Reference
2.8 Object References
Syntax: dim <name>, <name>, <name>, ... <data_type>
... as ->
Example:
Object references are variables of type “objref.” These variables can refer to an instance of an object. There are two types of object references: typed and untyped.
dim var1, var2, var3, var4 as string
2.7.2
Variable Initialization
When writing Qlarity code, the name of an object may be used wherever a reference to the object is desired. 2.8.1
Syntax:
Untyped Object References init <name> := <value>
Syntax: Examples:
dim <name> as objref dim myVar as integer init myVar := 10
Example: (assume that the object “mycircle” exists)
dim myString as string init myString := "Hello World.\r\n"
Description: The “init” keyword is used to initialize a variable to a specified value at compilation. Variables can be initialized with literal values, other constants, enumerations, or names of object instances (for objref variables). The value given must be of the same type as the variable, and it cannot be an expression containing another variable. Runtime changes to variable values must take place within a function. 2.7.3
Private and Protected Variables
Syntax: private dim <name> as <data_type> protected dim <name> as <data_type> func untypedobjref( ) dim anyobject as objref init anyobject := mycircle [...] endfunc
Description: Variables of this type cannot be used to access object properties or methods, but they can be used anywhere else that an object name or object reference is required. For example, an objref can be passed to the Attach() API function to attach an object to a parent container. Because they are untyped, these variables can refer to an object of any type. It is possible to convert an untyped object reference to a typed object reference by converting the untyped object reference to a string then converting the string to a typed object reference.
Example: private dim hiddenvar as integer
Description: Object property variables can be declared as private or protected. A variable that is declared as private can be used throughout the object template but cannot be referenced outside the template (including in any object instance). A protected variable may be accessed in the object template and in the declaration of an object instance of that template (i.e., in variable initializations and in override functions within the instance). Protected variables may also be accessed by templates that extend (inherit from) the template in which they are defined. No other access is allowed.
func ConvertToButton(untypedObj as objref) dim buttonRef as objref ButtonV2 dim buttonName as string buttonName = str(untypedObj) check error val(buttonRef, buttonName) on error _print("Illegal conversion") rethrow() enderr return buttonRef [...] endfunc
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 23
QSI Qlarity-based Terminal Programmer’s Reference
2.8.2
Typed Object References
11 init <array_name> := [ <val_0>, ... , -> <val_n-1> ]
Syntax: dim <name> as objref <template_name> init <name> := <obj_instance>
Example: func typedobjref( ) dim bmpobjref as objref bitmapobj init bmpobjref := mybitmap bmpobjref.enabled := true bmpobjref.foo(10,20) [...] endfunc
Description: Variables of this type can only refer to instances of the named template. For example, if there is an object template “gauge” and the variable “mygauge” is an objref of “gauge,” then “mygauge” can only refer to gauge objects and to no other types. In addition to the capabilities of an untyped objref, typed objrefs can be used to access properties and methods of the specified object template. Typed objrefs are empty by default. If you use an objref that is empty, a runtime error will occur. Use the keyword “empty” to indicate an empty objref.
Description: Array declaration is similar to variable declaration. The array size is an integer value. Arrays can be declared of any type except string and unistring. A byte array (or string) may be assigned a string of alphanumeric characters within double quotes. An array that is not initialized is automatically initialized to the default value of the data type. An array can be initialized with fewer values than the size of the array. In which case, the remainder of the array is initialized to the default value. The array size is optional. If it is omitted, the array will be sized to the number of elements in the initialization. If the initialization is omitted, the array will remain unsized until it is assigned. Array elements can be accessed using integer indices in the array as follows: <array_name> [ <index> ]
Arrays in Qlarity are zero-based; that is, the index of the first element of an array is 0. Example:
In the example above, the function has a typed objref of type “bitmapobj” that is used to access the enabled property of the bitmap object instance. The “bitmapobj” method “foo” is also called.
dim myArray[11] as byte init myArray := "Hello World" dim anotherArray[4] as integer init anotherArray := [1, 2, 3, 4]
2.8.3
An array can be assigned to another array, as shown in the following example. The array receiving the assignment is automatically resized to the size of the assigned array.
Special Object References
The object reference “me” always refers to the object in which it is used. “Me” is considered a typed object reference. “Default,” when used in the context of an untyped object reference, refers to the root container. When “default” is used in the context of a typed object reference, it is the same as “empty.” The object reference "empty" indicates that no object is referenced by this value. This is similar to a "null" or a "nil" value in other programming languages.
2.9 Arrays Syntax: dim <array_name> [ <array_size> ] as -> <data_type>
Example: func test( ) dim a[2], b[2] as integer. init a := "hi" b = a ' a[ ] is being assigned to b[ ] endfunc
Unlike some languages, arrays that are passed as parameters to a function are not passed by reference unless the array parameter to the function is a reference parameter. Therefore, passing large arrays to functions can be computationally expensive.
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 24
12
QSI Qlarity-based Terminal Programmer’s Reference
2.10 Operators
2.10.3 Assignment Operators
2.10.1 Arithmetic Operators
Operator
Description
:=
Strict assignment operator. This must be used anytime a variable or constant is initialized. No validation is performed on the assignment. In most cases, this operator should not be used to assign properties of other objects because no validation will be performed.
=
Validation assignment operator. This operator is the same as := unless a validation function with the same name as the variable was written. If this is the case, then a validation is performed. Validation functions are discussed later. Generally, this operator should be used except when initializing a variable or assigning a value inside a validation function.
Arithmetic operators can be used with any float, integer, byte or unibyte value. Operator
Description
+
Addition operator
-
Subtraction operator
∗
Multiplication operator
/
Division operator Modulus operator (cannot be used with floats)
mod
2.10.2 Boolean Operators See section 2.12.3 for an explanation of validation methods. Operator
Description not or !
NOT operator1
2.10.4 Dereference Operator and or &
AND operator1
Operator
Description or or |
OR operator1 xor or ^
XOR operator1
.
This operator allows access to object properties and methods from outside the object. The dot is placed between the object name or reference and the property or method that will be accessed.
<=
Less than or equal to2
>=
Greater than or equal to2
<
Less than2
2.10.5 Miscellaneous Operators
>
Greater than2
Line Continuation
==
Equals operator3
Operator
Description
->
This construct is used to continue a statement on the next line if it is too long. This can increase the readability of your code.
<>
3
Not equal to
1. Can be used with variables, literals, or constants of the following types: boolean, byte, unibyte. 2. Can be used with variables, literals, or constants of the following types: float, integer, byte, unibyte, and arrays of those types. A boolean expression is generated. 3. Can be used with variables, literals, constants, or arrays of any type. A boolean expression is generated.
Strings (enclosed in double or single quotes; e.g., “Hello world”) Operator
Description
\n
Newline character
\r
Carriage return
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770
Page 25
QSI Qlarity-based Terminal Programmer’s Reference
Operator
Description
\xDD
Represents a hexadecimal value where each “D” is 0 through 9 or A through F. You must use two hexadecimal digits; for example, \x00 not \x0, \x0A not \xA, and so on.
\\
Backslash character
\"
Quotation marks
\'
Apostrophe
2.11 Casting It is possible to convert a value from one numeric data type to another. While Qlarity will automatically convert data types when necessary, it is occasionally useful to manually cast a value to a new data type. You can cast values to the following built-in data types: integer float byte unibyte Additionally, you can cast the keyword “default” to type “objref.” To cast a value, type the data type to which you want to cast, and then enter the value to cast in parentheses. You can use the casting operators as part of any legal expression. Example:
'Calculate the sum of two simulated dice die1 = integer(6∗ GetRandomNum()) + 1 die2 = integer(6∗ GetRandomNum()) + 1 diceTotal = die1 + die2 Casting is commonly used to allow integer literals to be used with bitwise operators such as the AND operator:
'Calculate the red, green, and blue -> components for a given color. 'The resultant values range from 0 to 7 rValue = (myColor and byte(0xE0)) / 32 gValue = (myColor and byte(0x1C)) / 4 bValue = myColor and byte(0x03) ∗ 2 Casting a value may cause a loss of precision if the target data type is incapable of expressing the original value. For example, the expression “integer(6.7)” produces the value 6
13
(since integers cannot express fractional values), and the expression “byte(257)” produces the value 1 (since bytes can only express values from 0 to 255). You may cast the keyword “default” to any of the allowed casting data types. The default value for integer, float, byte, and unibyte is zero (0). You may also cast “default” to an “objref.” The value obtained from this cast is the root or global container. This is useful for certain comparisons; for example: if parent == objref(default) then transmit (com1, str(me) + -> "is attached to ROOT.\n", false) endif
2.12 Functions Syntax: func <name> ([<var> as <data_type>, ...]) -> [returns <data_type>] [handles <msg_name>] [Variable declarations and -> initializations] [statements...] endfunc
Example: func circ_area(x as float) returns float dim pi as integer init pi := 3.1415927 return x∗ x∗ pi endfunc
Description: A function can contain any number of input parameters (separated by commas) and statements. If the function handles any event messages, the “handles” statement is used. The “handles” statement must be the first statement in the function. The “dim” and “init” statements must be the next statements in a function. The actual order of “dim” and “init” statements is unimportant. When a message is received by an object, the function that handles the message is called. The “handles” statement specifies the message type that must be sent for the function to be called. There can be more than one “handles” message in a function as long as they appear before any other declaration or statement. See Chapter 3, “Messages and Message Handler Prototypes” for a complete listing of messages.
QSI Corporation Fax 801-466-8792 Web www.qsicorp.com Phone 801-466-8770