Morfik Basic Overview

This chapter will provide you will a general overview of Morfik Basic. It is presumed that the user already has some familiarity with some dialect of Basic, such as Visual Basic (VB), Access Basic, Visual Basic for Applications (VBA), etc.

Our focus will be on providing a quick reference for most language features and a more detailed explanation for those features which are unique to Morfik Basic.

Through the evolution of its usage in the industry, Basic has undergone a huge amount of change. The most popularly know dialect of the Basic Language is, without doubt, Microsoft's Visual Basic so most of the time when we refer to Basic (not Morfik Basic) we will be referring to Microsoft's dialect of the Basic language.

Morfik Basic Modules

Morfik Basic has the concept of Modules, which is present in a weaker form in most versions of Visual Basic. A Module in Morfik can have three forms, a browser side module, a server side module or a dual module. In all three cases the module is represented by a single .mmd file.

While, traditionally, the file was the only defining factor of the existence of module, in Morfik Basic a formal code construct defines the module, for each side: browser and server. This construct, is called a namespace. The simplest module that is possible to create with Morfik Basic can be seen in Listing 1.

Listing 1 – Empty module in Morfik Basic.

Namespace Globals
  ' This is an empty module
End Namespace

Module Duality – Browser and Server

In Morfik applications modules can be Browser side, Server side or dual. Dual modules are both Browser and Server at the same time. The Morfik IDE will show you a tabbed view of the module allowing you to edit the part of the module that you want: Browser or Server.

Referencing Modules

In order to access the functions, variables and types which are declared in other Morfik Modules it is necessary to import them. This is done in Basic through the Import clause as shown in Listing 2.

Listing 2 – Empty module in Morfik Basic, importing SystemUtilities.

Imports SystemUtilities   ' This line imports module SystemUtilities
Namespace Globals
  ' This is an empty module
End Namespace

One Import clause should be used per module you wish to import.

It is important to have in mind that referencing a module in the Browser side of another module will not make the server side of this second module import the same module’s server side counterpart. If this is required, it must be done manually.

Module Initialization and Finalization

Morfik modules have Initialization and Finalization sections. In Basic these are implemented through two named code blocks. These blocks are called Initialization and Finalization and have a special syntax as shown in Listing 3.

Listing 3 – Module with Initialization and Finalization.

Imports LogMod
 
Namespace Globals
  ' This is a module with Initialization and Finalization Sections
Initialization
    InitLogFile
End Initialization
 
Finalization
    FlushLogFile
End Finalization
 
End Namespace

The code contained in the Initialization section is called when your XApp is first loaded, while the code in the Finalization is called when your application is terminated. The Finalization section is only effective on the server where you can be sure that your application will actually get finalized. While syntactically correct, a Finalization section on the browser side will never be called. On the server side of the application, however, the Finalization section will always be called on application shutdown.

It is also important to note that modules that are not statically bound on the browser side will have their Initialization sections called when they are first loaded and not at the initial page load. For more information on statically binding a module, refer to the Compiler Directives section of this chapter.

Adding Comments

Morfik Basic offers three different ways of creating comments:

REM This a comment
 
' This is another comment
 
/* This is yet another comment 
  Which can span multiple lines*/

Note that though most commonly used dialects of Basic do not have multi-line comments, we have added these to Morfik Basic. This was done to allow for a cleaner and more precise implementation of automatic code translation between Morfik Syntaxes and for a simpler way of creating MorfikDoc comments in Basic.


Note: It is best to create small comments within your code using the one line comments such as the single quote so that if you decide to comment out a section of a routine, you can do it with the multi-line comments. A sample of this can be seen below:
/* I’m commenting this section out for debugging purposes
Label1.Caption = "A caption..." ’Changes the caption
*/
In this case, the multi-line comment is not affected in any way for including a line with a single line comment.

MorfikDoc Comments

Morfik, taking its cue from work done for the Java platform, created an extension to the normal comments that allows the developer’s comments to be integrated with the IDE’s code completion system. These special comments are a partial implementation of the JavaDoc syntax, but unlike JavaDoc, which uses its own separate compiler, MorfikDoc comments are processed by Morfik high-level language compilers as part of the background compilation process and are thus, immediately available within the IDE’s code completion system.

Listing 4 – MorfikDoc comment sample

/**
  This is a morfikdoc comment for Test Sub
*/
Public Sub Test
' ...
End Sub

The element that the compiler uses to identify a comment as a documentation comment is the second asterisk( * ) used in the opening of the comment. Any asterisks used in the start of any lines of MorfikDoc comments will be stripped out, so that the code in Listing 5 will have the same effect as that in Listing 4.

Listing 5 – MorfikDoc comment sample with extra asterisk

/**
 * This is a morfikdoc comment for Test Sub
 */
Public Sub Test
' ...
End Sub

Variables

Usually you declare variables at the start of a sub routine through the use of the Dim statement. This statement a short version of the word Dimension has been in use in Basic, since its very beginning.

Listing 6 – Dim statement for variable declaration

Public Sub Test
  Dim X As Integer
  Dim Y As Double
  X = 2
  X = X+1
  Y = X/5
End Sub

Initialized Variables

Variables can be pre-initialized at declaration. This is done by a simple attribution in the Dim statement as in the following samples:

Dim  index As integer = 10
private Dim  index As Integer = 10

You can declare initialized variables also within sub routines as shown in Listing 7.

Note: Note that initialized variables cannot be used at the local scope level if you have chosen Delphi as your platform backend compiler. In this case you could use Typed Constants, as described later in this chapter, assuming you have Assignable typed constants turned on.

Listing 7 – Sub routine with initialized variables.

Public Sub Test
        Dim x As Integer = 20
        Dim y As Integer = 30
   '...
End Sub

You can also have the equivalent to initialized variables through the use of a new language feature called Typed Constants.


Note: It is important to note that Morfik Basic is case insensitive and that you cannot, therefore declare two objects at the same scope level, with the same name. Because of this, it is common practice to write code using "Proper" cased identifiers since in some cases they can make your code a lot easier to read. You can see this in the following examples:
public function thisfunctiongetsabithardtoread() as integer
public function ThisFunctionIsMuchEasierToRead() as integer

Dim thisisavariablewithalongname as string
Dim ThisIsAVariableWithALongName as string

Constants

Just as they have variables, most applications have constants. Constants are normally used to hold literal values that are used in several different places within an application. Here is a small sample of constant declaration:

Public Const myConst = 10
Public Const Str = "Hello"

This sample shows the declaration of numeric and string literal constants. Constants declared in this form cannot have their values changed at runtime.

Typed Constants

Typed constants are a bit different from what you might expect, considering that they are constant only in name. Typed constants are declared in the following form:

Public Const MyInitVar As''' Integer = 20
Private Const MyDouble Var As Double = 2.15

Notice that typed constants, as the first part of the name implies, are declared with a formal type and function, basically, as initialized variables of that type.

Note: The Morfik Basic compiler does not generate an error if you try to assign a value to a non-typed constant, however, at compile time the backend platform compiler will generate an error.

Operators

Operators are symbols within your Morfik Basic code which allow you to compare or perform logical or arithmetic operations on all data types. There are operators to add, subtract, multiply, divide, etc.

In this section we will see the Morfik Basic operators and how they are used.

Assignment Operator

The Assignment Operator allows you to assign new values to variables or object properties. Morfik Basic utilizes the ’=’ (equals) sign as the symbol for this operator. The following example means that the variable called Variable1 is assigned the value of 5.

 Variable1 = 5

Equality Operator

Morfik Basic uses as the equality operator the same symbol as for the assignment operator, inferring its meaning by the context within which it is used. This means that the same ’=’ (equals) sign is used when comparing values for equality as seen in the following example:

 If (X = Y) Then

Inequality Operator

Morfik Basic uses the pair ’<>’ (Less than and Greater than) of signs to represent the Inequality operator. You can see the usage of this operator in the following example:

 If (A <> B) Then

Logical Operators: AND, OR, NOT

These operators are used for logical combinations in comparisons. The following are examples of their usage:

If (X = Y) And (A <> B) Then
 
If (X = Y) or (A = B) Then
 
If (X <> Y) And (Not (A <> B)) Then
Note: Through the usage of parenthesis you can combine several different logical operations in one more complex operation as seen in the last of the previous examples.

Other Comparison Operators

The following is a list of other comparison operators used in Morfik Basic and of their respective definitions.


Operator Definition
< Less than. Yields a true value when the element to the left side holds a value which is "smaller" than the value held by the element to the right side.
> Greater than. Yields a true value when the element to the left side holds a value which is "larger" than the value held by the element to the right side.
<= Less than or equal. Yields a true value when the element to the left side holds a value which is "smaller" or equal to the value held by the element on the right side.
>= Greater than or equal. Yields a true value when the element to the left side holds a value which is "larger" or equal to the value held by the element to the right side.

Arithmetic Operators

Morfik Basic has a full set of arithmetic operators which should be reasonably familiar to anyone with minimal programming experience. The following is a full list of the arithmetic operators.


Operator Definition
+ Addition operator, used on all numeric data types.
- Subtraction operator used on all numeric data types.
*
Multiplication operator used on all numeric data types.
/ Division operator used on floating point numeric data types.
\ Integer Division operator used on integer numeric data types.
mod Remainder of division operator. This operator yields the value of the remainder of the integer division of the term to the left by the term on the right side.

String

String is a data type which is used to represent groups of characters. The following are examples of string variables and constants:

    Public Const Str = "Hello"
 
    Private Dim MyStr As String
    Private Dim MyStr2 As String = "Hello!"

More details on string can be found in the Data Types Chapter of this guide.

Application Flow Control

All programming languages share some constructs which are specific for application flow control. These structures are mainly divided into two groups: Branching and Loops.


Branching

The Morfik Basic language offers two constructs specific for branching control. These constructs have equivalents in most programming languages and are quite straightforward in usage.

If … Then … ElseIf … Else … End If

The "IF" construct in most languages allows for choosing among two execution paths based on a Boolean expression. In Morfik Basic, however, due to the existence of the ElseIf clause in the "IF" statement you can actually branch into any number of different paths. The code snippet in Listing 8 shows an example of using the "IF" statement with several possible execution paths.

Listing 8 – Multiple paths with the "If" statement.

  If (((x + y) > 10) and (not ((x - y) > 0))) Then
    z = x / y
  ElseIf (X > Y) Then
    Y = Z * X
  ElseIf (Y = Z) Then
    Z = 25
  Else
    x = y / z
  End if

If you place the Else clause before an ElseIf clause, the compiler will indicate an error.


Note: When writing an extensive "IF" statement it is important to keep in mind that the Else clause should always be the last one, remaining as the final option if no other condition was met.

Select Case .. Case .. Case Else .. End Select

The Select Case statement is used for conditional execution of code based on a list of values. The Select Case statement can have an optional Case Else clause whose dependent code will be executed if the value of the condition did not match any of the Case values. The code snippet in listing 9 shows an example of the usage of this language construct.

Listing 9 – Code snippet showing the usage of Select Case.

  Select Case z
    Case 1
      z = z + 2
    Case 2
      y = x + 5
    Case Else
      if z > (x + y) then x = y + 5
  End Select

Loops

Loop constructs handle repetitions in the execution of certain code blocks within an application.

While … End While

The While statement in Morfik Basic is very much straightforward in its utilization. While the condition specified is true, the contained code block will be executed. The following code snippet shows an example of its usage.

  While Length(StringToConsume) > 0
    Delete(StringToConsume, 1, 1)
  End While
Note: In executing a While statement, the condition is always verified before the first execution of the code block contained by the statement. This means that it is possible that the contained code will never execute, if the condition starts out as false.

Do (While|Until| )… Loop (While|Until )

The Do Loop statement is quite flexible and can have the condition expressed at the top or the bottom of the code section it contains. Having the condition at the bottom of the contained section means that the contained code will be executed at least once, since the condition will only be checked afterwards. If the condition is at the top of the contained section there is a possibility that the contained code will never be executed.

The main difference between using the While or Until clauses in the condition of your Do Loop statement is that when you use While, the contained code will be execute as long as the condition is true and when you use Until the code will be executed until that condition becomes true. The code snippet in Listing 10 shows examples of the possible usages of the Do Loop statement.

Listing 10 – Do Loop usage Examples.

  If StringsEqual(LoopKind, "DoUntilLoop") Then
    i = XApp.OpenForms.Count - 1
    Do Until i < 0
      f = XApp.OpenForms.Items(i)
      ShowMessage(f.InstanceName)
      i = i - 1
    Loop
  ElseIf StringsEqual(LoopKind, "DoWhileLoop") Then
    i = XApp.OpenForms.Count - 1
    Do While i >= 0
      f = XApp.OpenForms.Items(i)
      ShowMessage(f.InstanceName)
      i = i - 1
    Loop
  ElseIf StringsEqual(LoopKind, "DoLoopWhile") Then
    i = XApp.OpenForms.Count - 1
    Do
      f = XApp.OpenForms.Items(i)
      ShowMessage(f.InstanceName)
      i = i - 1
    Loop while i >= 0
  ElseIf StringsEqual(LoopKind, "DoLoopUntil") Then
     i = XApp.OpenForms.Count - 1
     Do
       f = XApp.OpenForms.Items(i)
       ShowMessage(f.InstanceName)
       i = i - 1
     Loop until i < 0
  End If
Note: There is essentially no difference between using a While..End While statement or a Do While..Loop statement. The choice of which construct to use is up to the developers personal preferences and does not influence how the code is executed.

For … Next … Step

When we have previous knowledge about the exact number of times a certain code block should be executed, we can use the For Next statement in one of its variations (ascending or descending). The execution of the contained code of a For Next statement is done in the following order:

1. The value of the control variable is checked against the end value.

2. The contained code is executed

3. The control variable is incremented or decremented (depending on whether it is an ascending or descending loop).

4. Return to step 1.

In order to have a descending For Next loop you should add the optional Step clause. The code snippet in Listing 11 shows examples of both ascending and descending For .. Next loops.

Listing 11 – Examples of the usage of For .. Next loops.

  If Descending Then
    For i = Max To Min step -1 'descending Loop
      ShowMessage(IntToStr(i))
    Next i
  Else
    For i = Min to Max
      ShowMessage(IntToStr(i)) 'ascending Loop
    Next i
  End If

Conditional Compilation

Morfik Basic supports conditional compilation of code sections based on the values of specified constants at compile time. This is useful to include or exclude certain code sections for debug purposes, for example.

In the following code snippet you can see an example of the usage of the #If..#Else #End If conditional compilation directives.

  #Const Aardvark = True
  #If Aardvark Then
    ShowMessage("you will see this!")
  #Else
     ShowMessage("you won't see this!")
  #End If

Wrapping it up

Morfik Basic included several new language features which makes it quite a bit more powerful than most existing implementations of the Basic language.

In the next chapters you will see more details on certain aspects of the Morfik Basic language such as its data types, sub routine and class construction.


Related Topics

See Also

Back to top