Mixing C and BCX code
BCX allows you pass inline "C" code using the "!" operator.
! inline "C" code operator
Syntax:
! /** C statement is placed here */
|
Purpose: Mix a block of C code with BCX code.
Example: The following sample shows how to create a C coded local DWORD variable.
PRINT GetMachineName$() FUNCTION GetMachineName$ LOCAL A$ ! DWORD b; /** allocate a local variable in "C" and include C comments! */ b = 256 GetComputerName(A$,&b) ' the& operator means "PASS THE ADDRESS" of b FUNCTION = A$ END FUNCTION
$CCODE directive
Purpose: BCX allows you pass inline "C" code using the $CCODE directive. A $CCODE directive is placed before and after the "C" code.
Syntax 1: $CCODE /** C statements go here */ $CCODE |
Remarks: When $CCODE ... $CCODE is used without an optional parameter the inline "C" code is embedded as placed in the translated code.
Example:
PRINT "BASIC Code Here." $CCODE // declare the variables: int nNumber; int *pPointer; // now, give a value to them: nNumber = 15; pPointer = &nNumber; // print out the value of nNumber: printf("nNumber is equal to : %d\n", nNumber); // now, alter nNumber through pPointer: *pPointer = 25; // prove that nNumber has changed as a result of the above // by printing its value again: printf("nNumber is equal to : %d\n", nNumber); $CCODE PRINT "BASIC Code Here."
Result:
BASIC Code Here. nNumber is equal to : 15 nNumber is equal to : 25 BASIC Code Here.
Syntax 2: $CCODE SET /** C statements go here */ $CCODE |
Parameters:
Syntax 3: $CCODE HEADER /** C statements go here */ $CCODE |
Parameters:
Syntax 4: $CCODE CONST /** C statements go here */ $CCODE |
Parameters:
Syntax 5: $CCODE UDT /** C statements go here */ $CCODE |
Parameters:
Syntax 6: $CCODE ENUM /** C statements go here */ $CCODE |
Parameters:
Syntax 7: $CCODE FUNCSUB /** C statements go here */ $CCODE |
Parameters:
Remarks: $CCODE FUNCSUB is used to enclose a complete C code procedure. A prototype is not needed in this case because the placement in the emitted "C" source is before the main procedure.
Example:
DIM a$ DIM b$ a$ = " this is a test " b$ = Remove_All_White_Space(a$) PRINT b$ END PROGRAM $CCODE FUNCSUB char* Remove_All_White_Space(char* str1) { char *obuf,*nbuf; if (str1) { for (obuf=str1,nbuf=str1;*obuf;++obuf) { if (!isspace(*obuf)) *nbuf++=*obuf; } *nbuf=0; } RETURN str1; } $CCODE
$HEADER directive
Purpose: The $HEADER directive works like $CCODE except everything sandwiched between two $HEADER statements is placed at the module level of the emitted "C" source. $HEADER is useful particularly for pragma statements and declaring prototypes for inlined C source functions. A $HEADER directive is placed before and after the "C" code.
Syntax: $HEADER /** C statements go here */ $HEADER |
Example:
$HEADER #define KitchenSinkIsIncluded $HEADER $HEADER #ifndef KitchenSinkIsIncluded #include <KitchenSink.h> #else #define CallThePlumber 1 #endif $HEADER
$CPROTO directive
Purpose: The $CPROTO directive is used for declaring prototypes for inlined C source functions.
Syntax 1: $CPROTO ! ProcedureDataType ProcedureName(ParameterDataType); |
Remarks: When using $CPROTO with a C language prototype, a space-exclamation mark-space must precede the prototype. Also, in C, a semicolon is required at the end of of the prototype.
Example:
$CPROTO ! CHAR * Remove_All_White_Space(CHAR *); DIM a$ DIM b$ a$ = " this is a test " b$ = Remove_All_White_Space(a$) PRINT b$ END PROGRAM ! char* Remove_All_White_Space(char* str1) ! { ! char *obuf,*nbuf; ! if (str1) ! { ! for (obuf=str1,nbuf=str1;*obuf;++obuf) ! { ! if (!isspace(*obuf)) ! *nbuf++=*obuf; ! } ! *nbuf=0; ! } ! RETURN str1; ! }
Syntax 2: $CPROTO [FUNCTION/SUB] Foo$(a%, b$) |
Remarks: $CPROTO also will accept a BASIC FUNCTION or SUB declaration and convert it to the approriate C language code.
Example:
$CPROTO FUNCTION Remove_All_White_Space$(a$) DIM a$ DIM b$ a$ = " this is a test " b$ = Remove_All_White_Space(a$) PRINT b$ END PROGRAM ! char* Remove_All_White_Space(char* str1) ! { ! char *obuf,*nbuf; ! if (str1) ! { ! for (obuf=str1,nbuf=str1;*obuf;++obuf) ! { ! if (!isspace(*obuf)) ! *nbuf++=*obuf; ! } ! *nbuf=0; ! } ! RETURN str1; ! }