In C / C++ languages one can use macros or as called "per-processor directives" to instruct the compiler how the code should be read. The simple commands of #def
, #ifdef
, #ifndef
, #else
, #endif
... give the compiler the ability to check for Operating system, compiler and other environment information. I know Octave and Scilab are interpreted languages, but I'm wondering if is there any way to tell the interpreter to replaces parts of script while loading it? For example can I write a code which is commented based on Scilab syntax //
and then instruct the interpreter to read them as Octave's commenting sytax as #
or %
? This seems to be one of the main issues for Scilab Octave inter compatibility.
If there is a way to instruct the interpreters to check for the interpreter's info Scilab/ScicoLab/Octave/FreeMat,Julia... and the version... and then based on that information there are some #ifdef
#endif
blocks... then one can write a code which is compatible with multiple of the above interpreters. I would appreciate if you could let me know if load time directives are possible, and if not if/how one can write code which is compatible with both Octave and Scilab?
P.S.1 Different approaches are:
- to have conventional
if then elseif else end
statements including a valid syntax across different interpreters with distinctive results. as suggested in the answers below. - use
gets
,exec
,execstr
from the Scilab side to load the.m
files. Some regex could be done to clean the code. Octave does have the xml like#<include>...</include>
- to have a tailor made
import
function like this one made to import MATLAB code into Octave
P.S.2 Octave has the version()
function, Scilab /ScicosLab have getversion()
, Julia has versioninfo
and VERSION
, FreeMat
also has the version
function. maybe that could also be used.
P.S.3 there is already Matlab/Octave Compatibility toolbox for scilab. And there is also Sci cosim to import variables from Scilab workspace into Octave using TCP port.
I want to answer from a different angle. Namely, if you feel the need to compare preprocessor directives, you may be thinking about scilab and octave all wrong. The reason preprocessor directives are necessary in C and C++ is because those are compiled languages. The preprocessor directives make changes to the actual code that will be compiled, before compilation takes place.
In an interpreted language like matlab, scilab and octave, this kind of thing is redundant. So a simple 'if / else' block performing a test that adequately distinguishes between the three environments should be adequate.
The octave manual suggests a way to distinguish between octave and matlab that does not carry a heavy performance penalty. I don't have scilab installed to provide an equivalent test, but I'm sure a simple test exists for scilab as well.
So, in the context of running different code by detecting the right environment, this is totally possible.
In the context of imitating an #include strategy, since a script is run sequentially, you could implement an 'if / else' block which simply runs a different base script at the right time.
PS. Matlab has been making some changes in the way scripts are interpreted, so this may lead to problems if this performs 'nested' error-checking rather than superficial error-checking. But, even if this does happen, simply instead of calling a script directly, you can use the run filename
syntax instead, or, worse case scenario, use eval
to call the script.
You can define a function isscilab
:
function [out] = isscilab()
out = length(zeros(2)) == 1;
endfunction
And use it to conditionally execute the code:
if isscilab()
do scilab...
else
do octave...
end
But I think the best choice is that you should implement different files for Octave .m and Scilab .sce and execute each one you want.
来源:https://stackoverflow.com/questions/55112860/are-there-c-like-pre-processor-directives-for-octave-and-scilab-to-be-used-for-i