I am currently working on a project that is accessing a piece of hardware using tons of hard coded memory locations. These locations can change based upon the electrical engineer's whim, so I'm looking to generate code from the engineer's memory map. Let's say the map is a simple text file like:
我目前正在开发一个使用大量硬编码内存位置访问硬件的项目。这些位置可能会根据电气工程师的心血来潮而改变,所以我希望从工程师的记忆图中生成代码。假设地图是一个简单的文本文件,如:
Name, Type, Address, Description
Foo, int, A001, Foo integer variable
Bar, float, A002, Bar float variable
I would like to automatically generate code (not IL) similar to:
我想自动生成代码(不是IL)类似于:
class MachineMap
{
/// <summary>
/// Foo integer variable
/// </summary>
public readonly Addressable<int> Foo = new Addressable<int>("A001");
/// <summary>
/// Bar float variable
/// </summary>
public readonly Addressable<float> Bar = new Addressable<float>("A002");
}
Does anyone have ideas on tools that would make this task easy, or easier?
有没有人有关于使这项任务变得简单或容易的工具的想法?
4 个解决方案
#1
4
Have a look at the built-in code generation ability of Visual Studio called T4. Or another option might be commercial product such as CodeSmith.
看看Visual Studio的内置代码生成功能T4。或者另一种选择可能是商业产品,例如CodeSmith。
Have a look at this article from Scott Hanselman:
T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret
看看Scott Hanselman撰写的这篇文章:T4(文本模板转换工具包)代码生成 - 最佳保存Visual Studio Secret
#2
3
Use a regular expression like this (using gvim
or vim
):
使用这样的正则表达式(使用gvim或vim):
:%s/\(.*\), \(.*\), \(.*\), \(.*\)/public readonly Addressable<\2> \1 = new Addressable<\2>("\3")/g
This solves the main parsing part. You then concatenate the contents with the header and footer files:
这解决了主要的解析部分。然后,您将内容与页眉和页脚文件连接起来:
type header.txt converted.txt footer.txt > source.c
If the map is more complex, then use a tool made for grammar parsing. Otherwise, if it truly is that simple, avoid using a tank for such a tiny nail.
如果地图更复杂,则使用用于语法分析的工具。否则,如果它真的那么简单,避免使用坦克这么小的钉子。
#3
0
You might be able to craft a DSL of your choosing, and then use M Grammar (part of Oslo) to parse it.
您可以制作自己选择的DSL,然后使用M Grammar(Oslo的一部分)来解析它。
#4
0
Similar to Nick's Oslo suggestion, StringTemplate is another way to create template-based source code from a well-defined grammar. It has a C# port, so it's easy enough to use from .NET.
与Nick的Oslo建议类似,StringTemplate是另一种从定义良好的语法创建基于模板的源代码的方法。它有一个C#端口,因此从.NET中使用它很容易。
#1
4
Have a look at the built-in code generation ability of Visual Studio called T4. Or another option might be commercial product such as CodeSmith.
看看Visual Studio的内置代码生成功能T4。或者另一种选择可能是商业产品,例如CodeSmith。
Have a look at this article from Scott Hanselman:
T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret
看看Scott Hanselman撰写的这篇文章:T4(文本模板转换工具包)代码生成 - 最佳保存Visual Studio Secret
#2
3
Use a regular expression like this (using gvim
or vim
):
使用这样的正则表达式(使用gvim或vim):
:%s/\(.*\), \(.*\), \(.*\), \(.*\)/public readonly Addressable<\2> \1 = new Addressable<\2>("\3")/g
This solves the main parsing part. You then concatenate the contents with the header and footer files:
这解决了主要的解析部分。然后,您将内容与页眉和页脚文件连接起来:
type header.txt converted.txt footer.txt > source.c
If the map is more complex, then use a tool made for grammar parsing. Otherwise, if it truly is that simple, avoid using a tank for such a tiny nail.
如果地图更复杂,则使用用于语法分析的工具。否则,如果它真的那么简单,避免使用坦克这么小的钉子。
#3
0
You might be able to craft a DSL of your choosing, and then use M Grammar (part of Oslo) to parse it.
您可以制作自己选择的DSL,然后使用M Grammar(Oslo的一部分)来解析它。
#4
0
Similar to Nick's Oslo suggestion, StringTemplate is another way to create template-based source code from a well-defined grammar. It has a C# port, so it's easy enough to use from .NET.
与Nick的Oslo建议类似,StringTemplate是另一种从定义良好的语法创建基于模板的源代码的方法。它有一个C#端口,因此从.NET中使用它很容易。