Small Basic: Grammar Basics

This article describes the main language elements or 'grammar' used by the Small Basic language and gives some suggestions for further study.  There are several 'getting started' guides, this is just another with a slightly different emphasis.

Language Elements

Programming tells the computer what to do.  We type commands into a text editor and when we want to run it, the words we have written are turned into instructions that the computer can execute - this is called compiling (turn the words of program into computer instructions).  Most programmers never bother with these instructions which are just very basic instructions for the cpu; they just use the words of the programming language they are using.  The words are called your program or source code.

While the computer is very fast, it is stupid - it only does what you tell it to do, nothing more and nothing less.  If your program doesn't work it is almost always your fault, perhaps you mis-typed something or the logic was faulty.  We all make mistakes and this is fine, it is how we learn.  Also, it is good because it is completely logical and we can work out where the problem is (debug the program), the computer never has a bad day!

You cannot damage anything by just trying stuff in Small Basic - it may not work, but no harm done.  The one exception in Small Basic is the File Object (you could delete files - so stay away from this to start with).

The Small Basic language has some main grammar features:

  1. Keywords - these control the logic of the program such as loop (repeat something For or While) or conditional (branch where the program goes depending on the value of a variable - If), define a subroutine (Sub) etc - they are colored blue in Small Basic.
  2. Variables (black) - these are names given to things we use and store and do things with - think of them like draws that we can put things in and store these things while we do stuff with them.
  3. Literals (orange) - Values that are not stored in variables, like a number 3.14 or set of characters in quotes "Hi There!".
  4. Objects (gray) - these are special functions that have been provided to do certain things, like add a picture to a display.  There are several of them in Small Basic, like TextWindow, Clock etc.
  5. Methods, Parameters and Events (dark red) - these are found for the different objects by typing a dot after the object.  They do the special things - they may also need some data to work with (called parameters) or return some data.
  6. Comments (green) - anything after a ' (single quote) is not read by the compiler it is just comments for you to remember what you did or why - make good use of these.

There are plenty more we could say about all these, but this is plenty to be getting on with.  The key is to learn by doing, like playing a sport or musical instrument.

Small Basic only has a small set of keywords, objects and methods compared to most languages which makes it easier to get started.

The first program we usually write in any language is the 'Hello World' program that writes this out.

In Small Basic, this is:

TextWindow.WriteLine("Hello World")

TextWindow is an Object
WriteLine is a Method, the brackets show that this method needs some data to work with - in this case, what to write.
"Hello World" is what we want the write and it is put in double quotes - and is called a literal since it is not a variable, but is just a value (not in a draw).

We then run the program (we can save it first if we want) and the computer converts it into instructions for the computer and then does it.

Statements

Each line of your code is called a statement, it is like a sentence and must adhere to the language grammar.

They may be one of the following:

Assignment

A variable is set to contain the result of something else.  '=' is used to define the assignment.  For example we can set the variable x to contain the value 3.14.

x = 3.14

Or we could set the variable 'ellipse' to a new circle (ellipse) with radius 20 pixels.

ellipse  = Shapes.AddEllipse(20,20)

Object Function

Perform an operation using an object, for example clear the GraphicsWindow.

GraphicsWindow.Clear()

Keyword

A special keyword statement like defining a subroutine block, a loop block or a conditional block.  A block is a group of statements within the special keywords.  These keyword blocks are are used to control the program flow by doing the contents of a subroutine, repeating something (loop) or doing different code depending on values in variables (conditional).

For i =  1 To 10
  Output()
EndFor
 
Sub Output
  If (Math.Remainder(i,2) = 0) Then
    TextWindow.WriteLine(i+" is even")
  Else
    TextWindow.WriteLine(i+" is odd")
  EndIf
EndSub

Note that a subroutine is called by a statement with the subroutine name followed by (). 
For a list of the 14 keywords used in Small Basic go here.

Next Steps

The first place to start is the 'Introducing Small Basic' pdf document that comes with the installation.  It can also be found online as a wiki article.

Next check out the links on http://smallbasic.com/, including the forum to ask questions and see what others are doing, the wiki to see Small Basic related articles, blog for featured articles, API reference and curriculum among others.  The curriculum is good place to work through the capabilities of Small Basic, testing yourself as you go.


Other Languages