Tutorial
26 Pages
English

Tutorial

-

Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

Description

Discovering FBSL By a beginner for beginners FBSL (Freestyle BASIC Language Script) © 2001-2008 by Gerome GUILLEMIN, Mehdi BOUAZIZ and Mike LOBANOVSKY Contents Introduction 8. Types and classes 1. Basics Definition of a type of data item First steps A type can in hiding another To calculate with FBSL Classes and methods 2. Data and variables Inheritance Assignment Similarity and uniqueness To display the value of a variable Overloading operators Reassignment 9. Meta-directives Multiple assignments Definitions (#Define) Operators and expressions Declarations (Declares) Composition Macros 3. Supervise implementation 10. Creating executables Flow Control Conclusion Repetitions in loop 4. Principal types of data Numbers Character strings Arrays 5. Procedures and functions Procedures Local variables and global variables Functions Default values for the parameters Optional parameters Passing arguments by value or by Reference 6, graphic interfaces (GUI) To display a message box To communicate with the computer To open a window A basic example of application 7. Files Using Files Working with files. Include files Namespaces 1Introduction These basic lessons are intended to introduce FBSL. They are directed towards beginners and hopefully ...

Subjects

Informations

Published by
Reads 17
Language English

Discovering FBSL
By a beginner for beginners





FBSL (Freestyle BASIC Language Script)
© 2001-2008 by Gerome GUILLEMIN, Mehdi BOUAZIZ and Mike LOBANOVSKY


Contents
Introduction 8. Types and classes
1. Basics Definition of a type of data item
First steps A type can in hiding another
To calculate with FBSL Classes and methods
2. Data and variables Inheritance
Assignment Similarity and uniqueness
To display the value of a variable Overloading operators
Reassignment 9. Meta-directives
Multiple assignments Definitions (#Define)
Operators and expressions Declarations (Declares)
Composition Macros
3. Supervise implementation 10. Creating executables
Flow Control Conclusion
Repetitions in loop 4. Principal types of data
Numbers
Character strings
Arrays
5. Procedures and functions
Procedures
Local variables and global variables
Functions
Default values for the parameters
Optional parameters
Passing arguments by value or by Reference
6, graphic interfaces (GUI)
To display a message box
To communicate with the computer
To open a window
A basic example of application
7. Files
Using Files
Working with files.
Include files
Namespaces




1Introduction

These basic lessons are intended to introduce FBSL. They are directed towards beginners and
hopefully they will enable them to become familiar with, and, to use the basics of this straightforward
and powerful language.
FBSL means “Freestyle BASIC Language Script”. Like many modern programming languages
contains flavours of Visual BASIC, QBasic, PHP, Pascal and C++.
It is a kind of hybrid language hence the “Freestyle”.
It is especially designed for the win32 platform (Windows 95 SR2 to Windows VISTA).
It is compatible with, and completely functional under, LINUX Wine.

Do not hesitate with to download the last version of FBSL.
And to join the forum if you have questions, suggestion about FBSL, or even criticisms… of this
document.

Remarks on the presentation of the document :
The code with a pale yellow background contain complete scripts which one can
copy/paste into a text editor and save with the extension .fbs.
You can then execute the program by a doubleclick on the icon or directly from the editor
if it allows (Eclecta is one such editor)
The code fragments with a pale blue background contains pieces of code intended to be
analysed and/or inserted in complete scripts to be able to test them.
The diagrams with a black background represent the result of script As it seen on the
screen.

Contents





















2
Chapter 1. Basics
This chapter demonstrates the fundamental knowledge needed to write your first program in FBSL.

First steps
We will start exploring FBSL using the “console” mode. Scripts can be written in any text editor that
gives plain text “notepad.exe” for example. But the FBSL distribution offers more specialised editors,
in the standard installation look in "C:\Program Files\FBSLv3\Samples" and select sub-directories
starting with IDE.

You can also launch 'FBSLv3 Editor (Eclecta Editor)' from start/Programs/Freestyle BASIC Script
Language. Then click on File/New or on in the bar of icon:



On the first line type: #apptype console to tell FBSL this one will use the console mode.
then type: pause, this prevents the console closing immediately.

This first script does not do anything in particular it is only to open one console.



Contents








3
Now on click on Compile and Run Script or simply press on the <F5> key, you get the following
window:


Press the <Enter> key, and the console is closed.

The console mode is useful for testing small fragments of code, algorithms and writing small utility
programs.

Always insert your code between #App Type Console and Pause







←Enter your code here






From here on we will use the Console mode quite extensively to demonstrate How to use FBSL

Contents










4To calculate with FBSL

FBSL can evaluate all the basic arithmetic operations.

Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation

Turn on the editor now and test this:

print 5+3
print 2-9
print 7 + 3 * 4
print (7+3) * 4
print 20/3


Do not forget to insert these lines between #AppType Console and Pause.

The command print displays rest of the line on the screen.

Note that spaces are optional between the figures and the operators (the editor adds automatically
spaces to make the code more readable) but space is essential between the command print and
what follows.

Click on Compile/Run Script or type on <F5> , you obtain the following window:


Contents









5Here an example of this script with accompanying notes:










(a


(b






(c


(d

(e





(f

(a Indicates that the application is to be of type “console”
(b The print instruction causes the following characters to be displayed on the console.
(c Causes the program to wait for the Enter key to be pressed.
(d enclosed in quotes you can print the operation code in front of the result.
Characters in quotes are printed “as is”, that is, literally
(e Remark
- Literal text is typed between quotation marks
- The elements of the post (quoted part, calculated part) are separated by commas
(f prints a blank line. The colon “:” indicates a separate instruction

Note that characters which are typed after the apostrophe (') are comments and are ignored by FBSL
when the program is executed.
They make it possible to explain in plain language what the instructions do and can also contribute to
the legibility of script by separating the various sections from the program.

The comments can also be preceded by a double oblique bar // instead of the apostrophe. One
can also write comments on several lines, which will form a block framed by /* at the beginning and
*/ at the end.

It is a good idea to comment anything that is a bit unusual or complicated; it is surprising how quickly
one forgets.

Blank lines can be used between lines of code to make it easier to read

It is possible to write several instructions on the same line by separating them by : (colon).

Contents
6
FBSL performs calculations following a hierarchy of operations.
The acronym PEMDAS: is a useful mnemonic to help memorise the order of priority for calculations

- P for parentheses. This is the highest priority. Any calculations surrounded by parentheses
are performed first. This makes it possible to ensure that calculations are performed in the
required sequence
2* (3-1) = 2*2 = 4 FBSL → ? 2 * (3 - 1)
(1+1) ^ (5-2) = 2^3 = 8 FBSL → ? (1 + 1) ^ (5 - 2)

- E for exponentiation. This is evaluated before other operations
2^1+1 = (2^1) + 1 = 3 and not 4 FBSL → ? 2 ^ 1 + 1
3*1^10 = 3 * (1^10) = 3 and not 59049 FBSL → ? 3 * 1 ^ 10

- M and D for multiplication and division, which have the same priority.
2*3-1 = 5 and not 4 FBSL → ? 2 * 3 - 1
2/3-1 = -0.3333 FBSL → ? 2/3 – 1

- A and S for addition and subtraction, which are carried out last

- If two operators have the same priority, the evaluation is done from left to right.
59*100/60 = 5900/60 = 98.3333 FBSL → ? 59 * 100/60

? is a short cut for print. There is also another synonym of print: echo which means the same thing.

The syntactic flexibility of FBSL makes it a rich language and makes it easy to convert between other
programming languages to FBSL, and, from FBSL to other programming languages.

Contents

















7Chapter 2. Data and variables

A computer program mainly handles data. The program accesses this data using “variables” of
various types. A variable generally consists of a variable name which the computer uses as a
reference to the address in memory that contains the actual data.
At this address one value is stored, it is data itself. That could be a number, a character string, an
array, a user defined type (UDT) etc.
Variable names can be freely chosen, but is preferable to keep the names short and descriptive. The
names should clearly show the type of information held. For example, ‘age’ is preferable to A, and
‘name’ to X.
Variables names can be made up of more than one word, but no spaces are allowed within the
variable. It is necessary to ensure that the variable name is readable. Imagine that you wish to use a
variable to record the name at birth. There are two conventions:
nameBirth – the so called camel code, capitalise the first letter of all words after the first.
name_birth – use an underscore between words.
Avoid where possible: namebirth, as it is less readable.

Assignment
Having chosen a suitable name for the variable, it is now required to “assign” a value to it.
The assignment operator is the equals sign “=”

Dim numbers, message
numbers = 7 'give numbers the value 7
message = “Hello” ' assign the value “Hello” to message

While it is possible to just start using a variable, having FBSL create the variable as it is needed, it is
preferable to choose and control the names yourself, using auto creation it is possible (likely) to get
strange errors through misspelling the name, to do this use #Option Implicit at the head of the
script.

CAUTION! since version 3.31.6 FBSL, Explicit is the default option , it is thus necessary to declare variables with
Dim to avoid any error.

Explicit assignment means that when you have chosen a variable name you must declare to FBSL
what the variable is the statement used is Dim (short for dimension) so that if you dim a variable and
make it a specific type of variable the exact amount of memory can be reserved. It is possible but not
compulsory that the value be assigned at this time.

Dim numbers = 7, message = “Hello”

See the chapter on Variable Declaration

To display the value of a variable
To display the value of a variable in the console, the command print or one of its synonyms is used.
Test:

#Option Implicit
#AppType Console
N = 7
msg = “Hello”
Print N
Print msg
Pause

N.B. : You can copy/paste the lines of code above in the editor. Then possibly use Format/Check Syntax before
launching script in order to check that there are no errors of syntax or typing errors


Contents
8Reassignment
You can change the value stored in a variable at any time and as often as needed.
The new value replaces the old values a shown in the next example:

#Option Implicit
#AppType Console
outdistance = 320
print distance
outdistance = 375
print distance
Pause

If you want to protect the value in a variable you can declare it as a constant by using the command
CONST instead of Dim. This is also useful for values that are used throughout a program, for
example, Const tax_rate = 10/100. Then when the rate changes you can simply change the value in
one place and the new value will be inserted whenever the tax_rate variable is used.

Test:

#AppType Console
Const C = 100
Print C
C = 1
Print C
Pause

You should obtain this result:

100
[SV] Var protected : 'c' <-- 1
100
Type Enter key to continue...
100
[
The variable C, declared as a constant is protected. Its value cannot be modified.
To easily locate the constants in a program there is a habit to type their name in capital letter.
It is not an obligation.
Contents

Multiple assignments
With FBSL one can assign a value with several variables at the same time thanks to the operator Let.
Example:

#Option Implicit
#AppType Console
Let (X, y) = 7
? X
? y
Pause

One can also assign a list of variables in only one operation:

#Option Implicit
#AppType Console
List (a, b) = {4, 8.33}
? a
? B
Pause

9The list entered between the { } corresponds to a array. Each element of the array is affected as a
value in the name of variable corresponding of the order List.

This functionality of FBSL is more interesting than it appears. For example, the values assigned 3, 4, 5
and 7 with A, B, C and D:

List (A, B, C, D) = {3, 4, 5, 7}

and one wants to now exchange the values of variables has and C and those of B and D:

List (A, B, C, D) = {C, D, A, B}

One could have also made:

Swap (A, C)
Swap (B, D)

Swap () allows to exchange the values of two variables, with List () and the arrays one can exchange
values of several variables.

Operators and expressions
It is possible to handle the variables and their value by combining them with operators to form
thus expressions :
a = 7.3
B = 12
y = 3 * a + B/3

After having affected the values 7.3 and 12 with the variables a and B, one assigns to a new variable y
the result of an expression which combines the operators *, + and / with the operands a, B, 3 and 5.

Composition
By combining them variables, them expressions and them instructions one will be able to program
more or less complex algorithms in a clear and concise way.
List (H, m, S) = {15, 27, 34}
Print “Numbers seconds passed since midnight = “, h*360 + m*60 + S

Variable Declaration
FBSL, like the other BASIC languages, is user friendly. This is why it makes it possible to create
variables simply without having to declare them explicitly. In other words, a variable exists from the
moment when you integrate it in your code (to see, for example, the paragraph “Operators and
expressions” above).

However implicit declaration variables is not a good practice of programming because that
can bring new variables into existence involuntarily; by mistyping or misspelling an existing variable.
Instead of generating an error message , FBSL seems to be unaware of this “new” variable, which
can to produce unexpected results. This kind of error can be rather difficult to locate in the code.

To avoid that, FBSL proposes the directive #Option Explicit by default which obliges us to
declare variables using Dim. In this manner, an error message will be published when an undeclared
variable is used.
Test:

#Option Implicit
#AppType Console

variable = 5
Print variable

Pause
10