MPL Documentation

Modern Pascal Introduction

Modern Pascal is a term that conveys an importance in distinguishing the evolution of the Pascal programming language. Originally in the late 60's and 70's there was "Pascal", a great language for education purposes. It was invented and marketed as an education tool, so the early 2nd generation and 3rd generation computer industry could acquire more interested persons in developing software for the mini-mainframes. By the mid-80's, the language did it's first major leap in evolution - to incorporate support for OOP (Object Oriented Programming), first by Apple with Object Pascal and then 3 years later by Borland. This era one compiler company was also making major strides in compilation time, Borland International, Ltd. with their Turbo Pascal compiler. Borland in the mid-90's introduced the next evolution by introducing the Delphi variance of the language.

Delphi v1.0 introduce a major development milestone with a completely GUI IDE environment. Delphi did not introduce Windows development to Pascal, that was available in BP7 for DOS, and later TPW (Turbo Pascal for Windows). Delphi was a roaring success across the software development industry and truly made a name for Borland and Delphi's lead engineer Anders Hejlsberg (now famed for authoring C# and truescript also). Delphi is still actively growing and evolving, however, for many of us the price - roughly spending basically $1000 a year for the new improvements each year is why we found other solutions, for me, I started developing MPC (Modern Pascal Compiler) in March of 2000.

Modern Pascal went through 10 years of development before it was even considered more than a script engine. The early versions were developed using Delphi to compile it. However, this restricted my focus to the Windows platform only, and by 2000, I was extremely active in Linux as a server and desktop solution. I migrated the code to FPK (now FPC, Free Pascal Compiler) as I could continue my development for Windows and Linux using a single compiler. In 2005, I was introduced to the Raspberry Pi, and found FPC was available for the Pi, and I ported MPC to work with Linux and Windows CE on the ARM CPU. By 2005/2006 I started tinkering on 64bit machine also - and some of the features I needed required me to either write C to Pascal translation code or to expand MPC's lexicon to support features from more modern languages. This is when the name Modern Pascal was born - I didn't want my product to be "another language", instead it should be recognized as a different evolution in the Pascal language.

As of current, Modern Pascal has all of the modern features you would expect, classes, units, variants, cross-platform. It provides you with JIT (Just-in-time compilation) support so you can make a change and go, without the overhead of manual compilation. It is very type safe, like the original goal of Pascal. It is still a high-level language, however, capable of low-level solutions like developing your own Operating System (we ported an edition of our product as an OSDev bare OS kernel), and through the TORO Kernel project, we have built a 64bit Modern Pascal Game Server OS that is self-healing and supports automated patches.

Modern Pascal as a language has a lot of extensions besides support for some C/JavaScript grammar and functions. Modern Pascal introduces Smart Records, a hybrid of OOP meets Turbo Pascal Records. I personally feel this is a major improvement when I work with projects that make everything into Objects and lazy Interface classes. I recently ported a file system written using Interfaces - the original code was pretty fast but bloated and limited to Windows, but, our redesign made it the core file layer for my SQL Server product (Premier SQL - Supporting multi-threaded write and read I/O streams).

Modern Pascal Products

Modern Pascal is available in 4 primary areas of development.
  1. Server-side Scripting
    This was the original focus of Modern Pascal, allowing you to extend your web servers capabilities through scripting. The server-side engine is an Apache Module, giving it the tightest integration to the Apache Web Server. This module, is available in embedded or enterprise server. Embedded runs the script as if Modern Pascal was built-in to Apache, whereas, the Enterprise edition communicates to the Celerity script engine using an XML protocol. This design was inspired by ColdFusion (originally by Allaire), allowing your web server hardware to be inexpensive, and focus on accepting connections and returning data. While all business intelligence operates on a more robust server which can optionally cluster to other Celerity servers in the same or different data centers. The Enterprise Edition has been managing a 16 data center cluster for one of the most popular 24 video chat apps.
  2. Command line Scripting
    You can make operating system utilities which do not need a web server or web browser to operate, nor a GUI desktop interface. While the Linux CLI (Command Line Interface) allows you to do extremely complex functionality, where Windows does not, Using Modern Pascal's mp CLI app, you can write command line applications, background disk utilities, etc. We have even ported Technojock's Turbo Toolkit v5.0 to native MP plug-in
  3. Desktop Applications
    Even though this was not a primary focus of Modern Pascal, you can extend the language with specially designed units (DLL's) that extend the grammar to interact with the operating system's GUI engine. We are currently evaluating if we should embed GTK2 support for cross-platform native GUI via our DLL units design. We actually use the next solution for our desktop projects - as it allows for cross-platform support without direct access to the native host's GUI engine.
  4. Standalone Server Scripting
    For Desktop applications, you can use client technology like TideSDK, WebKit, etc. to make a front-end that communicates to the localhost socket layer. The plus of this approach is code written for one operating system usually works with minimal to no code changes on all other supported operating systems. Our Standalone Server Scripting goes by the codename CodeRunner was designed to build larger server applications. CodeRunner handles all communications using my DXSock Server SDK Suite, threading, and scripting transparently to you, your scripts are called after the connection is established. Allowing you to quickly develop solutions like EMail, Web, Chat and even Game Servers. In 2016, CodeRunner was redesigned to support multiple ports and multiple scripts, making it perfect for all-in-one server development.
Modern Pascal follows the common structure of other Pascal dialects, making it perfect to run existing Turbo Pascal, Apple Pascal, Object Pascal, Turbo Pascal, even old Delphi 7 and Free Pascal code. The Pascal language is a procedural programming language, designed in 1968 and published in 1970 by Niklaus Wirth and named in honour of the French mathematician and philosopher Blaise Pascal. Pascal compilers are available for a variety of platforms, such as Windows, Mac OS, and various versions of UNIX/Linux. Same applies to our Modern Pascal products, the Compiler and Interpreter, Web Plug-in and Standalone script server are available for Windows, Mac OS X, and various versions of UNIX/Linux.

This tutorial will give you great understanding of Pascal to proceed with Modern Pascal and other related frameworks etc. Modern Pascal itself supports a hybrid syntax, while staying true to Pascal, we have extended the grammar to support many C operators, C data types, etc.


Screenshot: Mac OS X

Audience

This tutorial is designed for Software Professionals who are willing to learn Pascal Programming Language in simple and easy steps. This tutorial will give you the understanding of Modern Pascal Programming concepts, and after completing this tutorial, you will be at an intermediate level of expertise from where you can take yourself to higher level of expertise.

Prerequisites

Before proceeding with this tutorial you should have an understanding of software basic concepts like: what is source code, a compiler, text editor and execution of programs, etc. If you already have an understanding on any other computer programming language, then it will be an added advantage to proceed.

Overview

Pascal is a general-purpose, high-level language that was developed for teaching programming as a systematic discipline and to develop reliable and efficient programs. Pascal offers several data types and programming structures. It is easy to understand and maintain Pascal programs.

Pascal has grown in popularity in the teaching and academia arena for various reasons:

Features of the Pascal Language

Pascal has the following features:
How Pascal is different from other languages:
Before we study the language, let us look at the bare minimum Pascal structure so we can take it as a reference in the upcoming language tutorials.

Pascal Program Structure

A Pascal program basically consists of the following parts (many are optional):
Every Pascal program generally has a header section, a declaration and an execution part structure in that order. Following format shows the basic syntax for a Modern Pascal program:
program HelloWorld;
uses display;

(* Here the main program block starts *)
begin
   WriteLn('Hello, World!'); { comment about this command }
   Readkey;
end.

Source Code Parts...

Looking at the above program source code:
To get a better understanding we will detail each of the basic building blocks of the pascal programming language.

Type Declarations

Type declarations provide you with the ability to extend or expand the variable types known by Modern Pascal. For example, a collection of characters is called a string, however, if you wanted to limit it to 20 characters maximum you could introduce a new string type of String[20]. When an identifier occurs on the left side of a type declaration, it's declared as a type identifier for the block in which the type declaration occurs.Type declarations are normally defined at the beginning of your code with the keyword type, followed by definitions of the variable types as follows:
type type_name1=existing_definition;  // "rename" a type for your better understanding
   type_name2=new_definition;         // add a new type definition to Modern Pascal
   type_name3=array_structure;        // simplify interaction with an array type
   type_name4=record_structure;       // introduce a type of multiple variables
   type_name6=class_structure;        // introduce a type with associated methods

Variables

Variables hold data which can be changed during the operation of your program. Variable definitions are normally defined at the beginning of your code with a var keyword, followed by the type of the variable. When you declare a variable, you must state its type. A variable's type circumscribes the set of values it can have and the operations that can be performed on it.
var variable1:variable_type;
   variable2:variable_type;
   variable3, variable4:variable_type;
For more information on primitive data types, you can click here and jump to the next chapter. The opposite of Variables are constants, Modern Pascal introduces a list of constants - some for the language, some for the platform.

Functions and Procedures

In Pascal, a Procedure is a set of instructions to be executed that do not return a value, and a Function is a procedure which returns a value. The code between the last begin and end in a program drives the logic of the program. In a very simple program, this section of code might be all you need. In larger, more complex programs, putting all your code here can make your program harder to read and understand-and more difficult to develop.

Procedures and functions let you divide the logic of a program into smaller, more manageable chunks, and are similar to subroutines in some other languages. All the action in a procedure or function occurs in the code between its begin and end just like in the main program block. Each of these segments of code performs a small, discrete task.

If you find your program does the same thing many times, you might want to put the logic into a procedure or function. You write the code in a procedure or function once and your program can use it as often as necessary. The definition of each follows:
Procedure Proc_Name(optional_parameters...);

Function Func_Name(optional_parameters...):Return_Value_Type;

Comments

Pascal is the only language which supports 3 different styles of comments. As we have discussed and shown on the Program Structure page, you can use (* ... *) for single or multiple lines of comment, you can use { ... } for single and multiple lines of comment too and // the double slash for comment the rest of the current line. Modern Pascal also supports the C style /* ... */ for single or multiple lines of comment. Making porting C code to Modern Pascal easier.
(* This is an example of a single line comment in this style *)

(* This is an example of using this style for multi-line
    comments as it will span multiple lines. *)

{This is an example of a single line comment in this style}

{This is an example of this style for multi-line
comments as it will span multiple lines.}

// Lastly, this is a single line comment - or
var a:Integer; // comment the rest of this line, or about this line.

/* Modern Pascal also supports C/Java/JavaScript style of comments
   which use the slash asterisk combination instead of parenthesis
   asterisk combination (like formal Pascal) */

Case Sensitivity

Pascal is not a case-sensitive language, which means you can write your variables, function and procedure in either case. Like A_Variable, a_variable and A_VARIABLE all have the same meaning in Pascal. It is a good writing habit though to be consistent as if the language was case-sensitive for working with certain tools and learning/using other programming languages which usually are case-sensitive.

Statements

The code section between begin and end contains statements that describe the actions the program can take and is called the statement part. Pascal programs and units are made of statements. Each statement specifies a task or job of the program. These jobs could be declarations, assignment, calculations, reading or writing data, comparisons, making logical decisions, transferring program flow control, etc. All statements end with a semicolon:
WriteLn('Hello, World!');
Ch:=Readkey;
if (Ch=#27) then WriteLn('Goodbye.');

Naming Convention

In Pascal, types, class names, constants, variables, function names and procedure names following a strict naming convention: The following are reserved characters and cannot be used except for their explicate functionality(s):