Liberty BASIC

From Bauman National Library
This page was last modified on 21 May 2016, at 23:39.
Liberty BASIC
Paradigm procedural , Event
Designed by Carl Gundel
Developer Shoptalk Systems
First appeared 1992
Stable release 4.04 / 31.07.2010
OS Microsoft Windows (Mac OS X and Linux in Liberty Basic 5)
License commercial
Filename extensions .bas, .tkn, .fr3
Website http://www.libertybasic.com/
Major implementations
LB Booster, GLBCC
Dialects
Just BASIC
Influenced by
QuickBASIC
Influenced
Run BASIC, Leopard

Liberty BASIC — a commercial programming language from BASIC family and the development environment for it. Is the interpreter and supports 16-and 32-bit Windows and OS/2. Liberty BASIC was created by Karl Gandel on Smalltalk and released by its Shoptalk Systems company in 1992. Its last version 4.04 appeared on July 31, 2010. The version for OS/2 very old, but free. In the developed version 5 support of Windows, Mac OS X and Linux is declared.

This language is was created and designed mainly for training in programming.

The environment of development of Liberty BASIC includes the editor of a code, a debugger, and also, in the latests version, the observer of interactive lessons.

Literals

A literal value is hard-coded into a program. Here are two literals, printed to the mainwin. The first is a number. The second is a string of text characters.

print 57
print "Hello World"

produces 
  57
  Hello World

Variables

A variable is a name used in the code that represents a string or numeric value. The value assigned to the variable name may change as the program runs. The program can always access the current value of the variable by refering to its name. In this example, a value of "3" is assigned to a variable called myNumber:

myNumber=3
print myNumber

produces 
  3

String variables

String variable names must end with a $ (dollar sign). Text is assigned to a string variable using the equals sign ( = ). This example assigns "Hello World" to the string variable hello$ and then prints it in the mainwin.

hello$ = "Hello World"
print hello$

The program above produces this in the mainwin:
  Hello World

NOTE- a string can have zero characters. Such a string is often called an empty string. In BASIC, an empty string can be expressed in a string literal as two quotation marks without any characters between them. For example (noCharactersHere$ is the name of our string variable):

let noCharactersHere$ = ""

Global

In general, variables used in the main program code are not visible inside functions and subroutines. Variables inside functions and subroutines are not visible in the main program.c Liberty BASIC 4 introduces the GLOBAL statement to create global variables. Variables declared as GLOBAL can be seen everywhere in a program. The special system variables like WindowWidth, WindowHeight, etc. now have true global status. GLOBALS are specified and used like this:

global true, false, filename$
true=1
false=0
filaname$ = "readme.txt"

Looping

Liberty BASIC provides three constructions for looping. One is FOR/NEXT another is WHILE/WEND, and the third is DO LOOP.

For/next

The FOR . . . NEXT looping construct provides a way to execute code a specific amount of times. A starting and ending value are specified:

for var = 1 to 10
  {BASIC code}
next var

In this case, the {BASIC code} is executed 10 times, with var being 1 the first time, 2 the second, and on through 10 the tenth time. Optionally (and usually) var is used in some calculation(s) in the {BASIC code}. For example if the {BASIC code} is print var ^ 2, then a list of squares for var will be displayed upon execution.

The specified range could just as easily be 2 TO 20, instead of 1 TO 10, but since the loop always counts +1 at a time, the first number must be less than the second. The way around this limitation is to place STEP n at the end of for FOR statement:

for index = 20 to 2 step -1
  {BASIC code}
next index

While/wend

As shown above, Liberty BASIC includes a for/next looping construct. It also has a while/wend looping construct. Here is the above program rewritten using while/wend.

'count to ten
  x = 0
  while x < 10
    x = x + 1
    print x
  wend
end

One useful thing that can be done with while/wend is to wrap the boolean expression in a NOT() function, which effectively turns while/wend into a "while condition is false" do construct:

'count to ten
  while not(x = 10)
    x = x + 1
    print x
  wend
end

Do loop

Liberty BASIC also provides a DO LOOP structure for cases when you want a loop that always executes once and then only loops back as long as a condition is met. It will continue looping back and executing the code as long as the booleanExpr evaluates to true. Here is the routine that counts to 10 using DO LOOP with while and DO LOOP with until.

'count to 10 using "loop while" and "loop until"
do
  print a
  a = a + 1
loop while a < 11

do
  print b
  b = b + 1
loop until b = 11

Arrays

Liberty BASIC supports single and double dimensioned arrays. These can be defined as string or numeric arrays. The extents of the dimensions can be in excess of 2 million elements, if there is enough RAM available.

Arrays that contain more than 10 elements must be dimensioned before they can be used. Arrays are dimensioned with the DIM statement.

DIM students$(20)
DIM ages(100)

Resizing is done to any array with the REDIM statement, but doing so will erase the contents of the array.

REDIM students$(30)

Double dimensioned arrays must always be dimensioned with the DIM statement before use.

DIM scores$(20, 10)

The equal sign ( = ) is used to assign a value to an element in an array. The element is referred to by its index number. The index may be expressed as a literal number or as a numeric variable. The element at index 2 in the following example is filled with "John." The element at index 7 is filled with the number 12.

students$(2) = "John"
ages(7) = 12

Sub 

sub subName ''zero or more comma separated parameter variable names''
  'code for the sub goes in here
end sub

Description:
This statement defines a subroutine. Zero or more parameters may be passed into the subroutine. A subroutine cannot contain another subroutine definition, nor a function definition.

The CALL statement is used to access the SUBROUTINE and to pass values into it. The values must be the same type as the SUB statement defines them to be. So the following example:

sub mySubName string$, number, string2$

is called like this:
call mySubName "string value", 123, str$("321")

Function 

function functionzame(zero or more parameter variable names)
  'code for the function goes in here
  functionName = returnValueExpression
end function

Description:
This statement defines a function. The function can return a string value, or a numeric value. A function that returns a string value must have a name that ends with the "$" character. A function that returns a numeric value must not include a "$" in its name. Zero or more parameters may be passed into the function. A function cannot contain another function definition, nor a subroutine definition.
Note that the opening parenthesis is actually part of the function name. Do not include a space between the name and the opening parenthesis, or the code generates an error.

Right:
  function functionName(var1, var2)

Wrong:
  function functionName (var1, var2)

Returning a Value

To return a value from the function, assign the value to be returned to a variable of the same name as the function. If no return value is specified, then numeric and string functions will return 0 and empty string respectively.

functionName = returnValueExpression

Recursion

Liberty BASIC supports recursive subroutine and function calls. This means that a function can call itself. When it does this it makes a copy of itself, reusing its variable names. The values are not overwritten. It is important to place an evaluation statement inside a recursive function that causes the function to finish and return to the main code. Care should be taken to avoid creating an endlessly looping recursive function. The two examples below contains an "IF...THEN" evaluation that, when met, causes the function to stop calling itself and return control to the main program.

Here is an example of a subroutine which counts down from a number.

'countdown
  print "Let's count down from three."
  call countDown 3
end

sub countDown number
  print number
  if number > 1 then call countDown number-1
end sub

Now here's an example of a recursive function that returns the factorial of a number. A factorial is obtained by taking a number and multiplying it in turn by each integer less than itself. The factorial of 5 is 5x4x3x2x1=120. The factorial of 7 is 7x6x5x4x3x2x1=5040. The numbers get big in a hurry after this. For example, the factorial of 15 is 1307674368000!!

'factorial
  input "Compute factorial for?"; n
  print factorial(n)
end

function factorial(a)
  factorial = 1
  if a > 1 then factorial = a*factorial(a-1)
end function

Examples of codes

The program «Hello world»

print "hello world"
end

The program, which opens with the message «Hello World!»

nomainwin
notice "Hello world!"
end


nomainwin
notice "Example program" + chr $ (13) + "Hello world!"
end

The program opens a window for input

nomainwin
prompt "Enter your name:"; response $
notice "Response:" + chr $ (13) + response $
end

Running third-party application

nomainwin
run "notepad.exe"
end

Reference