Good morning Mister Tyler! Going down? |
||
-- Aerosmith video "Love In An Elevator", "Pump" (1989). |
We are going down all the way right to the core, the core of Scilab. Though this is the most technical and most complex chapter, it is by no means true that writing a native Scilab function is unmanageable by for ordinary mortals. A strict programming discipline, patience, persistence, and a thorough knowledge of what makes up the stack-structures involved, let us overcome the difficulties.
To be able to exactly specify the interface Scilab provides for extensions, we use Ada-like syntax, which is introduced in Section 7.1. Equipped with this explanatory aid of a strongly typed language, we proceed in Section 7.2 by explaining the internal data structures like e.g. the stack. The real meat of the chapter starts in Section 7.3, with an extensive discussion of a native Scilab functions, functionals, and dispatch tables. Closely linked to writing a native function is taking care of the errors on a low-level (We do not mean ignoring them!), a topic that is discussed in Section 7.4.
The lack of a comprehensive and tabular documentation of the Scilab is taken care of in Section 7.5 and Section 7.6, which close the chapter.
Instead of simply repeating the Fortran-77 and C statements that make up the Scilab stack, the API, etc., we introduce a new language that is better suited for this job: a pseudo-form of Ada[1], called pAda from hereon, which is much more expressive. The syntax follows Ada, and the pAda types are mapped onto Fortran-77 and C types as listed in Table 7-1, Table 7-2, and Table 7-3. What might look like an artificial complication, the introduction of new types, actually is a major simplification (Three cheers for Ada!):
The name of the type now makes clear exactly what it is used for.
Distinct types designate distinct things, i.e. stuff that never should be mixed up.
The valid ranges of the sub-types are explicity mentioned in the types' definition.
The description of the Fortran-77 interface (Section 7.5) and the C interface (Section 7.6) can be uniformly documented.
Table 7-1. pAda to Fortran-77 and C type mappings – elementary types
pAda | Fortran-77 | C |
---|---|---|
Integer | INTEGER | int |
Float | DOUBLE PRECISION, REAL*8 | double |
Boolean | LOGICAL | n/a, substitute: int, 0 meaning false, everything else meaning true; |
Character | CHARACTER | char |
type String is array (1..N) of Character | CHARACTER*N | char[N + 1] |
subtype Natural is Integer range 0..Integer'Last | INTEGER with the restriction to non-negative values, i.e., allowed are 0, 1, ... | int with the restriction to non-negative values, i.e., allowed are 0, 1, ... |
Table 7-2. pAda type mappings – Scilab Fortran-77 interface
pAda | Fortran-77 |
---|---|
type ComplexFlag is (RealVariable, ComplexVariable) | INTEGER = 0, 1 |
type ParameterStackAddress is new Integer range 1..Integer'Last | INTEGER = 1, 2, ... |
type DataStackIndex is new Integer range 1..Integer'Last | INTEGER = 0, 1, ... |
Table 7-3. pAda type mappings – Scilab C interface
pAda | C |
---|---|
type AccessNatural is access all Natural; | int*, pointer to modifiable integer |
type ConstAccessNatural is access constant Natural; | const int*, pointer to read-only integer |
type AccessString is access all String | char*, pointer to modifiable character |
type ConstAccessString is access constant String | const char*, pointer to read-only character |
type TypeString is String (1 .. 1); | char[2] (see also Table 7-4) |
subtype ParameterStackIndex is Integer range 1..Integer'Last; | int |
type AccessDataStackIndex is access DataStackIndex; | int* |
[1] |
We apologize to all Ada programmers for the abuse of this wonderful language, but Ada's expressiveness and clarity are unmatched for the job we have in mind. |