Outils pour utilisateurs

Outils du site


issue111:tutoriel2

Ceci est une ancienne révision du document !


Many PC programmers started with one of Borland’s products, back in the day. Both Turbo Pascal and Turbo C ran on the MS-DOS operating system, and had excellent integrated development environments comprised not only of the compiler itself, but also a nice editor. To the point, they also contained a system of text-based widgets (buttons, radios, editing areas, menus…) called Turbo Vision that was not only used to build the IDE itself, but was also at the disposition of programmers for their own applications. For simplicity and for elegance, it has not yet been surpassed as a text-oriented human interface. This is not only my own very honest opinion, but also that of some others - as seen on the Internet. So, it must be true. It was with interest that I discovered the existence of Free Pascal (www.freepascal.org), a project led by Florian Paul Klämpfl, that aims to bring back a similar Pascal compiler, but open-source and adaptable to many different architectures. It also contains the interface environment, but adapted to modern times. This has been named Free Vision. As an aside, Lazarus (http://wiki.freepascal.org/Lazarus_Faq) is a companion project that continues along the same lines as the Borland Delphi graphical development interface; Vince DuBeau gave us a brief description of that project some time back in FCM#77 (http://fullcirclemagazine.org/issue-77/). In this series of articles, I will be building a text-based application with Free Pascal, using its text-based interface for user interaction. This will be combined with other, more modern, technologies such as database access using SQL, and Web access with HTTP. The final aim of the project is to demonstrate how Pascal can be used to build a modern application, while avoiding the overhead associated with a graphical interface (using a widget set such as GTK or Qt). This first part will describe installing the tools and writing some simple programs.

Why? Before delving into the technical intricacies of setting up the Free Pascal and Free Vision combination, perhaps it would be judicious to answer this basic question: why is it worthwhile for me to spend time on this technology? After all, there is a plethora of other languages for the programmer to choose from, such as Go, Swift, Haskell… just to name a few. The obvious answer is: “speed!” Just like that, with an exclamation mark. This stuff is seriously lightweight, and thus it also runs seriously fast. It gets to the point where a Raspberry Pi running an interface written in Free Vision can beat a proper modern computer with a multicore CPU and an SSD running a similar application, but interfaced through the Web. Which also makes sense, in a way, since the RPi has more processing horsepower than the 286 or 386 on which most Turbo Pascal programming was done. Meanwhile, the program using Free Vision is a compiled, executable binary that does not need to contend with all the quirks of a graphical interface or HTTP server. These are available, but the programmers do not need to waste CPU or RAM on them unless they are actually useful to what they are doing. A second, equally valid, answer may be: “for old times’ sake.” Pascal is also a nice, clear and structured programing language that can help us get into good habits - habits that JavaScript is not known for nurturing, just to give a quick example.

Installation Free Pascal is installable using standard tools (apt, synaptic…) from Ubuntu 16.04’s repositories, where it is identified as package fp-ide. Package fp-units-base will also be required to make standard units available, as well as fp-units-fv for Free Vision, fp-units-db to access databases, etc. Just to see if there were differences with the original developer’s version, I downloaded FreePascal version 3.0.0 for the Intel 86-64 (64-bit) architecture, from the project’s web page at http://www.freepascal.org/download.var . The download is a .tar file, that in turn contains a further three compressed files (in the .tar and .tar.gz formats) that contain the compiler itself in binary or executable format, examples of programs, and the documentation. An install script is also provided. Running the install script as root will allow us to install the lot, choosing between either /usr or /usr/local as a starting point for our files. The decompression process is fast and hassle-free. Once done, we have the appropriate PATH variable set in our system, and can proceed with creating a directory for our source files, and opening it within a terminal. Something along the lines: mkdir Desktop/pascal cd Desktop/pascal fp

There were no detectable differences with the version from the repositories. The final command launches the integrated Pascal IDE (Integrated Development Environment), which will bring back memories to users of Turbo Pascal. Some of us have spent hours staring at this screen. A configuration file is created for the environment within this directory. At this point, we can immediately begin programming. However, if we need to use units such as Crt, we will need to direct the IDE to use the appropriate unit directory. This is a step the installer does not seem to take care of. Simply choose Options > Directories in the main menu, and add the following two lines. If you have installed in /usr instead of in /usr/local, do the appropriate modifications: /usr/local/lib/fpc/3.0.0/units/x86_64-linux /usr/local/lib/fpc/3.0.0/units/x86_64-linux/* The first line is to include the unit directory itself, while the second also includes any subdirectories within the search path.

Compiling a console program To start, let us write a very simple program. This will serve just to test the compilation process, and see if units are in fact correctly detected. Choose File > New in the menu, and type in a test program (such as http://pastebin.com/wJhgry5A). Keyboard shortcut command F2 saves the file (with extension .pas), and F9 compiles it (top right). The program can be both compiled and executed in one step using command Ctrl+F9. The result is shown in the terminal, and execution goes back to the IDE whenever a key is pressed.

A first Free Vision application Let us get on to using the modern equivalent of Borland’s Turbo Vision text-based user interface, now called Free Vision. The corresponding units are grouped in subdirectory: /usr/local/lib/fpc/3.0.0/units/x86_64-linux/fv The first one to use is simply the App unit itself. With the ten-liner in http://pastebin.com/13UfNBNS, we can set up a sample text-based application. The corresponding code is quite minimalistic. We have simply created a new application type, TMyApp, based on the pre-existing TApplication. This application is then instantiated in a variable. The application is initialized in method Init. Method Run is the main loop, where user keyboard and mouse events are handled. Finally, we clean up in method Done. That’s it - but it is already a working application, written in less lines of code than are needed to describe the process. var MyApp : TMyApp; begin MyApp.Init; MyApp.Run; MyApp.Done; end.

One point that can be noticed is that the resulting executable files are rather small, even if using FreeVision. The first one - using merely Crt - takes up about 400 kBytes, and the second - with fv - takes less than 800 kBytes. This is quite small by modern standards. $ ls -lh test? test?.pas -rwxrwxr-x 1 alan alan 416K jun 7 09:05 test1 -rw-rw-r– 1 alan alan 175 jun 7 09:04 test1.pas -rwxrwxr-x 1 alan alan 781K jun 7 09:06 test2 -rw-rw-r– 1 alan alan 162 jun 7 08:45 test2.pas This will be all for this first part in our series on Free Pascal - and Free Vision. In the next part, we will make the user interface responsive, and create some simple dialog windows.

issue111/tutoriel2.1471280633.txt.gz · Dernière modification : 2016/08/15 19:03 de d52fr