1

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.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • You want to write code which runs on Scilab, ScicoLab, Octave/FreeMat,Julia... ? Can you write Code compatibel with C++ and Java using preprocessor directives? – Andy Mar 12 '19 at 06:30
  • well, mainly Octave and Scilab. mixed programming is possible. Fortran and C / C++ for example. not in a same file though. you can call a Fortran subroutine from C or vise versa... but Octave and Scilab are both somewhat replicas of a same language i.e MATLAB. it is expected for them to be compatible. – Foad S. Farimani Mar 12 '19 at 07:13
  • 1
    MATLAB parses the whole M-file before it starts to interpret it. This is part of the JIT. This means that you can’t hide illegal syntax in an `if`/`else` construct. Octave doesn’t have a JIT yet, but I hear that work is ongoing. If so, the only recourse you have is to create code in a platform-agnostic way, and “compile” it to be Octave code or Scilab code. – Cris Luengo Mar 12 '19 at 14:12

2 Answers2

3

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.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • Ideally there should be `importScilab`, `ImportScicosLab`, `importOctave`, `importFreemat`, `importJulia`... for all of these languages so one can just import an script without worrying about the compatibility and different syntax. – Foad S. Farimani Mar 12 '19 at 14:37
  • 2
    btw, Julia is a completely different language, there's no point including it in this list. It bears, like, 10% similarity with the rest, but that's about it. As for import scripts, that's what I'm trying to say, this is not practical or desirable. What _is_ possible is to write in the specific subset of the language that is common to all the rest, and redirect 'specialised' code if necessary. But preferably, if I had to write code for these platforms, I would write explicit code for each one. Worst case scenario, I'd split the 'common' bits and provide specialised wrappers. – Tasos Papastylianou Mar 12 '19 at 15:06
  • following this discussion I think the world of interpreted languages need something like [Pandoc](https://pandoc.org/) for the markup languages. It is even easier IMO. – Foad S. Farimani Mar 12 '19 at 22:13
1

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.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • awsome. does octave has something like C inlcude or Python import? the file extension restrictions are annoying. – Foad S. Farimani Mar 12 '19 at 09:08
  • octave does have `# .... ` and maybe we can have a special import function like the ones out there for MATLAB. – Foad S. Farimani Mar 12 '19 at 09:35
  • 1
    Include in c is plain text inclusion but import in python is importing modules that is more advanced inclusion. C and c++ doesn't have such a module system. In MATLAB you can import contents of packages (Folders that contain codes) but this feature has not been implemented in octave. Instead you need to place functions to related m files then you can add the directory that contains your files to the octave path so the function will be available. Based of your need you may choose other options. – rahnema1 Mar 12 '19 at 10:07
  • 1
    `# .... `. are some type of comments and Markup and don't include code. – rahnema1 Mar 12 '19 at 10:10
  • well, having different files is not always easy. for example some functions/syntax might be already inter-compatible, and the ones which are not is safer to be handled automatically. After all computers are to make our lives easier and automate as much as possible. I'm working on [this serial port communication library](https://gist.github.com/Foadsf/44fe9b5e2bcb02b22392adf6d8f7a871) and I was wondering if it could also work on Octave, FreeMat, Julia... – Foad S. Farimani Mar 12 '19 at 10:42
  • 1
    Octave and Scilab espousal has started nearly [a year ago](https://www.scilab.org/scilab-love-octave). But I think much of works are needed for the relation to become a real marriage! – rahnema1 Mar 12 '19 at 10:50
  • Tha's a good news after Scilab Scicos divorce :)) but there is a huge amount of code base already developed for each platform. It would be a great advantage if we could find a way for automatic interoperability. – Foad S. Farimani Mar 12 '19 at 11:51