Home






CPP - Compiler Preprocessor
Contents of this website are freeware and/or copyrighted material, and may not be sold under any circumstances.
Email: dogsbody@dogsbodynet.com     Home: http://dogsbodynet.com


Introduction:

CPP is a macro preprocessor.   Instead of using numbers in RCode (say to represent voice commands), you can declare meaningful labels to improve readability.  Use CPP to substitute your labels, remove comments, and apply conditional code statements before uploading handwritten code to AIBO.. 

The tool originated many years ago to when the C programming language was first developed.   CPP fortunately works with any language, not just C, and therefore helps us write better RCode.   This version of CPP was created by Sun Microsystems, and modified slightly (to display a help screen from the command line, and support DOS pathname conventions).  Source code is available upon request.

Usage (from dos prompt):


CPP  [options]  infile  outfile

 
The command line options are:
-B Support C++ style comment indicator '//'.   With this indicator everything on a line after // is treated as a comment and filtered.  Recommended always.
-C Do not filter any comments.  By default, CPP strips all comments (C-style, and C++ style if the -B specified).
-I<dir> Add include file directory.
-N No default include files.
-P No compiler line control information.    Only use if CPP outfile used directly on AIBO.  Don't use in conjunction with ReCode, since the compiler line info is required to generate accurate error messages. 
-Vi Verbose include expansion.
-Vm Verbose macro definition.
-Vx Verbose macro expansion.
-Xi Output #pragma on include files.  Never use this with AIBO.


Example 1 (CPP output used directly on AIBO):
   CPP -B -P infile.r R-CODE.R

Example 2 (CPP used along with ReCode):
   CPP -B infile.r tempfile
   ReCode tempfile
 

CPP Directives:
All cpp directives start with a hash symbol (#) as the first character on a line.  White space (SPACE or TAB characters) can appear after the initial # for proper indentation. 

#define  target  replacement-string
 Create a simple macro definition.  Replaces subsequent instances of 'target' with 'replacement-string'.   Example: 
 
Before After
#define MAX 42 
PRINT:%d:MAX
PRINT:%d:42

#define target(argument [, argument] ...  ) replacement-string
Create a macro definition with arguments.  There can be no space between the target name and the `('.   Replaces subsequent instances of target name (followed by a parenthesized list of arguments) with the replacement-string.  Each occurrence of an argument in the replacement-string is replaced by the corresponding value from the comma-separated arguments.  Example: 
 
Before After
#define MAX 42 
#define LT(x,y) IF x < y 
LT(12,34) PRINT:%d:MAX
IF 12 < 34 PRINT:%d:42

#undef target
Remove definition for the macro.   Nothing is permitted to appear after 'target' on the line.

#include "filename"
Read in the contents of filename at location of #include statement.   The data read from the file is processed by CPP as if it were part of the original/current file.   CPP will search for the "filename" in the current directory, and any other directories listed by the -I option (see above).  Example: 
 
Before After
Current File: 
    PRINT "HELLO" 
    #include "file2.r" 

file2.r: 
    PRINT "WORLD"

PRINT "HELLO" 
PRINT "WORLD"

#ap_include "filename"
Read in the contents of filename at location of #ap_include statement.   The data read from the file is processed by CPP as if it were part of the original/current file.   CPP will search for the "filename" in the current directory, and any other directories listed by the -I option (see above). 

The #AP_INCLUDE command was defined by AiboPet for use on AIBO, where it includes files from AIBO's memory stick.   When compiled on a PC however, your computer cannot access the memory stick files.   You must make a local copy of them.   Using AP_INCLUDE on PC compiled code isn't recommend and CPP will issue a warning.   Use the "#include" directive instead.

Example:
Before After
Current File: 
    PRINT "HELLO" 
    #AP_INCLUDE "file2.r" 

file2.r: 
    PRINT "WORLD"

PRINT "HELLO" 
PRINT "WORLD"

#if constant-expression
Subsequent lines up to a matching #else, #elif, or #endif directive appear in output if the "constant-expression" yields a non-zero value.  All binary non-assignment C operators, including "&&", "||", "?:", and unary "-" "!" "~" operators are legal in constant-expressions.  Only integer constants and names known by CPP are usable within constant-expressions.  You cannot reference variables, but macro's from previous #define statements are ok.   Example: 
 
Before After
#define MAX 42 
#if (MAX>100) 
   PRINT "ACK" 
#elif (MAX>10) 
   PRINT "PHBBT" 
#else 
   PRINT "NOMAX" 
#endif
PRINT "PHBBT"

#ifdef name
Subsequent lines up to a matching  #else,  #elif,  or #endif appear in the output only if name has been defined with a #define directive (assuming no intervening #undef directive).  Example: 
 
Before After
#define MAX 42 
#ifdef MAX 
   PRINT "ACK" 
#else 
   PRINT "PHBBT" 
#endif
PRINT "ACK"

#ifndef name
Subsequent lines up to a matching  #else,  #elif,  or #endif appear in the output only if name has NOT been defined with a #define directive, or if an intervening #undef directive removed the name definition.

#elif constant-expression
Any number of #elif (else-if) directives may appear between an #if, #ifdef, or #ifndef directive and a matching #else or #endif directive.  The lines following the #elif directive appear in the output, only if the previous conditional statements was false, and the constant-expression of the elif evaluates to non-zero.   If the constant-expression evaluates to non-zero, all subsequent #elif and #else directives are ignored until a matching #endif.

#else
Inverts the sense of the previous conditional directive.  If all preceding conditional statements are false, then the lines between #else and #endif are output.   Conditional directives and corresponding #else directives can be nested.

#endif
End a section of lines begun by one of the conditional directives #if, #ifdef, or #ifndef.  Each such conditional directive must have a matching #endif.



Legalese: These programs are provided AS IS without any warranty, expressed or implied.  This includes without limitation the fitfulness for a particular purpose or application.    People using the software bear all risk as to its quality and performance.   The user of the software is responsible for any damages whether direct, indirect, special, incidental or consequential arising from a failure of these programs to operate in any manner desired.   Etc, etc...

"AIBO" is a registered trademark of Sony Corporation.  "AIBO Master Studio", "R-Code", and "Memory Stick" are trademarks of Sony Corporation.