Intermixing C++ With SiMPLE


[Note: This webpage is intended for users of SiMPLE.]

Click Here to Read About 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, you will have to accept some limitations in how the module can interact with the SiMPLE main program. For example, only functions of type "void" can be directly invoked by the main program. All other types of functions (int, float, etc) cannot be directly invoked by the main program.

Furthermore, it is recommended that information be passed to those void functions only via SiMPLE "Common" statements rather than by using calling parameters. However it is usually possible to communicate via calling parameters as well (as long as you remember to pass the parameters by reference only). But it is not guaranteed that the system will always accept the use of calling parameters.

As an example, the following program communicates with its module by using both a calling parameter and a Common statement:

     Win-SiMPLE [Example]
     Common Int z
     Do p = 1 To 3
        z = 10 - p
        mycode (p) @
     Loop

     Append L: mycode.cpp
where the Local Append Library contains the following "mycode.cpp" source file:
     void mycode_100 (int &a)
     {
        Out ("The calling parameter is: ");
        Output (a);
        Out ("The value of z is: ");
        Output (z_);
     }
When you run the program, you should see the following output in a window:
     The calling parameter is: 1
     The value of z is: 9
     The calling parameter is: 2
     The value of z is: 8
     The calling parameter is: 3
     The value of z is: 7


Technical Notes: The following is a list of some of the C++ "#include" statements that are automatically declared for you in the Win-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>


The following is a list of the C++ "#include" statements that are automatically declared for you in the Dos-SiMPLE environment:

                #include <stdlib.h>
                #include <stdio.h>
                #include <fstream.h>
                #include <math.h>
                #include <conio.h>
                #include <dos.h>
                #include <string.h>
                #include <io.h>
                #include <dir.h>
                #include <iostream.h>


If your program needs to declare additional "#include" statements, you cannot merely place tham at the beginning of your C++ source listing. Instead you must add them to SiMPLE's "ALL.H" file:

In the Win-SiMPLE environment, the path to the "ALL.H" file is: \SiMPLE\Includew\ALL.H

In the Dos-SiMPLE environment, the path to the "ALL.H" file is: \SiMPLE\Included\ALL.H



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