6.2. PikaScript C module development process

    To write a new module, you first need to write a module interface file, for example, to write a math calculation module Math, the first step is to write Math.pyi.

    6.2.2. Writing class interfaces

    Now we can create new classes inside Math.pyi. For example, if we want to create a new class to implement the relevant addition operations, we can add the Adder class inside Math.pyi.

    Then we want Adder to provide addition operations for plastic and floating-point data, so we can add the byInt and byFloat methods.

    Use ... to replace pass is also avaliable,for example:

    1. # Math.pyi
    2. class Addr:
    3. def byInt(self, a:int, b:int)->int:...
    4. def byFloat(self, a:float, b:float)->float:...

    The above code defines the Adder class and adds two method declarations, byInt(self, a:int, b:int)->int, indicating that the method name is byInt, the input parameters are a and b, the type of a and b are both int, and the return value is also int. and the return value is determined by ->int, which is the standard python syntax for writing with type annotations.

    The first argument of a method of a class in python is self, which is required by python syntax.

    We add a Multiplier class to math.py to implement multiplication, which is written as follows.

    1. # Math.pyi
    2. class Multiplier:
    3. def byInt(self, a:int, b:int)->int:
    4. pass
    5. def byFloat(self, a:float, b:float)->float:
    6. pass

    This is the end of the interface. We introduce the Math module in main.py so that the Pika precompiler will go ahead and precompile the Math module.

    Double-click to run the pika precompiler.

    Opening the pikascript-api folder shows that our newly written module interface is ready to be compiled.

    Let’s add the two newly compiled -api.c files we just made to the project and try compiling them.

    _images/131119636-3c3d52ce-a7c2-48a4-beb4-5498dfd4f279.png

    found that the compilation reported an error, suggesting that there are four functions not found in the definition.

    This is normal because we did not write implementations for the classes of the Math module before, and we will write implementations for those classes below.

    For the convenience of module management, we put all the implementation files in the pikascript-lib folder.

    https://user-images.githubusercontent.com/88232613/171089246-ebf24d32-53b1-471b-8c3f-594a96943df5.png

    Under the pikascript-lib folder, create a new Math folder to hold the implementation code for the Math module.

    image

    Then create a new .c file in the Math folder. It is recommended to use the naming scheme “module_class_name.c” to create a new .c file for each class to improve the clarity of the code.

    _images/131120619-45ae3520-7b63-434b-8831-5b4d9f900cad.png

    This is easy, we open Math_Multiplier.h and Math_Adder.h to find that the implementation functions we need to write have already been declared.

    1. /* Math_Multiplier.h */
    2. /* ******************************** */
    3. /* Warning! Don't modify this file!
    4. /* ******************************** */
    5. #ifndef __Math_Multiplier__H
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8. #include "PikaObj.h"
    9. double Math_Multiplier_byFloat(PikaObj *self, double a, doutlb b);
    10. int Math_Multiplier_byInt(PikaObj *self, int a, int b);
    11. #endif
    1. /* Math_Adder.h */
    2. /* ******************************** */
    3. /* Warning! Don't modify this file!
    4. /* ******************************** */
    5. #ifndef __Math_Adder__H
    6. #define __Math_Adder__H
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9. #include "PikaObj.h"
    10. PikaObj *New_Math_Adder(Args *args);
    11. double Math_Adder_byFloat(PikaObj *self, double a, double b);
    12. int Math_Adder_byInt(PikaObj *self, int a, int b);
    13. #endif

    Then we directly implement these four functions in Math_Adder.c and Math_Multipler.c and we’re good to go.

    1. /* Math_Multipler.c */
    2. #include "pikaScript.h"
    3. double Math_Multiplier_byFloat(PikaObj *self, double a, double b)
    4. {
    5. }
    6. int Math_Multiplier_byInt(PikaObj *self, int a, int b)
    7. {
    8. return a * b;
    9. }

    At this point,compile the project again and it will pass.

    6.2.4. Test the effect

    Let’s test our newly written module with the following main.py

    1. # main.py
    2. import Math
    3. adder = Math.Adder()
    4. muler = Math.Multiplier()
    5. res1 = adder.byInt(1, 2)
    6. print('1 + 2')
    7. print(res1)
    8. res2 = adder.byFloat(2.3, 4.2)
    9. print('2.3 + 4.2')
    10. print(res2)
    11. res3 = muler.byInt(2, 3)
    12. print('2 * 3')
    13. print(res3)
    14. res4 = muler.byFloat(2.3, 44.2)

    The result of the run is as follows.

    This shows that the module we wrote is working correctly.

    The following table lists all the type declarations supported by PikaScript, and how they correspond to the native types of the C language.

    6.2.6. Publishing modules

    In the spirit of open source, it is very cool and exciting to publish your own modules.

    All you need to do to publish a module is to publish the class interface and class implementation files.

    For example, to publish the newly written Math module, you publish the Math.pyi file and the pikascript-lib/Math folder.

    Please refer to the documentation in the Participate in Community Contributions section to distribute the modules you write.