I'm having problems figuring out what you mean.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
If you quote that then you should have no problem. For instance:
#define BLOCK(msg) blockHandler(msg)
void blockHandler(const std::string& blk) {
std::stringstream ss(blk);
std::string s;
while (ss >> s)
std::cout << "Token: " << s << std::endl;
}
int main () {
BLOCK("This is a quoted block in the program");
return 0;
}
Yields
Token: This
Token: is
Token: a
Token: quoted
Token: block
Token: in
Token: the
Token: program
However, you would need to integrate with the compiler to support special lexing considerations for unquoted free-form text.
As an alternative to inlining, you could always store the data externally and provide the file as a parameter to the macro. Something like:
#define BLOCK(file) do { \
std::ifstream if__(file); \
std::string fc__((std::istreambuf_iterator<char>(if__)), \
std::istreambuf_iterator<char>()); \
blockHandler(fc__); \
} while (0)
void blockHandler(const std::string& blk) {
std::stringstream ss(blk);
std::string s;
while (ss >> s)
std::cout << "Token: " << s << std::endl;
}
int main () {
BLOCK("t.t");
return 0;
}
At that point, though, you might just as well provide the file name to your function.
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
That was helpful, but it didn't exactly solve my problem. ...
Asdeceptikon mentioned and I stated in my response, free-form text is not supported by a C/C++ compiler unless you extend it.
What is it exactly that C++ can not support that you are trying to implement? If it is just a new language, why not just build a simple compiler?
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
While they have a steep learning curve, lex and yacc are good tools for this. There are other tools that are available but I am not familiar with how well they work.
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
It can probably be done with a lot of macro manipulation, but at some point a different language altogether would be of greater benefit.
It sounds like you're interested in the concept of a Domain Specific Language.
Ruby has some language extensions for other languages you might be able to use (mixing Ruby with C or C++).
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
As thines01 points out, but more precisely, what you really seem to be trying to do is to create a Domain Specific Embedded Language (or DSEL for short).
This can be done directly in C++, to a large extent. However, it is far from easy. Generally, it involves a heavy amount of template meta-programming, specifically, a technique called expression templates. The most amazing example of this is probably the Boost.Spirit library, and if you just take a quick glimpse at it you will probably figure out that its internal mechanics are far from being easy to grasp.
If all you want to do is to be able to insert some custom code written in some other language (that you may or may not have invented yourself) inside a C++ source file, then I would suggest that you simply create your own pre-processor (or pre-pre-processor). It is easy to create a program or script and run it on the source files before gcc. Then, all you need is to look for that token (i.e. like your MACROs name) and then you can do whatever you want with the custom code you find there. I'm guessing you will also have to parse that code somehow anyways to either turn it into valid C++ code or do something else. So, as L7Sqr said, you should look into parsing libraries like lex and yacc, or even Boost.Spirit (which is a library for creating parsers and code-generators).
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
>>Is there any way to add my own language to code::blocks
CodeBlocks isn't the best for this purpose, it is pretty much just designed for C++, afaik. For custom syntax highlighting and things like that, most enhanced text editors allow you to define custom grammars to be parsed and how the syntax should be highlighted, good candidates include Emacs and Kate .
As for configuring a different compiler, well I'm not so familiar with the CodeBlocks build system, so I can't say. But any decent build system will allow you to customize the compilation as much as you like, cmake is one such example of a build system which allows you to do just about anything you want (for example, I configured it to generate the doxygen documentation for my code, and before doing that, to update the time-stamps of the file-mod dates). Most build-system will allow that kind of pre-processing, launching shell scripts, special programs or commands, do some amount of parsing folders, files and file-contents.
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
Please explain why it is difficult to simply have your own code in a different file, then read this file and pass the resulting string to your function for executing other language code?
Zcool31
Junior Poster in Training
55 posts since Mar 2009
Reputation Points: 10
Solved Threads: 3