BASIC to C
Martin Carradus explains how to use his BASIC to C translator
Introduction to the use of !BBC_C
These notes describe what the application !BBC_C does and the various options that !BBC_C presents to the user. !BBC_C translates from Acorn BBC BASIC held in its associated file into Acorn ANSI C as a text file suitable for an Acorn C compiler. In order to understand the advantages of translation from BASIC to C you will need to know something about the difference between compilers (C) and interpreters (BASIC).
All computer languages need to be translated into the fundamental instructions of the particular computer (machine code). A compiler is presented with the complete program and translates the whole thing into machine code. The machine code is then run on the computer in a separate stage. An interpreter, however, translates the program and obeys it as it goes along. The advantage is that you do not need a preceding stage before you run the program. The disadvantage is that, since the whole program has not been "seen", the final code is usually less efficient and runs more slowly than compiled code. The compiled C that !BBC_C produces seems to run about three times faster than the same interpreted BBC BASIC.
In order for an interpreter to execute the program efficiently, it is usually coded so that it can identify keywords/commands quickly. The main "trick" is to code the keywords (words like GOTO, or IF or PRINT) into only a couple of characters (bytes), known as a token. This means that they can be easily recognised every time the interpreter runs across them in the program. There are over 160 different "tokens" used in BBC BASIC files.
At the beginning of each line of the program, the interpreter will need to know the line number of that line and where the next line is. Both are coded for greater efficiency.
Almost all types of BASIC use an interpreter.
Due to the nature of BASIC, it can do things "on the fly" that a compiled language cannot. Also BBC BASIC was designed without any formal structure (the grammar of the language) in mind, whereas the C language has a formal grammar. In particular, the C language and its compiler are very particular about the mixture of different data types (i.e. storage locations (variables) designated as integer, floating point or character string) whereas BBC BASIC uses these locations interchangeably. For these reasons, you should know something about the C language before using !BBC_C in order to understand any compiler error messages.
Drag the Application to a safe (preferable new) Directory on your hard disk. Ensure you have the latest versions of the Modules required by using !SysMerge with the version of !System supplied. !BBC_C issues a warning message if your modules are out of date. For RISC PCs double click on !Boot. Clicking on "System" on the panel presented offers you the option of merging the !System supplied on this disc with your !System.
Double click with the mouse on the !BBC_C icon to install it on the icon bar. Click on bar icon to bring up the application panel. Click the "menu" button on your mouse on the bar icon for other functions including Info and Help. The RISC OS on-line !Help application gives descriptions of the options available from the main application panel.
For !BBC_C run the BBC BASIC Program through !BBC_C by typing in the file name, dragging the file icon into the application window, double clicking on the file icon or dragging the file icon onto the bar icon. Choose the options required from the panel presented and click on the "Run" button. The effect of these options is more fully described below.
The C text file is eventually offered for saving prefixed by a capital "C". After checking it principally for variably dimensioned arrays and undeclared variables (commented out) , place it in a directory called "c" in another directory along with "h" which contains the files "leaf" and "data", and a directory "o", all of which are needed by the compiler.
Present the C file to the compiler. There may be many messages concerned with incorrect casts as BBC BASIC is not too particular about data types, whereas the C compiler is. The author has attempted to minimise such messages.
A beta version of "LeafLib" is included in the directory "$.o". Include its file name under "Libraries", from the menu associated with the compiler panel, separated by a comma from the other standard libraries. It is needed during the "Link" phase of the compilation in order to supply certain routines not given in the Acorn BBC specific library.
Though every attempt has been made to encompass all forms of construct available in BBC BASIC, !BBC_C still sometimes doesnt recover from syntax errors and produces a blank output file.
Additional Notes for Acorn C Compiler Version 5
For the Acorn C Compiler Version 5 onwards you will also need the non-standard header files "bbc.h" and "os.h" in your project "h" directory and to reference the library "RISC_OSlib", (Needed during the Link phase) under "Libraries" option off the main panel. This is in addition to "LeafLib".
The Translator now only seems to object to the operator "=" if it is a test for equality. If you mean it to be this enclose it in brackets (e.g. A=(B=C) instead of A=B=C ). Similarly "=" used in FNs to return values must be separated from any preceding expression. Logical expressions after IF, WHILE and UNTIL are taken to be C logical expressions (with "&&", "||", "^^" and "!" for AND, OR, EOR and NOT respectively. If any logical sub-expression is bracketed then it is taken in a bitwise manner with "&","|","^" and "~". In all other contexts logical expressions are taken to be bitwise.
FNs returning character strings are sometimes taken as returning a numerical value and the Translator grammar objects. You can force such FNs to be character string type by giving them a name postfixed with "$"(e.g. strcat$ ) to indicate that the return value is a character string. Generally the Translator determines the return value of a FN by the context in which it finds the FN.
For the reasons above, a logic expression starting with an FN will be taken to be numeric to start with. If the FN is in fact returning a character string and this is being compared with a string literal e.g FNmess>•Hello• then reversing the expression i.e. ”Hello•<FNmess will make !BBC_C take the FN as returning a character string. !BBC_C then does not produce an error message. Also !BBC_C "remembers" the return value of a FN from above.
Another advantage of translation to C is that C is a portable language and compilers for it exist on other brands of computer. In simple cases of program it is possible to get the translated BBC BASIC program to eventually do the same thing on another computer. However, BBC BASIC in more complicated programs makes use of facilities that only exist on Acorn computers and which are handled differently on other machines.
Two examples are the use of raw Acorn assembler (translates directly into the machine code of Acorn computers) and SWIs (SoftWare Interrupts). SWIs are pre-written routines that enable the handling of various Acorn constructs and do not exist on other machines. There are also literally hundreds of SWIs that are available.
Lastly BBC BASIC programs sometimes make calls to the operating system of the Acorn computer and the commands are different on other computers.
Using the !BBC_C Set Up Panel.
The generated C is offered for saving within the same directory as the source BASIC program with the same name prefixed by a capital "C". Click on "OK" or press the Return key if you are satisfied with this, otherwise alter the file name. If you hold down "Control" and press "U", the slot is cleared and a new name can be typed in and the icon dragged to the directory of your choice.
Choosing this option could cause calculations with the relevant variables in other places to be truncated, so it should be used with caution.
Single Precision Button.
Remove REMs Button.
All Lower Case Button.
Integer, String and Float Terminators.
Note this principal difference between BBC BASIC and the C language, as mentioned above. In BBC BASIC, variable names become available immediately they are mentioned anywhere in the code whereas the C language requires that all variables must be declared before they are used.
The BBC BASIC interpreter is also able to cope with such names beginning with numerics and containing keywords in the language. The C compiler requires that identifiers begin with an alphabetical character, so numerics are converted to "a" - "i" and "z" for zero e.g. "1st" becomes "ast", "0th" becomes "zth" and a procedure called PROCTO becomes "procto".
There should not be any need to alter the default values. As with variable name terminators only up to two upper or lower case characters or the underscore character may be entered in these slots.
There should not be any need to alter the default value supplied. As above only upper and lower case characters or the underscore character may be entered in this slot.
DEF Argument Terminators.
Should you wish to disable this facility, clear out the slot.
Primary and Secondary Indentation.
The secondary indentation is added to the primary for each programming structure encountered (IF, REPEAT, WHILE, FOR and CASE). It is reduced by the same amount when the structure ends (with ENDIF, UNTIL, ENDWHILE, NEXT or ENDCASE respectively). This also makes the translated C code more readable. The indentation in no way affects the speed or size of the compiled code, as the compiler ignores any embedded spaces. However the supplied BBC BASIC may have been "squeezed" to remove spaces, as these do affect the speed of the interpreted BASIC.
Errors:- Quiet Button.
Some programs produce many error messages, so this option is enabled by default to avoid constantly having to respond to the error panels.
If !BBC_C terminates with a "Untrapped Parser Error" message, then it has fallen over at some point and the output file will be empty. In any case, the total number of errors is given. Use the tips above to reduce the errors to a minimum. In some cases, badly bracketed programming structures result in !BBC_C searching for a terminating statement (containing ENDIF, ENDWHILE, UNTIL, NEXT or ENDCASE), which is never found and the program falls over.
!BBC_C does attempt keep track of structures and close higher structures when lower ones close e.g. a WHILE loop within a CASE structure will be closed with an injected ENDWHILE upon encountering a new WHEN or OTHERWISE.
Order of Identifier Declarations.
With 0 in it, the identifiers are just added to the end of the list every time a new identifier is encountered, so they are declared at the head of the generated C code in "As Found" order. With 1 in the icon, the identifiers are kept in alphabetical order and with 2 in the icon, the identifiers are further sorted on their "type", that is whether they are variables, functions or procedures. The option "2" is selected by default.
It is recommended that you should at least select option "1", or better still "2", because it makes it easier to search for a particular identifier in the declarations at the head of the generated C code. Also with "As Found" order, the search for new identifiers in the symbol table is less efficient and can slow down !BBC_C.
Keep Separate List of Local Variables.
Variables local to the function or procedure are their arguments and any variables declared as LOCAL in BBC BASIC within the routine.
This option can often reduce the number of syntax errors and furthermore BBC BASIC functions are often given much more accurate data types for the value that they return. For these reasons, this option is enabled by default.
Named Wimp Control Block/s.
Further Technical Information.
Certain BBC BASIC constructs were found to be untranslatable into an equivalent construct in the C language. !BBC_C will warn you in the generated C code where this occurs and the supplied "TechNote" gives a list of those things that !BBC_C is unable to handle. Most of these constructs are only rarely used.
If you clear the variable terminator slots to spaces, !BBC_C returns to its default e.g. for the integer variable terminator, it will take the value "_i" if the slot is cleared out. However the other terminator slots can be cleared to a null string.
Having entered a writable slot, pressing the "Return" (Enter) key moves the cursor in turn to the next writable slot. Pressing the "Return" (Enter) key when the final slot has been reached has the effect of running the application.
StartUp Banner Panel.
Description of the Icon Bar Menu.
When the !BBC_C application is run by double-clicking on its icon, the icon is loaded onto the icon bar. Clicking "Select" on this bar icon gives the "Set Up" panel described above, but clicking "Menu" will present the user with the following menu.
Clicking on "Quit" immediately removes the icon from the icon bar and aborts the application.
Clicking on "Help" causes a window to appear giving descriptions of the options available from the "Set Up" panel.
Clicking on "Save options" will cause the set of options that you have chosen from the "Set Up" panel and this menu to be permanently saved to a file within !BBC_C. This means that your version of this application will always come up with your set of options rather than default ones every time you run !BBC_C.
Moving right over the arrow besides "Info" displays an information box.
Principally this will tell you the version and date of the Translator you have.
Moving right over the arrow besides "Options" will produce a display.
The first set of options you are given causes !BBC_C to automatically run when it receives the file name without having to click the "Run" button - Auto Run. Secondly you can cause the translated C to be automatically saved without having to click "OK" on the "Save As" box mentioned above - Auto Save. Both options are enabled by clicking on the menu item which causes it to be ticked on the menu display.
Moving right over the arrow besides "Display" will produce a display.
By default when !BBC_C runs it produces textual information. By ticking "Summary", instead a Summary window is displayed when !BBC_C runs. This looks like this.
The number of lines given is not the number of lines of C code output, but the number of lines of text that would be displayed if the "Text" option had been chosen. Not both "Summary" and "Text" can be chosen at the same time.
All the menu options chosen will be saved when "Save options" is clicked on.
Tips for avoiding !BBC_C Syntax Errors.
Determining return values of FNs.
Bitwise ANDs, ORs, NOTs and EORs.
Also, where the code is expecting a logical expression, !BBC_C can distinguish between those expressions that require to be taken in a logical or bitwise manner. Mixed expressions will cause !BBC_C to emit syntax error messages.
Badly bracketed programming structures.
Returning values from FNs.
Similarly a test for equality in an IF statement (e.g. IF a%=b% c%+=1) should be bracketed (i.e. IF (a%=b%) c%+=1) because of the same conflict as to whether the "=" is being used as a test for equality or for returning a value from a function.
EVAL, Variable GOTOs and Variable GOSUBs.