I was wondering if there is any way using the mingw g++ compiler to make code that acts like this:

#define myLanguageBlock(X) doMyLanguageBlock(#X)
void doMyLanguageBlock(const char* code){/*this executes the code as if it were in another language, I have already written this function*/}
int main()
{
    myLanguageBlock(
        this is all code written in my language. It has comma's, periods, and {brackets)][);
    return 0;
}

I am having trouble figuring this one out.

I'm having problems figuring out what you mean.

I want to be able to write a different language into my c++ code. Something like the __asm() function. The thing is that I was wondering if I could tell the compiler to ignore normal syntax rules so that I could do something like this:

int main()
{
    myLanguageTranslator(
        please realize that this is supposed to be viable code in
         another programming language. I just want to be able to do "this"
         without needing escape characters, is it at all possible?
    );
    return 0;
}

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.

That was helpful, but it didn't exactly solve my problem. I want to be able to write code like this (C++ example):

#define myLanguage(X) myCPPCompiler(#X)//I need this to actually work somehow
myLanguage(//assuming my language is c++ at this point
    int temp=0,temp2=0;
    cin>>temp,temp2;
    cout<<temp*temp2;
)

It's not possible without writing your own preprocessor that adds appropriate escape characters for the stringizing operator.

That was helpful, but it didn't exactly solve my problem. ...

As deceptikon 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?

How do I build a simple compiler? I think that that would be exactly what I would need.

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.

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++).

Is there any way to add my own language to code::blocks, that way it could get the highlighting right and maybe I can define a compiler for it to use?

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).

>>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.

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?

Well I was hoping to be able to have in-line language parsing, so that I can switch between the languages on a whim. Right now I have it fully functional is the form of Compile(const char*) but that requires unusual escape characters and a complete nullification of syntax highlighting. Perhaps I will look into Code::Blocks compiler options and see if I can throw my own executable in before gcc. Thank you.

I just thought I would share what I got working so far. Here is a sample quine in my modified version of the brainfuck language:

#include "ParseBrainFuck.h"//lol, nicely done daniweb on the IN-CODE consorship! :) My code uses the actual letters, not * so it actually works
int main()
{
    //Is a quine!!!
    bfCompile(":\
              \
              'DATA'\
              >}>>}>>}0>}}>}:>}.>}_>}>>}[>})>}X>}0>}}>}>>}.>}0>}}>}}>}.>}(>}X>}.>}>>}]>}_>}>>}[>}.>}>>}]\
              'OUTPUT'\
              >>0}:.'The initial colon needs to be outputted.'\
              _>['Start the output loop. Believe it or not we are now sitting at the start of the double data'\
                )X'Shift ahead of our data'\
                0}>.'Output the initial > character.'\
                0}}.'Output the initial } character.'\
                (X'Shift back'\
                .'Output the character.'\
                >'Shift to the next character.'\
                ]\
              _>['Now just output the values'\
                .\
                >\
                ]\
                    ");
    return 0;
}

Using a modified c++ highlighter, if I remove the \ after the : then I get properly highlighted code. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.