Home:ALL Converter>Are there C like pre-processor directives for Octave and Scilab to be used for intercompatible code?

Are there C like pre-processor directives for Octave and Scilab to be used for intercompatible code?

Ask Time:2019-03-12T09:38:30         Author:Foad S. Farimani

Json Formatter

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:

  1. 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.
  2. 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>
  3. 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.

Author:Foad S. Farimani,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55112860/are-there-c-like-pre-processor-directives-for-octave-and-scilab-to-be-used-for-i
Tasos Papastylianou :

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.\n\nIn 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.\n\nThe 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.\n\nSo, in the context of running different code by detecting the right environment, this is totally possible.\n\nIn 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.\n\nPS. 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.",
2019-03-12T14:17:20
yy