开发者

Sharing constants across languages

开发者 https://www.devze.com 2023-01-12 14:10 出处:网络
I have a long list of constants that I need access to in several projects which are in different languages(Verilog, C, C++ and C#). Rather than repeating them in each language, is there a good way to

I have a long list of constants that I need access to in several projects which are in different languages(Verilog, C, C++ and C#). Rather than repeating them in each language, is there a good way to share these?

The only thing I could think of would be a text file 开发者_C百科and a preprocessing script? Is this the best solution or is there something easier/more elegant?


A preprocessing script which automatically updates those constants within your code is probably the best method. Commit the code with your project to ensure correctness and have it a part of the build script.


You can keep them in a XML document and write XSLT scripts for each language to generate the appropriate source files in each build.


Can you use your makefile (or equivalent) to define these constants? For C and C++. you can use the compiler's CLI options to define pre-processor values for the constants. I haven't done much build customization for Verilog, but I suspect something similar might exist there as well.


You can write a simple file in the form of

const1 = value1
const2 = value2
const3 = value3

and then apply something like, for c:

s/\([a-zA-Z][a-zA-Z1-9_]*\)[ \t]*=[ \t]*\(.*\)/#define \1 \2/

Its worth noting that you might need to specify types because not all languages will allow you to use a preprossessor macro that doesn't care about type.

Alternatively, you can make a lexer/parser in Flex/Bison to parse the configuration file. This will be clearer and easier to extend.


You might have an XML file with the constants to be shared and have it parsed in each language.


For verilog (at least for system verilog) and c++ you can have all constants described as a list (assuming that all of them are of the same type), like the following:

a=0, b= 1, c = 2, ..;

in c++ you would use

const int
#include <myconsts>

in verilog (at least in system verilog) you can use this

parameter int
`include "myconsts"

I guess c# does not have includes. So, you would need a pre-pcocessing script there at least to include your constants in a class. You might be able to use 'cpp' for this. Sorry, do not know much about c#.

Actually, to make all similar i would probably use cpp to generate the file i need:

#ifdef CPP
    const int
#elsif VERILOG
    parameter int
#elsif CSHARP
    class Constants {
       const int
#endif

    a = 0,
    c = 1,
    d = 2;

#ifdef(CSHARP)
    };
#endif
0

精彩评论

暂无评论...
验证码 换一张
取 消