0

Introduction (dont read if you dont want, this is just for background)
I am creating an API for C++ so that it has python-similar functionalities, without loosing its essence. I have successfully developed an automatic generic exception handling system (I asked how to do this here, and @MacZamborg told me how it works, so I started dismantling everything from scratch). Like I said, I managed to overload quite a lot of things so that the will automatically throw the according exception (which inherits from std::exception). The only one I had trouble with, was the ZERO_DIVISION exception, because it seems you cant overload operators just for ints, that is, one of the arguments, needs to be an object. So I created the Object Num, which uses an union to store any numeric data available) -> this means, I have to overload each operator 11 times, so I wanted to avoid the need to overload the X= operators. (Also asked something about it here).


Question

Is there a way in any IDE, to make smart macros? what is a smart macro, you may ask, well, lets pretend there is a new language called C+++, which implements smart macros > a smart macro should behave like this >

#define "regEx" "Literal replacement"

In practice:

#define '([^\d ]?\w+\s*)\+=' '$groups[0] = $groups[0] + '

With ([^\d ]?\w+\s*) being the only element in the groups array (The syntax comes from python's regEx, I may have some mistakes, but the idea is the same). That part of the regEx, will match any valid variable name (including reserved keywords [not derirable, but we get rid of them with the \+=]), that is, any word that starts with anything but a space or a digit, contains any character, and ends with spaces (0 or more). After that, it looks for the += operator, and if matched, it repaces the whole match with '$groups[0] = $groups[0] + ', that is, whatever was found on the only group (denoted by parenthesis), 'space' '=' 'space' 'what was found again' 'space' '+'

What all of this is addressed to, is to avoid having to overload the += operator (read intro if you may)

Community
  • 1
  • 1
J3STER
  • 1,027
  • 2
  • 11
  • 28

1 Answers1

5

It sounds like all you want is a custom preprocessor that understands and expands "smart macros". You can implement that e.g. as a python script that transforms text files.

As for how you can integrate this into your build process, that depends on what software you're using.

For example, with make you can do something like the following:

%.cpp: %.cppx
        mysmartpp $< > $@

Now if you have a source file called foo.cppx, make will automatically invoke mysmartpp foo.cppx > foo.cpp to create foo.cpp from it. (This assumes you've written a mysmartpp script that understands the "smart macro" syntax).

melpomene
  • 84,125
  • 8
  • 85
  • 148