Morfik Pascal Overview

This chapter will provide you will a general overview of Morfik Pascal. It is presumed that the user already has some familiarity with some dialect of Pascal, such as Delphi, Virtual Pascal, FreePascal, 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 Pascal.

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


Morfik Pascal Modules

Morfik Pascal has the concept of Modules, which is present in a weaker form in most versions of Visual Pascal. 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 Pascal a formal code construct defines the module, for each side: browser and server. This construct, is called a unit. The simplest module that is possible to create with Morfik Pascal can be seen in Listing 1.

Listing 1 – Empty module in Morfik Pascal.

Unit Globals;
 
Interface
  // This is an empty module
Implementation
 
End.

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 Pascal through the Uses clause as shown in Listing 2.

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

Unit Globals;
 
Interface
 
Uses SystemUtilities;   // This line imports module SystemUtilities
  // This is an empty module
Implementation
  //...
end.


One Uses clause is used to declare all the modules you wish to import in the interface section of a module, and another in the implementation section.

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 imports 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 Pascal 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.

Unit Globals;
 
Uses LogMod;
 
Interface
 
Implementation
  // This is a module with Initialization and Finalization Sections
Initialization
    InitLogFile;
 
Finalization
    FlushLogFile;
 
End.

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.

Note: We recommend that the user accept the default behavior implemented by Morfik which makes all Forms dynamically loadable and all pure code Modules statically loaded - typically these settings should not be altered manually.

Adding Comments

Morfik Pascal offers three different ways of creating comments:

// This a comment
{ This is another comment which can 
  span multiple lines }
(* This is yet another comment 
   which can span multiple lines*)

There are basically two types of comments in Morfik Pascal: end of line and multiple line. End of line comments render all following text on the same line into comments. Multi line comments render everything between the start and stop comment strings into a comment.

Note: It is best to create small comments within your code using the one line comments (to end of line) so that if you decide to comment out a section of a routine, you can do it with 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 by 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 normal comments that allows the developer’s comments to be integrated with the Morfik's code completion system. These special comments which uses its own separate compiler, MorfikDoc comments are processed by the Morfik high-level language compilers as part of the Morfik 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 Procedure
*)
Procedure Test;
Begin
// ...
End;

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
 *)
Procedure Test;
Begin
// ...
End;

Variables

Usually you declare variables at the start of a procedure through the use of the Var statement. This statement is a short version of the word Variable that has been in use in Pascal since its very beginning.

Listing 6 – var statement for variable declaration

Procedure Test;
Var
  X : Integer;
  Y : double;
Begin
    X := 2;
    X := X+1;
    Y := X/5;
End;

Initialized Variables

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

 Var index : integer = 10;

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

Note: The 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 in your Delphi configuration.

Listing 7 – Procedure with initialized variables.

Procedure Test;
Var
   x : Integer = 20;
   y : Integer = 30;
Begin
   //...
End;

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 Pascal 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:
function thisfunctiongetsabithardtoread: integer;
function ThisFunctionIsMuchEasierToRead: integer;

Var
thisisavariablewithalongname: string;
ThisIsAVariableWithALongName: string;

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 a constant declaration:

    Const 
      myConst = 10;
      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.

In Morfik Pascal you can declare multiple constants under a single const clause as shown in the previous example.

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:

    Const 
      MyInitVar: Integer = 20;
      MyDoubleVar: Double = 2.15;


Notice that typed constants are declared with a formal type and function as initialized variables of that type.

Note: The Morfik Pascal 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 Pascal 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 Pascal operators and how they are used.

Assignment Operator

The Assignment Operator allows you to assign new values to variables or object properties. Morfik Pascal utilizes the ’:=’ 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 Pascal uses as the equality operator the ’=’ (equals) sign when comparing values as seen in the following example:

 If (X = Y)  Then

Inequality Operator

Morfik Pascal 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 definitions of other comparison operators used in Morfik Pascal.


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 Pascal 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.
div 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:

    Const Str = 'Hello';
    Var
      MyStr: String;
      MyStr2: 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.

All flow control structures (both for branching and loops) in Morfik Pascal can be used with a single statement or a statement block (code between a begin..end pair).

Branching

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

If .. Then .. Else

The "IF" construct in most languages allows for choosing among two execution paths based on a Boolean expression. In Morfik Pascal, however, due to the possibility of nesting 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..Then..Else construct.

  If (((x + y) > 10) And (Not ((x - y) > 0))) Then
    z := x / y
  Else
    If (X > Y) Then
      Y := Z * X
    Else
      If (Y = Z) Then
        Z := 25
      Else
        x := y / z;


If you place a ";" before the Else clause, the compiler will generate an error.


Note: It is important to remember that the Else clause is a part of the "IF" statement and should therefore never be preceded by a semi-colon (";").

Case .. Else .. End

The Case statement is used for conditional execution of code based on a list of values. The Case statement can have an optional 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.

  Case z of
    1:
      z = z + 2;
    2:
      y = x + 5;
    Else
      If z > (x + y) Then x := y + 5;
  End;

Loops

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

While .. do

The While statement in Morfik Pascal 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 do
    Delete(StringToConsume, 1, 1);


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.

Repeat .. Until

The Repeat..Until loop structure has its condition set at 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. This means that even if the condition is false at the start of the block the code will be executed once, regardless.

The main difference between using the While..do or Repeat..Until constructs is that when you use While..do, the contained code will be execute as long as the condition is true and when you use Repeat..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 Repeat..Until construct.

Listing 10 – Do Loop usage Examples.

     i := XApp.OpenForms.Count1;
     Repeat
       frm := XApp.OpenForms.Items[i];
       ShowMessage(frm.InstanceName);
       i := i – 1;
     Until i < 0;

For .. Do

When we have previous knowledge about the exact number of times a certain code block should be executed, we can use the For do statement in one of its variations (ascending or descending). The execution of the contained code of a For do 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 do loop you should change the "to" keyword for the "downTo" keyword, inverting the maximum and minimum value order. The code snippet in Listing 11 shows examples of both ascending and descending For .. Next loops.

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

  If Descending Then
    For i := Max DownTo Min Do //descending loop
      ShowMessage(IntToStr(i))
  Else
    For i := Min To Max Do     //ascending loop
      ShowMessage(IntToStr(i))

Conditional Compilation

Morfik Pascal supports conditional compilation of code sections based on constants defined (through the $Define directive) at compile time. This is useful to include or exclude certain code sections for debug purposes, for example.

The following is a code snippet which exemplifies the usage of the $IfDef..$Else $EndIf conditional compilation directives.

  {$Define Aardvark}
  {$IfDef Aardvark}
    ShowMessage('you will see this!');
  {$Else}
     ShowMessage('you won''t see this!');
  {$EndIf}

Wrapping it up

Morfik Pascal is a full featured, powerful implementation of the Object Pascal language. In the next chapters you will see more details on certain aspects of the Morfik Pascal language such as its data types, sub routine and class construction.


Related Topics

See Also

Back to top