GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | UNIT

Edit History

UNIT

UNIT name

Home > Reference > System

Declares the source module as a unit. Units are a set of procedures, functions, constants or variables that can be used by another program or unit.

  • UNIT supports ‘namespace’ (Namespaces allow reuse of same names in different contexts. e.g. BitLib.Set(x) and StrLib.Set(x) are both using a function with the same name, “Set”, but in different contexts).
  • While UNIT can be used as a collection of sub-routines for your own program, UNIT is particularly useful for creating a general-purpose library. General purpose libraries can be useful for many programs or projects, the same way the internal routine “PRINT” is useful for many programs, and not only for specific one.

Use EXPORT to export procedures, functions, constants or variables. Only exported names can be accessed in the main program.

Example 1: Simple Unit

First, an example of the unit. Please save it with the filename “MyTestUnit.bas”

UNIT MyTestUnit

export MyFunc
export MySub
export MyConst = 3.1415
export MyVar = 5

func MyFunc(a,b)
    return a + b
end

sub MySub(a,b)
    print "a + b = "; a + b
end

Second, an example on how to use the unit.

import MyTestUnit as u

u.MySub(1,2)

print u.MyFunc(2,3)
print u.MyConst

u.MyVar = u.MyVar + 5
print u.MyVar

Example 2: An unit for using strings

The UNIT file. Please save as strlib.bas.

' File: strlib.bas
' ------------
' In this demo we are using UNIT to add more useful String commands to
' SmallBASIC, by carefully creating our own String-Library: strlib.bas
'
' This demo includes only two useful functions, Lset() and Rset(); You
' can add more useful functions, subs, constants - but make sure that
' they are all String commands (not Array, File, Data, etc).
'
' Write your code carefully and efficiently, because you will use these
' commands frequently in other projects. And don't forget to debug the
' code and to add clear documentation, for you and for others.
'
' See also Home-->Article-->Welcome to SmallBASIC-->Units.
' ------------
'

' Here we declare that this file is a UNIT file.
' Note: Keep file-name and unit-name the same. That helps the SB to
'       automatically recompile the required units when it is needed (i.e.
'       to create strlib.sbu file).
'       The actual file name must be in lower case for Linux OS.
Unit strlib ' (without .bas extension)
' Here we allow other SmallBASIC files to use some of our library keywords:
Export Lset, Rset

' Left justify string s in buffer of length b.
' example: x = Lset(" SmallBASIC ", 15) ' --> x is " SmallBASIC    "
Func Lset(s, b)
  Local l = Len(s)

  If l >= b Then
    Lset = Left(s, b)
  Else
    Lset = s + Space(b - l)
  Fi
End
' Right justify string s in buffer of length b.
' example: x = Rset(" SmallBASIC ", 15) ' --> x is "    SmallBASIC "
Func Rset(s, b)
  Local l = Len(s)

  If l >= b Then
    Rset = Right(s, b)
  Else
    Rset = Space(b - l) + s
  Fi
End

Demo file, demo.bas, which is using strlib.bas above:

' Here we declare that this file is using another UNIT file.
' Note: The actual file name must be in lower case for Linux OS.
Import strlib ' (without .bas extension)
' Note: To access a member of a UNIT we must use the unit-name, a point
'       and the name of the member, e.g. Strlib.Lset("Hello", 10).
' demo:
Color 14, 1
Locate 4, 30
Print Strlib.Lset("  -->>", 25)
Locate 5, 30
Print Strlib.Lset(Strlib.Rset("This way...", 18), 25)
Locate 6, 30
Print Strlib.Rset("-->>  ", 25)
Pause
Code samples using UNIT
4tune.bas   base64.bas   bolmo.bas   crgb.bas   crow.bas   dogstar5.bas   matrix_solve.bas   neolithic.bas   properties.bas   rogue trader.bas   sierpinski in space.bas   space.bas   star_field.bas   star_field.bas   startrek.bas   startrek.bas   strlib.bas   try rainbow v2.bas   try rainbow.bas   vigenere_cipher.bas  
System
CHAIN   COMMAND   CWD   DELAY   ENV   ENV   EXEC   EXPORT   FRE   HOME   IMPORT   INCLUDE   MAXINT   NIL   OPTION   PAUSE   PI   PROGLINE   RANDOMIZE   RUN   SBVER   SELF   STKDUMP   TROFF   TRON   UNIT  
This page was last edited on Tue, 12 Sep 2023 14:32:33 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.