Computer Languages

Chipmunk Basic Interpreter

[ Back to Lecture Notes ] [ MS QBasic ] [ Yabasic ] [ JavaScript ]
[ Resources > BASIC Interpreters ]

Interpreted Program Translation

An interpreted computer language is a high-level language that is translated into binary machine code each time it is read from a source file. Disadvantages of interpreted languages are mainly that there is a translation time penalty each time the program is run, and the language interpreter must be present on each computer that will run the program. However, as a development tool interpreted languages are useful because a program prototype can be written and tested quickly, and since most interpreters step through the program line by line errors are easily localized and identified. Since most assemblers and compilers have a steep learning curve just to master the development environment interpreters are also ideal as learning tools.

CIS 120 is about computer concepts in general, not about programming in particular. With that in mind only the very essential BASIC statements, functions, and operators will be used. Recall that, as a minimum, we want a program to organize data, calculate results, and present the results. We will be looking at ways BASIC can get user input, manipulate the input (data), control program flow as needed, and produce output useful to the user. Consult the documentation for the BASIC implementation you are using for additional information.

[ TOP ]

BASIC Data Types and Operators

BASIC supports most of the data types discussed in the text book in one form or another: integers, real numbers (floating point), characters, and Boolean as well as character strings, arrays, and records. We will only be working with the basic, sometimes called "primitive," types. In BASIC the character string is considered a basic type rather than a data structure. Unlike Microsoft QBasic (and the older GW-BASIC), most BASIC's do not have a distinct integer type; all numbers are real numbers in a precision specified in the documentation for the specific implementation. Recent BASIC interpreters are typically written in C or C++, and therefore inherit the precision of the of the compiler that built them. Strings are specified by appending a ' $ ' character to the end.

BASIC uses mostly familiar or intuitive operators, such as +, -, *,and / for addition, subtraction, multiplication, and division. An operator that may be new to you that is supported in virtually all programming languages is the Modulus division operator. Modulus division returns the remainder rather than the quotient. In QBasic, and many other BASIC's including Chipmunk BASIC, the modulus operator is MOD ; In Yabasic modulus is implemented as a function.

This differs from JavaScript and the "C Family" of languages that use % for modulus division. For example 19 MOD 4 is 3: 19 divided by 4 is 4 with a remainder of 3. Modulus division is very useful for many problems in computers; think back to our procedure for converting a decimal number to binary or hexadecimal. We started with the highest power of the base that could be divided into our decimal number, then kept working down by dividing the remainder by each successive lower power of the base.

Boolean conditions (named after George Boole) are tested with the familiar < and > symbols used in mathematics for "less than" and "greater than". "Less than or equal to" and "greater than or equal to" use the <= and >= operators respectively. Equality is tested with the = operator and inequality ( "not equal to" ) is tested with the <> operator. This differs from the "C Family" of languages that use == for equality and != for inequality. Note that BASIC uses the same operator ( = ) for assignment and equality; the interpreter knows the difference from context.

[ TOP ]

Input and Output

We will use the INPUT statement for program input and the PRINT statement for program output. The following BASIC statements will prompt the user for a name and then issue a greeting:

Output:

The INPUT statement takes an optional prompt string in quotation marks followed by a comma separated variable list. It is generally best to only fill one input variable for each INPUT statement. Notice how any extra spaces must be included inside the quoted strings in the PRINT statement; the semi-colons separating the PRINT arguments cause the arguments to run together.

[ TOP ]

Conditional Control Statements

The Chipmunk BASIC control statements that allow a program to branch to different areas based on some condition are:

Chipmunk BASIC is a bit quirky in this area. The block IF . . . ENDIF does not use the THEN keyword. You cannot add a comment on an IF line that does not use THEN. You must include the colon ( : ) between the end of the THEN statement and before the ELSE. And finally, there is no support for an ELSEIF, which would be handy for multiple tests.

Output: Look at line 100 in the code sample above:
100 IF order >= 100

Why do we us the "greater than or equal" operator ( >= )?
What happens if we modify it to this:
100 IF order > 100

We can also write the condition using ELSE, which makes the statement's intent more explicit that this is an "either - or" test:

MS QBasic and Yabasic support ELSEIF (but not quite the same syntax!). Chipmunk BASIC does not implement ELSEIF, so multiple conditions are tested with a series of IF statements. You must be very careful to ensure that only one of the IF statements will evaluate to TRUE under any given input. However, notice how this construct is very explicit and "self documenting."

Output:
[ TOP ]

Iteration (Loops)

The Chipmunk BASIC control structures that allows a program to repeat a section of code more than once based on the value of a condition are WHILE . . . WEND and FOR . . . NEXT. All loops can be written with one basic form even if that one form is not always the clearest or most efficient. However, when the number of repetitions is known in advance, the FOR . . . NEXT loop is generally preferred.

Here is a simple Chipmunk BASIC loop using both forms:

Output:
    0 1 2 3 4 5 6 7 8 9
    0 1 2 3 4 5 6 7 8 9

The WHILE . . . WEND loop tests whether the condition is TRUE or FALSE at the beginning of the loop. If, going into the loop, the condition is FALSE the program code in the body of the loop will never execute. See the next example where we "pre-load" the condition variable with a value guaranteed to test TRUE so that we ensure the code in the loop body executes at least one time.

Note in the WHILE . . . WEND loop the importance of doing something that will eventually result in the condition returning FALSE. If the condition variable never changes, the loop will execute forever (assuming the initial condition value was TRUE) resulting in an infinite loop.

This next loop is a bit more involved; it prompts the user for a number and saves it in a variable called sum, then enters a loop where it prompts for another number to add to sum. Note how, before entering the loop, the answer variable is pre-loaded to force at least one pass through the loop. At the end of each loop the user is asked if she wants to continue. Note also how the response is converted to upper case to simplify the test for a no answer.

Output:

Most high-level languages support more than one looping construct so that you can write code that expresses your logic. Microsoft QBasic has a DO {WHILE | UNTIL condition} LOOP, and Yabasic has a REPEAT . . . UNTIL ( condition ).

[ TOP ]

Strings

BASIC has some interesting and useful string manipulation functions. Research the LEN, MID$, ASC, and HEX$ functions. Here is an example of how you might extract various pieces of a data record to build a string for a report:

Output:

Here is a way to handle extracting pieces of a string when you don't know where or how long the individual pieces are. Note how the string length function LEN is used as a loop upper bound control; then the MID$ extracts one character at a time, checking for a blank space. Each character is then concatenated (look it up in a dictionary!) to a new string.

Recall the discussion of ASCII character codes and the need to frequently know how to represent numbers in hexadecimal as well as decimal:

Output:

Now if we were to modify the above example to take input from the keyboard inside a loop it might actually be useful from time to time. Hmmm...

[ TOP ]

Where to Next?

Although this discussion is mostly about the Chipmunk BASIC interpreter, an excellent source of additional BASIC information is the QBasic Help system (see QBasic lecture notes and QBasic download information on the Resources Page) and the Yabasic documentation. Both QBasic Help and Yabasic documentation have many examples. Run these examples in Chipmunk BASIC, then experiment by making small, incremental modifications to see what happens.

Put a copy of basic.exe in your working directory, or put it in a directory that is in your PATH. You can download the source files from the links below, or you can copy the code from the examples into a text editor and save them as BASIC source files (the usual extension for BASIC source files is .BAS). Execute a BASIC program in Chipmunk BASIC as follows:

Chipmunk BASIC programs run from the command line as above do not need line numbers. However, you can also enter programs into Chipmunk BASIC directly from the console, run them, make changes, and save them. When entering from the console you need line numbers; to modify a line just type the line number and the new text for the line at the Chipmunk BASIC prompt. All of the above examples can be copied from the browser and pasted into a Chipmunk BASIC console and executed with the RUN command. Quit a Chipmunk BASIC console session by entering any of the following commands:

Here is an example Chipmunk BASIC console session. The > character on the left edge of the screen is the Chipmunk BASIC prompt. Program lines begin with a line number. Commands immediately following the > prompt are Chipmunk BASIC commands. The BASIC language is not case sensitive; BASIC language statements and Chipmunk BASIC commands are shown in upper case to make them easier to see.

Chipmunk BASIC console session
Chipmunk BASIC console session

The only way to learn a language, like learning to play a musical instrument, is to jump in and practice. Look at some examples in the QBasic Help topics for various keywords and try to put a few together to do something useful. However, unlike learning a new musical instrument, at least you won't annoy the neighbors or make the dog howl...

[ TOP ]

Example Source Code

Download example source code.

Source files are listed in the order presented above:

[ TOP ]

Revised: 10 MAY 2002 09:46