Modern Pascal Introduction
Modern Pascal is a term that conveys 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 educational 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 a 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 introduced 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, as 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.- 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. - 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 mp2 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 - 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. - 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.
This tutorial will give you a 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 a 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 of 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 academic arena for various reasons:
- Easy to learn.
- Structured language.
- It produces transparent, efficient and reliable programs.
- It can be compiled on a variety of computer platforms.
- There are decades of source available to learn from.
Features of the Pascal Language
Pascal has the following features:- Pascal is a strongly typed language.
- It offers extensive error checking.
- It offers several data types like arrays, records, files, and sets.
- It offers a variety of programming structures.
- It supports structured programming through functions and procedures.
- It supports object-oriented programming.
How Pascal is different from other languages:
- It is not case-sensitive, so foo, Foo and FOO are the same.
- It uses the keyword procedure or function for methods instead of just function.
- It uses the semicolon to separate individual statements within a compound statement.
- It does not use a separate file for your header or object definition.
- It does not have a problem tracking variable usage (no need for #ifndef variable slop).
- It has the support of a true Boolean type.
- It has a more abstract high-level method for dealing with bit-wise data and sets.
- It is more strict with floating types.
- Standard Pascal requires variables to be defined outside of the compound statement.
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):- Comments
- Program or Unit name
- Comments
- Header or Unit public declarations
- Comments throughout the declarations
- Public Uses command
- Constant declarations
- Variable declarations
- Type declarations
- Class declarations
- Constant declarations (again)
- Variable declarations (again)
- Function declarations
- Procedure declarations
- Main Program or Unit private implementations
- Comments throughout declarations and implementations
- Private Uses command
- Private Constant declarations
- Private Variable declarations
- Private Type declarations
- Private Class declarations
- Private Constant declarations (again)
- Private Variable declarations (again)
- Class implementations
- Statements and Expressions within each
- Function implementations
- Statements and Expressions within each
- Procedure implementations
- Statements and Expressions within each
- Program Main Routine
- Unit Initialization
- Unit Finalization
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:- The first line of the source code states this is a Program called HelloWorld which should correspond to the filename.
- The next line states a preprocessor command, telling the compiler to include the unit display's commands before going to actual compilation.
- Next is one style of comment, which will be ignored by the compiler and is there to help explain the functionality to another programmer - or remind yourself what a routine is doing. Comments are totally optional, some businesses require comments to help others read your code.
- The next section is the Main Program implementation, enclosed within the begin and end statements. Every block of code in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the Main Program source is followed by a full stop (.) period instead of an end of block semicolon (;).
- The begin statement of the Main Program block is where the modern pascal execution begins.
- The statement WriteLn('Hello, World!'); uses the WriteLn function available in the display Unit which causes the message "Hello, World!" to be displayed on the screen with a Carriage Return and Linefeed.
- The WriteLn function is followed by another style of an optional comment using braces { ... } usually used to denote something about the command to the left, or sometimes used to disable a block or piece of code during debugging.
- The statement Readkey; allows the display to pause until the user presses a key on the keyboard. It is also part of the display unit. Again, a unit is a library or collection of reusable Types, Objects, Classes, Constants, Variables, Functions, and Procedures.
- The last statement end. terminates the execution of your program block.
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 does 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.');The following is not easy to read, however, it is valid code to the compiler. Each statement is terminated by the 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:-
First Character must be A to Z, a to z or _.
Second Character onward can also include 0..9.
-
~, `, !, %, &, |, \, ? (question mark) are not used in Pascal
' (apostrophe) is used for a string 'start and end'.
" (quotation) is also supported for a string "start and end". (deviation to standards!)
$ is used for hexadecimal example $0A.
# is used for embedding an ASCII character, like #39 for an apostrophe.
@ is used for the address of a variable.
^ is used to de-reference a pointer or pointer structure.
* is used for multiplication.
- is used for subtraction.
is used for addition.
= and == are used for equality comparisons. (deviation of standards!)
/ is used for division which could result in fractional where div is used where a result is a whole number.
( ) are used for grouping logic, passing parameters are part of the (* ... *) comment.
{ } are used for comments.
[ ] are used for array elements.
: used for the variable name and type separator and part of := used for assignment.
; used to denote the end of each statement or end of a block of code.
< is used for less than comparison and <= is used for less than or equal to comparison.
> is used for greater than comparison and >= is used for greater than or equal comparison.
<> and != are used to denote not equal. (deviation of standards!)
, a comma is used in certain cases for multiple element separation.
. is used in an object to access its properties, methods, events, etc.