FreeBASIC

From Wikipedia, the free encyclopedia
Jump to: navigation, search
FreeBASIC
The FreeBASIC Logo
Paradigm(s) Procedural, object-oriented
Appeared in 2004
Designed by Andre Victor
Developer The FreeBASIC Development Team
Stable release 0.23 (August 7, 2011; 5 months ago (2011-08-07))
Typing discipline Static
Influenced by QuickBASIC, C
OS DOS, FreeBSD, Linux, Microsoft Windows
License GNU GPL, Standard libraries licensed under the GNU LGPL
Website www.freebasic.net

FreeBASIC is a free/open source (GPL), 32-bit BASIC compiler[1] for Microsoft Windows, protected-mode DOS (DOS extender), Linux, FreeBSD and Xbox.[2]

The official FreeBASIC website states: 'When used in its "QB" language mode, FreeBASIC provides a high level of support for programs written for QuickBASIC. Many programs written for QuickBASIC will compile and run in this mode with no changes needed. However, for compilation in the FreeBASIC default language mode, most substantial programs will require changes.'[3] FreeBASIC is not as related to Microsoft's later Visual Basic language. For that, Gambas is the most relevant free software project.

FreeBASIC is the name for the compiler. It does not come bundled with an IDE. Most users will need to download an IDE such as FBide, FBedit, or Geany.

Contents

[edit] Features

FreeBASIC is a self-hosting compiler, with roughly 120,000 lines of source code, core only, not including libraries.

It makes use of the GNU binutils programming tools as backends and can produce console and graphical/GUI executables, besides dynamic and static libraries. FreeBASIC fully supports the use of C libraries and partial C++ library support. This lets programmers use and create libraries for C and many other languages.

It supports a C style preprocessor, capable of multiline macros, conditional compiling and file inclusion. However it is not a true preprocessor as it occurs at the same time as parsing. This means that it can access symbol information, and even set the dialect, which restarts parsing.

FreeBASIC has been rated close in speed with mainstream tools, such as GCC.[4]

[edit] Syntax

FreeBASIC syntax attempts to stay as close to the BASIC syntax as possible, specifically that of QuickBASIC. Although the syntax attempts to stay compatible with its predecessor, FreeBASIC follows modern standards and coding practices. Standard procedural features, along with object-oriented features such as types as objects, operator overloading, function overloading, namespaces, etc., have been added in FreeBASIC.

FreeBASIC's lines end when the end-of-line characters are found, or with a colon. Because of this, lines don't need a special character (such as the semicolon in C) to notify the compiler of the end of line. Multiple statements may be written on one line by separating each statement with a colon :.

FreeBASIC supports block commenting along with end of line remarks. Full line comments are made with an apostrophe ', while blocks of commented code begin with /' and end with '/.

[edit] Compatibility

FreeBASIC is a successor to the QuickBASIC programming language.[1] Changes were made during development to keep FreeBASIC compatible with modern utilities, and facilitate advanced programming features. In order to keep the compiler moving forward, GCC compliant, and to also retain the ability to use a QuickBASIC-compatible language, the -lang option set was created.

  • When choosing Language Set FB (-lang fb as a command-line argument), all of the new features that FreeBASIC offers are available, and the "hackish" features from QuickBASIC (that were incompatible with modern programming practices) are disallowed.
  • Choosing Language Set FB-Lite (-lang fblite) gives access to most of the new, non-object-oriented features of FreeBASIC, but allows a coding style similar to that of older types of BASIC. Syntax rules, such as allowing implicit variables, suffixes, GOSUB / RETURN, numeric labels, etc., are allowed in this lang option.
  • Choosing Language Set QB (-lang qb) is similar to -lang fblite, but is more focused on specifically replicating QuickBASIC-like behavior. -lang qb is designed to make it easier to run programs originally written for QuickBASIC, and is useful in cases where -lang fblite is not compatible enough.

[edit] Example code

As in QuickBASIC, a program to write a line of text to the screen can be done with a simple statement:

Print "Hello, World!"

However, FreeBASIC adds many object-oriented features such as methods, constructors, dynamic memory allocation, properties and temporary allocation.

' A Vector motion user-defined-type (UDT).
Type Vector
        W As Integer
        H As Integer
        Declare Constructor (nW As Integer, nH As Integer)
End Type
 
Constructor Vector (nW As Integer, nH As Integer)
        W = nW
        H = nH
End Constructor
 
' Create an object class.
Type Object
        Private:
                X As Integer
                Y As Integer
                Movement As Vector Pointer
        Public:
                ' Create public methods, including a destructor for automated cleanup.
                Declare Constructor (nX As Integer, nY As Integer)
                Declare Destructor ()
                Declare Sub SetMotion (Motion As Vector Pointer)
                Declare Sub Move ()
                Declare Property GetX As Integer
End Type
 
' Set initial coordinates.
Constructor Object (nX As Integer, nY As Integer)
        X = nX
        Y = nY
End Constructor
 
' Clean up allocated memory.
Destructor Object ()
        Delete Movement
End Destructor
 
' Set the motion Vector.
Sub Object.SetMotion (Motion As Vector Pointer)
        Movement = Motion
End Sub
 
' Move the object based on its motion Vector.
Sub Object.Move ()
        X += Movement->W
        Y += Movement->H
End Sub
 
' A getter for X, as X is private.
Property Object.GetX As Integer
        Return X
End Property
 
        ' MAIN CODE.
 
' Create a new instance of Object at coordinates 100, 100.
Dim Player As Object = Type<Object>(100, 100)
 
' Dynamically allocate a new Vector object. Moving left 10 units and down 5
Player.SetMotion(New Vector (-10, 5))
 
' Make the Player update its location.
Player.Move()
 
' Display new X, 90.
Print Player.GetX
 
' Because Player is a local variable, its destructor is called at the end of scope automatically.

[edit] Graphics library

FreeBASIC has a built-in 2D, QuickBASIC compatible, software graphics library known as FBgfx (or Gfxlib), which provides simple graphics primitives (such as rectangles, lines, and circles), blitting, and more features which were not in QuickBASIC's graphics library. The library simply wraps the backend renderer. It uses OpenGL on GNU/Linux and DirectX on Microsoft Windows, but can be forced to use OpenGL under Microsoft Windows. This abstraction makes the built-in graphics library portable across platforms.

Although the library is built-in, it is only included if users choose to use it, which is done simply by including a call to the FBgfx Screen command. Using common libraries such as OpenGL and creating a window with an API (Microsoft Windows, GNU/Linux, etc.) for hardware acceleration can be used without interfering with the FreeBASIC graphics library.

Another alternative to the built-in graphics library is Cairo which can render vector images and raster images.

[edit] Future development

FreeBASIC continues to make development progress toward its goal of using GCC as a back-end,[5] which would allow portability to nearly any modern system, and advanced optimizing methods.

With the release of version 0.17, object-oriented programming (OOP) was introduced by adding classes and extending the basic type. Further implementations are expected.

In the developer version of the compiler inheritance has been added, so this feature will probably become part of the next stable release.

[edit] References

[edit] External links

Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export
Languages