FreeBASIC
FreeBASIC
|
|
---|---|
Page from the manual |
|
Basic data
|
|
developer | André Victor The Freebasic Development Team ( Open Source ) |
Publishing year | 2004 |
Current version |
1.07.1 ( September 28, 2019 ) |
operating system | Windows , Linux , DOS and FreeBSD (the latter only limited) |
programming language | BASIC |
category | Compiler |
License | GPL / LGPL |
German speaking | Yes |
www.freebasic.net (official website) www.freebasic-portal.de |
Free Basic (short FB ) is a free open source - compiler and programming language whose syntax in QuickBasic builds. It enables the generation of 32-bit and 64-bit - application programs , games etc. for Microsoft Windows , Linux , FreeBSD and DOS , partly for the Xbox . Versions for other operating systems are planned.
C-compatible libraries can be used directly, C ++ libraries at least partially in FreeBASIC. For example, GTK +, GSL, SDL, Allegro, Lua or OpenGL can be used, and the number of supported libraries increases with each new version.
The first version was released in November 2004.
features
The syntax of the FreeBASIC programming language is strongly based on the dialect defined by the DOS -based QuickBASIC , which the compiler -lang qb
reinforces with the directive , which makes it possible to compile QBasic code for modern applications without major adjustments. In addition, FreeBASIC is characterized by numerous improvements and modern features:
- Inline assembler
- Pointers to variables and functions
- Overloading of functions, subroutines (functions without return) and operators. In addition, functions and subroutines can be created with optional parameters .
- C-compatible interface for function calls. This enables projects in several programming languages and you can fall back on almost all functions that today's programmers use, including the WinAPI and other (C-compatible) libraries , etc. a. OpenGL or DirectX .
- Shorthands, macros, etc. Ä. That relieve the programmer of some typing work
- Basics of object-oriented programming such as classes (“types”) and single inheritance (some features such as interfaces or the keyword “class” instead of “type” have already been considered, but have not yet been implemented).
- All functions (including file access, character strings, etc.) of the runtime library and all internal graphics commands are platform-independent, i. H. can be used on Windows , Linux and DOS alike. This makes it easy to write platform-independent applications .
The compiler
The FreeBASIC compiler (“FBC”) generates machine code which is compiled and linked with the auxiliary programs of the GCC suite . This means that libraries from the GCC suite can also be used, and you get small executable binary files for Windows , DOS and Linux . As an alternative to the direct generation of machine code by the compiler, it is also possible to include the C compiler or the LLVM backend: The compiler is able to use a corresponding compiler option to generate C code , especially for the GCC suite generate (so-called "C backend", in contrast to the "assembler backend"). A similar compiler option can also be used to generate intermediate code that can be further processed via the LLVM framework. With these two options, direct integration into known structures is possible.
The compiler is able to create 32-bit x86 and 64-bit programs. The C backend also makes other platforms such as ARM available.
syntax
There are currently over 400 key words , e.g. T. can be used in several ways. There are generally three types of keywords:
- Instructions: Instructions perform a specific task, such as putting text on the screen.
-
Functions: They are characterized by the fact that after they have been called a value is made available that represents the result of the function call. The value can represent the sole purpose of the function (e.g. with mathematical functions such as
LOG
) or only inform about the status of the function call (functions with instructional character, such asGETMOUSE
; this function queries the status of the mouse and returns a number that confirms that the query was successful or whether an error occurred). - Clauses: They influence the behavior of statements and functions and cannot be used separately from them.
As usual with BASIC, upper / lower case is insignificant. " GetMouse
" And " GETMOUSE
" are treated the same by the compiler.
variables
When dealing with values (numbers, strings, or other types of information), variables and constants are used, STRING
-variables ( strings ) are put in double quotation marks «" ». prefixed also can escape sequences are used (eg. Print !"Hello\nWorld"
- produces a line break between "Hello" and "World") With these variables and constants can different. Operators are working For numbers, variables such as different stand. mathematical operators to available (+, -, *, /, LOG
, EXP
, SIN
, ASIN
, ...). For strings, however, the selection of the operators is limited, is possible only the concatenation (+ or &) and indexing (by means of brackets).
Functions and subroutines
It is also possible to create your own commands within a program, which consist of a sequence of existing commands. Such a custom command can be one SUB
(abbreviation for subroutine ) that behaves like an instruction, or one FUNCTION
that, like FreeBASIC's own functions , can return a value. Both can work with parameters that can also be optional. Variable parameter lists analogous to C are also supported.
In contrast to many other languages, program commands are not terminated with a semicolon , but with a line break as in QBasic . If you want to extend a command over several lines, an underscore must be written at the end of each line that does not end the command :
PRINT "Hallo, dieser String" + _
" ist ein einzelner String."
Several commands can be strung together without a line break using a colon:
PRINT "Hallo, dieser String": PRINT " ist ein einzelner String."
Another difference to other languages is that when subroutines are called, the setting of brackets is optional, since a subroutine can Subroutine(Parameter)
be called with or with Subroutine Parameter
. This property is also derived from QBasic. However, this is not possible with functions, brackets must always be used there.
Code examples
Hello World
A simple hello world program in FreeBASIC looks like this:
PRINT "Hallo Welt!"
SLEEP
-
PRINT
is an instruction that causes a text to be displayed on the screen. -
"Hallo Welt!"
is a parameter here; in this case it isHallo Welt!
output on the screen. - The
SLEEP
command is not required. It is only used to prevent the application's output window from closing automatically if it was not started from the console.
object oriented programing
'Vektorklasse
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
'Klasse zur Erstellung eines Objekts
Type AObject
Private:
X as Integer
Y as Integer
Movement as Vector Pointer
Public:
'Öffentliche Methoden inklusive eines Konstruktors und eines Destruktors
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
'Initialwerte setzen
Constructor AObject (nX as Integer, nY as Integer)
X = nX
Y = nY
End Constructor
'Allozierten Speicher freigeben
Destructor AObject ()
delete Movement
End Destructor
'Bewegungsvektor setzen
Sub AObject.SetMotion (Motion as Vector Pointer)
Movement = Motion
End Sub
'Das Objekt anhand seines Bewegungsvektors bewegen
Sub AObject.Move ()
X += Movement->W
Y += Movement->H
End Sub
'Rückgabe von X, welches sonst nicht zugänglich wäre
Property AObject.GetX as Integer
Return X
End Property
'Hier beginnt das eigentliche Programm
'Eine neue Instanz von 'AObject' an den Koordinaten 100, 100 erstellen
Dim Player as AObject = Type<AObject>(100, 100)
'Ein neues Vektorobjekt dynamisch allozieren und dessen Position um 10 nach links und 5 nach unten verschieben
Player.SetMotion(new Vector (-10, 5))
'Die Position von 'Player' aktualisieren
Player.Move()
'Den neuen Wert von X (90) anzeigen
Print Player.GetX
'Weil 'Player' eine lokale Variable ist, wird sein Destruktor am Ende des Gültigkeitsbereichs ("scope") automatisch aufgerufen
'Vor Programmende auf Tastendruck warten
Sleep
IDE
In addition to support from well-known development environments such as Geany, there are also two editors specially designed for FreeBASIC:
FBIde
The FBIde is a slim editor and the oldest IDE for FreeBasic. The current version is 0.4.6r4.
FBEdit
The FreeBASIC editor, or FBEdit for short, offers more options . There is a separate interface for the design of dialogs using resource files and the Windows API . The current version is 1.0.7.6c from December 14, 2013.
Web links
- FreeBASIC - website of the compiler (English)
- FreeBASIC portal - with introductions and code examples (German)
- Command reference - (German)
- Programming competition - for FreeBASIC and QuickBasic (German)
Individual evidence
- ↑ Release 1.07.1 . September 28, 2019 (accessed September 28, 2019).
- ↑ unofficial website of the German FreeBASIC "Community", see German FreeBASIC portal
- ↑ z. B. instead of a = a + 10 the short form a + = 10
- ↑ with the option "-gen gcc"
- ↑ with the option "-gen llvm"
- ↑ For the C backend and the LLVM backend see also the message on the homepage of the FreeBasic portal (accessed January 12, 2014).
- ↑ http://www.freebasic-portal.de/befehlsreferenz/ Weblinks: Online command reference
- ↑ http://www.freebasic-portal.de/befehlsreferenz/auslassung-ellipsis-556.html Ellipsis in the German command reference
- ↑ http://www.freebasic-portal.de/befehlsreferenz/sleep-400.html
- ↑ http://fbide.freebasic.net/
- ↑ https://www.freebasic-portal.de/downloads/ides-fuer-freebasic/fbedit-ide-30.html