My current task right now is to write one program which takes as input some textual specification for the compression of 64 bit instructions into 32 bit instructions. From that specification I am to build two executable programs: an encoder and a decoder.
Currently I'm just writing a parser class to tokenize the textual specification I've designed, but eventually I will have to turn the information I get into the two new programs. The only way I know I could do it is print out to a new .cpp file using ofstream then using system('g++ new_file.cpp -o new.x')
to create the executable. Then perhaps system('rm new开发者_StackOverflow社区_file.cpp')
to clean up.
I have looked around all over for other ways to do this, but have found nothing. If you have any advice to give, I'd be very grateful.
Thanks
p.s. I didn't include any of my code because the code is irrelevant. For simplicity's sake my goal could be to write a program whose output is a "Hello, World!" -esque executable.
Have your program do this:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
{
ofstream out( "a.cpp" );
out << "#include <iostream>\nint main() { std::cout << \"hello world\\\n\"; } \n";
}
system( "g++ a.cpp -o hello" );
}
Just tested this - it does produce a compiled hello world program.
Hmm, sounds like a nice task for a bit of XML and XSLT. Use XML to define what you want, and XSLT to generate the actual C++ code.
EDIT: For those who downvoted:
<?xml version="1.0" encoding="UTF-8" ?>
<proggy>
<prompt input='uname' type='std::string'>
<text>Name: </text>
<output><text>Hello </text><field name="uname"/><text>!</text></output>
</prompt>
</proggy>
The XSL to transform this to C++
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="no"/>
<xsl:variable name="co"><xsl:text>std::cout</xsl:text></xsl:variable>
<xsl:variable name="ci"><xsl:text>std::cin</xsl:text></xsl:variable>
<xsl:variable name="e"><xsl:text> << std::endl;</xsl:text></xsl:variable>
<xsl:variable name="so"><xsl:text> << </xsl:text></xsl:variable>
<xsl:variable name="si"><xsl:text> >> </xsl:text></xsl:variable>
<xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable>
<!-- Root node -->
<xsl:template match="/">
<xsl:text>
#include <iostream>
int main(void)
{
</xsl:text>
<xsl:apply-templates select="//proggy"/>
<xsl:text>
return 0;
}
</xsl:text>
</xsl:template>
<xsl:template match="proggy">
<xsl:apply-templates select="prompt"/>
</xsl:template>
<xsl:template match="prompt">
<xsl:value-of select="$co"/><xsl:value-of select="$so"/>
<xsl:text>"</xsl:text><xsl:value-of select="text"/><xsl:text>"</xsl:text>
<xsl:value-of select="$e"/><xsl:value-of select="$nl"/>
<xsl:value-of select="@type"/><xsl:text> </xsl:text><xsl:value-of select="@input"/><xsl:text>;</xsl:text>
<xsl:value-of select="$nl"/>
<xsl:text>if (</xsl:text>
<xsl:value-of select="$ci"/><xsl:value-of select="$si"/><xsl:value-of select="@input"/>
<xsl:text>){</xsl:text>
<xsl:value-of select="$nl"/>
<xsl:apply-templates select="output"/>
<xsl:value-of select="$nl"/>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="output">
<xsl:value-of select="$co"/>
<xsl:apply-templates select="./*" mode="so"/>
<xsl:value-of select="$e"/>
</xsl:template>
<xsl:template match="text" mode="so">
<xsl:value-of select="$so"/><xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="field" mode="so">
<xsl:value-of select="$so"/><xsl:value-of select="@name"/>
</xsl:template>
</xsl:stylesheet>
I challenge you to write a smaller C++ program to generate the same C++ code! And this is a trivial example.
精彩评论