PureBasic

From Bauman National Library
This page was last modified on 8 June 2016, at 19:32.
PureBasic
Paradigm structured, imperative, procedural
Family BASIC
Designed by Fantaisie Software
Developer Fantaisie Software
Stable release 5.42 LTS / March 1, 2016; 6 years ago (2016-03-01)
License commercial
Website www.purebasic.com

PureBasic is a native 32-bit and 64-bit programming language based on established BASIC rules. The key features of PureBasic are portability (Windows, Linux and MacOS X are currently supported), the production of very fast and highly optimized executables and, of course, the very simple BASIC syntax. PureBasic has been created for the beginner and expert alike. We have put a lot of effort into its realization to produce a fast, reliable system friendly language.

In spite of its beginner-friendly syntax, the possibilities are endless with PureBasic's advanced features such as pointers, structures, procedures, dynamically linked lists and much more. Experienced coders will have no problem gaining access to any of the legal OS structures or API objects and PureBasic even allows inline ASM.

History

PureBasic started life in 1995 as a command-set expansion for BlitzBasic after PureBasic’s author, Frédéric Laboureur hit many limitations with BlitzBasic while programming an application called ‘TheBoss’, a powerful application launcher for the Commodore Amiga. The expansion named ‘NCS’ (NewCommandSet) was entirely coded using 68000 assembly, as all new commands had to be coded in assembly at this time of Blitz’s development. Fred’s progress was quite slow to begin with as good documentation was hard to find for assembly programming and of course online forums didn’t then exist for BlitzBasic plug-in programming. Development of ‘NCS’ continued for about a year after which Fred received very positive comments regarding his work, through which he became very familiar with assembly programming and debugging. He was also astonished with the incredible things that could be achieved with an old 68000 processor if everything was programmed correctly. Around this time, IBM PowerPC based processor cards for the Amiga started to appear and were a very powerful alternative to the Motorola 68000 processor. They were very fast and sold at a relatively cheap cost, even compared to the high-end 68060 processors. With the arrival of these new chips, people wanted a native version of Blitz to support them, as it was an extremely popular language at this time, but everyone knew that all development had been put on hold for the Amiga platform in favor of the Intel x86 based PCs. An opportunity had presented itself for the creation of a new language which would be the logical replacement and enhancement of BlitzBasic, which also would have full support for the 680x0 and PowerPC processors.

The early design and the first version of PureBasic started in 1998. The main differences between

PureBasic and ‘normal’ compilers then was the inclusion of a ‘virtual processor’ (which actually used the 680x0 assembly mnemonics) right from the start to allow different kinds of assembly output (or any language) possible without changing the compiler core. After the initial design was finished and programming began, things started to move very fast. Fred fully dedicated all his time to program the compiler and learned a great deal including the C language to be able eventually to produce a fully portable compiler. The first version of PureBasic was initially released for the Amiga and (even if it was badly bugged) had an integrated and fully cross platform editor, an integrated debugger and a huge internal commandset, you guessed it, taken directly from the former ‘NCS’ Blitz package. While refining and bug testing, Fred also studied other programming languages to give him a firm grounding in other areas and to give him the best foundation to make better, more informed decisions about the internal design and how PureBasic should grow and expand in the future. During the fourth year of Fred’s computer science diploma, the Amiga was starting to be considered a dead platform and many of Fred’s fellow students asked why wasn’t he working on a Windows based version. Fred of course defended himself saying that it would be a piece of cake to port PureBasic to a new system, but he had to prove it!

The development philosophy

The development philosophy of PureBasic is slightly different from that of other languages in many respects. Here is a list of some of PureBasic’s development goals and policies.

  1. After the initial purchase of a PureBasic license, all future updates are free of charge for life.
  2. All Programs compiled using PureBasic can be sold commercially free of any further cost or royalties.
  3. All programs should compile at the minimum file size they can be and contain no bloated code.
  4. All compiled programs should not rely on any runtimes and be completely ‘stand-alone’ executables

The above list is a big selling point in itself and contrary to many development philosophies of rival companies. Can you imagine Microsoft giving you free version upgrades of VB.NET for life? Me neither. PureBasic’s development philosophy is one of creating a programming environment that is both fun and functional to use. It’s all about giving the users the power to create the programs they need in the simplest way possible. With all past version upgrades there has been included bug fixes, new commands plus brand new IDEs and a Visual Designer, the last two of which are covered in later areas of this book. So not only are bug fixes a feature of future upgrades, there are also additions to the language command-set along with the addition of useful tools and updates to the actual development environment itself.

Code Examples

Let’s try a small example to show this in action, type this (exactly) :

OpenConsole()
Print("Press enter to exit")
Input()
End

After running this little program it opens a console window, then prints a line of text to it informing the user in order to exit he or she must press ‘enter’, during which we wait for input. After the enter key is pressed then we then end the program nicely. If you place the flashing IDE cursor upon any keywords that are used in this program, then press ‘F1’, the helpfile is brought to the front and flipped to the page that describes that command. For example, place your flashing cursor anywhere within the ‘OpenConsole()’ keyword and hit ‘F1’. You will now see the ‘OpenConsole()’ help page appear like magic!

Here is an example showing the creation of all variable types using PureBasic code:

ByteVariable.b = 123
CharVariable.c = 222
WordVariable.w = 4567
LongVariable.l = 891011
QuadVariable.q = 9223372036854775807
FloatVariable.f = 3.1415927
DoubleVariable.d = 12.53456776674545
StringVariableOne.s = "Test String One"
StringVariableTwo$ = "Test String Two"
StringVariableThree.s{6} = "abcdef"
StringVariableFour${3} = "abc"

Data Types

PureBasic provides seven numeric data types for you to use in your programming, each one uses a different amount of RAM and all have different numerical limits.

Type Byte Char (Ascii) Char (Unicode) Word Long Quad Float Double
Suffix .b .c .c .w .l .q .f .d
Memory Usage (RAM) 1 byte (8 bits) 1 byte (8 bits) 2 bytes (16 bits) 2 bytes (16 bits) 4 bytes (32 bits) 8 bytes (64 bits) 4 bytes (32 bits) 8 bytes (64 bits)
Numerical Limit 128 to 127 0 to 255 0 to 65535 -32768 to 32767 -2147483648 to 2147483647 -9223372036854775808 to 9223372036854775807 Unlimited* Unlimited*

Strings are probably the simplest data type to understand because they are so easy to use. As long as you remember the double quotation marks around the characters then you have a String.

Type String String Fixed Length String Fixed Length String
Suffix .s $ .s{length} ${length}
Memory Usage (RAM) 4 bytes (32 bits) 4 bytes (32 bits) 4 bytes (32 bits) 4 bytes (32 bits)
Character Limit Unlimited Unlimited User Defined* User Defined*

Strings can be made up from any character in the ASCII character set, including the control characters (See Appendix B (Helpful Charts) for a full listing of ASCII characters) except the null character as that is used to signify the end of a String. Strings that use the null character to define its end are referred to as ‘Zero Terminated Strings’.

PureBasic best features

  1. Executables are very small and you don't need to ship a runtime library with your program.
  2. Writing a simple GUI with its gadgets is really easy, and all the gadgets maps to native controls on the various OSes, so they look right.
  3. Can create a single EXE, with extremely reduced dependencies (usually only the DLLs shipped with the OS unless you use the 3D commands requiring the supplied OGRE DLL).
  4. Can easily create portable programs, self-contained in their home directory, not requiring an installer or tampering with the registry.
  5. Generated code is reasonably fast, roughly comparable to the code generated by old C compilers but it does not offer real optimizations.
  6. It uses the well known FASM to compile the assembly generated by the compiler (YASM on OSX), and it does support inline assembly in your PB code.
  7. The vast majority of Windows API functions are supported as native commands (you can always import the missing ones similarly as you do in other languages).
  8. Compilation time is blazing fast (consider it's a single pass compiler, so this helps).
  9. The editor is pretty good (based on Scintilla), the debugger is very good.
  10. In debug mode is possible to examine the contents of memory buffers and the state of windows, files, threads, gadgets, even visualize image objects with their alpha channel if present.
  11. The whole package (compiler + libs + IDE) it's compact and self contained. You install/configure it in minutes and you go in and out from it in seconds.
  12. It does support projects with multiple target builds (a project is a container for the many related source files you need to manage when writing bigger programs).
  13. It comes with a kind of bounds checker, called Purifier. Very useful to spot problems with pointers, buffer overruns and the like.
  14. If has an interesting GUI object called CanvasGadget useful to build custom graphical controls on it.
  15. It's especially nice to build cross-platform gadgets looking the same on different OSes and works well with the 2D drawing library, which also supports useful alpha channel operations.
  16. The compiler can generate code for 32 and 64 bit, and the language makes very easy to write a single source targeting both the architectures. You need to install two instances of PureBasic though, one for 32 and one for 64 bit.
  17. It supports ASCII, UNICODE/UTF8, multithreading all natively with specific keywords/libraries.
  18. You can launch different versions of the compiler (i.e. 4.50 x86, 5.00 x86, 5.10 x64 and x86, etc.) from a single instance of the IDE.
  19. The language is somewhat an hybrid of BASIC and C, and it's easy for a beginner but not limited to the really old "BASIC" stuff.
  20. You can use macros to expand code at compilation time.
  21. Documentation is reasonably good even if not very strict and if you have a question not covered in the manual you can almost certainly find an answer in the very active forum.
  22. It does natively support the usual data types supported by BASIC, plus some extra data structures like linked list and maps.
  23. The version 5.20 brought the implementation of modules.
  24. It comes with a large and growing set of libraries. Some of them are too simplistic but the majority are useful and they can speed up developing time considerably. HTTP and Mail libraries in particular were almost useless but they have been enhanced in 5.40.

External links

Articles
Libraries and Open Source Code Archives