是否有用于Octave和Scilab的类似于C的预处理器指令可用于相互兼容的代码? [英] Are there C like pre-processor directives for Octave and Scilab to be used for intercompatible code?
问题描述
在C/C ++语言中,可以使用宏或称为每处理器指令"的指令来指示编译器应如何读取代码. #def
,#ifdef
,#ifndef
,#else
,#endif
等简单命令使编译器能够检查操作系统,编译器和其他环境信息.我知道Octave和Scilab是解释语言,但是我想知道是否有任何方法告诉解释器在加载脚本时替换脚本的某些部分?例如,我可以编写一个基于Scilab语法//
进行注释的代码,然后指示解释器将其作为Octave的注释语法(#
或%
)读取吗?这似乎是Scilab Octave相互兼容性的主要问题之一.
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.
如果有一种方法可以指示口译员检查口译员的信息,例如Scilab/ScicoLab/Octave/FreeMat,Julia ...以及版本...,然后根据该信息提供一些#ifdef
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 :不同的方法是:
- 具有常规的
if then elseif else end
语句,其中包括跨不同解释器的有效语法,并具有独特的结果.如以下答案中所建议. - 从Scilab端使用
gets
,exec
,execstr
加载.m
文件.可以做一些正则表达式来清除代码.八度确实具有#<include>...</include>
之类的xml
- 具有定制的
import
函数,例如此函数,用于将MATLAB代码导入Octave
- 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具有version()
功能,Scilab/ScicosLab具有getversion()
,Julia具有versioninfo
和VERSION
,FreeMat
也具有version
功能.也许也可以使用.
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.
PS3 ,已经有 Matlab/八度兼容性工具箱(适用于scilab).还有 Sci cosim 可以使用TCP端口将变量从Scilab工作区导入Octave.
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.
推荐答案
我想从另一个角度回答.也就是说,如果您觉得需要比较预处理器指令,则可能是在考虑scilab和octave都错了.在C和C ++中必须使用预处理程序指令的原因是,因为它们是编译语言.预处理指令会在编译发生之前对将要编译的实际代码进行更改.
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.
在像matlab,scilab和octave这样的解释语言中,这种事情是多余的.因此,一个简单的"if/else"块执行足以区分这三种环境的测试就足够了.
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.
在模仿#include策略的情况下,由于脚本是按顺序运行的,因此您可以实现一个'if/else'块,该块仅在正确的时间运行另一个基本脚本.
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已对脚本的解释方式进行了一些更改,因此,如果执行嵌套"错误检查而不是表面错误检查,则可能会导致问题.但是,即使确实发生了这种情况,也可以直接使用run filename
语法代替直接调用脚本,或者在更糟的情况下使用eval
调用脚本.
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.
这篇关于是否有用于Octave和Scilab的类似于C的预处理器指令可用于相互兼容的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!