简体   繁体   English

是否存在用于Octave和Scilab的类似于C的预处理器指令用于兼容代码?

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

In C / C++ languages one can use macros or as called "per-processor directives" to instruct the compiler how the code should be read. 在C / C ++语言中,可以使用宏或称为“每处理器指令”的指令来指示编译器应如何读取代码。 The simple commands of #def , #ifdef , #ifndef , #else , #endif ... give the compiler the ability to check for Operating system, compiler and other environment information. #ifdef #def#ifdef#ifndef#else#endif ...这些简单的命令使编译器能够检查操作系统,编译器和其他环境信息。 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? 我知道Octave和Scilab是解释语言,但是我想知道是否有任何方法告诉解释器在加载脚本时替换脚本的某些部分? 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 % ? 例如,我可以编写一个基于Scilab语法//注释的代码,然后指示解释器以#%读取它们作为Octave的注释语法吗? This seems to be one of the main issues for Scilab Octave inter compatibility. 这似乎是Scilab Octave相互兼容性的主要问题之一。

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. 如果有一种方法可以指示口译员检查口译员的信息Scilab / ScicoLab / Octave / FreeMat,Julia ...以及版本...然后基于该信息,则有一些#ifdef #endif块...那么您就可以编写与上述多个解释器兼容的代码。 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? 如果您可以让我知道是否可以使用加载时间指令,以及是否/如何编写与Octave和Scilab兼容的代码,我将不胜感激?

PS1 Different approaches are: PS1不同的方法是:

  1. to have conventional if then elseif else end statements including a valid syntax across different interpreters with distinctive results. 具有传统的if then elseif else end语句, if then elseif else end语句包含跨不同解释器的有效语法,并具有独特的结果。 as suggested in the answers below. 如以下答案中所建议。
  2. use gets , exec , execstr from the Scilab side to load the .m files. 从Scilab端使用getsexecexecstr加载.m文件。 Some regex could be done to clean the code. 可以做一些正则表达式来清除代码。 Octave does have the xml like #<include>...</include> 八度确实具有xml,例如#<include>...</include>
  3. to have a tailor made import function like this one made to import MATLAB code into Octave 有一个定制的import之类的函数这个由导入MATLAB代码到八度

PS2 Octave has the version() function, Scilab /ScicosLab have getversion() , Julia has versioninfo and VERSION , FreeMat also has the version function. PS2 Octave具有version()函数,Scilab / ScicosLab具有getversion() ,Julia具有versioninfoVERSIONFreeMat也具有version功能。 maybe that could also be used. 也许也可以使用。

PS3 there is already Matlab/Octave Compatibility toolbox for scilab. PS3已经为scilab提供了Matlab / Octave兼容性工具箱 And there is also Sci cosim to import variables from Scilab workspace into Octave using TCP port. 还有Sci cosim ,可使用TCP端口将变量从Scilab工作区导入Octave。

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. 即,如果您需要比较预处理器指令,则可能是在考虑scilab和octave都错了。 The reason preprocessor directives are necessary in C and C++ is because those are compiled languages. 在C和C ++中必须使用预处理程序指令的原因是,因为它们是编译语言。 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. 在像matlab,scilab和octave这样的解释语言中,这种事情是多余的。 So a simple 'if / else' block performing a test that adequately distinguishes between the three environments should be adequate. 因此,执行足以区分三种环境的测试的简单“ if / else”块就足够了。

The octave manual suggests a way to distinguish between octave and matlab that does not carry a heavy performance penalty. 八度音程手册提出了一种区分八度音程和matlab的方法,该方法不会带来严重的性能损失。 I don't have scilab installed to provide an equivalent test, but I'm sure a simple test exists for scilab as well. 我没有安装scilab来提供等效测试,但是我敢肯定scilab也存在一个简单的测试。

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. 在模仿#include策略的情况下,由于脚本是按顺序运行的,因此您可以实现一个'if / else'块,该块仅在正确的时间运行另一个基本脚本。

PS. 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. Matlab已对脚本的解释方式进行了一些更改,因此,如果执行“嵌套”错误检查而不是表面错误检查,则可能会导致问题。 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. 但是,即使确实发生了这种情况,也可以直接使用run filename语法代替直接调用脚本,或者在更糟的情况下使用eval调用脚本。

You can define a function isscilab : 您可以定义一个函数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. 但是我认为最好的选择是,您应该为Octave .m和Scilab .sce实现不同的文件,并执行所需的每个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM