Commodore‎ > ‎BASIC‎ > ‎

Syntax

Syntax is just the technical rules for writing computer programs.  For example, the correct spelling of keywords, naming of variables (or literals), order of expressions, and the proper use of punctuation.  Computers are very meticulous, and often are very picky about minor details that people easily overlook.  I think this is because most people are much more focused on semantics: the meaning/purpose of the instructions (not the details of how they are written, which is syntax).  In contrast, computers have little or no idea of the meaning/purpose.  To them, a program is just a task they must perform.
 
This page gives a summary of the syntax used in Commodore BASIC, and is meant as a reference to accompany other pages that give details on particular keywords.  The main points would be:
  • Programs consist of a set of numbered lines of psudo-English text.
  • A program line begins with a line number (0 to 64999).
  • A statement(s) follows the line number.
  • Multiple statements may appear on the same line by seperating them with a colon (:).
  • The length of the line is limited based on the host computer (typically 80 characters).
  • Within a statement, and between statements, the inclusion of spaces is often optional.
  • The keywords (and variable names) used in statements are entered in-full without any use of the SHIFT key.
  • Many keywords (no variable names) may be abbreviated by using the SHIFT key.
A bit off-topic, but the Commodore BASIC interpreter allows for full-screen editing.  So the user may reposition the cursor anywhere on-screen and type-over, delete, or insert characters to revise a line listed on the screen, and save the update by pressing the Enter or Return key.  This (or something similar) is typical of programming these days, but many other BASIC interpreters at the time were line-oriented.  So a program line shown on-screen could not be directly edited by the user in most other versions of BASIC at the time; those other versious would need to invoke a special EDIT command to specify which line they wished to change.  Even today, some operating systems are line-oriented (how quaint).
 
As a practical matter, BASIC programs run faster and use less storage space when multiple statments appear on the same line.  Because of this, and along with the limited line length mentioned above, it was common to find (or create) programs with virtually no spaces!  Spacing is generally only important in quoted text (i.e., a literal string).  This might also be due to human laziness.  Regardless, the result is hard to read, as you might imagine:
10A=A+1:PRINTA:IFA<7THENGOTO10 
Imagine seeing hundreds or thousands of lines like that... and much longer ones would be more typical.  Yuck!  Examples I provide generally use a lot of spaces for ease of reading, but feel free to omit spaces if you like.
 
Another trick to put more statements on a line is with the use of abbreviations with the SHIFT key.  The most common example is probably entering ? (Shift+/) to abbreviate the PRINT keyword.  Another example is GO[Shift+S] to abbreviate GOSUB.  With the exception of ? as an alias for PRINT (common in most every BASIC), the use of abbreviations for keywords is unique to Commodore BASIC.  There are a couple of problems with this scheme.  First is that some relatively long words have no abbreviation due to the way abbreviations are "inferred".  Related to this is that it may not be obvious what is the correct abbreviation.  I have listed the abbreviation (if there is one) on my detailed sub-pages.  Another problem is that when listed, the keywords will be printed in-full (not abbreviated) which means a line may be printed longer than 80 characters which makes it difficult to edit (due to line length limitation noted above).
 
I guess this would be a good place to point out that the same rules apply to entering a command, except there is no line number at the beginning.  A command is executed as soon as the Enter/Return key is pressed.  This is known as direct mode; the line is not stored in the program.  Most program statements can be entered as commands in direct mode.  Similarly, most commands can be included as statements within a program line.  Consequently, the destinction between "statements" and "commands" is a bit arbitrary, although a few will only work in one mode (direct or program) but not the other.  My CBM BASIC Reference always tells you which mode(s) is(are) valid.
 
Many of the statements used in BASIC require (or allow) multiple arguments to be specified in addition to the keyword that begins the statement.  My CBM BASIC Reference describes the BASIC statements and commands using a style to represent the syntax expected by the BASIC interpreter.  In summary, these styles are:
  • LETTERS{Shift+Char} is used to note an abbreviation; for example GO{Shift+S} for GOSUB.  No spaces between the characters!
  • UPPERCASE is used to note any keyword spelled in-full.  No spaces between the characters!
  • BOLDUPPER is used to note the specific keyword (some statments use multiple keywords)
  • italicized_text reperesents a user-supply value; generally a literal value or a variable name
  • { Option1 | Option 2 } is used when one of the 2 (or more) possibilites is required
  • [ Option ] is used when a single argument may be omitted
  • An elipses (...) indicates the previous option(s) may repeated an arbitrary number of times
  • Other punction (commas, parentheses, etc.) must be used used where shown
Some BASIC statements allow multiple optional arguments, and/or allow an optional argument which has a required possibility.  Since that can be confusing, I give some examples to clarify the issue.
 
Example 1:
O{Shift+P}
OPEN file_number, device_number [ , channel [ , filename ] ]
 
Here OPEN is the keyword which may be abbrievated as O Shift-P (in lower-case font, "oP").  File_number and device_number are required and must be seperated by a comma (,).  Spaces may appear between OPEN and the file_number, between file_number and the comma, and/or between the comma and the device_number.  The words "file_number" and "device_number" are NOT typed as shown. Instead the programmer must substitute an appropriate value. (In this case they may be a literal number [such as 8], a variable name [such as D], or a generic expression [such as D+1] ).
 
The channel is an optional parameter.  If it is included, a comma must precede it.  The filename is also optional, but if it is included then a comma and the preceding (formerly optional) channel and its comma must all be included.  So here are some concrete examples:

OPEN 8,8

OPEN 8, 8, 15

OPEN8, 8, 15, "S0:MYFILE"

OPEN8, 8, , "S0:MYFILE" : REM this generates SYNTAX ERROR (missing channel)

 
Example 2:
D{Shift}L
DLOAD filename [ { , ON device_number } ]
 
Here DLOAD is the keyword (when spelled in-full).  The parameter filename is always required.  The stuff enclosed in square brackets at the end is optional -- you don't need to specify device_number.  However if you want to include something in the square brackets, then you must choose either a literal comma or else the keyword ON (not both!), along with the letter U (typed literally), and followed by the device_number.  I've simplified the syntax for this example (in reality there are other optional parameters).  Anyway, here are some concrete examples:

DLOAD "MYPROG"

DLOAD "MYPROG" ,U 10

DLOAD "MYPROG" ON U 10

DLOAD"MYRPOG",U10

 
Hopefully you found this more informative than confusing.  Like I said, computers can be picky about little details.
 

© H2Obsession, 2014
Comments