# Futurebasic/FBtoC
### What is FBtoC?

FBtoC is a replacement for the FB Compiler (FB Compiler.app) . Translating the code to C allows Apple's developer tools to create a universal binary. The diagrams below illustrate the process:
| FB Compiler: | FB source code → legacy app (PPC-only, CFM format, resource fork...) |
| FBtoC: | FB source code → C source code → OS X app package with Carbon universal binary in Mach-O format |
FBtoC translates appearance projects and standalone files, and console-type text-only standalone files. PG projects cannot be translated. Using FBtoC normally requires no detailed knowledge of C.

#### Why translate to C when I code in BASIC?

* The compiler (gcc) has more powerful optimizations for speed than the FB compiler.
* Native Intel code runs ≈3 times faster (or more) than PPC code under Rosetta.
* Framework access is much simpler than in FB (see later).
* FBtoC can automatically copy specified files into the app's resources (see later).
* Apple's modern development tools work on apps built by FBtoC. Tools include the Xcode debugger and the performance analyser Shark.
* An app can be built as small as 64 KB.
** (window 1 : print "Hello, world!" : do HandleEvents : until gFBQuit)
* On an Intel Mac, crash reports are interpretable, unlike those of a legacy FB app.
* Unlike FB, there is no 32k limit on local function variables.
* And then there's the coveted Universal Binary status!

## Installation

This section describes the installation of both FutureBASIC and FBtoC. Before continuing, you will need to obtain the following:
* The FBtoC installation files and documentation from: FBtoC Project Page
* The Apple Develop Tools, available on your OS C installation DVD or from Apple's Developer Connection Web site

### System Requirements

FBtoC should work on OS X 10.3.9 and later. Most development and testing has been done on OS X 10.4 and 10.5. Before using FBtoC you should have installed Apple's free Developer Tools (a.k.a. Xcode Tools), from your OS X install CDs or DVD.
To create universal binaries, you must have OS X 10.4 or higher, gcc 4.0 or higher, and install the Cross-development SDKs. Without /Developer/SDKs/MacOSX10.4u.sdk, only the native architecture of your Mac can be built. The Finder view (below) of the Developer folder shows the SDKs after installation. Cross-development is an optional part of the Xcode Tools install (from the OS X DVD or CDs).
See also Apple's documentation: Apple Developer Tools Documentation

### Installing FBtoC

Just drag-and-drop. The folder containing FBtoC.app must also contain three special folders: build_goodies, Headers, and User Libraries.
The build-goodies folder contains pretranslated parts of the runtime. Changing any of these files is likely to break FBtoC.
The Headers folder contains a subset of the standard FB Headers files. Most of them needed modification to make them work with FBtoC. In general, an existing FB Headers file will not work if simply copied into this folder.
User Libraries has the same function as in FB, except that the little-known special file UserFloatPrefs is ignored by FBtoC.

## Differences Between FutureBASIC and FBtoC
### Mildly bad news

Some things that have to be done differently in FBtoC:
| compile long if | replace by #if.... |
| enterproc | label must match the enterproc fn name |
| autoXREFCurr& | different syntax; see later |
| DynamicRemoveItems | different syntax; see later |
| ControlButtonContentInfo | different syntax with '.u'; see later |
| HMHelpContent | different syntax with '.u'; see later |
| Hndl | replace by Handle |
| proc | different syntax; also, label *must* precede line in which proc appears |
| 100#, 100! | FBII format; rewrite both as 100.0 |

## Features not implemented

Due to FBtoC operational constraints and other more obvious constraints (e.g. Intel doesn't support PowerPC assembler -duh), some legacy syntax cannot be implemented. However, a modern equivalent to achieve a similar goal often exists. Updating legacy source code to a modern equivalent benefits from newer features and helps preserve the longevity of the source code.

### 1. Legacy syntax and language

| beginassem ... endassem | FB assembler is incompatible with gcc (even on PPC) |
| tbalias | discussed later |
| dim record | FBII pseudo-record; use true record instead |
| dim as int x;0, hi as byte, lo as byte | aliased vars would lead to endian bugs |
| myLong&[0] | would lead to endian bug |

### 2. Not currently implemented

appleeventmessage$ def SomeAreButSomeAreNotAvailable DynamicInsertItems event& event% flushevents // instead use the Toolbox: call FlushEvents( _everyEvent, 0 ) folder get field on appleevent on break on edit on expr on finderinfo on lprint on overflows on stop override local fn SomeFunction, override runtime SomeRuntimeThing (but override _someConst = 1 IS supported) parentID picture field syserror timer [function]

### 3. Toolbox aliases not supported

The TBalias statement in FB allows old Toolbox names to be kept and applied to new functions when introduced by Apple. The idea must have been to save programmers a minute or two updating function names, a task otherwise tiresomely required every few years. The TBalias feature is now Considered Harmful and will not be supported by FBtoC. You may have already upgraded your project to use the correct (Apple's official) Toolbox names. If not, be prepared for FBtoC error messages indicating dodgy names:
The simple, recommended, and only, workaround is to replace each occurrence in your code of the fossil RmveResource by RemoveResource.
Some Toolbox functions that were commonly aliased in old FB code are listed below:
| FB, FBtoC and C | FB-only alias |
| GetIntlResource | IUGetIntl |
| MemError | MemErr |
| DisposeHandle | DisposHandle |
| DisposePtr | DisposPtr |
| AppendResMenu | AddResMenu |
| GetMenuHandle | GetMHandle |
| CountMenuItems | CountMItems |
| EnableMenuItem | EnableItem |
| DisableMenuItem | DisableItem |
| GetControlValue | GetCtlValue |
| GetControlReference | GetCRefcon |
| PBReadSync | PBRead |
| PBCatMoveSync | CatMove |
| PBHCreateSync | HCreate |
| PBFlushVolSync | FlushVol |
| AEPutParamDesc | AEPutKeyDesc |
| SecondsToDate | Secs2Date |
| LongSecondsToDate | LongSecs2Date |
| RemoveResource | RmveResource |

### 4. Missing Headers files

FBtoC's Headers folder doesn't have this file yet.

### 5. Incomplete Headers files

Util_Files.incl is present in FBtoC's Headers folder; the error arises because the function usr FSFileExists has been conditionalised out by some petty official, one hopes temporarily. [Note added later: FSFileExists is now implemented]

## Sample translations by FBtoC

Elderly FB code preserved in the FBtoC runtime:
One of FBtoC's many internal functions: