Intermixing C++ With SiMPLE


Both Win-SiMPLE and Dos-SiMPLE let you write programs and tasks that contain C++ statements intermixed with SiMPLE statements. When writing such a "hybrid" program, there are four principal rules you must remember:

In addition, if (for whatever reason) you wish to invoke SiMPLE tasks or functions in a C++ statement, there are two additional rules:


Including User-defined C++ Functions

The previous discussion assumed that your hybrid program consisted of only a main program. However, there may be times when you also want to define your own C++ functions along with your main program. When doing so, there is one more additional rule you must remember:

The reason for this is because whenever the SiMPLE translator encounters a line beginning with a plus sign, it simply passes that line to the C++ compiler without trying to interpret the contents of that line. Therefore, unless you use an End statement to explicitly indicate the physical end of your main program's source listing, SiMPLE will think that the subsequent C++ statements that define your functions are all part of the main program!

As an example of how to include a user-defined C++ function, let's write a hybrid program that invokes a user-defined mytask function:

     Dos-SiMPLE
               //The first statement must NOT be C++.
     Int null  //Therefore this is just a do-nothing.

     +void mytask (Text);  //function declaration
     //Task main
       param = "Hello, World"
       +mytask (param_);
       Pause
       Cls; Quit
     End main  //MUST be included here!

     +void mytask (Text string)
     +{
     +  cout << string.get_str () << endl;
     +}

Notice that, even though we must use an End statement to indicate the end of the main program, we do not use a Task main statement to indicate the start of the program. (SiMPLE generates one for you automatically.)


The "C++" and "C--" Compiler Directives

As of version 12.05.15, SiMPLE recognizes two additional compiler directives named C++ and C--.  Instead of using plus signs ( + ) to flag individual lines of code as being C++ statements (as was described at the beginning of this document), you can also declare entire blocks of code to be C++ statements without having to declare each line individually. The "C++" directive declares that all the lines that follow it are C++ statements, until the "C--" directive is encountered. For example:

     Dos-SiMPLE
     Do x = 1 To 10
        C++   //This starts a block of C++ statements
           if (x_%2)
           {
              cout << "Two times " << x_ << " is: " << 2*x_ << endl;
           }
        C--   //This ends the block of C++ statements
     Loop
     Display "Done"

There is no limit to the number of separate block of C++ code that you can incorporate in your program. But every "C++" directive that starts a block of code must be paired with a corresponding "C--" directive to indicate the end of the block.

By using SiMPLE's C++ and C-- compiler directives, you can easily write an entire C++ program in the SiMPLE environment. For example:

     Win-SiMPLE [A Program Written Entirely In C++]
     C++
        Cls (1);
        for (int n=15; n>0; n--)
        {
           Solidcolor (n);
           Solidcircle (320, 240, 15*n);
           Delay (100);
           if (n%2)
           {
              Pop ();
           }
           else 
           {
              Honk ();
           }
        }
        Delay (500);
        Bell ();
     C--
Or, if the program doesn't need to be run in a window, the "Win-SiMPLE" line (at the very top of the listing) can be eliminated completely, as is done in the following example program:
     C++
        HFONT hfnt, holdfnt;
        HDC hdc=Gethdc();
        hfnt = CreateFont(96,0,0,0,0,
         0,0,0,0,0,0,0,0,"Comic Sans MS");
        holdfnt=(HFONT)SelectObject(hdc,hfnt);
        SetTextColor(hdc,Rgb(255,64,64));
        SetBkColor(hdc,0L);
        TextOut(hdc,200,300,"SiMPLE for Kids!",16);
        SelectObject(hdc,holdfnt);
        DeleteObject(hfnt);
     C--
This capability of being able to easily write an entire C++ program in the SiMPLE environment is especially useful to beginning C++ programmers who would like to write C++ programs for the Windows environment, but who don't want to deal with interfacing directly with all of the necessary Windows APIs.


The "Append" Compiler Directive

As of version 12.05.15, SiMPLE's "Append" compiler directive recognizes the ".cpp" extension as a way of appending a C++ source module to the end of your program listing. However, when using this approach, the main program is extremely limited in its ability to communicate information to/from the appended module: No calling parameters are allowed. Information can be passed only via a SiMPLE "Common" statement. For example:

     Dos-SiMPLE
     Common Int p
     Do p = 1 To 10
        Call mycode @  
     Loop
     Display "Done"

     Append L: mycode.cpp
where the Local Append Library contains the following "mycode.cpp" source file:
     void mycode_100 ()
     {
        if (p_%2)
        {
           cout << p_ << " is odd" << endl;
        }
        else
        {
           cout << p_ << " is even" << endl;
        }
     }
Because of this severe limitation in communicating data between the main program and the appended function, using the "Append" compiler directive to incorporate C++ code into a SiMPLE program is generally not very useful, except in special cases.


Technical Note: The following is a list of some of the C++ "#include" statements that are automatically declared for you in the SiMPLE environment. (So you don't need to declare them yourself in your C++ source listings):

                #include <stdlib.h>
                #include <stdio.h>
                #include <fstream.h>
                #include <math.h>
                #include <conio.h>
                #include <dos.h>
                #include <string.h>
                #include <dir.h>
                #include <windows.h>
                #include <mmsystem.h>
                #include <windowsx.h>


  [
Webmaster | FAQ's | Home Page | Contact Us ]