Extending c++ with a new keyword -
Extending c++ with a new keyword -
i want extend c++ keywords, example, "property", add together properties class/object (like visual studio extend c++ in "managed" version). code example:
class illustration { public: [...] property int size { get: { homecoming __size; } set: { if( value > 0 ) __size = value; else throw new exception("invalid size"); } }; }; use:
[...] int totalsize = example1.size + example2.size + example3.size; example1.size = 2 * example1.size; what want utilize form of translator translate c++ "extended" source code "normal" source code, qt moc do, parses c++ source code , generated c++ source code, qt "things" translated bare c++. of course, need extend translator (write code can understand "property keyword"). know of "translator" can utilize want?
processing header done using pyparsing. python-lib, powerful easy solution. utilize myself @ work parse c-inspired definition file , produce valid c-code it. example, re-create header file re-write property definition, e.g. using this:
property := literal('property') id id literal('{') [getter] [setter] literal('}') literal(';') getter := literal('get:') literal('{') cpp_block literal('}') setter := literal('set:') literal('{') cpp_block literal('}') id := word(alphas + '_', alphanums + '_') cpp_block := ... don't know match la .*? ... you attach actions setter , getter re-write cpp_block method.
the difficulty starts re-writing c++ code. simple parser not know if example1.size access struct field (which should not changed), property-get or property-set call. need know type of example1 if access lvalue or rvalue because example1.size = 1 has translate example1.set_size(1) int = example1.size has become int = example1.get_size().
so essentially, code translation need ast , symbol table looking type of variable. antlr has c++ parser (see here) i'm not sure if generates symbol table you.
c++
Comments
Post a Comment