[Hbasic-userinfo] Setting up methods for HBasic
Status: Beta
Brought to you by:
mengels
From: <mar...@t-...> - 2003-07-16 18:27:01
|
Hi Ahmad, updating the function library is an good idea. This functions will be implemented in C or C++ so that you should have no problems. Implementing functions requires two different steps. 1) Create a function definition in C or C++ within a HBasic package description. I have put all functions in the hbasic_stdgui.so packages which will always be loaded on startup of HBasic but you may also copy this package and create your own if you like. 2) Since compiling the C functions to a library only gives me some machine language code with an entry point for the function HBasic needs some additional information about the number of parameters, the return type of the method and the type of the parameter values. For popup info's I have added some additional description line and the name of the parameters will also be stored in this information. This info files (called <package_name.dso>) will be created with the libdesc program which will also be installed with HBasic. Let me give you an example. If you take a look at the file <HBASICDIR>/packages/hbasic_stdgui/hbasic_stdgui.cpp you will find the method definitions for functions like double fn_sin( double d ) { return( sin( d )); } This is the method which HBasic calls whenever the parser finds a sin(d) method call in the HBasic program. I first also tried to call this C function sin() but some compilers got into trouble with this naming so I have renamed them to fn_sin() ... Let's try to insert a new method which will multiply a double number with 2.0. Insert some code like double mult2( double par1 ) { return( par1 * 2.0 ); } into the hbasic_stdgui.cpp file and run make and make install. This should compile the new library and copy it to /usr/local/hbasic/packages. The *.so file is the library which will be loaded as a shared library by HBasic and the *.dso is the current version of the description file needed by HBasic. For the second step we have to start the libdesc program (Sourcecode in libdesc subdirectory). Click on Menu "Load/Load description" and select the hbasic_stdgui_1.dso file from the /usr/local/hbasic/packages directory. You should now see a list of the components, constants and methods that have been implemented in the hbasic_stdgui package. We want to add a new method description so click on the "Method list" folder with the right mouse button and select "Add method". In the dialog that pop's up you should fill in the following fields: 1) Method name is the name that should be used in HBasic to call the method. We could call this "multiply_two" (or whatever you like) for our example. 2) Symbol to call is the name of the symbol that the compiler has generated for the object code of the C function implementation. For C code this is the same as the name in the C sourcecode but for C++ code this is a little bit more complicated. I normally use something like objdump -T /usr/local/hbasic/packages/hbasic_stdgui_1.dso | grep mult2 to find the name of the object symbol. You may also click on the S button on the right side of the editline for the symbol. This will also list the symbols in the library in a new dialog. Type mult2 as a search string in the editline and select the symbol which is _Z8fn_mult2d for my gcc-3.2 compiler. 3) Select "double" as return type 4) Select "double" as parameter type, give a name to the parameter and click on add to add the parameter description to the list below. You may also add a short one line method description for each method which will be displayed in the editor of the HBasic IDE. If you save this description it should now be possible to use this method in HBasic sourcecode. Startup HBasic and insert a new button into the form. Doubleclick on the button and insert some example program like the following in the sourcecode editor: Sub button1_clicked() Dim d As double d = 12.34 print multiply_two( d ) End Sub Running this program should display the result 24.68 on the form which means out method defined in C has been called. Maybe I haven't mentioned all the methods that I have already defined in the hbasic_stdgui library in the documentation of HBasic. Some of the methods that you wanted to implement might already be in the library. Have a look at the method list after loading the library into the libdesc program to see the list of defined methods. Looking at the glibc documentation there will be some more functions that might be useful in HBasic so there is much to do and any help is welcome. I hope the description helps you to set up new methods with HBasic. Send me some info if you have problems or further questions. To help other people I will also send this info to the HBasic mailing list that I have set up on sourceforge (hba...@li...). Marcus Engels |