Using variables


Dim statement

<Dim statement> ::= <Dim-token> <type-desc-list>

Every variable that will be used within a HBasic program should be declared before it will be referenced. In this way HBasic knows the type of the variable and can create better access functions to the variable memory. A Dim statement simply lists the name and type of the variable. The first token in a Dim statement may be Dim, Public or Private.

Range of variables

If you declare a variable within a Sub .. End Sub structure the range of the variable will be the body of the sub statement. If you declare a variable in the sourcecode of a for definition the range of the variable will be the sourcecode of the form which means it may be used within all Subroutine definitions of the sourcecode of the appropriate form. If you replace the token Dim by Public for a variable declaration outside of a function body the variable will be global which means it may be referenced everywhere in the sourcecode. A variable definition within a global sourcecode will always define a global variable.

Example:
Dim i As Integer
declares a variable with name i and type Integer.


<Dim-token>
::= Dim | Public | Private

<type-desc-list> ::= <variable-declaration> { , <variable-declaration> }

<variable-declaration> ::= <identifier> { , <identifier> } As <typedesc>

<typedesc> ::= <simpletype> |
<arraytype> |
<componenttype> |
<classtype> |
<usertype>

<simpletype> ::= integer | double | string | boolean | short | object
More types like byte, char, long (64 bit integer), float (4 byte value), xdouble (16 byte float) and unsigned integer types may follow later. The 1.0 version of HBasic should at least support integer, double, string and object variables. Object variables may store values of any other type (like variant-variables within other BASIC interpreter).

<arraytype> ::= identifier [ dimension-value , [dimension-value]] As simpletype

Example:
Dim a[4,8] As Integer

Example: arr_access.bas

<component-type> ::= identifier

identifier is the name of a component that has been loaded with a HBasic package or the name of a Qt class if you have loaded Qtc support to your project.

<classtype> ::= identifier

identifier is the name of a class that has been defined within the HBasic program somewhere else.

<usertype> ::= identifier

Identifier is the name of a structure that has been defined within a <struct-desc>.



Structure definition

You may define your own structures which may be used later within a typelist of a Dim statement.

<struct-desc> ::=
   Type identifier
       <variable-declaration>
   End Type




Assign statement

The assign statement is the typical way to change the value of a variable in a program. The variable name should normally have been declared within a Dim statement before it will be used. Otherwise HBasic will create a new variable of type object.

<assign-statement> ::= identifier = <expression>

<expression> ::= <factor> [ <operator> <expression> ]  |
NOT <expression>
( <expression> )

<operator> ::= + | - | * | / | AND | OR | XOR

<factor> ::= <constant> |
    <varname> |
    <function-call>  |
    <struct-access> |
    <class-access> |
    <component-access>

<constant> ::= <integer-constant> |
    <double-constant> |
    <string-constant> |
    <component-constant-identifier>

<struct-access> ::= struct-idf . entry-name

<class-access> ::= identifier . class-property |
    identifier . class-method ( <parameterlist> )

<component-access>  ::= identifier . component-property |
    identifier . component-method ( <parameter-list> )

<varname> ::= identifier

<identifier> ::= letter | '_' | number

<number> ::= 0123456789
<letter> ::= a..z | A..Z